调整组操作
This commit is contained in:
parent
e6cdc4fb63
commit
947fd3374b
@ -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>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user