show reload alert dialog

This commit is contained in:
codetypes committed 2024-11-21 23:04:39 +08:00
1 parent 78ee7bcc76
commit 1cced07da9
3 files changed
+18 -22

No files matched your search

+4
View File
@@ -342,9 +342,13 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
const file = readJson(editor.path) as TreeModel; const file = readJson(editor.path) as TreeModel;
editor.data = b3util.createTreeData(file.root); editor.data = b3util.createTreeData(file.root);
editor.autoId = b3util.refreshTreeDataId(editor.data); editor.autoId = b3util.refreshTreeDataId(editor.data);
editor.unsave = false;
updateState();
if (editor.graph) {
editor.graph.changeData(editor.data); editor.graph.changeData(editor.data);
editor.graph.layout(); editor.graph.layout();
restoreViewport(); restoreViewport();
}
}; };
const checkSubtree = () => { const checkSubtree = () => {
+5 -11
View File
@@ -28,10 +28,10 @@ export const Workspace: FC = () => {
edit: useWorkspace((state) => state.edit), edit: useWorkspace((state) => state.edit),
editing: useWorkspace((state) => state.editing), editing: useWorkspace((state) => state.editing),
editors: useWorkspace((state) => state.editors), editors: useWorkspace((state) => state.editors),
modifiedEditors: useWorkspace((state) => state.modifiedEditors),
fileTree: useWorkspace((state) => state.fileTree), fileTree: useWorkspace((state) => state.fileTree),
find: useWorkspace((state) => state.find), find: useWorkspace((state) => state.find),
init: useWorkspace((state) => state.init), init: useWorkspace((state) => state.init),
modifiedTime: useWorkspace((state) => state.modifiedTime),
isShowingSearch: useWorkspace((state) => state.isShowingSearch), isShowingSearch: useWorkspace((state) => state.isShowingSearch),
onShowingSearch: useWorkspace((state) => state.onShowingSearch), onShowingSearch: useWorkspace((state) => state.onShowingSearch),
openProject: useWorkspace((state) => state.openProject), openProject: useWorkspace((state) => state.openProject),
@@ -96,17 +96,11 @@ export const Workspace: FC = () => {
useEffect(() => { useEffect(() => {
const editor = workspace.editing; const editor = workspace.editing;
if (editor && workspace.modifiedEditors.includes(editor)) { if (editor?.alertReload) {
if (isShowingAlert) { if (isShowingAlert) {
return; return;
} }
setShowingAlert(true); setShowingAlert(true);
const modifiedEditors = workspace.modifiedEditors.filter((v) => v !== editor);
console.log(
"modified:",
editor.path,
modifiedEditors.map((v) => v.path)
);
const alert = modal.confirm({ const alert = modal.confirm({
centered: true, centered: true,
content: ( content: (
@@ -123,21 +117,21 @@ export const Workspace: FC = () => {
<Button <Button
type="primary" type="primary"
onClick={() => { onClick={() => {
editor.alertReload = false;
editor.dispatch("reload"); editor.dispatch("reload");
alert.destroy(); alert.destroy();
keysRef.current?.focus(); keysRef.current?.focus();
setShowingAlert(false); setShowingAlert(false);
useWorkspace.setState({ modifiedEditors: modifiedEditors });
}} }}
> >
{t("reload")} {t("reload")}
</Button> </Button>
<Button <Button
onClick={() => { onClick={() => {
editor.alertReload = false;
alert.destroy(); alert.destroy();
keysRef.current?.focus(); keysRef.current?.focus();
setShowingAlert(false); setShowingAlert(false);
useWorkspace.setState({ modifiedEditors: modifiedEditors });
}} }}
> >
{t("cancel")} {t("cancel")}
@@ -147,7 +141,7 @@ export const Workspace: FC = () => {
), ),
}); });
} }
}, [workspace.modifiedEditors, workspace.editing]); }, [workspace.editing, workspace.modifiedTime]);
const showSaveDialog = (editor: EditorStore) => { const showSaveDialog = (editor: EditorStore) => {
if (isShowingAlert) { if (isShowingAlert) {
+9 -11
View File
@@ -51,6 +51,8 @@ export class EditorStore {
dragDstId?: string; dragDstId?: string;
unsave: boolean = false; unsave: boolean = false;
modifiedTime: number = Date.now(); modifiedTime: number = Date.now();
alertReload: boolean = false;
historyStack: NodeModel[] = []; historyStack: NodeModel[] = [];
historyIndex: number = 0; historyIndex: number = 0;
selectedId?: string | null; selectedId?: string | null;
@@ -130,9 +132,10 @@ export type WorkspaceStore = {
allFiles: Map<string, FileMeta>; allFiles: Map<string, FileMeta>;
fileTree?: FileTreeType; fileTree?: FileTreeType;
editors: EditorStore[]; editors: EditorStore[];
modifiedEditors: EditorStore[];
editing?: EditorStore; editing?: EditorStore;
modifiedTime: number;
isShowingSearch: boolean; isShowingSearch: boolean;
onShowingSearch: (isShowingSearch: boolean) => void; onShowingSearch: (isShowingSearch: boolean) => void;
@@ -392,6 +395,8 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
} }
}, },
modifiedTime: 0,
isShowingSearch: false, isShowingSearch: false,
onShowingSearch: (isShowingSearch) => { onShowingSearch: (isShowingSearch) => {
set({ isShowingSearch }); set({ isShowingSearch });
@@ -500,17 +505,10 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
} else { } else {
const fullpath = fs.realpathSync(`${workspace.workdir}/${filename}`); const fullpath = fs.realpathSync(`${workspace.workdir}/${filename}`);
const editor = workspace.find(fullpath); const editor = workspace.find(fullpath);
if ( if (editor && editor.modifiedTime + 500 < fs.statSync(fullpath).mtimeMs) {
editor &&
editor.modifiedTime + 500 < fs.statSync(fullpath).mtimeMs &&
!workspace.modifiedEditors.includes(editor)
) {
if (editor.unsave) { if (editor.unsave) {
const modifiedEditors = workspace.modifiedEditors; editor.alertReload = true;
console.log("modified1:", editor.path, modifiedEditors); set({ modifiedTime: Date.now() });
modifiedEditors.push(editor);
set({ modifiedEditors: modifiedEditors });
console.log("modified2:", editor.path, modifiedEditors);
} else { } else {
editor.dispatch("reload"); editor.dispatch("reload");
} }