Skip to content

Commit

Permalink
detect file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongfq committed Nov 21, 2024
1 parent 298cedb commit cf1c338
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
filterFocus: true,
filterType: "content",
placeholder: "",
isFocused: false
isFocused: false,
});

const onSearchChange = (option: FilterOption) => {
Expand Down Expand Up @@ -327,7 +327,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
}
};

const reload = () => {
const refresh = () => {
const rootData = findDataById("1");
const rootNode = b3util.createNode(rootData);
editor.data = b3util.createTreeData(rootNode);
Expand All @@ -337,11 +337,13 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
restoreViewport();
};

const reload = () => {};

const checkSubtree = () => {
if (editor.graph) {
const data = findDataById("1");
if (b3util.isSubtreeUpdated(data)) {
reload();
refresh();
pushHistory();
onChange();
}
Expand Down Expand Up @@ -383,7 +385,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
b3util.copyFromNode(data, newNode);
data.size = b3util.calcTreeDataSize(data);
if (oldNode.path !== newNode.path) {
reload();
refresh();
selectNode(null);
selectNode(data.id);
} else {
Expand Down Expand Up @@ -623,6 +625,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
export: editor.export,
desc: editor.desc,
} as TreeModel;
editor.modifiedTime = Date.now();
fs.writeFileSync(path, JSON.stringify(treeModel, null, 2));
workspace.updateFileMeta(editor);
editor.unsave = false;
Expand Down Expand Up @@ -684,7 +687,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
} as TreeModel;
fs.writeFileSync(subpath, JSON.stringify(subtreeModel, null, 2));
data.path = workspace.relative(subpath);
reload();
refresh();
pushHistory();
onChange();
}, 200);
Expand Down Expand Up @@ -1006,6 +1009,10 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
redo();
break;
}
case "refresh": {
refresh();
break;
}
case "reload": {
reload();
break;
Expand Down Expand Up @@ -1274,7 +1281,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
filterStr: "",
filterType: "content",
placeholder: "",
isFocused: false
isFocused: false,
});
keysRef.current?.focus();
}}
Expand Down
2 changes: 1 addition & 1 deletion src/components/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export const Explorer: FC = () => {
console.log("editor move", editor.path === newPath, editor.path, newPath);
if (editor.path.startsWith(newPath)) {
console.log("editor reload", editor.path === newPath, editor.path, newPath);
editor.dispatch("reload");
editor.dispatch("refresh");
}
}
};
Expand Down
1 change: 0 additions & 1 deletion src/components/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export const Menu: FC<LayoutProps> = () => {
accelerator: "CmdOrCtrl+S",
enabled: enabled,
click: () => {
console.log("save1");
workspace.save();
},
},
Expand Down
23 changes: 19 additions & 4 deletions src/contexts/workspace-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export type EditEvent =
| "jumpNode"
| "undo"
| "redo"
| "reload"
| "refresh"
| "rename"
| "reload"
| "updateTree"
| "updateNode"
| "searchNode"
Expand All @@ -49,6 +50,7 @@ export class EditorStore {
dragSrcId?: string;
dragDstId?: string;
unsave: boolean = false;
modifiedTime: number = Date.now();
historyStack: NodeModel[] = [];
historyIndex: number = 0;
selectedId?: string | null;
Expand All @@ -66,6 +68,7 @@ export class EditorStore {

const file = readJson(path) as TreeModel;
this.data = b3util.createTreeData(file.root);
this.modifiedTime = fs.statSync(path).mtimeMs;
this.desc = file.desc ?? "";
this.export = file.export !== false;
this.name = file.name || path.slice(0, -5);
Expand Down Expand Up @@ -393,6 +396,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
},

open: (path) => {
path = fs.realpathSync(path);
const workspace = get();
let editor = workspace.editors.find((v) => v.path === path);
if (!editor) {
Expand Down Expand Up @@ -488,8 +492,19 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
hasEvent = true;
}
}
if (event === "change" && filename === "node-config.b3-setting") {
workspace.loadNodeDefs();
if (event === "change" && filename) {
if (filename === "node-config.b3-setting") {
workspace.loadNodeDefs();
} else {
const fullpath = fs.realpathSync(`${workspace.workdir}/${filename}`);
const editor = workspace.find(fullpath);
if (editor) {
console.log(filename, editor, editor.modifiedTime, fs.statSync(fullpath).mtimeMs);
if (editor.modifiedTime + 500 < fs.statSync(fullpath).mtimeMs) {
console.log("reload", filename);
}
}
}
}
});
} catch (e) {
Expand Down Expand Up @@ -550,7 +565,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
nodeDefs.set(v.name, v);
}
set({ nodeDefs });
workspace.editing?.dispatch("reload");
workspace.editing?.dispatch("refresh");
},

// node edit
Expand Down

0 comments on commit cf1c338

Please sign in to comment.