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