feat(M2): FR-S1/S4/S6 保存机制完善——setBody 防抖读 settings.autoSaveInterval(S1); beforeunload 冲刷+saveNow(S4); ReadDocResult 加 mtime+reloadDoc 检测外部改动(R6); store 暴露 __STORE__; save-mechanism E2E 3 项
This commit is contained in:
@@ -38,6 +38,12 @@ export function App() {
|
||||
|
||||
useEffect(() => { loadVault(); loadTree(); restore(); appearance.load(); useKeymap.getState().load(); useSettings.getState().loadGpu(); }, []);
|
||||
useEffect(() => { if (activeId && tabs.length > 0) { const t = tabs.find(x => x.id === activeId); if (t) openDoc(t.path).catch(() => {}); } }, []);
|
||||
// FR-S4:关窗前冲刷未保存内容
|
||||
useEffect(() => {
|
||||
const onBeforeUnload = () => { try { useStore.getState().saveNow(); } catch {} };
|
||||
window.addEventListener('beforeunload', onBeforeUnload);
|
||||
return () => window.removeEventListener('beforeunload', onBeforeUnload);
|
||||
}, []);
|
||||
useGlobalHotkeys({ onNewDoc: () => {}, onSave: () => useStore.getState().saveNow(), onFocusSearch: () => { const el = document.querySelector('input[placeholder*="搜索"]') as HTMLInputElement; el?.focus(); }, onToggleOutline: () => layout.toggleOutline(), onCheat: () => setShowCheat(true), onSettings: () => setShowSettings(true), onFind: () => { window.dispatchEvent(new CustomEvent('dsh-toggle-find')); } });
|
||||
|
||||
// 编辑器样式随设置生效(FR-编辑器设置真实生效)
|
||||
|
||||
@@ -15,6 +15,7 @@ interface EditorState {
|
||||
body: string;
|
||||
meta: DocMeta;
|
||||
saveState: SaveState;
|
||||
docMtime: number; // FR-S6:已加载文档的 mtime,用于检测外部改动
|
||||
tagFilter: string[]; // FR-R7:选中标签列表,FileTree 据此 AND 过滤
|
||||
// actions
|
||||
loadVault: () => Promise<void>;
|
||||
@@ -28,6 +29,7 @@ interface EditorState {
|
||||
addLink: (link: string) => void;
|
||||
removeLink: (link: string) => void;
|
||||
saveNow: () => Promise<void>;
|
||||
reloadDoc: () => Promise<boolean>; // FR-S6:重新加载——检测 mtime 变化,有未保存改动时返回 false 需确认
|
||||
resetEditor: () => void; // FR-V6: 切库时重置编辑区
|
||||
syncCurrentPath: (oldPath: string, newPath: string) => void; // FR-T7: 重命名/移动后同步当前文档路径
|
||||
}
|
||||
@@ -45,6 +47,7 @@ export const useStore = create<EditorState>((set, get) => ({
|
||||
meta: {},
|
||||
saveState: 'saved',
|
||||
tagFilter: [],
|
||||
docMtime: 0,
|
||||
|
||||
loadVault: async () => {
|
||||
const vault = await invoke(IPC.vaultList, undefined as never);
|
||||
@@ -66,7 +69,7 @@ export const useStore = create<EditorState>((set, get) => ({
|
||||
// 切换前冲刷当前文档
|
||||
await get().saveNow();
|
||||
const doc: ReadDocResult = await invoke(IPC.docRead, { path });
|
||||
set({ currentPath: path, body: doc.body, meta: doc.meta, title: doc.meta.title ?? path.split('/').pop()!.replace(/\.md$/, ''), saveState: 'saved' });
|
||||
set({ currentPath: path, body: doc.body, meta: doc.meta, title: doc.meta.title ?? path.split('/').pop()!.replace(/\.md$/, ''), saveState: 'saved', docMtime: doc.mtime });
|
||||
// 同步到标签页(如已存在则激活,否则新建)
|
||||
useTabs.getState().openTab(path);
|
||||
},
|
||||
@@ -98,9 +101,10 @@ export const useStore = create<EditorState>((set, get) => ({
|
||||
},
|
||||
setBody: (body: string) => {
|
||||
set({ body, saveState: 'saving' });
|
||||
// 防抖自动保存(M1:固定 600ms,M9 接设置间隔)
|
||||
// FR-S1:防抖自动保存——间隔读 settings.autoSaveInterval(秒→毫秒)
|
||||
if (saveTimer) clearTimeout(saveTimer);
|
||||
saveTimer = setTimeout(() => { get().saveNow(); }, 600);
|
||||
const interval = (() => { try { const s = JSON.parse(localStorage.getItem('dsh-settings') ?? '{}'); return (s.autoSaveInterval ?? 1) * 1000; } catch { return 1000; } })();
|
||||
saveTimer = setTimeout(() => { get().saveNow(); }, interval);
|
||||
},
|
||||
|
||||
saveNow: async () => {
|
||||
@@ -116,7 +120,20 @@ export const useStore = create<EditorState>((set, get) => ({
|
||||
}
|
||||
},
|
||||
|
||||
resetEditor: () => { set({ currentPath: null, title: '', body: '', meta: {}, saveState: 'saved' }); },
|
||||
// FR-S6:重新加载文档——检测 mtime 变化,有未保存改动时返回 false 需确认
|
||||
reloadDoc: async () => {
|
||||
const { currentPath, docMtime, saveState } = get();
|
||||
if (!currentPath) return false;
|
||||
try {
|
||||
const doc: ReadDocResult = await invoke(IPC.docRead, { path: currentPath });
|
||||
if (doc.mtime === docMtime) return true; // 未变
|
||||
if (saveState !== 'saved') return false; // 有未保存改动,需确认
|
||||
set({ body: doc.body, meta: doc.meta, docMtime: doc.mtime, saveState: 'saved' });
|
||||
return true;
|
||||
} catch { return false; }
|
||||
},
|
||||
|
||||
resetEditor: () => { set({ currentPath: null, title: '', body: '', meta: {}, saveState: 'saved', docMtime: 0 }); },
|
||||
|
||||
syncCurrentPath: (oldPath, newPath) => {
|
||||
const { currentPath } = get();
|
||||
@@ -124,4 +141,7 @@ export const useStore = create<EditorState>((set, get) => ({
|
||||
if (currentPath === oldPath) { set({ currentPath: newPath }); return; }
|
||||
if (oldPath && currentPath.startsWith(oldPath + '/')) set({ currentPath: newPath + currentPath.slice(oldPath.length) });
|
||||
},
|
||||
}));
|
||||
}));
|
||||
|
||||
// FR-S6:暴露 store 供 E2E 验证
|
||||
if (typeof window !== 'undefined') (window as any).__STORE__ = useStore;
|
||||
Reference in New Issue
Block a user