no message

This commit is contained in:
KuroSago 2025-05-08 16:31:42 +08:00
parent af88867ec4
commit afc9146785
2 changed files with 29 additions and 15 deletions

View File

@ -12,9 +12,16 @@ const ICON_DELETE =
/** /**
* - * -
* @param allowRemoveWithChildren false
* @returns * @returns
*/ */
export function getCustomNodeContent() { export function getCustomNodeContent(
params: {
allowRemoveWithChildren?: boolean;
} = {
allowRemoveWithChildren: false,
}
) {
// 按钮配置 // 按钮配置
const buttonConfig = { const buttonConfig = {
gap: 4, // 按钮间距 gap: 4, // 按钮间距
@ -24,7 +31,7 @@ export function getCustomNodeContent() {
}; };
// 所有按钮配置 // 所有按钮配置
const buttons = [ const baseButtons = [
{ {
icon: ICON_PLUS, icon: ICON_PLUS,
color: "#4CAF50", color: "#4CAF50",
@ -35,11 +42,6 @@ export function getCustomNodeContent() {
{ icon: ICON_DELETE, color: "#F44336", title: "删除", action: "delete" }, { icon: ICON_DELETE, color: "#F44336", title: "删除", action: "delete" },
]; ];
// 预计算总宽度
const totalWidth =
buttons.length * buttonConfig.width +
(buttons.length - 1) * buttonConfig.gap;
// 跟踪激活状态和元素,用于更新显示/隐藏 // 跟踪激活状态和元素,用于更新显示/隐藏
const nodeActiveState: Record<string, boolean> = {}; const nodeActiveState: Record<string, boolean> = {};
const nodeElements: Record<string, HTMLElement> = {}; const nodeElements: Record<string, HTMLElement> = {};
@ -47,11 +49,27 @@ export function getCustomNodeContent() {
return { return {
// 创建自定义DOM元素 // 创建自定义DOM元素
create: (node: MindMapNode) => { create: (node: MindMapNode) => {
const { getMindMapInstance } = useMindMapStore();
const container = document.createElement("div"); const container = document.createElement("div");
container.style.gap = `${buttonConfig.gap}px`; container.style.gap = `${buttonConfig.gap}px`;
container.style.display = "none"; container.style.display = "none";
// 根据节点是否有子节点以及 allowRemoveWithChildren 参数确定显示的按钮
const buttons = baseButtons.filter((btn) => {
if (btn.action === "delete" && !params.allowRemoveWithChildren) {
return node.getChildrenLength() === 0;
}
return true;
});
// 重新计算总宽度
const totalWidth =
buttons.length * buttonConfig.width +
(buttons.length - 1) * buttonConfig.gap;
nodeElements[node.uid] = container; nodeElements[node.uid] = container;
// 存储节点的激活状态 // 存储节点的激活状态
@ -88,8 +106,6 @@ export function getCustomNodeContent() {
btn.onclick = (e) => { btn.onclick = (e) => {
e.stopPropagation(); e.stopPropagation();
const { getMindMapInstance } = useMindMapStore();
// 根据按钮类型执行不同操作 // 根据按钮类型执行不同操作
switch (action) { switch (action) {
case "addChild": case "addChild":
@ -131,9 +147,6 @@ export function getCustomNodeContent() {
container.appendChild(btn); container.appendChild(btn);
}); });
// 监听节点激活状态变化
const { getMindMapInstance } = useMindMapStore();
getMindMapInstance()?.on( getMindMapInstance()?.on(
"node_active", "node_active",
(_: MindMapNode, activeNodeList: MindMapNode[]) => { (_: MindMapNode, activeNodeList: MindMapNode[]) => {
@ -193,7 +206,6 @@ export function getCustomNodeContent() {
if (elementWidth > nodeWidth) { if (elementWidth > nodeWidth) {
// //
} }
}, },
}; };
} }

View File

@ -49,7 +49,9 @@ export const useMindMapStore = defineStore(
isUseCustomNodeContent : true, // 是否使用自定义节点内容 isUseCustomNodeContent : true, // 是否使用自定义节点内容
// 添加自定义节点内容 // 添加自定义节点内容
addCustomContentToNode: getCustomNodeContent(), addCustomContentToNode: getCustomNodeContent({
allowRemoveWithChildren: false,
}),
demonstrateConfig: { demonstrateConfig: {
openBlankMode: true, openBlankMode: true,