调整组操作

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();
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,
});
}
}
</script>

View File

@ -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<string, any>
}
) {
export async function insertChildNode(params: {
parentNodeId: string | number;
beforeInsertCallback: () => Promise<
{ uid: number | string; text: string } | boolean
>;
nodeOptions?: Record<string, any>;
}) {
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;
}
}
}

View File

@ -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<string, any> = { uid: createUid(), text: " - " }
) {
export async function insertSiblingNode(params: {
beforeInsertCallback: () => Promise<
{ uid: number | string; text: string } | boolean
>;
nodeId: string | number;
nodeOptions?: Record<string, any>;
}) {
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;
}
}