From 3da1407bb4c95234efc14efd2ced93b5f92b75fc Mon Sep 17 00:00:00 2001 From: KuroSago Date: Mon, 12 May 2025 15:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20getCustomNodeContent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/helpers/addCustomContentToNode.ts | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/web3/packages/mind-map/src/store/helpers/addCustomContentToNode.ts b/web3/packages/mind-map/src/store/helpers/addCustomContentToNode.ts index fdfb71c2..da658f58 100644 --- a/web3/packages/mind-map/src/store/helpers/addCustomContentToNode.ts +++ b/web3/packages/mind-map/src/store/helpers/addCustomContentToNode.ts @@ -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 = ''; @@ -20,8 +21,8 @@ export function getCustomNodeContent( addChild?: (node: MindMapNode) => void; edit?: (node: MindMapNode) => void; } = { - allowRemoveWithChildren: false, - } + allowRemoveWithChildren: false, + } ) { // 按钮配置 const buttonConfig = { @@ -58,7 +59,7 @@ export function getCustomNodeContent( container.style.display = "none"; const buttons = [...baseButtons]; - + const totalWidth = buttons.length * buttonConfig.width + (buttons.length - 1) * buttonConfig.gap; @@ -131,46 +132,46 @@ export function getCustomNodeContent( }); const mindMap = getMindMapInstance(); - - // 注册节点激活事件和数据更新事件 - if (mindMap) { - // 节点激活状态变化处理 - mindMap.on( - "node_active", - (_: MindMapNode, activeNodeList: MindMapNode[]) => { - const isActive = activeNodeList.some( - (activeNode) => activeNode.uid === node.uid - ); - // 如果激活状态改变,更新按钮组的可见性 - if (nodeActiveState[node.uid] !== isActive) { - nodeActiveState[node.uid] = isActive; - const container = nodeElements[node.uid]; - if (container) { - container.style.display = isActive ? "flex" : "none"; - // 在激活时更新按钮状态 - if (isActive) updateButtonsVisibility(container, node) - } + // 注册节点激活事件和数据更新事件 + // 节点激活状态变化处理 + mindMap && mindMap.on( + "node_active", + throttle((_: MindMapNode, activeNodeList: MindMapNode[]) => { + const isActive = activeNodeList.some( + (activeNode) => activeNode.uid === node.uid + ); + + // 如果激活状态改变,更新按钮组的可见性 + if (nodeActiveState[node.uid] !== isActive) { + nodeActiveState[node.uid] = isActive; + const container = nodeElements[node.uid]; + if (container) { + container.style.display = isActive ? "flex" : "none"; + // 在激活时更新按钮状态 + if (isActive) updateButtonsVisibility(container, node); } } - ); - - // 节点数据变化处理(当添加/删除子节点时) - mindMap.on("node_data_change", (changedNode: MindMapNode) => { - if (changedNode.uid === node.uid) { - const container = nodeElements[node.uid]; - if (container && nodeActiveState[node.uid]) updateButtonsVisibility(container, node); + }, 90, null) + ); + + // 节点数据变化处理(当添加/删除子节点时) + mindMap && mindMap.on("node_data_change", throttle((changedNode: MindMapNode) => { + if (changedNode.uid === node.uid) { + const container = nodeElements[node.uid]; + if (container && nodeActiveState[node.uid]) { + asyncRun(() => updateButtonsVisibility(container, node)); } - }); - } - + } + }, 90, null)); + // 更新按钮的可见性 function updateButtonsVisibility(container: HTMLElement, node: MindMapNode) { // 查找删除按钮 const deleteBtn = Array.from(container.children).find( (btn) => (btn as HTMLButtonElement).dataset.action === "delete" ) as HTMLButtonElement | undefined; - + // 只有在不允许删除有子节点的节点时才进行处理 if (deleteBtn && !params.allowRemoveWithChildren) { const hasChildren = node.getChildrenLength() > 0;