diff --git a/web3/packages/mind-map/src/components/ToolBar/InsertSiblingNode.vue b/web3/packages/mind-map/src/components/ToolBar/InsertSiblingNode.vue index f850a5a1..ac86ed74 100644 --- a/web3/packages/mind-map/src/components/ToolBar/InsertSiblingNode.vue +++ b/web3/packages/mind-map/src/components/ToolBar/InsertSiblingNode.vue @@ -15,11 +15,32 @@ import { createUid } from "simple-mind-map/src/utils"; const { insertSiblingNode } = useMindMapStore(); async function addNewSiblingNode() { - insertSiblingNode(async () => { - return { - uid: createUid(), - text: "新节点22222", - }; - }); + + const { activeNodes } = useMindMapStore(); + + const activeNodesIds = activeNodes.map((node) => node.uid); + + if (activeNodesIds.length === 0) return; + + for (const uid of activeNodesIds) { + const uuid = createUid(); + const parentNodeId = uid; + console.log("uuid", { + parentNodeId, + uuid, + }); + + insertSiblingNode({ + beforeInsertCallback: async () => { + return { + uid: uuid, + text: uuid, + }; + }, + nodeId: uid, + }); + } + + } diff --git a/web3/packages/mind-map/src/store/helpers/insertChildNode.ts b/web3/packages/mind-map/src/store/helpers/insertChildNode.ts index 3505b619..f67c54cf 100644 --- a/web3/packages/mind-map/src/store/helpers/insertChildNode.ts +++ b/web3/packages/mind-map/src/store/helpers/insertChildNode.ts @@ -1,5 +1,5 @@ import { useMindMapStore } from "../../store/index"; -import { createUid } from 'simple-mind-map/src/utils' +import { createUid } from "simple-mind-map/src/utils"; /** * 追加子节点 @@ -7,34 +7,43 @@ import { createUid } from 'simple-mind-map/src/utils' * @param nodeOptions 节点选项,可自定义节点属性 * @returns 返回Promise,可用于链式调用 */ -export async function insertChildNode( - params : { - parentNodeId: string | number, - beforeInsertCallback: () => Promise<({ uid: number | string, text: string } | boolean)>, - nodeOptions?: Record - } -) { +export async function insertChildNode(params: { + parentNodeId: string | number; + beforeInsertCallback: () => Promise< + { uid: number | string; text: string } | boolean + >; + nodeOptions?: Record; +}) { const { getMindMapInstance } = useMindMapStore(); const _nodeOptions = { uid: createUid(), text: " - ", - ...(params.nodeOptions || {}) - } + ...(params.nodeOptions || {}), + }; try { // 如果提供了回调函数,先执行回调 const result = await params.beforeInsertCallback(); - if (!result) return; // 如果直接 false 则不再执行插入操作 + if (!result) return; if (result) { - Object.assign(_nodeOptions, result); + Object.assign(_nodeOptions, result); } - + + const node = getMindMapInstance()?.renderer?.findNodeByUid( + params.parentNodeId + ); + // 执行插入子节点命令 - return getMindMapInstance()?.execCommand("INSERT_CHILD_NODE", false, [], _nodeOptions); + return getMindMapInstance()?.execCommand( + "INSERT_CHILD_NODE", + false, + [node], + _nodeOptions + ); } catch (error) { console.error("插入子节点失败:", error); throw error; } -} \ No newline at end of file +} diff --git a/web3/packages/mind-map/src/store/helpers/insertSiblingNode.ts b/web3/packages/mind-map/src/store/helpers/insertSiblingNode.ts index 1d0c9da5..ddcc84a4 100644 --- a/web3/packages/mind-map/src/store/helpers/insertSiblingNode.ts +++ b/web3/packages/mind-map/src/store/helpers/insertSiblingNode.ts @@ -1,5 +1,5 @@ import { useMindMapStore } from "../../store/index"; -import { createUid } from 'simple-mind-map/src/utils' +import { createUid } from "simple-mind-map/src/utils"; // 插入同级节点 /** @@ -8,31 +8,41 @@ import { createUid } from 'simple-mind-map/src/utils' * @param nodeOptions 节点选项,可自定义节点属性 * @returns 返回Promise,可用于链式调用 */ -export async function insertSiblingNode( - beforeInsertCallback: () => Promise<({ uid: number | string, text: string } | boolean)>, - nodeOptions: Record = { uid: createUid(), text: " - " } -) { +export async function insertSiblingNode(params: { + beforeInsertCallback: () => Promise< + { uid: number | string; text: string } | boolean + >; + nodeId: string | number; + nodeOptions?: Record; +}) { const { getMindMapInstance } = useMindMapStore(); const _nodeOptions = { - ...nodeOptions, - } - - try { - // 如果提供了回调函数,先执行回调 - const result = await beforeInsertCallback(); + uid: createUid(), + text: " - ", + ...params.nodeOptions, + }; - if (!result) return; // 如果直接 false 则不再执行插入操作 + try { + const result = await params.beforeInsertCallback(); + + if (!result) return; if (result) { - Object.assign(_nodeOptions, result); + Object.assign(_nodeOptions, result); } - + + const node = getMindMapInstance()?.renderer?.findNodeByUid(params.nodeId); + // 执行插入同级节点命令 - return getMindMapInstance()?.execCommand("INSERT_NODE", false, [], _nodeOptions); + return getMindMapInstance()?.execCommand( + "INSERT_NODE", + false, + [node], + _nodeOptions + ); } catch (error) { console.error("插入同级节点失败:", error); throw error; } } -