* Move a node
(id: string, newPath: string)
| 307 | * Move a node |
| 308 | */ |
| 309 | moveNode(id: string, newPath: string): FileNode | undefined { |
| 310 | const node = this.nodes.get(id); |
| 311 | if (!node) { |
| 312 | return undefined; |
| 313 | } |
| 314 | |
| 315 | const normalizedNewPath = normalizePath(newPath); |
| 316 | |
| 317 | // Check if target path already exists |
| 318 | if (this.pathIndex.has(normalizedNewPath)) { |
| 319 | throw new Error(`Target path already exists: ${normalizedNewPath}`); |
| 320 | } |
| 321 | |
| 322 | // Get new parent node |
| 323 | const newParentPath = getParentPath(normalizedNewPath); |
| 324 | const newParent = this.getByPath(newParentPath); |
| 325 | if (!newParent) { |
| 326 | throw new Error(`New parent not found: ${newParentPath}`); |
| 327 | } |
| 328 | |
| 329 | // Remove from old parent |
| 330 | if (node.parentId) { |
| 331 | const oldParent = this.nodes.get(node.parentId); |
| 332 | if (oldParent && oldParent.children) { |
| 333 | oldParent.children = oldParent.children.filter((cid) => cid !== id); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // Update path index |
| 338 | const oldPath = node.path; |
| 339 | this.pathIndex.delete(oldPath); |
| 340 | |
| 341 | // Update node |
| 342 | node.path = normalizedNewPath; |
| 343 | node.name = getFileName(normalizedNewPath); |
| 344 | node.parentId = newParent.id; |
| 345 | |
| 346 | this.pathIndex.set(normalizedNewPath, id); |
| 347 | |
| 348 | // Add to new parent |
| 349 | newParent.children = [...(newParent.children || []), id]; |
| 350 | |
| 351 | // If it's a folder, recursively update children paths |
| 352 | if (node.type === 'folder' && node.children) { |
| 353 | this.updateChildrenPaths(node); |
| 354 | } |
| 355 | |
| 356 | this.emit('node_updated', id, normalizedNewPath); |
| 357 | return node; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Recursively update children paths |
no test coverage detected