0.1.0 思维导图卡顿修复

This commit is contained in:
liaibo
2026-01-05 18:34:04 +08:00
parent 7480f58c6e
commit d19884d139
4 changed files with 22 additions and 52 deletions
@@ -13,28 +13,34 @@ export const MindmapCount = ({ mindmap }: CountProps) => {
const calculateCounts = (data: any) => {
let nodes = 0;
let textStr = "";
let words = 0;
const stripHtmlLen = (html: string) => {
// 简易去标签统计长度:避免每次 data_change 都创建 DOM 容器造成卡顿
const plain = html
.replace(/<[^>]*>/g, "")
.replace(/&nbsp;/g, " ")
.replace(/\s+/g, " ")
.trim();
return plain.length;
};
const walk = (nodeData: any) => {
if (!nodeData) return;
nodes++;
// simple-mind-map node data structure: { data: { text: ... }, children: [...] }
// simple-mind-map 节点数据结构:{ data: { text: ... }, children: [...] }
const text = String(nodeData.data?.text || "");
textStr += text;
words += stripHtmlLen(text);
if (nodeData.children && Array.isArray(nodeData.children)) {
nodeData.children.forEach(walk);
}
};
walk(data);
const root = data?.root ?? data;
walk(root);
// Remove HTML tags for word count
const tempDiv = document.createElement("div");
tempDiv.innerHTML = textStr;
const words = tempDiv.textContent?.length || 0;
setCounts({ words, nodes });
setCounts((prev) => (prev.words === words && prev.nodes === nodes ? prev : { words, nodes }));
};
const onDataChange = (data: any) => {
@@ -42,14 +48,13 @@ export const MindmapCount = ({ mindmap }: CountProps) => {
};
mindmap.on("data_change", onDataChange);
// Initial calculation if data is available
// mindmap.getData() might return full data object
// 初次渲染时尝试计算一次(mindmap.getData() 可能返回完整数据对象)
try {
const initialData = mindmap.getData();
if (initialData) calculateCounts(initialData);
} catch (e) {
console.warn("Failed to get initial mindmap data for count", e);
console.warn("获取思维导图初始数据用于统计失败", e);
}
return () => {