save & save all

This commit is contained in:
zhandouxiaojiji committed 2020-11-04 10:32:39 +08:00
1 parent 3392630e6b
commit c4eb53ac68
5 files changed
+66 -5

No files matched your search

+2
View File
@@ -2,4 +2,6 @@ export default {
OPEN_FILE: "OPEN_FILE", OPEN_FILE: "OPEN_FILE",
OPEN_WORKSPACE: "OPEN_WORKSPACE", OPEN_WORKSPACE: "OPEN_WORKSPACE",
CREATE_NODE: 'CREATE_NODE', CREATE_NODE: 'CREATE_NODE',
SAVE: 'SAVE',
SAVE_ALL: 'SAVE_ALL',
} }
+18
View File
@@ -59,6 +59,24 @@ export const createTreeData = (bNode: BehaviorNodeModel, settings: Settings) =>
return treeData; 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 => { export const findParent = (node: TreeGraphData, id: string): TreeGraphData | null => {
if (node.children) { if (node.children) {
for (let child of node.children) { for (let child of node.children) {
+13
View File
@@ -101,6 +101,19 @@ export default class AppMenu {
submenu: workspaceItems, submenu: workspaceItems,
}, },
{ type: 'separator' }, { type: 'separator' },
{
label: "保存",
click: () => {
this.webContents.send(MainEventType.SAVE);
},
},
{
label: "全部保存",
click: () => {
this.webContents.send(MainEventType.SAVE_ALL);
},
},
{ type: 'separator' },
{ {
label: "关闭", label: "关闭",
click: () => { click: () => {
+14 -4
View File
@@ -1,17 +1,17 @@
import * as React from 'react'; import * as React from 'react';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
import { Row, Col, message } from 'antd'; import { Row, Col, message } from 'antd';
import NodePanel from './NodePanel'; import NodePanel from './NodePanel';
import { INode } from '@antv/g6/lib/interface/item'; import G6, { TreeGraph } from '@antv/g6';
import G6, { TreeGraph, Util } from '@antv/g6';
import { TreeGraphData, IG6GraphEvent } from '@antv/g6/lib/types';
import { G6GraphEvent } from '@antv/g6/lib/interface/behavior'; import { G6GraphEvent } from '@antv/g6/lib/interface/behavior';
import './Editor.css';
import * as Utils from '../../common/Utils'; import * as Utils from '../../common/Utils';
import { BehaviorTreeModel, GraphNodeModel, BehaviorNodeModel } from '../../common/BehaviorTreeModel'; import { BehaviorTreeModel, GraphNodeModel, BehaviorNodeModel } from '../../common/BehaviorTreeModel';
import TreePanel from './TreePanel'; import TreePanel from './TreePanel';
import Settings from '../../main-process/Settings'; import Settings from '../../main-process/Settings';
import './Editor.css';
export interface EditorProps { export interface EditorProps {
filepath: string; filepath: string;
} }
@@ -303,6 +303,16 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
this.graph.set('animate', true); 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() { render() {
const { curNodeId, treeModel, settings } = this.state; const { curNodeId, treeModel, settings } = this.state;
var curNode: any; var curNode: any;
+19 -1
View File
@@ -2,7 +2,7 @@
import * as ReactDOM from "react-dom"; import * as ReactDOM from "react-dom";
import Editor from "./Editor"; import Editor from "./Editor";
import React, { Component } from "react"; import React, { Component } from "react";
import { Layout, Tabs } from 'antd'; import { Layout, Tabs, message } from 'antd';
import { ipcRenderer, remote } from 'electron'; import { ipcRenderer, remote } from 'electron';
import * as path from 'path'; import * as path from 'path';
@@ -39,6 +39,24 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
const editor = this.editors[curPath]; const editor = this.editors[curPath];
editor.createNode(name); 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) { openFile(path: string) {