( node: MindMapTreeNode, nodeId: string, )
| 127 | })); |
| 128 | |
| 129 | const addSiblingRecursive = ( |
| 130 | node: MindMapTreeNode, |
| 131 | nodeId: string, |
| 132 | ): { changed: boolean; node: MindMapTreeNode } => { |
| 133 | const childIndex = node.children.findIndex((child) => child.id === nodeId); |
| 134 | if (childIndex >= 0) { |
| 135 | const nextChildren = [...node.children]; |
| 136 | nextChildren.splice(childIndex + 1, 0, { |
| 137 | id: nextId(node.id), |
| 138 | label: '新分支', |
| 139 | summary: '', |
| 140 | children: [], |
| 141 | }); |
| 142 | return { changed: true, node: { ...node, children: nextChildren } }; |
| 143 | } |
| 144 | |
| 145 | let changed = false; |
| 146 | const nextChildren = node.children.map((child) => { |
| 147 | const result = addSiblingRecursive(child, nodeId); |
| 148 | if (result.changed) changed = true; |
| 149 | return result.node; |
| 150 | }); |
| 151 | |
| 152 | return { changed, node: changed ? { ...node, children: nextChildren } : node }; |
| 153 | }; |
| 154 | |
| 155 | export const addSiblingToNode = (root: MindMapTreeNode, nodeId: string): MindMapTreeNode => { |
| 156 | if (root.id === nodeId) return root; |
no test coverage detected