Editor add unsave state

This commit is contained in:
zhandouxiaojiji committed 2020-12-05 11:49:37 +08:00
1 parent efd93fe03e
commit db709a28ec
3 files changed
+50 -28

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "behavior3editor", "name": "behavior3editor",
"version": "0.1.5", "version": "0.1.6",
"description": "行为树编辑器", "description": "行为树编辑器",
"main": "./dist/main.js", "main": "./dist/main.js",
"scripts": { "scripts": {
+12 -1
View File
@@ -19,6 +19,7 @@ import { clipboard } from "electron";
export interface EditorProps { export interface EditorProps {
filepath: string; filepath: string;
onChangeSaveState: (unsave: boolean) => void;
} }
interface EditorState { interface EditorState {
@@ -50,6 +51,10 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
this.autoId = Utils.refreshNodeId(this.data); this.autoId = Utils.refreshNodeId(this.data);
} }
shouldComponentUpdate(nextProps: EditorProps, nextState: EditorState) {
return this.props.filepath != nextProps.filepath || this.state.curNodeId != nextState.curNodeId;
}
componentDidMount() { componentDidMount() {
const graph = new TreeGraph({ const graph = new TreeGraph({
container: this.ref.current, container: this.ref.current,
@@ -329,6 +334,8 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
this.graph.changeData(); this.graph.changeData();
this.graph.layout(); this.graph.layout();
this.graph.set("animate", true); this.graph.set("animate", true);
this.props.onChangeSaveState(true);
} }
save() { save() {
@@ -343,6 +350,7 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
filepath, filepath,
JSON.stringify(treeModel) JSON.stringify(treeModel)
); );
this.props.onChangeSaveState(false);
return treeModel; return treeModel;
} }
@@ -390,6 +398,8 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
this.graph.layout(); this.graph.layout();
this.graph.fitCenter(); this.graph.fitCenter();
this.graph.set("animate", true); this.graph.set("animate", true);
this.props.onChangeSaveState(true);
} }
pushUndoStack(keepRedo?: boolean) { pushUndoStack(keepRedo?: boolean) {
@@ -424,8 +434,8 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
} }
render() { render() {
console.log("render tree");
const { curNodeId } = this.state; const { curNodeId } = this.state;
console.log("render tree", curNodeId);
var curNode: any; var curNode: any;
if (curNodeId) { if (curNodeId) {
curNode = this.graph.findDataById(curNodeId); curNode = this.graph.findDataById(curNodeId);
@@ -449,6 +459,7 @@ export default class Editor extends React.Component<EditorProps, EditorState> {
} }
const item = this.graph.findById(id); const item = this.graph.findById(id);
item.draw(); item.draw();
this.props.onChangeSaveState(true);
}} }}
pushUndoStack={() => { pushUndoStack={() => {
this.pushUndoStack(); this.pushUndoStack();
+37 -26
View File
@@ -14,14 +14,19 @@ interface TreeTabsProps {
onTabSelected: (path: string) => void; onTabSelected: (path: string) => void;
} }
interface TreeInfo {
filepath: string;
unsave?: boolean;
}
interface TreeTabsState { interface TreeTabsState {
filepaths: string[]; trees: TreeInfo[];
curPath?: string; curTree?: string;
} }
export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> { export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
state: TreeTabsState = { state: TreeTabsState = {
filepaths: [], trees: [],
}; };
editors: { [path: string]: Editor } = {}; editors: { [path: string]: Editor } = {};
@@ -48,7 +53,7 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
}); });
ipcRenderer.on(MainEventType.SAVE, (event: any) => { ipcRenderer.on(MainEventType.SAVE, (event: any) => {
const { curPath } = this.state; const { curTree: curPath } = this.state;
if (!curPath) { if (!curPath) {
return; return;
} }
@@ -78,8 +83,8 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
componentDidUpdate() { componentDidUpdate() {
setTimeout(() => { setTimeout(() => {
this.props.onTabSelected(this.state.curPath); this.props.onTabSelected(this.state.curTree);
}, 100); }, 500);
} }
getOpenTreesModel() { getOpenTreesModel() {
@@ -94,7 +99,7 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
} }
getCurEditor() { getCurEditor() {
const { curPath } = this.state; const { curTree: curPath } = this.state;
if (!curPath) { if (!curPath) {
return; return;
} }
@@ -102,26 +107,26 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
} }
openFile(path: string) { openFile(path: string) {
if (this.state.filepaths.indexOf(path) < 0) { if (!this.state.trees.find(e => e.filepath == path)) {
const filepaths = this.state.filepaths; const trees = this.state.trees;
filepaths.push(path); trees.push({ filepath: path });
this.setState({ filepaths, curPath: path }); this.setState({ trees, curTree: path });
} else { } else {
this.setState({ curPath: path }); this.setState({ curTree: path });
} }
} }
closeFile(path: string) { closeFile(path: string) {
let filepaths = this.state.filepaths; let trees = this.state.trees;
filepaths = filepaths.filter((filepath) => (filepath != path)); trees = trees.filter(tree => tree.filepath != path);
const length = filepaths.length; const length = trees.length;
this.setState({ filepaths, curPath: length > 0 ? filepaths[0] : null }); this.setState({ trees: trees, curTree: length > 0 ? trees[0].filepath : null });
} }
render() { render() {
console.log("render tabs"); const { trees, curTree } = this.state;
const { filepaths, curPath } = this.state; console.log("render tabs", trees);
if (!curPath) { if (!curTree) {
return <div />; return <div />;
} }
this.editors = {}; this.editors = {};
@@ -130,10 +135,10 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
hideAdd hideAdd
className="tabs" className="tabs"
type="editable-card" type="editable-card"
defaultActiveKey={curPath} defaultActiveKey={curTree}
activeKey={curPath} activeKey={curTree}
onChange={(activeKey) => { onChange={(activeKey) => {
this.setState({ curPath: activeKey }); this.setState({ curTree: activeKey });
}} }}
onEdit={(targetKey, action) => { onEdit={(targetKey, action) => {
if (action == "remove") { if (action == "remove") {
@@ -141,13 +146,19 @@ export default class TreeTabs extends Component<TreeTabsProps, TreeTabsState> {
} }
}} }}
> >
{filepaths.map((filepath) => { {trees.map((tree) => {
return ( return (
<TabPane tab={path.basename(filepath)} key={filepath}> <TabPane tab={`${path.basename(tree.filepath)}${tree.unsave?"*":''}`} key={tree.filepath}>
<Editor <Editor
filepath={filepath} filepath={tree.filepath}
onChangeSaveState={(unsave) => {
if(tree.unsave != unsave) {
tree.unsave = unsave;
this.forceUpdate();
}
}}
ref={(ref) => { ref={(ref) => {
this.editors[filepath] = ref; this.editors[tree.filepath] = ref;
}} }}
/> />
</TabPane> </TabPane>