Skip to content

Commit

Permalink
show reload alert dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongfq committed Nov 21, 2024
1 parent 78ee7bc commit 1cced07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
10 changes: 7 additions & 3 deletions src/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,13 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
const file = readJson(editor.path) as TreeModel;
editor.data = b3util.createTreeData(file.root);
editor.autoId = b3util.refreshTreeDataId(editor.data);
editor.graph.changeData(editor.data);
editor.graph.layout();
restoreViewport();
editor.unsave = false;
updateState();
if (editor.graph) {
editor.graph.changeData(editor.data);
editor.graph.layout();
restoreViewport();
}
};

const checkSubtree = () => {
Expand Down
16 changes: 5 additions & 11 deletions src/components/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export const Workspace: FC = () => {
edit: useWorkspace((state) => state.edit),
editing: useWorkspace((state) => state.editing),
editors: useWorkspace((state) => state.editors),
modifiedEditors: useWorkspace((state) => state.modifiedEditors),
fileTree: useWorkspace((state) => state.fileTree),
find: useWorkspace((state) => state.find),
init: useWorkspace((state) => state.init),
modifiedTime: useWorkspace((state) => state.modifiedTime),
isShowingSearch: useWorkspace((state) => state.isShowingSearch),
onShowingSearch: useWorkspace((state) => state.onShowingSearch),
openProject: useWorkspace((state) => state.openProject),
Expand Down Expand Up @@ -96,17 +96,11 @@ export const Workspace: FC = () => {

useEffect(() => {
const editor = workspace.editing;
if (editor && workspace.modifiedEditors.includes(editor)) {
if (editor?.alertReload) {
if (isShowingAlert) {
return;
}
setShowingAlert(true);
const modifiedEditors = workspace.modifiedEditors.filter((v) => v !== editor);
console.log(
"modified:",
editor.path,
modifiedEditors.map((v) => v.path)
);
const alert = modal.confirm({
centered: true,
content: (
Expand All @@ -123,21 +117,21 @@ export const Workspace: FC = () => {
<Button
type="primary"
onClick={() => {
editor.alertReload = false;
editor.dispatch("reload");
alert.destroy();
keysRef.current?.focus();
setShowingAlert(false);
useWorkspace.setState({ modifiedEditors: modifiedEditors });
}}
>
{t("reload")}
</Button>
<Button
onClick={() => {
editor.alertReload = false;
alert.destroy();
keysRef.current?.focus();
setShowingAlert(false);
useWorkspace.setState({ modifiedEditors: modifiedEditors });
}}
>
{t("cancel")}
Expand All @@ -147,7 +141,7 @@ export const Workspace: FC = () => {
),
});
}
}, [workspace.modifiedEditors, workspace.editing]);
}, [workspace.editing, workspace.modifiedTime]);

const showSaveDialog = (editor: EditorStore) => {
if (isShowingAlert) {
Expand Down
20 changes: 9 additions & 11 deletions src/contexts/workspace-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export class EditorStore {
dragDstId?: string;
unsave: boolean = false;
modifiedTime: number = Date.now();
alertReload: boolean = false;

historyStack: NodeModel[] = [];
historyIndex: number = 0;
selectedId?: string | null;
Expand Down Expand Up @@ -130,9 +132,10 @@ export type WorkspaceStore = {
allFiles: Map<string, FileMeta>;
fileTree?: FileTreeType;
editors: EditorStore[];
modifiedEditors: EditorStore[];
editing?: EditorStore;

modifiedTime: number;

isShowingSearch: boolean;
onShowingSearch: (isShowingSearch: boolean) => void;

Expand Down Expand Up @@ -392,6 +395,8 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
}
},

modifiedTime: 0,

isShowingSearch: false,
onShowingSearch: (isShowingSearch) => {
set({ isShowingSearch });
Expand Down Expand Up @@ -500,17 +505,10 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
} else {
const fullpath = fs.realpathSync(`${workspace.workdir}/${filename}`);
const editor = workspace.find(fullpath);
if (
editor &&
editor.modifiedTime + 500 < fs.statSync(fullpath).mtimeMs &&
!workspace.modifiedEditors.includes(editor)
) {
if (editor && editor.modifiedTime + 500 < fs.statSync(fullpath).mtimeMs) {
if (editor.unsave) {
const modifiedEditors = workspace.modifiedEditors;
console.log("modified1:", editor.path, modifiedEditors);
modifiedEditors.push(editor);
set({ modifiedEditors: modifiedEditors });
console.log("modified2:", editor.path, modifiedEditors);
editor.alertReload = true;
set({ modifiedTime: Date.now() });
} else {
editor.dispatch("reload");
}
Expand Down

0 comments on commit 1cced07

Please sign in to comment.