调整组操作

This commit is contained in:
KuroSago 2025-05-07 11:21:11 +08:00
parent e6cdc4fb63
commit 947fd3374b
3 changed files with 77 additions and 37 deletions

View File

@ -15,11 +15,32 @@ import { createUid } from "simple-mind-map/src/utils";
const { insertSiblingNode } = useMindMapStore(); const { insertSiblingNode } = useMindMapStore();
async function addNewSiblingNode() { async function addNewSiblingNode() {
insertSiblingNode(async () => {
return { const { activeNodes } = useMindMapStore();
uid: createUid(),
text: "新节点22222", 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,
});
}
} }
</script> </script>

View File

@ -1,5 +1,5 @@
import { useMindMapStore } from "../../store/index"; 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 * @param nodeOptions
* @returns Promise * @returns Promise
*/ */
export async function insertChildNode( export async function insertChildNode(params: {
params : { parentNodeId: string | number;
parentNodeId: string | number, beforeInsertCallback: () => Promise<
beforeInsertCallback: () => Promise<({ uid: number | string, text: string } | boolean)>, { uid: number | string; text: string } | boolean
nodeOptions?: Record<string, any> >;
} nodeOptions?: Record<string, any>;
) { }) {
const { getMindMapInstance } = useMindMapStore(); const { getMindMapInstance } = useMindMapStore();
const _nodeOptions = { const _nodeOptions = {
uid: createUid(), uid: createUid(),
text: " - ", text: " - ",
...(params.nodeOptions || {}) ...(params.nodeOptions || {}),
} };
try { try {
// 如果提供了回调函数,先执行回调 // 如果提供了回调函数,先执行回调
const result = await params.beforeInsertCallback(); const result = await params.beforeInsertCallback();
if (!result) return; // 如果直接 false 则不再执行插入操作 if (!result) return;
if (result) { 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) { } catch (error) {
console.error("插入子节点失败:", error); console.error("插入子节点失败:", error);
throw error; throw error;
} }
} }

View File

@ -1,5 +1,5 @@
import { useMindMapStore } from "../../store/index"; 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 * @param nodeOptions
* @returns Promise * @returns Promise
*/ */
export async function insertSiblingNode( export async function insertSiblingNode(params: {
beforeInsertCallback: () => Promise<({ uid: number | string, text: string } | boolean)>, beforeInsertCallback: () => Promise<
nodeOptions: Record<string, any> = { uid: createUid(), text: " - " } { uid: number | string; text: string } | boolean
) { >;
nodeId: string | number;
nodeOptions?: Record<string, any>;
}) {
const { getMindMapInstance } = useMindMapStore(); const { getMindMapInstance } = useMindMapStore();
const _nodeOptions = { const _nodeOptions = {
...nodeOptions, uid: createUid(),
} text: " - ",
...params.nodeOptions,
try { };
// 如果提供了回调函数,先执行回调
const result = await beforeInsertCallback();
if (!result) return; // 如果直接 false 则不再执行插入操作 try {
const result = await params.beforeInsertCallback();
if (!result) return;
if (result) { 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) { } catch (error) {
console.error("插入同级节点失败:", error); console.error("插入同级节点失败:", error);
throw error; throw error;
} }
} }