Files
mnote/wolai-frontend/src/components/editor/blocks/MindmapBlock.tsx
T

2562 lines
95 KiB
TypeScript
Raw Normal View History

"use client";
import "simple-mind-map/dist/simpleMindMap.esm.css";
import React, {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import { createPortal } from "react-dom";
import { createReactBlockSpec } from "@blocknote/react";
2026-01-07 07:25:07 +08:00
import type {
BlockConfig,
BlockNoteEditor,
DefaultInlineContentSchema,
DefaultStyleSchema,
PropSchema,
SpecificBlock,
} from "@blocknote/core";
import type { CustomBlockSchema } from "../schema";
import { MindmapToolbar } from "./MindmapToolbar";
import { MindmapSidebar } from "./MindmapSidebar";
import { MindmapSidebarTrigger } from "./MindmapSidebarTrigger";
import { MindmapNavigator } from "./MindmapNavigator";
import { MindmapMiniMap } from "./MindmapMiniMap";
import { MindmapCount } from "./MindmapCount";
import type { SidebarPanel } from "./mindmapSidebarConfig";
2026-01-07 07:25:07 +08:00
import type { MindMapNode } from "./mindmapTypes";
2025-12-31 19:26:38 +08:00
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
2026-01-07 07:25:07 +08:00
import { ArrowUp, ArrowDown, ArrowLeft, ArrowRight } from "lucide-react";
2025-12-30 18:39:38 +08:00
import iconConfig from "./mindmapIconConfig";
2026-01-02 07:25:50 +08:00
import { emitAssetsChanged } from "@/lib/events";
// 避免 SSR/Node 环境提前评估 simple-mind-map 内部依赖 document
const loadIconModules = async () => {
2026-01-07 07:25:07 +08:00
const { nodeIconList } = await import("simple-mind-map/src/svg/icons.js");
2026-01-02 07:25:50 +08:00
const { mergerIconList } = await import("simple-mind-map/src/utils/index.js");
return { nodeIconList, mergerIconList };
};
2025-12-31 19:26:38 +08:00
// 安全获取图片尺寸
const getImageSizeSafe = (url: string): Promise<{ width: number; height: number } | null> =>
new Promise((resolve) => {
if (!url) return resolve(null);
const img = new Image();
img.onload = () => resolve({ width: img.naturalWidth, height: img.naturalHeight });
img.onerror = () => resolve(null);
img.src = url;
});
type MindMapInstance = {
2026-01-07 07:25:07 +08:00
execCommand: (command: string, ...args: unknown[]) => void;
destroy: () => void;
setData: (data: unknown) => void;
getData: (withConfig?: boolean) => unknown;
2026-01-07 07:25:07 +08:00
on?: <TArgs extends unknown[]>(event: string, handler: (...args: TArgs) => void) => void;
emit?: (event: string, ...args: unknown[]) => void;
2026-01-07 07:25:07 +08:00
off?: <TArgs extends unknown[]>(event: string, handler: (...args: TArgs) => void) => void;
setMode?: (mode: string) => void;
command: { clearHistory: () => void };
2026-01-07 07:25:07 +08:00
editNodeClassList?: string[];
view: {
fit: () => void;
scale: number;
enlarge: () => void;
narrow: () => void;
setScale: (scale: number, cx: number, cy: number) => void;
};
renderer: {
activeNodeList: unknown[];
lastActiveNodeList?: unknown[];
root?: unknown;
renderTree: { _node: unknown };
setRootNodeCenter?: () => void;
clearActiveNodeList?: () => void;
addNodeToActiveList?: (node: unknown, isActive?: boolean) => void;
emitNodeActiveEvent?: (node: unknown) => void;
findNodeByUid?: (uid: string) => unknown;
copy?: () => void;
paste?: () => void;
beingCopyData?: unknown;
};
painter?: { startPainter: () => void };
doExport?: { export: (type: string, isDownload?: boolean, name?: string) => Promise<unknown> };
width: number;
height: number;
miniMap?: unknown;
getThemeConfig: (key: string) => unknown;
setThemeConfig: (config: unknown) => void;
setTheme: (theme: string) => void;
setLayout: (layout: string) => void;
};
2026-01-02 07:25:50 +08:00
type MindMapData = {
data: Record<string, unknown>;
children?: unknown[];
};
export const defaultMindmapData = {
data: { text: "中心主题" },
children: [],
};
2026-01-07 21:21:22 +08:00
// simple-mind-map 的 RichText 插件初始化会对节点文本做 HTML 转义;
// 若数据里出现 `data.text === undefined`,会在内部调用 `undefined.replace(...)` 直接崩溃。
// 这里在“保存/恢复”链路上做一次兜底归一化,确保跨视图重建实例时不会白屏。
const normalizeMindmapData = (input: unknown): unknown => {
if (!input || typeof input !== "object") return defaultMindmapData;
const root = (input as { root?: unknown }).root ?? input;
const walk = (node: any) => {
if (!node || typeof node !== "object") return;
if (!node.data || typeof node.data !== "object") node.data = {};
const rawText = (node.data as any).text;
(node.data as any).text = typeof rawText === "string" ? rawText : String(rawText ?? "");
// 概要(generalization)数据结构里也存在 text 字段,缺失会导致 RichText 初始化崩溃
const gen = (node.data as any).generalization;
const fixGen = (g: any) => {
if (!g || typeof g !== "object") return;
const t = (g as any).text;
(g as any).text = typeof t === "string" ? t : String(t ?? "");
};
if (Array.isArray(gen)) gen.forEach(fixGen);
else fixGen(gen);
if (Array.isArray(node.children)) node.children.forEach(walk);
};
walk(root);
return input;
};
// 持久化/初始化统一使用“根节点对象”作为数据载体,避免把包含额外字段的 wrapper 误传给 simple-mind-map
// 从而触发 RichText 对 wrapper.data 的处理(wrapper.data.text 可能不存在 → htmlEscape 崩溃)。
const canonicalizeMindmapData = (input: unknown): MindMapData => {
const normalized = (normalizeMindmapData(input) ?? defaultMindmapData) as any;
const root = (normalized && typeof normalized === "object" && "root" in normalized)
? (normalized as any).root
: normalized;
return (normalizeMindmapData(root) ?? defaultMindmapData) as MindMapData;
};
const STORAGE_PREFIX = "wolai-mindmap-autosave-";
function downloadJson(data: unknown, name: string) {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = `${name}.json`;
anchor.click();
URL.revokeObjectURL(url);
}
const createSimplePrompt = (title: string, placeholder = "") => {
const value = window.prompt(title, placeholder);
if (!value || !value.trim()) return null;
return value.trim();
};
2026-01-02 07:25:50 +08:00
// 修补 svg.js rbox 在节点未挂载时抛出的异常
const patchSvgRbox = async () => {
// 仅在浏览器环境生效
if (typeof window === "undefined") return;
const svgModule = await import("@svgdotjs/svg.js");
2026-01-07 07:25:07 +08:00
type SvgCtorPrototype = {
rbox?: (ref?: unknown) => unknown;
__wolaiRboxPatched?: boolean;
};
type SvgCtor = { prototype?: SvgCtorPrototype } | undefined;
const svgModuleTyped = svgModule as unknown as { Element?: SvgCtor; G?: SvgCtor };
const candidates = [svgModuleTyped.Element, svgModuleTyped.G];
2026-01-05 06:59:54 +08:00
let warned = false;
2026-01-02 07:25:50 +08:00
candidates.forEach((Ctor) => {
if (!Ctor?.prototype) return;
if (Ctor.prototype.__wolaiRboxPatched) return;
const original = Ctor.prototype.rbox;
if (typeof original !== "function") return;
Ctor.prototype.rbox = function patchedRbox(ref?: unknown) {
try {
return original.call(this, ref);
} catch (error) {
2026-01-05 06:59:54 +08:00
// 退化到 DOM 的 getBoundingClientRect 计算,避免初次渲染时出现 (0,0)
2026-01-07 07:25:07 +08:00
type SvgElementLike = { node?: Element | null; el?: Element | null };
const maybeThis = this as unknown as SvgElementLike;
const el = maybeThis?.node ?? maybeThis?.el ?? null;
2026-01-05 06:59:54 +08:00
const rect =
el && typeof el.getBoundingClientRect === "function"
? el.getBoundingClientRect()
: null;
const viewportWidth = typeof window !== "undefined" ? window.innerWidth : 0;
const viewportHeight =
typeof window !== "undefined" ? window.innerHeight : 0;
const defaultWidth = Math.max(120, Math.floor(viewportWidth * 0.2));
const defaultHeight = Math.max(60, Math.floor(viewportHeight * 0.1));
const fallback = rect && rect.width && rect.height
? {
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
x2: rect.x + rect.width,
y2: rect.y + rect.height,
cx: rect.x + rect.width / 2,
cy: rect.y + rect.height / 2,
}
: {
x: Math.max(0, (viewportWidth - defaultWidth) / 2),
y: Math.max(0, (viewportHeight - defaultHeight) / 2),
width: defaultWidth,
height: defaultHeight,
x2: Math.max(0, (viewportWidth + defaultWidth) / 2),
y2: Math.max(0, (viewportHeight + defaultHeight) / 2),
cx: Math.max(0, viewportWidth / 2),
cy: Math.max(0, viewportHeight / 2),
};
if (!warned) {
console.warn("rbox 失败,使用 DOM 边界框降级避免崩溃", error, fallback);
warned = true;
}
return fallback;
2026-01-02 07:25:50 +08:00
}
};
Ctor.prototype.__wolaiRboxPatched = true;
});
};
const applyToActiveNodes = (
mindmap: MindMapInstance | null,
handler: (node: unknown) => void,
) => {
if (!mindmap) {
window.alert("思维导图尚未初始化");
return;
}
2026-01-05 06:59:54 +08:00
const renderer = mindmap.renderer;
const list = renderer?.activeNodeList;
if (!list || list.length === 0) {
2026-01-05 06:59:54 +08:00
// 优先尝试 lastActiveNodeList(部分操作会短暂清空 activeNodeList
2026-01-07 07:25:07 +08:00
const lastActive = renderer?.lastActiveNodeList?.[0];
2026-01-05 06:59:54 +08:00
const root = renderer?.root ?? renderer?.renderTree?._node;
const fallback = lastActive ?? root;
if (fallback) {
renderer?.addNodeToActiveList?.(fallback);
mindmap.execCommand?.("SET_NODE_ACTIVE", fallback, true);
} else {
window.alert("请选择至少一个节点再执行该操作");
return;
}
}
2026-01-05 06:59:54 +08:00
(renderer?.activeNodeList ?? []).forEach(handler);
};
// 确保存在激活节点后再执行命令,若无则自动选中根节点
const ensureActiveBefore = (mindmap?: MindMapInstance | null) => {
const mm = mindmap ?? null;
if (!mm) {
window.alert("思维导图尚未初始化");
return null;
}
2026-01-05 06:59:54 +08:00
const renderer = mm.renderer;
const list = renderer?.activeNodeList ?? [];
if (!list || list.length === 0) {
2026-01-07 07:25:07 +08:00
const lastActive = renderer?.lastActiveNodeList?.[0];
2026-01-05 06:59:54 +08:00
const root = renderer?.root ?? renderer?.renderTree?._node;
const fallback = lastActive ?? root;
if (fallback) {
// 主动维护激活列表,避免首次插入子节点时因无激活节点导致插入位置错误
// 先确保根节点居中,避免后续插入节点跑到 (0,0)
2026-01-09 07:22:27 +08:00
// 注意:setRootNodeCenter 内部依赖 renderer.root(而不是 renderTree._node)。
// 在“刚初始化/刚插入第二张导图”的窗口期里 renderer.root 可能仍是 null
// 此时调用会触发内部解构异常,导致快捷键/工具栏命令中断。
if (renderer?.root && renderer?.setRootNodeCenter) {
try {
renderer.setRootNodeCenter();
} catch {
// ignore
}
}
2026-01-05 06:59:54 +08:00
mm.view?.fit?.();
2026-01-07 07:25:07 +08:00
if (renderer?.clearActiveNodeList) renderer.clearActiveNodeList();
2026-01-05 06:59:54 +08:00
if (renderer?.addNodeToActiveList) renderer.addNodeToActiveList(fallback, true);
if (renderer?.emitNodeActiveEvent) renderer.emitNodeActiveEvent(fallback);
mm.execCommand?.("SET_NODE_ACTIVE", fallback, true);
} else {
window.alert("请先选中一个节点");
return null;
}
}
return mm;
};
const MindmapBlockView = ({
block,
editor,
fullscreen = false,
}: {
2026-01-07 07:25:07 +08:00
block: SpecificBlock<
CustomBlockSchema,
"mindmap",
DefaultInlineContentSchema,
DefaultStyleSchema
>;
editor: BlockNoteEditor<CustomBlockSchema>;
fullscreen?: boolean;
}) => {
const containerRef = useRef<HTMLDivElement | null>(null);
const fileInputRef = useRef<HTMLInputElement | null>(null);
2026-01-07 07:25:07 +08:00
const [mindmap, setMindmap] = useState<MindMapInstance | null>(null);
2026-01-05 06:59:54 +08:00
const mindmapReadyRef = useRef(false);
const mindmapRef = useRef<MindMapInstance | null>(null);
const hasLocalEditsRef = useRef(false);
const applyingRemoteRef = useRef(false);
const [canBack, setCanBack] = useState(false);
const [canForward, setCanForward] = useState(false);
2026-01-07 07:25:07 +08:00
const [activeNodes, setActiveNodes] = useState<MindMapNode[]>([]);
const [painterMode, setPainterMode] = useState(false);
const [showMiniMap, setShowMiniMap] = useState(false);
const [activeSidebar, setActiveSidebar] = useState<SidebarPanel | null>(null);
2025-12-30 18:39:38 +08:00
const [showNoteModal, setShowNoteModal] = useState(false);
const [noteContent, setNoteContent] = useState("");
const [localFullscreen, setLocalFullscreen] = useState(false);
const effectiveFullscreen = fullscreen || localFullscreen;
const wrapperRef = useRef<HTMLDivElement | null>(null);
const hotkeyScopeRef = useRef(false);
2026-01-09 07:22:27 +08:00
const lastInteractionAtRef = useRef(0);
const skipNextPasteRef = useRef(false);
const persistDataRef = useRef<((data: unknown) => void) | null>(null);
const recentNodeDblclickRef = useRef(false);
2026-01-09 07:22:27 +08:00
const [fullscreenApiActive, setFullscreenApiActive] = useState(false);
const deletingRef = useRef(false);
const getRootNode = useCallback((mm?: MindMapInstance | null) => {
const instance = mm ?? mindmap;
return instance?.renderer?.root ?? instance?.renderer?.renderTree?._node ?? null;
}, [mindmap]);
2026-01-02 07:25:50 +08:00
const docId = useMemo(
() =>
block.props.docId ||
(typeof window !== "undefined"
? window.location.pathname.split("/").pop() ?? ""
: ""),
2026-01-07 07:25:07 +08:00
[block.props.docId],
2026-01-02 07:25:50 +08:00
);
2026-01-08 06:28:14 +08:00
const mindmapId = block.id;
2026-01-02 07:25:50 +08:00
useEffect(() => {
hasLocalEditsRef.current = false;
applyingRemoteRef.current = false;
}, [docId]);
2026-01-08 06:28:14 +08:00
const autosaveKey = useMemo(() => {
if (docId) return `${STORAGE_PREFIX}${docId}:${mindmapId}`;
return `${STORAGE_PREFIX}${mindmapId}`;
}, [docId, mindmapId]);
const initialDataRef = useRef<unknown>(null);
if (initialDataRef.current === null) {
const cached = typeof window !== "undefined" ? window.localStorage.getItem(autosaveKey) : null;
if (cached) {
try {
2026-01-07 21:21:22 +08:00
initialDataRef.current = canonicalizeMindmapData(JSON.parse(cached));
} catch {
2026-01-07 21:21:22 +08:00
initialDataRef.current = canonicalizeMindmapData(block.props.data ?? defaultMindmapData);
}
} else {
2026-01-07 21:21:22 +08:00
initialDataRef.current = canonicalizeMindmapData(block.props.data ?? defaultMindmapData);
}
}
2026-01-02 07:25:50 +08:00
// 优先加载本地文件,其次 Supabase(通过后端 API
useEffect(() => {
let cancelled = false;
if (!docId) return;
(async () => {
try {
2026-01-08 06:28:14 +08:00
// 多导图:按 docId + mindmapId 拉取
const resp = await fetch(`/api/mindmap/${docId}/${mindmapId}`);
2026-01-02 07:25:50 +08:00
if (!resp.ok) return;
const payload = await resp.json().catch(() => null);
const data = payload?.data;
if (!data || cancelled) return;
// 若用户已在本地做过编辑,则不再用远端数据覆盖,避免“插入节点后又被重置”
if (hasLocalEditsRef.current) return;
2026-01-07 21:21:22 +08:00
initialDataRef.current = canonicalizeMindmapData(data);
2026-01-02 07:25:50 +08:00
if (mindmap) {
applyingRemoteRef.current = true;
try {
2026-01-07 21:21:22 +08:00
mindmap.setData(initialDataRef.current);
mindmap.command.clearHistory();
} finally {
window.setTimeout(() => {
applyingRemoteRef.current = false;
}, 0);
}
2026-01-02 07:25:50 +08:00
}
} catch (error) {
console.warn("加载本地/远端思维导图失败", error);
}
})();
return () => {
cancelled = true;
};
2026-01-08 06:28:14 +08:00
}, [docId, mindmap, mindmapId]);
2026-01-02 07:25:50 +08:00
// 记录“最近一次指针交互是否发生在思维导图块内”,用于键盘快捷键作用域
useEffect(() => {
const onPointerDownCapture = (e: Event) => {
const wrapper = wrapperRef.current;
if (!wrapper) return;
2026-01-09 07:22:27 +08:00
const targetNode = e.target as Node | null;
const inWrapper = !!(targetNode && wrapper.contains(targetNode));
hotkeyScopeRef.current = inWrapper;
lastInteractionAtRef.current = inWrapper ? Date.now() : 0;
// 关键:simple-mind-map 的画布/节点有时会 stopPropagation,导致外层 onPointerDown
// 不触发,从而无法把焦点从 BlockNote 编辑器移到思维导图块上,最终表现为 Enter/Tab
// 等快捷键不生效。
// 这里用 capture 阶段兜底:只要点击发生在 wrapper 内,就把焦点拉到 wrapper 上。
if (!inWrapper) return;
const target = (targetNode as HTMLElement | null) ?? null;
if (target) {
const tag = target.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
// 节点文本编辑中不抢焦点,避免影响输入/粘贴(仅判断 simple-mind-map 的节点编辑元素)
const mm = mindmapRef.current;
const editClasses = mm?.editNodeClassList;
if (editClasses) {
for (const cls of editClasses) {
if (!cls) continue;
if (target.classList?.contains(cls)) return;
const found = target.closest?.(`.${cls}`);
if (found) return;
}
}
}
// 这里用 setTimeout(0) 而不是 microtaskBlockNote/ProseMirror 可能会在同一轮事件里重新抢回焦点,
// 导致“选中节点后 Ctrl+V 把思维导图替换成纯文本”。延后一拍把焦点拉回 wrapper,保证快捷键/粘贴作用域稳定。
window.setTimeout(() => {
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}, 0);
};
2026-01-09 07:22:27 +08:00
document.addEventListener("pointerdown", onPointerDownCapture, true);
document.addEventListener("mousedown", onPointerDownCapture, true);
return () => {
2026-01-09 07:22:27 +08:00
document.removeEventListener("pointerdown", onPointerDownCapture, true);
document.removeEventListener("mousedown", onPointerDownCapture, true);
};
}, []);
2026-01-09 07:22:27 +08:00
// 关键:阻止鼠标事件冒泡到 BlockNote/ProseMirror(它们会在 contenteditable 上处理 mousedown,从而产生 NodeSelection)。
// 不能用 React 的 onMouseDown(事件委托在 document,太晚了),必须用原生监听挂在 wrapper 上,确保在 bubble 链路中先于 editor DOM。
useEffect(() => {
const wrapper = wrapperRef.current;
if (!wrapper) return;
const stopBubble = (e: Event) => {
const target = e.target as HTMLElement | null;
if (!target) return;
// 节点文本编辑态 / 输入框:允许事件继续冒泡,避免影响输入法/选择/粘贴
const tag = target.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
const mm = mindmapRef.current;
const editClasses = mm?.editNodeClassList;
if (editClasses) {
for (const cls of editClasses) {
if (!cls) continue;
if (target.classList?.contains(cls)) return;
const found = target.closest?.(`.${cls}`);
if (found) return;
}
}
// 只要在思维导图块内点击,就阻止冒泡到 editor,避免“Ctrl+V 替换整块”
e.stopPropagation();
hotkeyScopeRef.current = true;
lastInteractionAtRef.current = Date.now();
// 同步/异步都尝试一次,把焦点拉到 wrapper,保证快捷键稳定
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
window.setTimeout(() => {
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}, 0);
};
wrapper.addEventListener("mousedown", stopBubble);
wrapper.addEventListener("pointerdown", stopBubble);
return () => {
wrapper.removeEventListener("mousedown", stopBubble);
wrapper.removeEventListener("pointerdown", stopBubble);
};
}, [effectiveFullscreen]);
// 标记“节点双击”事件,避免和“画布双击进入全屏”产生冲突
useEffect(() => {
if (!mindmap) return;
const mark = () => {
recentNodeDblclickRef.current = true;
window.setTimeout(() => {
recentNodeDblclickRef.current = false;
}, 0);
};
mindmap.on?.("node_dblclick", mark);
return () => {
mindmap.off?.("node_dblclick", mark);
};
}, [mindmap]);
const exitLocalFullscreen = useCallback(() => {
setActiveSidebar(null);
if (fullscreen) return;
if (typeof document === "undefined") {
setLocalFullscreen(false);
return;
}
if (document.fullscreenElement) {
document
.exitFullscreen()
.catch(() => {
// ignore
})
.finally(() => setLocalFullscreen(false));
return;
}
setLocalFullscreen(false);
}, [fullscreen]);
const enterLocalFullscreen = useCallback(() => {
if (fullscreen) return;
setLocalFullscreen(true);
setActiveSidebar(null);
if (typeof document === "undefined") return;
if (!document.fullscreenEnabled) return;
if (document.fullscreenElement) return;
// 必须在“用户手势回调”中同步调用,避免 Fullscreen API 被浏览器拒绝
try {
2026-01-07 07:25:07 +08:00
const target = document.documentElement as unknown as {
requestFullscreen?: () => Promise<void>;
};
const p = target.requestFullscreen?.();
if (p && typeof p.catch === "function") {
p.catch(() => {
// ignore:失败则保持 Portal 伪全屏
});
}
} catch {
// ignore:失败则保持 Portal 伪全屏
}
}, [fullscreen]);
// 沉浸式全屏:锁定页面滚动、支持 ESC 退出(仅对内嵌全屏生效)
useEffect(() => {
if (!localFullscreen) return;
if (typeof document === "undefined") return;
const prevOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
// 进入全屏后尽量把焦点放到思维导图容器上,保证快捷键立即生效
queueMicrotask(() => {
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
});
const onKeyDown = (e: KeyboardEvent) => {
if (e.key !== "Escape") return;
if (fullscreen) return; // 外部传入的 fullscreen 不在这里处理
e.preventDefault();
e.stopPropagation();
exitLocalFullscreen();
};
window.addEventListener("keydown", onKeyDown, true);
return () => {
window.removeEventListener("keydown", onKeyDown, true);
document.body.style.overflow = prevOverflow;
};
}, [localFullscreen, fullscreen, exitLocalFullscreen]);
// “真全屏”:使用 Fullscreen API 隐藏浏览器/桌面窗口的外层 UIElectron/Web 都可用)
useEffect(() => {
if (typeof document === "undefined") return;
const onFsChange = () => {
const active = Boolean(document.fullscreenElement);
setFullscreenApiActive(active);
2026-01-07 18:38:56 +08:00
// 注意:浏览器在打开原生对话框(例如 file picker / alert / confirm)时可能会自动退出
// Fullscreen API。此时不应退出“沉浸式全屏”UI,否则会导致用户在全屏编辑中执行导入/新建
// 等操作时被强制退出全屏。
};
document.addEventListener("fullscreenchange", onFsChange);
return () => {
document.removeEventListener("fullscreenchange", onFsChange);
};
}, [localFullscreen]);
// 让 Enter/Tab/复制/粘贴在“思维导图块”内生效(避免 BlockNote 抢走快捷键)
useLayoutEffect(() => {
const onKeyDownCapture = (e: KeyboardEvent) => {
2026-01-09 07:22:27 +08:00
const inScope =
hotkeyScopeRef.current ||
(lastInteractionAtRef.current > 0 &&
Date.now() - lastInteractionAtRef.current < 60_000);
if (!inScope) return;
const mm = mindmapRef.current;
if (!mm || !mindmapReadyRef.current) return;
const target = e.target as HTMLElement | null;
2026-01-07 07:25:07 +08:00
const editClasses = mm.editNodeClassList;
const wrapper = wrapperRef.current;
const isInWrapper = Boolean(wrapper && target && wrapper.contains(target));
2026-01-09 07:22:27 +08:00
const isNodeTextEditing = (() => {
if (!target || !isInWrapper) return false;
const tag = target.tagName;
2026-01-09 07:22:27 +08:00
if (tag === "INPUT" || tag === "TEXTAREA") return true;
// 仅在 simple-mind-map 的“节点文本编辑元素”上判定为编辑态;不要用 isContentEditable 泛化判断,
// 否则会把库内部的隐藏输入层误判成编辑态,导致 Ctrl+C/Ctrl+V 失效。
if (editClasses) {
for (const cls of editClasses) {
2026-01-09 07:22:27 +08:00
if (!cls) continue;
if (target.classList?.contains(cls)) return true;
const found = target.closest?.(`.${cls}`);
if (found) return true;
}
}
2026-01-09 07:22:27 +08:00
return false;
})();
const isMod = e.ctrlKey || e.metaKey;
const key = e.key;
const lower = key.toLowerCase();
const stop = () => {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
};
const pickTargetNode = (inst: MindMapInstance) => {
2026-01-07 07:25:07 +08:00
const renderer = inst.renderer;
const active = renderer.activeNodeList ?? [];
const last = renderer.lastActiveNodeList ?? [];
const root = renderer.root ?? renderer.renderTree?._node ?? null;
return active[0] ?? last[0] ?? root ?? null;
};
if (key === "Enter" && !e.shiftKey && !e.altKey && !isMod) {
stop();
const inst = ensureActiveBefore(mm);
if (!inst) return;
const node = pickTargetNode(inst);
if (!node) return;
inst.execCommand?.("SET_NODE_ACTIVE", node, true);
inst.execCommand?.("INSERT_NODE", false, [node]);
2026-01-09 07:22:27 +08:00
// 兜底:某些情况下 INSERT_NODE 不触发 data_change(例如被外层捕获键盘
// 事件拦截导致内部 Keyboard 插件不走),这里主动做一次防抖保存,确保
// 切换全屏/刷新后不会丢失。
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
return;
}
if (key === "Tab" && !e.shiftKey && !e.altKey && !isMod) {
stop();
const inst = ensureActiveBefore(mm);
if (!inst) return;
const node = pickTargetNode(inst);
if (!node) return;
inst.execCommand?.("SET_NODE_ACTIVE", node, true);
inst.execCommand?.("INSERT_CHILD_NODE", false, [node]);
2026-01-09 07:22:27 +08:00
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
return;
}
2026-01-09 07:22:27 +08:00
// 节点文本编辑中:只接管 Enter/Tab;其余快捷键交给默认输入行为
//(例如 Ctrl+C/Ctrl+V 作为文本复制粘贴)。
if (isNodeTextEditing) return;
if (isMod && lower === "c") {
stop();
const inst = ensureActiveBefore(mm);
if (!inst) return;
2026-01-09 07:22:27 +08:00
const node = pickTargetNode(inst);
if (node) {
inst.execCommand?.("SET_NODE_ACTIVE", node, true);
}
2026-01-07 07:25:07 +08:00
inst.renderer.copy?.();
return;
}
if (isMod && lower === "v") {
2026-01-09 07:22:27 +08:00
// 兜底:某些场景下(尤其是嵌入编辑器时)浏览器仍会触发原生 paste 事件。
// 这里标记一次,避免我们在 paste capture 里重复执行粘贴逻辑。
skipNextPasteRef.current = true;
window.setTimeout(() => {
skipNextPasteRef.current = false;
}, 200);
stop();
const inst = ensureActiveBefore(mm);
if (!inst) return;
2026-01-07 07:25:07 +08:00
const renderer = inst.renderer;
const copyData = renderer.beingCopyData ?? null;
if (copyData) {
inst.execCommand?.("PASTE_NODE", copyData);
2026-01-09 07:22:27 +08:00
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
return;
}
2026-01-07 07:25:07 +08:00
renderer.paste?.();
2026-01-09 07:22:27 +08:00
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
}
};
window.addEventListener("keydown", onKeyDownCapture, true);
return () => {
2026-01-09 07:22:27 +08:00
window.removeEventListener("keydown", onKeyDownCapture, true);
};
}, []);
// 兜底:在非全屏嵌入 BlockNote 时,Ctrl+V 可能仍然触发编辑器的 paste,导致思维导图块被替换成纯文本。
// 这里在 capture 阶段拦截 paste:当“最近一次指针交互在思维导图块内”且当前不在节点文本编辑态时,
// 强制把粘贴交给 simple-mind-map,并阻止 BlockNote 继续处理。
useLayoutEffect(() => {
const onPasteCapture = (e: ClipboardEvent) => {
const inScope =
hotkeyScopeRef.current ||
(lastInteractionAtRef.current > 0 &&
Date.now() - lastInteractionAtRef.current < 60_000);
if (!inScope) return;
const mm = mindmapRef.current;
if (!mm || !mindmapReadyRef.current) return;
const wrapper = wrapperRef.current;
const active = (typeof document !== "undefined"
? (document.activeElement as HTMLElement | null)
: null);
const isTextEditingNow = (() => {
if (!wrapper || !active) return false;
if (!wrapper.contains(active)) return false;
const tag = active.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return true;
const editClasses = mm.editNodeClassList;
if (editClasses) {
for (const cls of editClasses) {
if (!cls) continue;
if (active.classList?.contains(cls)) return true;
const found = active.closest?.(`.${cls}`);
if (found) return true;
}
}
return false;
})();
// 节点文本编辑态:允许默认粘贴(粘贴到节点文本)
if (isTextEditingNow) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation?.();
// 如果刚刚已经在 keydown 里处理过 Ctrl+V,这里只负责阻止 BlockNote 的 paste
if (skipNextPasteRef.current) {
skipNextPasteRef.current = false;
return;
}
const inst = ensureActiveBefore(mm);
if (!inst) return;
// 优先交给 simple-mind-map 自己的 clipboard 逻辑:支持外部文本/图片/链接以及内部节点复制格式
try {
inst.renderer.paste?.();
} catch {
// ignore
}
// 兜底持久化:避免快速切换视图导致“看起来没保存”
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
};
window.addEventListener("paste", onPasteCapture, true);
return () => {
window.removeEventListener("paste", onPasteCapture, true);
};
}, []);
// 兜底:Ctrl+C 可能被 BlockNote/ProseMirror 先行拦截,导致我们 keydown 捕获不到。
// 这里直接在 copy 事件的 capture 阶段接管,确保“选中节点 -> Ctrl+C”一定能复制节点数据。
useLayoutEffect(() => {
const onCopyCapture = (e: ClipboardEvent) => {
const inScope =
hotkeyScopeRef.current ||
(lastInteractionAtRef.current > 0 &&
Date.now() - lastInteractionAtRef.current < 60_000);
if (!inScope) return;
const mm = mindmapRef.current;
if (!mm || !mindmapReadyRef.current) return;
const wrapper = wrapperRef.current;
const active = (typeof document !== "undefined"
? (document.activeElement as HTMLElement | null)
: null);
const isTextEditingNow = (() => {
if (!wrapper || !active) return false;
if (!wrapper.contains(active)) return false;
const tag = active.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return true;
const editClasses = mm.editNodeClassList;
if (editClasses) {
for (const cls of editClasses) {
if (!cls) continue;
if (active.classList?.contains(cls)) return true;
const found = active.closest?.(`.${cls}`);
if (found) return true;
}
}
return false;
})();
if (isTextEditingNow) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation?.();
const inst = ensureActiveBefore(mm);
if (!inst) return;
const renderer = inst.renderer;
const node =
renderer.activeNodeList?.[0] ??
renderer.lastActiveNodeList?.[0] ??
renderer.root ??
renderer.renderTree?._node ??
null;
if (node) inst.execCommand?.("SET_NODE_ACTIVE", node, true);
try {
inst.renderer.copy?.();
} catch {
// ignore
}
};
window.addEventListener("copy", onCopyCapture, true);
return () => {
window.removeEventListener("copy", onCopyCapture, true);
};
}, []);
// 额外兜底:部分浏览器/编辑器会优先通过 beforeinput(insertFromPaste) 走粘贴路径,
// 即便我们拦截了 paste,也可能仍触发“替换掉思维导图块”的输入。
// 这里在 capture 阶段统一阻止 paste 相关的 beforeinput,并把实际粘贴交给 simple-mind-map。
useLayoutEffect(() => {
const onBeforeInputCapture = (e: InputEvent) => {
const inputType = (e as unknown as { inputType?: string }).inputType || "";
if (!inputType || !inputType.toLowerCase().includes("paste")) return;
const inScope =
hotkeyScopeRef.current ||
(lastInteractionAtRef.current > 0 &&
Date.now() - lastInteractionAtRef.current < 60_000);
if (!inScope) return;
const mm = mindmapRef.current;
if (!mm || !mindmapReadyRef.current) return;
// 节点文本编辑态:允许默认粘贴(粘贴到节点文本)
const wrapper = wrapperRef.current;
const active = (typeof document !== "undefined"
? (document.activeElement as HTMLElement | null)
: null);
const isTextEditingNow = (() => {
if (!wrapper || !active) return false;
if (!wrapper.contains(active)) return false;
const tag = active.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return true;
const editClasses = mm.editNodeClassList;
if (editClasses) {
for (const cls of editClasses) {
if (!cls) continue;
if (active.classList?.contains(cls)) return true;
const found = active.closest?.(`.${cls}`);
if (found) return true;
}
}
return false;
})();
if (isTextEditingNow) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation?.();
const inst = ensureActiveBefore(mm);
if (!inst) return;
try {
inst.renderer.paste?.();
} catch {
// ignore
}
hasLocalEditsRef.current = true;
try {
const snapshot = inst.getData?.(true) ?? inst.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
};
window.addEventListener("beforeinput", onBeforeInputCapture, true);
return () => {
window.removeEventListener("beforeinput", onBeforeInputCapture, true);
};
}, []);
const persistData = useCallback(
(data: unknown) => {
if (!editor) return;
2026-01-07 21:21:22 +08:00
// 关键:全屏/非全屏切换会重建 mindmap 实例,初始化数据必须跟随最新编辑
// 否则切换视图时会用旧数据,表现为“看起来没保存”
const safe = canonicalizeMindmapData(data);
initialDataRef.current = safe;
window.localStorage.setItem(autosaveKey, JSON.stringify(safe));
editor.updateBlock(block, { props: { ...block.props, data: safe } });
2026-01-02 07:25:50 +08:00
if (docId) {
// 同步到本地文件 + Supabase(弱依赖)
2026-01-08 06:28:14 +08:00
fetch(`/api/mindmap/${docId}/${mindmapId}`, {
2026-01-02 07:25:50 +08:00
method: "POST",
headers: { "Content-Type": "application/json" },
2026-01-07 21:21:22 +08:00
body: JSON.stringify({ data: safe }),
2026-01-02 07:25:50 +08:00
})
.then((resp) => {
if (resp.ok) {
2026-01-08 06:28:14 +08:00
const fileName = `mindmap-${mindmapId}.json`;
2026-01-05 06:59:54 +08:00
emitAssetsChanged(docId, {
2026-01-08 06:28:14 +08:00
id: mindmapId,
2026-01-05 06:59:54 +08:00
document_id: docId,
asset_type: "mindmap",
2026-01-08 06:28:14 +08:00
file_name: fileName,
file_url: `/documents/${docId}/${fileName}`,
2026-01-05 06:59:54 +08:00
});
2026-01-02 07:25:50 +08:00
}
})
.catch((err) => console.warn("思维导图同步失败", err));
}
},
2026-01-08 06:28:14 +08:00
[autosaveKey, block, docId, editor, mindmapId],
);
2026-01-09 07:22:27 +08:00
useEffect(() => {
persistDataRef.current = persistData;
}, [persistData]);
2026-01-09 07:22:27 +08:00
// “有上限的防抖保存”:连续 data_change 只会合并为一次保存,但不会因为持续变化
// 而无限期推迟(避免测试/用户快速切换全屏时出现“看起来没保存”)。
const persistTimerRef = useRef<number | null>(null);
const pendingPersistRef = useRef<unknown | null>(null);
const schedulePersist = useCallback(
(data: unknown) => {
pendingPersistRef.current = data;
if (typeof window === "undefined") return;
if (persistTimerRef.current != null) return;
persistTimerRef.current = window.setTimeout(() => {
persistTimerRef.current = null;
const pending = pendingPersistRef.current;
pendingPersistRef.current = null;
if (pending) persistData(pending);
}, 800);
},
[persistData],
);
useEffect(() => {
return () => {
if (persistTimerRef.current != null && typeof window !== "undefined") {
window.clearTimeout(persistTimerRef.current);
persistTimerRef.current = null;
}
const pending = pendingPersistRef.current;
pendingPersistRef.current = null;
if (pending) {
try {
persistData(pending);
} catch {
// ignore
}
}
};
}, [persistData]);
2026-01-02 07:25:50 +08:00
const initialSyncDone = useRef(false);
useEffect(() => {
if (!docId || !mindmap || initialSyncDone.current) return;
initialSyncDone.current = true;
2026-01-07 21:21:22 +08:00
const data = canonicalizeMindmapData(
mindmap.getData?.(true) ?? mindmap.getData?.() ?? initialDataRef.current ?? defaultMindmapData,
);
2026-01-05 06:59:54 +08:00
(async () => {
try {
2026-01-08 06:28:14 +08:00
const resp = await fetch(`/api/mindmap/${docId}/${mindmapId}`, {
2026-01-05 06:59:54 +08:00
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data }),
});
if (!resp.ok) {
console.warn(
"初次创建思维导图文件失败",
resp.status,
await resp.text().catch(() => ""),
);
2026-01-02 07:25:50 +08:00
}
2026-01-05 06:59:54 +08:00
} catch (err) {
console.warn("初次创建思维导图文件失败", err);
} finally {
// 即便远端失败,也通知侧边栏刷新,保证本地文件树及时更新
2026-01-08 06:28:14 +08:00
const fileName = `mindmap-${mindmapId}.json`;
2026-01-05 06:59:54 +08:00
emitAssetsChanged(docId, {
2026-01-08 06:28:14 +08:00
id: mindmapId,
2026-01-05 06:59:54 +08:00
document_id: docId,
asset_type: "mindmap",
2026-01-08 06:28:14 +08:00
file_name: fileName,
file_url: `/documents/${docId}/${fileName}`,
2026-01-05 06:59:54 +08:00
});
}
})();
2026-01-08 06:28:14 +08:00
}, [docId, mindmap, mindmapId, initialDataRef]);
2026-01-02 07:25:50 +08:00
// 初始选中根节点,后续不强制抢焦点,允许用户自由选择
useEffect(() => {
if (!mindmap || activeNodes.length > 0) return;
const rootNode = mindmap.renderer?.root ?? mindmap.renderer?.renderTree?._node;
if (rootNode) {
2026-01-07 07:25:07 +08:00
setActiveNodes([rootNode as MindMapNode]);
mindmap.renderer.activeNodeList = [rootNode];
mindmap.renderer.lastActiveNodeList = [rootNode];
mindmap.emit?.("node_active", rootNode, [rootNode]);
}
}, [mindmap, activeNodes.length]);
2026-01-08 06:28:14 +08:00
// mindmap 实例一旦就绪,立刻向侧边栏广播,确保文件树即时显示 mindmap-<id>.json
2026-01-05 06:59:54 +08:00
useEffect(() => {
if (!docId || !mindmap) return;
2026-01-08 06:28:14 +08:00
const fileName = `mindmap-${mindmapId}.json`;
2026-01-05 06:59:54 +08:00
emitAssetsChanged(docId, {
2026-01-08 06:28:14 +08:00
id: mindmapId,
2026-01-05 06:59:54 +08:00
document_id: docId,
asset_type: "mindmap",
2026-01-08 06:28:14 +08:00
file_name: fileName,
file_url: `/documents/${docId}/${fileName}`,
2026-01-05 06:59:54 +08:00
});
2026-01-08 06:28:14 +08:00
}, [docId, mindmap, mindmapId]);
2026-01-05 06:59:54 +08:00
useEffect(() => {
let destroyed = false;
2026-01-05 06:59:54 +08:00
let createdInstance: MindMapInstance | null = null;
(async () => {
2026-01-07 21:21:22 +08:00
// 进入/退出全屏会切换渲染树,ref 在某些时序下可能短暂为 null。
// 若这里直接 return,会导致新实例永远不创建,表现为“切换视图后空白/像没保存”。
const waitForContainer = async (): Promise<HTMLDivElement | null> => {
for (let i = 0; i < 60; i += 1) {
if (destroyed) return null;
if (containerRef.current) return containerRef.current;
await new Promise((r) => window.setTimeout(r, 50));
}
return containerRef.current;
};
const hostContainer = await waitForContainer();
if (!hostContainer) return;
const [
{ default: MindMap },
{ default: Painter },
{ default: AssociativeLine },
{ default: OuterFrame },
{ default: Exporter },
{ default: Formula },
{ default: RichText },
{ default: MiniMapPlugin },
{ default: Select },
{ default: Drag },
{ default: KeyboardNavigation },
2025-12-31 19:26:38 +08:00
{ default: NodeImgAdjust },
2026-01-02 07:25:50 +08:00
{ default: Scrollbar },
{ default: RainbowLines },
{ default: Watermark },
{ default: TouchEvent },
{ default: Cooperate },
{ default: Demonstrate },
{ default: MindMapLayoutPro },
{ default: NodeBase64ImageStorage },
{ default: ExportPDF },
{ default: ExportXMind },
2026-01-05 06:59:54 +08:00
MindMapNodeModule,
] = await Promise.all([
import("simple-mind-map"),
import("simple-mind-map/src/plugins/Painter.js"),
import("simple-mind-map/src/plugins/AssociativeLine.js"),
import("simple-mind-map/src/plugins/OuterFrame.js"),
import("simple-mind-map/src/plugins/Export.js"),
import("simple-mind-map/src/plugins/Formula.js"),
import("simple-mind-map/src/plugins/RichText.js"),
import("simple-mind-map/src/plugins/MiniMap.js"),
import("simple-mind-map/src/plugins/Select.js"),
import("simple-mind-map/src/plugins/Drag.js"),
import("simple-mind-map/src/plugins/KeyboardNavigation.js"),
2025-12-31 19:26:38 +08:00
import("simple-mind-map/src/plugins/NodeImgAdjust.js"),
2026-01-02 07:25:50 +08:00
import("simple-mind-map/src/plugins/Scrollbar.js"),
import("simple-mind-map/src/plugins/RainbowLines.js"),
import("simple-mind-map/src/plugins/Watermark.js"),
import("simple-mind-map/src/plugins/TouchEvent.js"),
import("simple-mind-map/src/plugins/Cooperate.js"),
import("simple-mind-map/src/plugins/Demonstrate.js"),
import("simple-mind-map/src/plugins/MindMapLayoutPro.js"),
2026-01-05 06:59:54 +08:00
import("simple-mind-map/src/plugins/NodeBase64ImageStorage.js"),
2026-01-02 07:25:50 +08:00
import("simple-mind-map/src/plugins/ExportPDF.js"),
import("simple-mind-map/src/plugins/ExportXMind.js"),
2026-01-05 06:59:54 +08:00
import("simple-mind-map/src/core/render/node/MindMapNode.js"),
]);
2026-01-05 06:59:54 +08:00
// React StrictMode(开发环境)会触发 effect 的“挂载-卸载-再挂载”流程:
// 若异步加载完成时已经卸载,不应再创建实例,否则可能留下残余 DOM,导致出现多个 root。
if (destroyed) return;
// 兜底修补 simple-mind-map 在创建富文本节点时 host/缓存未就绪导致的空指针
2026-01-07 07:25:07 +08:00
type MindMapNodeCtorLike = {
prototype: {
__wolaiRichtextPatched?: boolean;
createRichTextNode?: (...args: unknown[]) => unknown;
};
};
const MindMapNodeCtor = (MindMapNodeModule as unknown as { default?: MindMapNodeCtorLike })?.default;
2026-01-05 06:59:54 +08:00
if (
MindMapNodeCtor &&
!MindMapNodeCtor.prototype.__wolaiRichtextPatched
) {
const originalCreate = MindMapNodeCtor.prototype.createRichTextNode;
MindMapNodeCtor.prototype.__wolaiRichtextPatched = true;
MindMapNodeCtor.prototype.createRichTextNode = function patched(...args: unknown[]) {
2026-01-07 07:25:07 +08:00
type RichtextThis = {
mindMap?: { el?: HTMLElement | null; commonCaches?: Record<string, unknown> } | null;
};
const self = this as unknown as RichtextThis;
2026-01-05 06:59:54 +08:00
// host 兜底:优先使用实例容器,否则退回 body
2026-01-07 07:25:07 +08:00
const host: HTMLElement = (self?.mindMap?.el as HTMLElement | null) ?? document.body;
if (!self.mindMap) {
self.mindMap = { el: host, commonCaches: {} };
2026-01-05 06:59:54 +08:00
}
2026-01-07 07:25:07 +08:00
const el = self.mindMap.el;
if (!el || typeof el.appendChild !== "function") {
self.mindMap.el = host;
2026-01-05 06:59:54 +08:00
}
2026-01-07 07:25:07 +08:00
const caches = self.mindMap.commonCaches ?? (self.mindMap.commonCaches = {});
const measureKey = "measureRichtextNodeTextSizeEl";
if (!caches[measureKey]) {
2026-01-05 06:59:54 +08:00
const measureDiv = document.createElement("div");
measureDiv.style.position = "fixed";
measureDiv.style.left = "-999999px";
2026-01-07 07:25:07 +08:00
(self.mindMap.el ?? document.body).appendChild(measureDiv);
caches[measureKey] = measureDiv;
2026-01-05 06:59:54 +08:00
}
if (typeof originalCreate === "function") {
return originalCreate.apply(this, args);
}
return null;
};
}
const plugins = [
{ name: "Painter", plugin: Painter },
{ name: "AssociativeLine", plugin: AssociativeLine },
{ name: "OuterFrame", plugin: OuterFrame },
{ name: "Export", plugin: Exporter },
{ name: "Formula", plugin: Formula },
{ name: "RichText", plugin: RichText },
{ name: "MiniMap", plugin: MiniMapPlugin },
{ name: "Select", plugin: Select },
{ name: "Drag", plugin: Drag },
{ name: "KeyboardNavigation", plugin: KeyboardNavigation },
2025-12-31 19:26:38 +08:00
{ name: "NodeImgAdjust", plugin: NodeImgAdjust },
2026-01-02 07:25:50 +08:00
{ name: "Scrollbar", plugin: Scrollbar },
{ name: "RainbowLines", plugin: RainbowLines },
{ name: "Watermark", plugin: Watermark },
{ name: "TouchEvent", plugin: TouchEvent },
{ name: "Cooperate", plugin: Cooperate },
{ name: "Demonstrate", plugin: Demonstrate },
{ name: "MindMapLayoutPro", plugin: MindMapLayoutPro },
{ name: "NodeBase64ImageStorage", plugin: NodeBase64ImageStorage },
{ name: "ExportPDF", plugin: ExportPDF },
{ name: "ExportXMind", plugin: ExportXMind },
];
plugins.forEach(({ name, plugin }) => {
if (!plugin) {
console.warn(`思维导图插件加载失败:${name}`);
return;
}
2026-01-07 07:25:07 +08:00
const MindMapCtor = MindMap as unknown as {
hasPlugin?: (p: unknown) => number;
usePlugin?: (p: unknown) => void;
};
const hasPlugin = MindMapCtor.hasPlugin;
const notRegistered =
typeof hasPlugin === "function" ? hasPlugin(plugin) === -1 : true;
const registerPlugin = MindMapCtor.usePlugin;
if (notRegistered && typeof registerPlugin === "function") {
console.log(`注册插件: ${name}`);
2026-01-07 07:25:07 +08:00
registerPlugin(plugin);
}
});
2026-01-02 07:25:50 +08:00
const { nodeIconList, mergerIconList } = await loadIconModules();
await patchSvgRbox();
2026-01-05 06:59:54 +08:00
if (destroyed) return;
const hostEl = hostContainer;
// 防止同一容器残留旧实例的 svg/domStrictMode 或异常 destroy 场景)
try {
hostEl.replaceChildren();
} catch {
hostEl.innerHTML = "";
}
2026-01-02 07:25:50 +08:00
2026-01-07 21:21:22 +08:00
// 全屏/非全屏切换会重建实例:创建新实例前优先从本地缓存读取最新数据
// 以避免“全屏里编辑 → 退出全屏后内容消失 / 反之亦然”。
let dataForInitSource = "unknown";
const rawForInit = (() => {
if (typeof window === "undefined") {
dataForInitSource = "ssr-fallback";
return block.props.data ?? initialDataRef.current ?? defaultMindmapData;
}
try {
const cached = window.localStorage.getItem(autosaveKey);
if (cached) {
dataForInitSource = "localStorage";
return JSON.parse(cached);
}
} catch {
// ignore
}
dataForInitSource = block.props.data ? "block.props.data" : "initialDataRef";
return block.props.data ?? initialDataRef.current ?? defaultMindmapData;
})();
const dataForInit = canonicalizeMindmapData(rawForInit);
initialDataRef.current = dataForInit;
2026-01-07 07:25:07 +08:00
type MindMapConstructor = new (options: {
el: HTMLElement;
data: unknown;
theme: string;
layout: string;
mousewheelAction: string;
enableFreeDrag: boolean;
enableCtrlKeyNodeSelection: boolean;
fit: boolean;
useLeftKeySelectionRightKeyDrag: boolean;
createNewNodeBehavior: string;
iconList: unknown[];
}) => MindMapInstance;
const MindMapCtor = MindMap as unknown as MindMapConstructor;
const instance = new MindMapCtor({
2026-01-02 07:25:50 +08:00
el: hostEl,
2026-01-07 21:21:22 +08:00
data: dataForInit,
theme: "classic",
layout: "logicalStructure",
mousewheelAction: "zoom",
enableFreeDrag: true,
enableCtrlKeyNodeSelection: true,
fit: true,
useLeftKeySelectionRightKeyDrag: true,
2026-01-05 06:59:54 +08:00
// 新建节点默认激活,编辑由我们手动触发,避免初次插入时定位到 (0,0)
createNewNodeBehavior: "activeOnly",
2025-12-30 18:39:38 +08:00
// 传入扩展图标表,和官方一致
iconList: mergerIconList([
...nodeIconList,
...(iconConfig as unknown[]),
]),
2026-01-07 07:25:07 +08:00
});
2026-01-05 06:59:54 +08:00
createdInstance = instance;
mindmapRef.current = instance;
2026-01-05 06:59:54 +08:00
// 尽早标记为可用:主页面通过 `/mind` 插入后,若依赖 render_end/尺寸探测,可能导致按钮永远不可用
//render_end 可能早于监听注册触发,或容器尺寸长时间为 0)。命令本身不需要等待 render_end。
mindmapReadyRef.current = true;
// 确保测量节点的缓存容器始终挂在有效 DOM 上,避免 appendChild 空指针
2026-01-07 07:25:07 +08:00
type MindMapWithCaches = MindMapInstance & { commonCaches?: Record<string, unknown> };
const instanceWithCaches = instance as MindMapWithCaches;
if (!instanceWithCaches.commonCaches) {
instanceWithCaches.commonCaches = {};
2026-01-02 07:25:50 +08:00
}
2026-01-07 07:25:07 +08:00
const measureKey = "measureRichtextNodeTextSizeEl";
if (!instanceWithCaches.commonCaches[measureKey]) {
2026-01-02 07:25:50 +08:00
const measureDiv = document.createElement("div");
measureDiv.style.position = "fixed";
measureDiv.style.left = "-999999px";
2026-01-07 07:25:07 +08:00
instanceWithCaches.commonCaches[measureKey] = measureDiv;
hostEl.appendChild(measureDiv);
2026-01-02 07:25:50 +08:00
}
instance.setMode?.("edit");
2026-01-09 07:22:27 +08:00
// 兜底:在某些环境/焦点状态下,simple-mind-map 的 data_change 事件可能不会触发,
// 或者键盘插件直接执行命令但未走到我们的保存逻辑,最终表现为“节点能插入但不会持久化”。
// 这里对 execCommand 做轻量包装:当执行潜在的“结构变更”命令时,主动触发一次防抖保存。
const shouldPersistAfterCommand = (cmd: unknown) => {
if (typeof cmd !== "string") return false;
if (
cmd === "INSERT_NODE" ||
cmd === "INSERT_CHILD_NODE" ||
cmd === "PASTE_NODE" ||
cmd === "REMOVE_NODE" ||
cmd === "DELETE_NODE"
) {
return true;
}
return (
cmd.startsWith("INSERT_") ||
cmd.startsWith("PASTE_") ||
cmd.startsWith("REMOVE_") ||
cmd.startsWith("DELETE_")
);
};
const originalExecCommand =
typeof instance.execCommand === "function"
? instance.execCommand.bind(instance)
: null;
if (originalExecCommand) {
(instance as unknown as { execCommand: (...args: any[]) => any }).execCommand = (
cmd: unknown,
...args: any[]
) => {
const ret = originalExecCommand(cmd as any, ...args);
if (
!applyingRemoteRef.current &&
!deletingRef.current &&
shouldPersistAfterCommand(cmd)
) {
hasLocalEditsRef.current = true;
try {
const snapshot =
instance.getData?.(true) ?? instance.getData?.() ?? null;
if (snapshot) persistDataRef.current?.(snapshot);
} catch {
// ignore
}
}
return ret;
};
}
2026-01-05 06:59:54 +08:00
const centerAndFit = (retry = 0) => {
try {
const hostRect = hostEl.getBoundingClientRect?.();
2026-01-09 07:22:27 +08:00
if (!hostRect || hostRect.width < 10 || hostRect.height < 10) {
2026-01-05 06:59:54 +08:00
if (retry < 5) {
window.setTimeout(() => centerAndFit(retry + 1), 80);
}
return;
}
// 只要容器尺寸已可用,就允许工具栏命令执行;render_end 可能早于监听注册触发
//(尤其在首次构造时同步渲染),因此不要完全依赖 render_end 来标记就绪。
mindmapReadyRef.current = true;
const renderer = instance.renderer;
2026-01-07 07:25:07 +08:00
const rootNode = renderer?.root ?? renderer?.renderTree?._node;
2026-01-05 06:59:54 +08:00
if (rootNode && renderer?.setRootNodeCenter) {
renderer.setRootNodeCenter();
}
instance.view?.fit?.();
} catch (error) {
if (retry < 3) {
window.setTimeout(() => centerAndFit(retry + 1), 50);
} else {
console.warn("思维导图初始居中失败,已跳过", error);
}
}
};
window.requestAnimationFrame(() => centerAndFit());
window.setTimeout(() => centerAndFit(), 80);
if (typeof window !== "undefined") {
// 便于开发阶段在控制台直接调试实例
window.__mindmapInstance = instance;
2026-01-08 06:28:14 +08:00
const w = window as unknown as {
__mindmapInstancesById?: Record<string, MindMapInstance>;
};
if (!w.__mindmapInstancesById) w.__mindmapInstancesById = {};
w.__mindmapInstancesById[mindmapId] = instance;
}
setMindmap(instance);
instance.on?.("back_forward", (index: number, len: number) => {
setCanBack(index > 0);
setCanForward(index < len - 1);
});
2026-01-05 06:59:54 +08:00
// 渲染完成后再二次居中,并确保根节点被标记为激活
const handleRenderEnd = () => {
const renderer = instance.renderer;
const root = getRootNode(instance);
if (root) {
if (renderer?.clearActiveNodeList) renderer.clearActiveNodeList();
if (renderer?.addNodeToActiveList) renderer.addNodeToActiveList(root, true);
if (renderer?.emitNodeActiveEvent) renderer.emitNodeActiveEvent(root);
}
mindmapReadyRef.current = true;
centerAndFit();
};
// 注意:部分版本的 simple-mind-map 中 `once` 回调的触发顺序可能早于 `on`,
// 若在 once 里 off 监听,可能导致 handleRenderEnd 永远不执行,进而使工具栏按钮不可用。
// 这里统一用 on + 手动自销毁,确保首个 render_end 一定会执行 handleRenderEnd。
let renderHandled = false;
const onRenderEndOnce = () => {
if (renderHandled) return;
renderHandled = true;
try {
handleRenderEnd();
} finally {
instance.off?.("render_end", onRenderEndOnce);
}
};
instance.on?.("render_end", onRenderEndOnce);
2026-01-07 07:25:07 +08:00
instance.on?.("node_active", (_node: unknown, list: unknown[]) => {
if (!list || list.length === 0) return;
2026-01-07 07:25:07 +08:00
setActiveNodes((list || []) as MindMapNode[]);
2026-01-09 07:22:27 +08:00
// 兜底:部分场景下点击节点不会触发外层 pointerdown(被 stopPropagation),
// 这里用内部事件标记“当前在思维导图作用域内”,确保 Ctrl+C/Ctrl+V 不会被 BlockNote 抢走。
hotkeyScopeRef.current = true;
lastInteractionAtRef.current = Date.now();
window.setTimeout(() => {
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}, 0);
});
instance.on?.("node_click", (node: unknown) => {
2026-01-09 07:22:27 +08:00
// 确保点击即可激活(调用节点自身的 active 逻辑,保持和官方一致)
2026-01-05 06:59:54 +08:00
const renderer = instance.renderer;
2026-01-07 07:25:07 +08:00
const nodeWithActive = node as unknown as { active?: () => void };
if (typeof nodeWithActive.active === "function") {
nodeWithActive.active();
} else {
// 兜底:手动维护激活列表
2026-01-07 07:25:07 +08:00
if (renderer?.clearActiveNodeList) renderer.clearActiveNodeList();
if (renderer?.addNodeToActiveList) renderer.addNodeToActiveList(node, true);
if (renderer?.emitNodeActiveEvent) renderer.emitNodeActiveEvent(node);
}
2026-01-05 06:59:54 +08:00
const list = renderer?.activeNodeList ?? [];
2026-01-07 07:25:07 +08:00
setActiveNodes(
list.length === 0 && node
? [node as MindMapNode]
: (list as MindMapNode[]),
);
2026-01-09 07:22:27 +08:00
hotkeyScopeRef.current = true;
lastInteractionAtRef.current = Date.now();
window.setTimeout(() => {
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}, 0);
});
instance.on?.("painter_start", () => setPainterMode(true));
instance.on?.("painter_end", () => setPainterMode(false));
2026-01-07 21:21:22 +08:00
instance.on?.("data_change", () => {
if (!applyingRemoteRef.current) {
hasLocalEditsRef.current = true;
}
2026-01-07 21:21:22 +08:00
// data_change 的参数在部分场景下可能不是“可直接序列化的完整数据”
//(例如 children 非数组、text 缺失等),全屏切换重建时会触发 RichText 初始化崩溃。
// 这里统一改为从实例读取快照,确保保存/恢复的数据结构稳定。
const snapshot =
2026-01-09 07:22:27 +08:00
instance.getData?.(true) ?? instance.getData?.() ?? null;
if (snapshot) schedulePersist(snapshot);
});
2026-01-05 06:59:54 +08:00
const renderer = instance.renderer;
const rootNode = getRootNode(instance);
if (rootNode) {
2026-01-05 06:59:54 +08:00
// 初始化时主动标记根节点为选中,确保后续插入子节点有合法的父节点
2026-01-07 07:25:07 +08:00
if (renderer?.clearActiveNodeList) renderer.clearActiveNodeList();
if (renderer?.addNodeToActiveList) renderer.addNodeToActiveList(rootNode, true);
if (renderer?.emitNodeActiveEvent) renderer.emitNodeActiveEvent(rootNode);
setActiveNodes([rootNode as MindMapNode]);
}
2026-01-05 06:59:54 +08:00
// 若首次渲染时 root 仍未就绪,设置兜底延迟激活
window.setTimeout(() => {
const root = getRootNode(instance);
if (!root) return;
2026-01-07 07:25:07 +08:00
if (renderer?.clearActiveNodeList) renderer.clearActiveNodeList();
if (renderer?.addNodeToActiveList) renderer.addNodeToActiveList(root, true);
if (renderer?.emitNodeActiveEvent) renderer.emitNodeActiveEvent(root);
setActiveNodes([root as MindMapNode]);
2026-01-05 06:59:54 +08:00
}, 120);
if (destroyed) {
instance.destroy();
}
})();
return () => {
destroyed = true;
2026-01-05 06:59:54 +08:00
try {
// 切换“内嵌/全屏”会导致实例重建:这里尽量在销毁前同步一次数据,避免丢失最后一次编辑
const skipPersist =
deletingRef.current ||
(() => {
if (!docId || typeof window === "undefined") return false;
try {
const w = window as unknown as {
2026-01-08 06:28:14 +08:00
__wolaiMindmapDeletingKeys?: Set<string>;
};
2026-01-08 06:28:14 +08:00
const key = `${docId}:${mindmapId}`;
return Boolean(
w.__wolaiMindmapDeletingKeys?.has(docId) ||
w.__wolaiMindmapDeletingKeys?.has(key),
);
} catch {
return false;
}
})();
if (!skipPersist && createdInstance && typeof window !== "undefined") {
const data =
createdInstance.getData?.(true) ?? createdInstance.getData?.();
if (data) {
2026-01-07 21:21:22 +08:00
const safe = canonicalizeMindmapData(data);
initialDataRef.current = safe;
try {
2026-01-07 21:21:22 +08:00
window.localStorage.setItem(autosaveKey, JSON.stringify(safe));
} catch {
// ignore
}
try {
2026-01-07 21:21:22 +08:00
editor?.updateBlock(block, { props: { ...block.props, data: safe } });
} catch {
// ignore
}
if (docId) {
2026-01-08 06:28:14 +08:00
fetch(`/api/mindmap/${docId}/${mindmapId}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
2026-01-07 21:21:22 +08:00
body: JSON.stringify({ data: safe }),
}).catch(() => {
// ignore
});
}
}
}
2026-01-05 06:59:54 +08:00
createdInstance?.destroy();
} catch {
// ignore
}
if (typeof window !== "undefined") {
if (window.__mindmapInstance === createdInstance) {
window.__mindmapInstance = null;
}
2026-01-08 06:28:14 +08:00
try {
const w = window as unknown as { __mindmapInstancesById?: Record<string, MindMapInstance> };
if (w.__mindmapInstancesById && w.__mindmapInstancesById[mindmapId] === createdInstance) {
delete w.__mindmapInstancesById[mindmapId];
}
} catch {
// ignore
}
2026-01-05 06:59:54 +08:00
}
setMindmap(null);
mindmapRef.current = null;
2026-01-05 06:59:54 +08:00
mindmapReadyRef.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [block.id, effectiveFullscreen]);
2026-01-05 06:59:54 +08:00
const getInstanceCandidate = () =>
mindmap ??
(typeof window !== "undefined"
2026-01-07 07:25:07 +08:00
? ((window as unknown as { __mindmapInstance?: MindMapInstance | null })
.__mindmapInstance ?? null)
2026-01-05 06:59:54 +08:00
: null);
const runWhenReady = (
fn: (mm: MindMapInstance) => void,
retry = 0,
) => {
const instance = getInstanceCandidate();
if (mindmapReadyRef.current && instance) {
fn(instance);
return;
}
if (retry < 60) {
window.setTimeout(() => runWhenReady(fn, retry + 1), 50);
}
};
const execWithReflow = (runner: (mm: MindMapInstance) => void) => {
const attempt = (retry = 0) => {
const instance = getInstanceCandidate();
if (!instance || !mindmapReadyRef.current) {
if (retry < 60) window.setTimeout(() => attempt(retry + 1), 80);
return;
}
const mm = ensureActiveBefore(instance);
if (!mm) return;
runner(mm);
};
attempt();
};
const pickActiveOrRoot = (mm: MindMapInstance) => {
const list = mm.renderer?.activeNodeList;
2026-01-07 07:25:07 +08:00
const last = mm.renderer?.lastActiveNodeList;
2026-01-05 06:59:54 +08:00
return (list && list[0]) || (last && last[0]) || getRootNode(mm) || null;
};
2026-01-07 07:25:07 +08:00
const getNodeUid = (node: unknown): string | null => {
const maybeNode = node as unknown as { getData?: (key: string) => unknown; uid?: unknown };
const raw = typeof maybeNode.getData === "function" ? maybeNode.getData("uid") : maybeNode.uid;
return typeof raw === "string" && raw ? raw : null;
};
2026-01-05 06:59:54 +08:00
const handleUndo = () => runWhenReady((mm) => mm.execCommand("BACK"));
const handleRedo = () => runWhenReady((mm) => mm.execCommand("FORWARD"));
const handlePainter = () => {
2026-01-05 06:59:54 +08:00
const instance = getInstanceCandidate();
if (!instance?.painter) {
window.alert("格式刷插件未就绪");
return;
}
2026-01-05 06:59:54 +08:00
instance.painter.startPainter();
};
2026-01-05 06:59:54 +08:00
const handleSibling = () =>
execWithReflow((mm) => {
const target = pickActiveOrRoot(mm);
if (!target) return;
2026-01-07 07:25:07 +08:00
const targetUid = getNodeUid(target);
2026-01-05 06:59:54 +08:00
mm.execCommand?.("SET_NODE_ACTIVE", target, true);
// openEdit=false:保持与 createNewNodeBehavior=activeOnly 的策略一致,避免强制进入编辑模式带来的时序问题
mm.execCommand?.("INSERT_NODE", false, [target]);
// 兜底:某些情况下插入后 activeNodeList 会短暂为空,导致下一步“同级/删除”无目标。
// 这里尝试在下一帧将新插入的节点设为激活(插入同级节点时,新节点位于 target 后)。
window.setTimeout(() => {
try {
2026-01-07 07:25:07 +08:00
const renderer = mm.renderer;
if ((renderer.activeNodeList?.length ?? 0) > 0) return;
if (!targetUid || typeof renderer.findNodeByUid !== "function") return;
const currentTarget = renderer.findNodeByUid(targetUid) as unknown as {
parent?: { children?: unknown[] } | null;
};
const parent = currentTarget?.parent ?? null;
const siblings = (parent?.children ?? []) as unknown[];
const idx = siblings.indexOf(currentTarget as unknown);
2026-01-05 06:59:54 +08:00
const inserted = idx >= 0 ? siblings[idx + 1] : null;
if (!inserted) return;
renderer?.clearActiveNodeList?.();
renderer?.addNodeToActiveList?.(inserted, true);
renderer.lastActiveNodeList = [inserted];
renderer?.emitNodeActiveEvent?.(inserted);
mm.execCommand?.("SET_NODE_ACTIVE", inserted, true);
} catch {
/* ignore */
}
}, 0);
});
const handleChild = () =>
execWithReflow((mm) => {
const parent = pickActiveOrRoot(mm);
if (!parent) return;
2026-01-07 07:25:07 +08:00
const parentUid = getNodeUid(parent);
2026-01-05 06:59:54 +08:00
mm.execCommand?.("SET_NODE_ACTIVE", parent, true);
// openEdit=false:同上,避免插入瞬间强制进入编辑导致异常
mm.execCommand?.("INSERT_CHILD_NODE", false, [parent]);
// 兜底:确保新插入的子节点成为激活节点,保证随后点击“同级节点/删除节点”可用
window.setTimeout(() => {
try {
2026-01-07 07:25:07 +08:00
const renderer = mm.renderer;
if ((renderer.activeNodeList?.length ?? 0) > 0) return;
if (!parentUid || typeof renderer.findNodeByUid !== "function") return;
const currentParent = renderer.findNodeByUid(parentUid) as unknown as {
children?: unknown[];
};
const children = (currentParent?.children ?? []) as unknown[];
2026-01-05 06:59:54 +08:00
const inserted = children.length > 0 ? children[children.length - 1] : null;
if (!inserted) return;
renderer?.clearActiveNodeList?.();
renderer?.addNodeToActiveList?.(inserted, true);
renderer.lastActiveNodeList = [inserted];
renderer?.emitNodeActiveEvent?.(inserted);
mm.execCommand?.("SET_NODE_ACTIVE", inserted, true);
} catch {
/* ignore */
}
}, 0);
});
const handleDelete = () => {
2026-01-05 06:59:54 +08:00
runWhenReady((instance) => {
const mm = ensureActiveBefore(instance);
if (!mm) return;
window.setTimeout(() => mm.execCommand("REMOVE_NODE"), 0);
});
};
const handleSummary = () => {
2026-01-05 06:59:54 +08:00
runWhenReady((instance) => {
const mm = ensureActiveBefore(instance);
if (mm) {
window.setTimeout(() => mm.execCommand("ADD_GENERALIZATION"), 0);
}
});
};
const handleAssociativeLine = () => {
2026-01-05 06:59:54 +08:00
runWhenReady((instance) => {
const mm = ensureActiveBefore(instance);
if (mm) {
window.setTimeout(() => mm.execCommand("ADD_ASSOCIATIVE_LINE"), 0);
}
});
};
const handleOuterFrame = () => {
2026-01-05 06:59:54 +08:00
runWhenReady((instance) => {
const mm = ensureActiveBefore(instance);
if (mm) {
window.setTimeout(() => mm.execCommand("ADD_OUTER_FRAME"), 0);
}
});
};
2025-12-31 19:26:38 +08:00
const [showImageModal, setShowImageModal] = useState(false);
const [imageUrl, setImageUrl] = useState("");
const [imageWidth, setImageWidth] = useState<number | string>(260);
const [imageHeight, setImageHeight] = useState<number | string>(200);
const [imageTitle, setImageTitle] = useState("");
const [imagePosition, setImagePosition] = useState<string>("top");
const fileInputForImage = useRef<HTMLInputElement | null>(null);
// 图片预览(双击节点图片)
const [showImageViewer, setShowImageViewer] = useState(false);
const [viewerSrc, setViewerSrc] = useState("");
const [viewerMeta, setViewerMeta] = useState<{ title?: string; width?: number; height?: number }>({});
const imgToolbarRef = useRef<HTMLDivElement | null>(null);
const [imgToolbarState, setImgToolbarState] = useState({
show: false,
x: 0,
y: 0,
placement: "top" as "top" | "bottom" | "left" | "right",
});
const imgToolbarHover = useRef(false);
const handleImage = () => {
2026-01-02 07:25:50 +08:00
const first = mindmap?.renderer?.activeNodeList?.[0] as MindMapNode | undefined;
2025-12-31 19:26:38 +08:00
if (first?.getStyle) {
2026-01-02 07:25:50 +08:00
const placement = first.getStyle("imgPlacement", false) as string;
2025-12-31 19:26:38 +08:00
if (placement) setImagePosition(placement);
}
setShowImageModal(true);
};
// 预览:监听 mindmap 事件
useEffect(() => {
if (!mindmap) return;
2026-01-02 07:25:50 +08:00
const handler = (node: MindMapNode, e?: Event) => {
2025-12-31 19:26:38 +08:00
e?.stopPropagation?.();
e?.preventDefault?.();
2026-01-07 07:25:07 +08:00
const srcRaw =
2025-12-31 19:26:38 +08:00
node?.nodeData?.data?.image ||
node?.getData?.("image") ||
node?.data?.image;
2026-01-07 07:25:07 +08:00
const src = typeof srcRaw === "string" ? srcRaw : "";
2025-12-31 19:26:38 +08:00
if (src) {
setViewerSrc(src);
2026-01-07 07:25:07 +08:00
const sizeRaw = node?.getData?.("imageSize");
const size =
sizeRaw && typeof sizeRaw === "object"
? (sizeRaw as { width?: unknown; height?: unknown })
: ({} as { width?: unknown; height?: unknown });
2025-12-31 19:26:38 +08:00
const title = node?.getData?.("imageTitle") || "";
setViewerMeta({
title: typeof title === "string" ? title : "",
2026-01-07 07:25:07 +08:00
width: Number(size.width) || undefined,
height: Number(size.height) || undefined,
2025-12-31 19:26:38 +08:00
});
setShowImageViewer(true);
}
};
const onActive = () => {
2026-01-02 07:25:50 +08:00
const list = (mindmap.renderer?.activeNodeList || []) as MindMapNode[];
const has = list.some((n) => !!n?.getData?.("image"));
2025-12-31 19:26:38 +08:00
if (!has) setImgToolbarState((s) => ({ ...s, show: false }));
};
2026-01-02 07:25:50 +08:00
const showToolbarOnClick = (node: MindMapNode, _svgImg: unknown, evt: Event | undefined) => {
const target = evt?.target as Element | undefined;
const bbox = target?.getBoundingClientRect?.();
const placement = (node?.getStyle?.("imgPlacement") || "top") as "top" | "bottom" | "left" | "right";
2025-12-31 19:26:38 +08:00
if (!bbox) return;
setImagePosition(placement);
setImgToolbarState({
show: true,
x: bbox.left,
y: bbox.top,
placement,
});
};
const hideToolbar = () => {
if (!imgToolbarHover.current) {
setImgToolbarState((s) => ({ ...s, show: false }));
}
};
mindmap.on?.("node_img_dblclick", handler);
mindmap.on?.("node_active", onActive);
mindmap.on?.("node_img_click", showToolbarOnClick);
mindmap.on?.("draw_click", hideToolbar);
return () => {
mindmap.off?.("node_img_dblclick", handler);
mindmap.off?.("node_active", onActive);
mindmap.off?.("node_img_click", showToolbarOnClick);
mindmap.off?.("draw_click", hideToolbar);
};
}, [mindmap]);
// 悬浮图片位置工具条
const renderImgToolbar = () => {
if (!imgToolbarState.show) return null;
const { x, y, placement } = imgToolbarState;
const setPlacement = (p: "top" | "bottom" | "left" | "right") => {
setImagePosition(p);
applyToActiveNodes(mindmap, (node) =>
2026-01-02 07:25:50 +08:00
mindmap?.execCommand?.("SET_NODE_STYLES", node, { imgPlacement: p }),
2025-12-31 19:26:38 +08:00
);
setImgToolbarState((s) => ({ ...s, placement: p }));
};
2026-01-02 07:25:50 +08:00
const btn = (
p: typeof placement,
Icon: React.ComponentType<{ className?: string }>,
title: string,
) => (
2025-12-31 19:26:38 +08:00
<button
key={p}
type="button"
className={`flex h-8 w-8 items-center justify-center rounded border bg-white/90 text-gray-700 shadow ${
placement === p ? "border-blue-500 text-blue-600" : "border-gray-200"
}`}
onMouseDown={(e) => {
e.stopPropagation();
e.preventDefault();
setPlacement(p);
}}
title={title}
>
<Icon className="h-4 w-4" />
</button>
);
return (
<div
ref={imgToolbarRef}
className="pointer-events-auto fixed z-[9999] flex gap-1 rounded-lg bg-black/50 p-2 backdrop-blur"
style={{ left: x, top: y - 42 }}
onMouseEnter={() => {
imgToolbarHover.current = true;
setImgToolbarState((s) => ({ ...s, show: true }));
}}
onMouseLeave={() => {
imgToolbarHover.current = false;
setImgToolbarState((s) => ({ ...s, show: false }));
}}
>
{btn("top", ArrowUp, "顶部")}
{btn("bottom", ArrowDown, "底部")}
{btn("left", ArrowLeft, "靠左")}
{btn("right", ArrowRight, "靠右")}
</div>
);
};
const handleIcon = () => {
setActiveSidebar("icons");
};
const handleLink = () => {
const href = createSimplePrompt("请输入超链接");
if (!href) return;
applyToActiveNodes(mindmap, (node) => mindmap?.execCommand("SET_NODE_HYPERLINK", node, href, href));
};
const handleNote = () => {
2025-12-30 18:39:38 +08:00
setShowNoteModal(true);
};
const handleTag = () => {
const tagsRaw = createSimplePrompt("请输入标签,使用逗号分隔", "重点,待验证");
if (!tagsRaw) return;
const tags = tagsRaw.split(",").map((item) => item.trim()).filter(Boolean);
applyToActiveNodes(mindmap, (node) => mindmap?.execCommand("SET_NODE_TAG", node, tags));
};
const handleFormula = () => {
setActiveSidebar("formula");
};
2025-12-30 18:39:38 +08:00
const handleNoteConfirm = () => {
const note = noteContent.trim();
setShowNoteModal(false);
if (!note) return;
applyToActiveNodes(mindmap, (node) => mindmap?.execCommand("SET_NODE_NOTE", node, note));
};
2025-12-31 19:26:38 +08:00
const handleImageConfirm = () => {
const url = imageUrl.trim();
const width = Number(imageWidth) || 260;
const height = Number(imageHeight) || 200;
if (!url) {
window.alert("请输入图片链接");
return;
}
setShowImageModal(false);
applyToActiveNodes(mindmap, (node) =>
mindmap?.execCommand("SET_NODE_IMAGE", node, {
url,
width,
height,
title: imageTitle || "",
position: imagePosition || "top",
}),
);
applyToActiveNodes(mindmap, (node) =>
mindmap?.execCommand?.("SET_NODE_STYLES", node, {
imgPlacement: imagePosition || "top",
}),
);
};
const handleAttachment = () => {
const url = createSimplePrompt("附件链接(http/https");
if (!url) return;
const name = createSimplePrompt("附件名称(可选)", "附件");
applyToActiveNodes(mindmap, (node) => mindmap?.execCommand("SET_NODE_ATTACHMENT", node, url, name ?? ""));
};
const handleAiPlaceholder = () => {
window.alert("AI 能力占位:后续接入大模型生成/优化节点内容。");
};
2026-01-02 07:25:50 +08:00
const handleImport = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
2026-01-02 07:25:50 +08:00
const reset = () => {
event.target.value = "";
};
const ext = (file.name.split(".").pop() || "").toLowerCase();
try {
// JSON / smm
if (ext === "json" || ext === "smm") {
const text = await file.text();
const data = JSON.parse(text);
mindmap?.setData(data);
mindmap?.command.clearHistory();
2026-01-09 07:22:27 +08:00
persistData(data);
2026-01-02 07:25:50 +08:00
return;
}
2026-01-02 07:25:50 +08:00
// XMind
if (ext === "xmind") {
const xmindParser = await import("simple-mind-map/src/parse/xmind.js");
const blob = new Blob([await file.arrayBuffer()]);
2026-01-07 07:25:07 +08:00
const data = await xmindParser.default.parseXmindFile(blob, (content) => {
const list = content;
2026-01-02 07:25:50 +08:00
if (list.length > 1) {
2026-01-07 18:38:56 +08:00
window.alert("检测到 XMind 多画布,自动导入第一个画布。");
2026-01-02 07:25:50 +08:00
}
return list.length > 0 ? list[0] : content;
});
mindmap?.setData(data);
mindmap?.command.clearHistory();
2026-01-09 07:22:27 +08:00
persistData(data);
2026-01-02 07:25:50 +08:00
return;
}
2026-01-07 18:38:56 +08:00
// MindManager (.mmap)
if (ext === "mmap") {
const { parseMindManagerMmapFile } = await import("./mindmapMindManagerImport");
const data = await parseMindManagerMmapFile(file);
mindmap?.setData(data);
mindmap?.command.clearHistory();
2026-01-09 07:22:27 +08:00
persistData(data);
2026-01-07 18:38:56 +08:00
return;
}
2026-01-02 07:25:50 +08:00
// Markdown
if (ext === "md" || ext === "markdown") {
const { transformMarkdownTo } = await import(
"simple-mind-map/src/parse/markdownTo.js"
);
const text = await file.text();
const data = transformMarkdownTo(text) as MindMapData;
if (!data.data) {
data.data = { text: file.name.replace(/\.(md|markdown)$/i, "") || "中心主题" };
}
mindmap?.setData(data);
mindmap?.command.clearHistory();
2026-01-09 07:22:27 +08:00
persistData(data);
2026-01-02 07:25:50 +08:00
return;
}
2026-01-07 18:38:56 +08:00
window.alert("暂不支持该文件类型,请选择 .json / .smm / .xmind / .mmap / .md");
2026-01-02 07:25:50 +08:00
} catch (error) {
console.error(error);
window.alert("导入失败:文件格式或内容错误");
} finally {
reset();
}
};
const handleNew = () => {
if (!window.confirm("确定要新建空白导图吗?当前未保存的修改将被覆盖。")) return;
mindmap?.setData(defaultMindmapData);
mindmap?.command.clearHistory();
2026-01-09 07:22:27 +08:00
persistData(defaultMindmapData);
};
const handleOpenDirectory = () => {
const saved = window.localStorage.getItem(autosaveKey);
if (!saved) {
window.alert("暂无本地目录记录,可使用“导入”载入文件。");
return;
}
try {
const data = JSON.parse(saved);
mindmap?.setData(data);
window.alert("已从本地目录恢复最新自动保存版本。");
} catch {
window.alert("本地目录数据损坏,建议重新导入。");
}
};
const handleExportJson = () => {
const data = mindmap?.getData?.(true) ?? block.props.data ?? defaultMindmapData;
downloadJson(data, "mindmap");
};
2026-01-02 07:25:50 +08:00
const handleExport = async (type: string, name = "mindmap") => {
try {
2026-01-02 07:25:50 +08:00
await mindmap?.doExport?.export(type, true, name);
} catch (error) {
console.error(error);
2026-01-02 07:25:50 +08:00
window.alert(`导出 ${type.toUpperCase()} 失败,请稍后再试`);
}
};
2026-01-02 07:25:50 +08:00
const handleExportPng = () => handleExport("png");
const handleExportSvg = () => handleExport("svg");
const handleExportPdf = () => handleExport("pdf");
const handleExportMd = () => handleExport("md");
const handleExportTxt = () => handleExport("txt");
const handleExportXmind = () => handleExport("xmind");
const handleSaveAs = () => handleExportJson();
2026-01-02 07:25:50 +08:00
const handleDeleteMindmap = useCallback(async () => {
if (!docId) {
deletingRef.current = true;
2026-01-02 07:25:50 +08:00
editor.removeBlocks([block.id]);
return;
}
const confirmed = window.confirm("确认删除当前思维导图?此操作会移除文件并清空侧边栏记录。");
if (!confirmed) return;
deletingRef.current = true;
2026-01-08 06:28:14 +08:00
const resp = await fetch(`/api/mindmap/${docId}/${mindmapId}`, { method: "DELETE" });
2026-01-02 07:25:50 +08:00
if (!resp.ok) {
const payload = await resp.json().catch(() => ({}));
window.alert(payload?.error ?? "删除思维导图失败");
deletingRef.current = false;
2026-01-02 07:25:50 +08:00
return;
}
try {
window.localStorage.removeItem(autosaveKey);
} catch {
// ignore
}
2026-01-08 06:28:14 +08:00
emitAssetsChanged(docId, undefined, undefined, false, [mindmapId]);
// 同时会触发全局删除监听(ASSETS_CHANGED_EVENT)进行块移除,这里做 try/catch 避免重复删除报错
try {
editor.removeBlocks([block.id]);
} catch {
// ignore
}
}, [autosaveKey, block.id, docId, editor, mindmapId]);
2026-01-02 07:25:50 +08:00
const toolbarProps = {
canBack,
canForward,
painterMode,
onUndo: handleUndo,
onRedo: handleRedo,
onPainter: handlePainter,
onSibling: handleSibling,
onChild: handleChild,
onDelete: handleDelete,
onImage: handleImage,
onIcon: handleIcon,
onLink: handleLink,
onNote: handleNote,
onTag: handleTag,
onSummary: handleSummary,
onAssociativeLine: handleAssociativeLine,
onFormula: handleFormula,
onAttachment: handleAttachment,
onOuterFrame: handleOuterFrame,
onAi: handleAiPlaceholder,
onImport: handleImport,
onNew: handleNew,
onOpenDirectory: handleOpenDirectory,
onSaveAs: handleSaveAs,
2026-01-02 07:25:50 +08:00
onDeleteMindmap: handleDeleteMindmap,
onExportJson: handleExportJson,
onExportPng: handleExportPng,
2026-01-02 07:25:50 +08:00
onExportSvg: handleExportSvg,
onExportPdf: handleExportPdf,
onExportMd: handleExportMd,
onExportTxt: handleExportTxt,
onExportXmind: handleExportXmind,
fileInputRef,
};
2025-12-30 18:39:38 +08:00
const noteModal = showNoteModal ? (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4">
<div className="w-full max-w-4xl rounded-xl bg-white shadow-2xl">
2025-12-31 19:26:38 +08:00
<div className="flex items-center justify-between border-b px-4 py-3">
<span className="text-lg font-semibold text-gray-800">备注</span>
2025-12-30 18:39:38 +08:00
<button className="text-gray-400 hover:text-gray-600" onClick={() => setShowNoteModal(false)}>
</button>
</div>
<div className="px-4 py-3">
<textarea
className="h-64 w-full resize-none rounded-md border border-gray-200 p-3 text-sm focus:border-blue-400 focus:outline-none"
placeholder="支持富文本/Markdown,内容将写入节点备注"
value={noteContent}
onChange={(e) => setNoteContent(e.target.value)}
/>
</div>
<div className="flex items-center justify-end gap-3 border-t px-4 py-3">
<button
className="rounded-md border border-gray-200 px-4 py-2 text-sm text-gray-600 hover:bg-gray-50"
onClick={() => setShowNoteModal(false)}
>
取消
</button>
<button
className="rounded-md bg-blue-500 px-4 py-2 text-sm text-white hover:bg-blue-600"
onClick={handleNoteConfirm}
>
确定
</button>
</div>
</div>
</div>
) : null;
2025-12-31 19:26:38 +08:00
const imageViewer = showImageViewer ? (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/70 p-4" onClick={() => setShowImageViewer(false)}>
<div className="relative max-h-full max-w-5xl text-white">
<button
className="absolute -top-3 -right-3 rounded-full bg-white/90 px-2 py-1 text-sm text-gray-700 shadow"
onClick={(e) => {
e.stopPropagation();
setShowImageViewer(false);
}}
>
关闭
</button>
<div className="mb-2 flex items-center justify-between gap-4 px-1 text-xs text-gray-200">
2026-01-02 07:25:50 +08:00
<span className="truncate">{viewerMeta.title || "图片预览"}</span>
2025-12-31 19:26:38 +08:00
<span className="shrink-0">{viewerMeta.width && viewerMeta.height ? `${viewerMeta.width} × ${viewerMeta.height}px` : ""}</span>
</div>
2026-01-02 07:25:50 +08:00
{/* eslint-disable-next-line @next/next/no-img-element */}
2025-12-31 19:26:38 +08:00
<img
src={viewerSrc}
alt={viewerMeta.title || "预览"}
className="max-h-[80vh] max-w-[80vw] rounded-lg shadow-2xl object-contain bg-white"
onClick={(e) => e.stopPropagation()}
/>
</div>
</div>
) : null;
const imgToolbar = renderImgToolbar();
const imageModal = showImageModal ? (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4">
<div className="w-full max-w-3xl rounded-xl bg-white shadow-2xl">
<div className="flex items-center justify-between border-b px-4 py-3">
<span className="text-lg font-semibold text-gray-800">插入图片</span>
<button className="text-gray-400 hover:text-gray-600" onClick={() => setShowImageModal(false)}>
</button>
</div>
<div className="space-y-4 px-4 py-3">
<div className="space-y-2">
<Label className="text-xs font-semibold text-gray-600">方式一:上传图片</Label>
<div className="flex items-center gap-2">
<button
className="rounded-md border border-gray-200 px-3 py-2 text-sm text-gray-700 hover:bg-gray-50"
onClick={() => fileInputForImage.current?.click()}
>
选择文件
</button>
<span className="text-xs text-gray-400 truncate">
{imageUrl.startsWith("data:") ? "已选择本地图片" : "未选择文件"}
</span>
<input
ref={fileInputForImage}
type="file"
accept="image/*"
className="hidden"
onChange={async (e) => {
const file = e.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = async () => {
const dataUrl = String(reader.result);
setImageUrl(dataUrl);
const size = await getImageSizeSafe(dataUrl);
if (size) {
setImageWidth(size.width);
setImageHeight(size.height);
}
};
reader.readAsDataURL(file);
}}
/>
</div>
</div>
<div className="space-y-2">
<Label className="text-xs font-semibold text-gray-600">方式二:图片地址</Label>
<Input
value={imageUrl}
onChange={(e) => setImageUrl(e.target.value)}
placeholder="https://example.com/image.png"
/>
</div>
<div className="space-y-2">
<Label className="text-xs font-semibold text-gray-600">快捷位置</Label>
<div className="flex gap-2">
<button
type="button"
className={`flex-1 rounded border px-2 py-2 text-sm ${imagePosition === "top" ? "border-blue-500 bg-blue-50 text-blue-600" : "border-gray-200"}`}
onClick={() => setImagePosition("top")}
title="顶部"
>
<ArrowUp className="mx-auto h-4 w-4" />
</button>
<button
type="button"
className={`flex-1 rounded border px-2 py-2 text-sm ${imagePosition === "bottom" ? "border-blue-500 bg-blue-50 text-blue-600" : "border-gray-200"}`}
onClick={() => setImagePosition("bottom")}
title="底部"
>
<ArrowDown className="mx-auto h-4 w-4" />
</button>
<button
type="button"
className={`flex-1 rounded border px-2 py-2 text-sm ${imagePosition === "left" ? "border-blue-500 bg-blue-50 text-blue-600" : "border-gray-200"}`}
onClick={() => setImagePosition("left")}
title="靠左"
>
<ArrowLeft className="mx-auto h-4 w-4" />
</button>
<button
type="button"
className={`flex-1 rounded border px-2 py-2 text-sm ${imagePosition === "right" ? "border-blue-500 bg-blue-50 text-blue-600" : "border-gray-200"}`}
onClick={() => setImagePosition("right")}
title="靠右"
>
<ArrowRight className="mx-auto h-4 w-4" />
</button>
</div>
</div>
<div className="grid grid-cols-2 gap-3">
<div className="space-y-1">
<Label className="text-xs text-gray-500">宽度(px</Label>
<Input
type="number"
value={imageWidth}
onChange={(e) => setImageWidth(e.target.value)}
min={10}
placeholder="260"
/>
</div>
<div className="space-y-1">
<Label className="text-xs text-gray-500">高度(px</Label>
<Input
type="number"
value={imageHeight}
onChange={(e) => setImageHeight(e.target.value)}
min={10}
placeholder="200"
/>
</div>
</div>
<div className="space-y-1">
<Label className="text-xs text-gray-500">标题(可选)</Label>
<Input
value={imageTitle}
onChange={(e) => setImageTitle(e.target.value)}
placeholder="图片标题"
/>
</div>
<div className="space-y-1">
<Label className="text-xs text-gray-500">位置</Label>
<select
className="h-9 w-full rounded-md border border-gray-200 px-3 text-sm"
value={imagePosition}
onChange={(e) => setImagePosition(e.target.value)}
>
<option value="center">居中</option>
<option value="top">顶部</option>
<option value="bottom">底部</option>
<option value="left">靠左</option>
<option value="right">靠右</option>
</select>
</div>
<p className="text-xs text-gray-400">可选择本地图片或粘贴 URL,默认尺寸 260×200</p>
</div>
<div className="flex items-center justify-end gap-3 border-t px-4 py-3">
<button
className="rounded-md border border-gray-200 px-4 py-2 text-sm text-gray-600 hover:bg-gray-50"
onClick={() => setShowImageModal(false)}
>
取消
</button>
<button
className="rounded-md bg-blue-500 px-4 py-2 text-sm text-white hover:bg-blue-600"
onClick={handleImageConfirm}
>
插入
</button>
</div>
</div>
</div>
) : null;
if (effectiveFullscreen) {
const fullscreenView = (
<div
ref={wrapperRef}
data-testid="mindmap-fullscreen"
tabIndex={0}
className="fixed inset-0 z-[9999] overflow-hidden bg-white"
>
<div className="fixed left-0 right-0 top-0 z-30 flex h-12 items-center justify-between border-b border-gray-200 bg-white/90 px-4 backdrop-blur">
<div className="text-sm font-medium text-gray-700">
2026-01-07 18:38:56 +08:00
思维导图编辑(全屏{fullscreenApiActive ? "·真" : ""}
</div>
<button
type="button"
title="退出全屏"
className="rounded p-2 text-gray-500 hover:bg-gray-100 hover:text-gray-700"
onClick={exitLocalFullscreen}
>
<span className="text-lg leading-none">×</span>
</button>
</div>
<div className="fixed left-1/2 top-14 z-20 flex w-fit -translate-x-1/2 transform items-center justify-center gap-4">
<MindmapToolbar {...toolbarProps} />
</div>
<div className="relative h-full w-full pt-12">
2026-01-02 07:25:50 +08:00
<div
ref={containerRef}
className="h-full w-full"
data-testid="mindmap-canvas"
2026-01-08 06:28:14 +08:00
data-mindmap-id={mindmapId}
2026-01-02 07:25:50 +08:00
contentEditable={false}
/>
<MindmapSidebarTrigger
activeSidebar={activeSidebar}
onSelect={setActiveSidebar}
/>
<MindmapSidebar
mindmap={mindmap}
2026-01-07 07:25:07 +08:00
activeNodes={activeNodes}
activeTab={activeSidebar}
onClose={() => setActiveSidebar(null)}
/>
<MindmapNavigator
mindmap={mindmap}
fullscreen={effectiveFullscreen}
toggleFullscreen={
fullscreen
? undefined
: () => {
if (localFullscreen) exitLocalFullscreen();
else enterLocalFullscreen();
}
}
onToggleMiniMap={() => setShowMiniMap(!showMiniMap)}
miniMapOpen={showMiniMap}
/>
<MindmapCount mindmap={mindmap} />
<MindmapMiniMap mindmap={mindmap} show={showMiniMap} />
2025-12-31 19:26:38 +08:00
{imgToolbar}
2025-12-30 18:39:38 +08:00
{noteModal}
2025-12-31 19:26:38 +08:00
{imageModal}
{imageViewer}
</div>
</div>
);
if (typeof document === "undefined") return null;
return createPortal(fullscreenView, document.body);
}
return (
<div
ref={wrapperRef}
data-testid="mindmap-embed"
tabIndex={0}
className="not-prose my-4 w-full overflow-hidden rounded-xl border border-gray-200 bg-[#f8fafc] shadow-sm"
>
<div className="flex flex-col gap-3 border-b border-gray-100 bg-white px-4 py-3">
<div className="w-full">
<MindmapToolbar {...toolbarProps} />
</div>
</div>
<div
data-testid="mindmap-stage"
2026-01-09 07:22:27 +08:00
className="relative h-[520px] w-full overflow-hidden bg-white"
onPointerDown={(e) => {
// 阻止事件冒泡到 BlockNote/ProseMirror,避免产生 NodeSelection 导致粘贴替换整块
e.stopPropagation();
hotkeyScopeRef.current = true;
2026-01-09 07:22:27 +08:00
lastInteractionAtRef.current = Date.now();
// 点击后把焦点落到容器上,避免快捷键被编辑器抢走
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}}
2026-01-09 07:22:27 +08:00
onMouseDown={(e) => {
e.stopPropagation();
hotkeyScopeRef.current = true;
2026-01-09 07:22:27 +08:00
lastInteractionAtRef.current = Date.now();
// 兼容:某些环境下 pointer 事件不触发
try {
wrapperRef.current?.focus?.({ preventScroll: true });
} catch {
// ignore
}
}}
onDoubleClick={() => {
if (recentNodeDblclickRef.current) return;
enterLocalFullscreen();
}}
>
2026-01-02 07:25:50 +08:00
<div
ref={containerRef}
className="h-full w-full"
data-testid="mindmap-canvas"
2026-01-08 06:28:14 +08:00
data-mindmap-id={mindmapId}
2026-01-02 07:25:50 +08:00
contentEditable={false}
/>
2026-01-07 07:25:07 +08:00
<MindmapSidebarTrigger
activeSidebar={activeSidebar}
onSelect={setActiveSidebar}
/>
2026-01-07 07:25:07 +08:00
<MindmapSidebar
mindmap={mindmap}
activeNodes={activeNodes}
activeTab={activeSidebar}
onClose={() => setActiveSidebar(null)}
/>
<MindmapNavigator
mindmap={mindmap}
fullscreen={effectiveFullscreen}
toggleFullscreen={
fullscreen
? undefined
: () => {
if (localFullscreen) exitLocalFullscreen();
else enterLocalFullscreen();
}
}
onToggleMiniMap={() => setShowMiniMap(!showMiniMap)}
miniMapOpen={showMiniMap}
/>
<MindmapCount mindmap={mindmap} />
<MindmapMiniMap mindmap={mindmap} show={showMiniMap} />
2025-12-31 19:26:38 +08:00
{imgToolbar}
2025-12-30 18:39:38 +08:00
{noteModal}
2025-12-31 19:26:38 +08:00
{imageModal}
{imageViewer}
</div>
</div>
);
};
export { MindmapBlockView };
2026-01-07 07:25:07 +08:00
type MindmapBlockViewProps = Parameters<typeof MindmapBlockView>[0];
export const mindmapBlock = createReactBlockSpec(
{
type: "mindmap",
propSchema: {
2026-01-02 07:25:50 +08:00
docId: { default: "" },
data: { default: defaultMindmapData },
},
content: "none",
2026-01-07 07:25:07 +08:00
} as unknown as BlockConfig<"mindmap", PropSchema, "none">,
{
2026-01-07 07:25:07 +08:00
render: (props) => (
<MindmapBlockView {...(props as unknown as MindmapBlockViewProps)} />
),
},
);