0.1.0 思维导图卡顿修复
This commit is contained in:
@@ -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(/ /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 () => {
|
||||
|
||||
Reference in New Issue
Block a user