diff --git a/src/components/editor.tsx b/src/components/editor.tsx index d3557ec..309b2a8 100644 --- a/src/components/editor.tsx +++ b/src/components/editor.tsx @@ -329,6 +329,20 @@ export const Editor: FC = ({ onChange, data: editor, ...props }) => }; } + const refreshGraph = async () => { + if (graph.hasSubtreeUpdated()) { + await graph.refreshSubtree(); + } else { + await graph.refresh(); + } + if (editor.focusId) { + graph.focusNode(editor.focusId); + editor.focusId = null; + } else if (graph.selectedId) { + graph.selectNode(graph.selectedId); + } + }; + // check should rebuild graph useEffect(() => { if (!editorSize || (editorSize.width === 0 && editorSize.height === 0)) { @@ -341,15 +355,7 @@ export const Editor: FC = ({ onChange, data: editor, ...props }) => if (graph && workspace.editing === editor) { graph.setSize(editorSize.width, editorSize.height); - - graph.refreshSubtree().then(() => { - if (editor.focusId) { - graph.focusNode(editor.focusId); - editor.focusId = null; - } else if (graph.selectedId) { - graph.selectNode(graph.selectedId); - } - }); + refreshGraph(); } }, [editorSize, workspace.editing, graph]); diff --git a/src/components/graph.ts b/src/components/graph.ts index fc3ab13..cf5b460 100644 --- a/src/components/graph.ts +++ b/src/components/graph.ts @@ -137,19 +137,21 @@ export class Graph { const data = JSON.parse(str) as TreeData; this.editor.declare.import = data.import.map((v) => ({ path: v, vars: [], depends: [] })); this.editor.declare.vars = data.vars.map((v) => ({ ...v })); - await this._update(data); + await this._update(data, true, true); // update tree inspector this.selectNode(null); this.onChange?.(); } - private async _update(data: TreeData, refreshId: boolean = true) { + private async _update(data: TreeData, refreshId: boolean = true, refreshVars: boolean = false) { this.editor.data = data; if (refreshId) { b3util.refreshNodeData(this.data.root, 1); } - workspace.refresh(this.editor.path); + if (refreshVars) { + workspace.refresh(this.editor.path); + } // clear current target avoid hover error const graph = this._graph as unknown as IGraph; @@ -909,9 +911,9 @@ export class Graph { this._storeHistory(); } - private _isSubtreeUpdated(data: NodeData) { + hasSubtreeUpdated() { let updated = false; - b3util.dfs(data, (node) => { + b3util.dfs(this.data.root, (node) => { if (node.path && b3util.files[node.path] !== node.mtime) { updated = true; } @@ -920,11 +922,8 @@ export class Graph { } async refreshSubtree() { - if (this._isSubtreeUpdated(this.data.root)) { - this.selectNode(null); - await this.refresh(); - this._storeHistory(); - } + await this.refresh(); + this._storeHistory(); } async save() { diff --git a/src/contexts/workspace-context.ts b/src/contexts/workspace-context.ts index 0ea51cb..15b6db2 100644 --- a/src/contexts/workspace-context.ts +++ b/src/contexts/workspace-context.ts @@ -519,7 +519,6 @@ export const useWorkspace = create((set, get) => ({ edit: (path, focusId) => { const workspace = get(); - const setting = useSetting.getState(); const editor = workspace.editors.find((v) => v.path === path); if (editor) { editor.focusId = focusId;