From c4eb53ac68f549e3a461df7f0f356afd6f5cf866 Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Wed, 4 Nov 2020 10:32:39 +0800 Subject: [PATCH] save & save all --- common/MainEventType.ts | 2 ++ common/Utils.ts | 18 ++++++++++++++++++ main-process/AppMenu.ts | 13 +++++++++++++ render-process/Editor/index.tsx | 18 ++++++++++++++---- render-process/TreeTabs.tsx | 20 +++++++++++++++++++- 5 files changed, 66 insertions(+), 5 deletions(-) diff --git a/common/MainEventType.ts b/common/MainEventType.ts index f81feed..193509d 100644 --- a/common/MainEventType.ts +++ b/common/MainEventType.ts @@ -2,4 +2,6 @@ export default { OPEN_FILE: "OPEN_FILE", OPEN_WORKSPACE: "OPEN_WORKSPACE", CREATE_NODE: 'CREATE_NODE', + SAVE: 'SAVE', + SAVE_ALL: 'SAVE_ALL', } \ No newline at end of file diff --git a/common/Utils.ts b/common/Utils.ts index c9ca944..e52d89f 100644 --- a/common/Utils.ts +++ b/common/Utils.ts @@ -59,6 +59,24 @@ export const createTreeData = (bNode: BehaviorNodeModel, settings: Settings) => return treeData; } +export const createFileData = (gNode: GraphNodeModel) => { + const nodeData: BehaviorNodeModel = { + id: Number(gNode.id), + name: gNode.name, + desc: gNode.desc || undefined, + args: gNode.args || undefined, + input: gNode.input || undefined, + output: gNode.output || undefined, + } + if (gNode.children) { + nodeData.children = []; + gNode.children.forEach(child => { + nodeData.children.push(createFileData(child)); + }) + } + return nodeData; +} + export const findParent = (node: TreeGraphData, id: string): TreeGraphData | null => { if (node.children) { for (let child of node.children) { diff --git a/main-process/AppMenu.ts b/main-process/AppMenu.ts index 755ed76..f8eeeba 100644 --- a/main-process/AppMenu.ts +++ b/main-process/AppMenu.ts @@ -101,6 +101,19 @@ export default class AppMenu { submenu: workspaceItems, }, { type: 'separator' }, + { + label: "保存", + click: () => { + this.webContents.send(MainEventType.SAVE); + }, + }, + { + label: "全部保存", + click: () => { + this.webContents.send(MainEventType.SAVE_ALL); + }, + }, + { type: 'separator' }, { label: "关闭", click: () => { diff --git a/render-process/Editor/index.tsx b/render-process/Editor/index.tsx index 1fe305f..65dff2d 100644 --- a/render-process/Editor/index.tsx +++ b/render-process/Editor/index.tsx @@ -1,17 +1,17 @@ import * as React from 'react'; import * as fs from 'fs'; +import * as path from 'path'; import { Row, Col, message } from 'antd'; import NodePanel from './NodePanel'; -import { INode } from '@antv/g6/lib/interface/item'; -import G6, { TreeGraph, Util } from '@antv/g6'; -import { TreeGraphData, IG6GraphEvent } from '@antv/g6/lib/types'; +import G6, { TreeGraph } from '@antv/g6'; import { G6GraphEvent } from '@antv/g6/lib/interface/behavior'; -import './Editor.css'; import * as Utils from '../../common/Utils'; import { BehaviorTreeModel, GraphNodeModel, BehaviorNodeModel } from '../../common/BehaviorTreeModel'; import TreePanel from './TreePanel'; import Settings from '../../main-process/Settings'; +import './Editor.css'; + export interface EditorProps { filepath: string; } @@ -303,6 +303,16 @@ export default class Editor extends React.Component { this.graph.set('animate', true); } + save() { + const { filepath } = this.props; + const root = Utils.createFileData(this.graph.findDataById('1') as GraphNodeModel); + fs.writeFileSync(filepath, JSON.stringify({ + name: path.basename(filepath).slice(0, -5), + root, + desc: root.desc, + })); + } + render() { const { curNodeId, treeModel, settings } = this.state; var curNode: any; diff --git a/render-process/TreeTabs.tsx b/render-process/TreeTabs.tsx index 4d72a4a..8365631 100644 --- a/render-process/TreeTabs.tsx +++ b/render-process/TreeTabs.tsx @@ -2,7 +2,7 @@ import * as ReactDOM from "react-dom"; import Editor from "./Editor"; import React, { Component } from "react"; -import { Layout, Tabs } from 'antd'; +import { Layout, Tabs, message } from 'antd'; import { ipcRenderer, remote } from 'electron'; import * as path from 'path'; @@ -39,6 +39,24 @@ export default class TreeTabs extends Component { const editor = this.editors[curPath]; editor.createNode(name); }); + + ipcRenderer.on(MainEventType.SAVE, (event: any) => { + const {curPath} = this.state; + if(!curPath) { + return; + } + const editor = this.editors[curPath]; + editor.save(); + message.success("已保存"); + }); + + ipcRenderer.on(MainEventType.SAVE_ALL, (event: any) => { + for(let k in this.editors){ + let editor = this.editors[k]; + editor.save(); + } + message.success("已保存所有行为树"); + }); } openFile(path: string) {