0.1.06文件树的选择问题及思维导图删除问题

This commit is contained in:
liaibo
2026-01-07 22:06:49 +08:00
parent afad811d85
commit 9de2d69c55
4 changed files with 109 additions and 8 deletions
@@ -238,6 +238,45 @@ export function BlockNoteEditor({
const debouncedSave = useDebouncedCallback(saveContent, 800);
const previousAssetsRef = useRef<Set<string>>(new Set());
const hadMindmapRef = useRef(false);
const clearMindmapAutosaveCache = useCallback((targetDocumentId: string) => {
if (typeof window === "undefined") return;
try {
const prefix = "wolai-mindmap-autosave-";
const targetPrefix = `${prefix}${targetDocumentId}`;
const keys: string[] = [];
for (let i = 0; i < window.localStorage.length; i += 1) {
const k = window.localStorage.key(i);
if (!k) continue;
if (k === targetPrefix || k.startsWith(targetPrefix)) {
keys.push(k);
}
}
keys.forEach((k) => window.localStorage.removeItem(k));
} catch {
// ignore
}
}, []);
const markMindmapDeleting = useCallback((targetDocumentId: string) => {
if (typeof window === "undefined") return;
try {
const w = window as unknown as {
__wolaiMindmapDeletingDocIds?: Set<string>;
};
if (!w.__wolaiMindmapDeletingDocIds) {
w.__wolaiMindmapDeletingDocIds = new Set<string>();
}
w.__wolaiMindmapDeletingDocIds.add(targetDocumentId);
window.setTimeout(() => {
try {
w.__wolaiMindmapDeletingDocIds?.delete(targetDocumentId);
} catch {
// ignore
}
}, 8000);
} catch {
// ignore
}
}, []);
const collectAssets = useCallback((blocks: Block<CustomBlockSchema>[]) => {
const assetIds = new Set<string>();
@@ -286,8 +325,11 @@ export function BlockNoteEditor({
console.error("删除思维导图失败", await resp.text());
return;
}
// 重要:删除思维导图文件后也要清理本地 autosave,否则用户再次插入导图会从旧缓存恢复,表现为“删除不干净/重复出现”
markMindmapDeleting(documentId);
clearMindmapAutosaveCache(documentId);
emitAssetsChanged(documentId);
}, [documentId]);
}, [clearMindmapAutosaveCache, documentId, markMindmapDeleting]);
// 监听侧边栏删除事件,主动移除编辑区遗留块
useEffect(() => {
@@ -301,6 +343,12 @@ export function BlockNoteEditor({
const assetIds = detail.assetIds ?? [];
const mindmapDeleted = Boolean(detail.mindmapDeleted);
if (assetIds.length === 0 && !mindmapDeleted) return;
if (mindmapDeleted) {
// 标记“删除中”,避免 MindmapBlock 卸载清理里把 autosave 写回导致“复活”
markMindmapDeleting(documentId);
// 双保险:等卸载完成后再清一次 autosave,避免删除后再次插入出现旧内容(重复)
window.setTimeout(() => clearMindmapAutosaveCache(documentId), 0);
}
const blocks = editor?.topLevelBlocks as Block<CustomBlockSchema>[] | undefined;
if (!blocks || blocks.length === 0 || !editor) return;
const toRemove: string[] = [];
@@ -327,7 +375,7 @@ export function BlockNoteEditor({
};
window.addEventListener(ASSETS_CHANGED_EVENT, handler);
return () => window.removeEventListener(ASSETS_CHANGED_EVENT, handler);
}, [documentId, editor]);
}, [clearMindmapAutosaveCache, documentId, editor, markMindmapDeleting]);
useEffect(() => {
if (!editor) {