优化 getCustomNodeContent

This commit is contained in:
KuroSago 2025-05-12 15:44:39 +08:00
parent b5bb540036
commit 3da1407bb4

View File

@ -1,5 +1,6 @@
import { useMindMapStore } from "../index";
import type MindMapNode from "simple-mind-map/types/src/core/render/node/MindMapNode";
import { throttle, asyncRun } from 'simple-mind-map/src/utils';
const ICON_PLUS =
'<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"><path fill="currentColor" fill-rule="evenodd" d="M8 0a8 8 0 1 0 0 16A8 8 0 0 0 8 0zM1.5 8a6.5 6.5 0 1 1 13 0a6.5 6.5 0 0 1-13 0zM8 4a.75.75 0 0 1 .75.75V7h2.25a.75.75 0 0 1 0 1.5H8.75v2.25a.75.75 0 0 1-1.5 0V8.5H5a.75.75 0 0 1 0-1.5h2.25V4.75A.75.75 0 0 1 8 4z" clip-rule="evenodd"/></svg>';
@ -133,11 +134,10 @@ export function getCustomNodeContent(
const mindMap = getMindMapInstance();
// 注册节点激活事件和数据更新事件
if (mindMap) {
// 节点激活状态变化处理
mindMap.on(
mindMap && mindMap.on(
"node_active",
(_: MindMapNode, activeNodeList: MindMapNode[]) => {
throttle((_: MindMapNode, activeNodeList: MindMapNode[]) => {
const isActive = activeNodeList.some(
(activeNode) => activeNode.uid === node.uid
);
@ -149,20 +149,21 @@ export function getCustomNodeContent(
if (container) {
container.style.display = isActive ? "flex" : "none";
// 在激活时更新按钮状态
if (isActive) updateButtonsVisibility(container, node)
}
if (isActive) updateButtonsVisibility(container, node);
}
}
}, 90, null)
);
// 节点数据变化处理(当添加/删除子节点时)
mindMap.on("node_data_change", (changedNode: MindMapNode) => {
mindMap && mindMap.on("node_data_change", throttle((changedNode: MindMapNode) => {
if (changedNode.uid === node.uid) {
const container = nodeElements[node.uid];
if (container && nodeActiveState[node.uid]) updateButtonsVisibility(container, node);
if (container && nodeActiveState[node.uid]) {
asyncRun(() => updateButtonsVisibility(container, node));
}
});
}
}, 90, null));
// 更新按钮的可见性
function updateButtonsVisibility(container: HTMLElement, node: MindMapNode) {