()
| 358 | }; |
| 359 | |
| 360 | const refreshTree = async () => { |
| 361 | if (!rootPath) return; |
| 362 | |
| 363 | // 保存当前展开状态 |
| 364 | const expandedPaths = new Set<string>(); |
| 365 | const collectExpandedPaths = (nodes: TreeNode[]) => { |
| 366 | for (const node of nodes) { |
| 367 | if (node.type === 'folder' && node.expanded) { |
| 368 | expandedPaths.add(node.path); |
| 369 | if (node.children) { |
| 370 | collectExpandedPaths(node.children); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | }; |
| 375 | collectExpandedPaths(tree); |
| 376 | |
| 377 | // 重新加载根目录,获取最新的文件结构 |
| 378 | try { |
| 379 | const entries = await TauriAPI.listDirectory(rootPath); |
| 380 | const children = entriesToNodes(entries); |
| 381 | |
| 382 | const rootName = rootPath.split(/[/\\]/).filter((p) => p).pop() || 'Project'; |
| 383 | let rootNode: TreeNode = { |
| 384 | name: rootName, |
| 385 | path: rootPath, |
| 386 | type: 'folder', |
| 387 | children: children, |
| 388 | expanded: true, |
| 389 | loaded: true |
| 390 | }; |
| 391 | |
| 392 | // 恢复展开状态 |
| 393 | if (expandedPaths.size > 0) { |
| 394 | const restoreExpandedState = async (node: TreeNode): Promise<TreeNode> => { |
| 395 | if (node.type === 'folder' && expandedPaths.has(node.path)) { |
| 396 | let children = node.children || []; |
| 397 | if (!node.loaded && node.children) { |
| 398 | children = await loadChildren(node); |
| 399 | } |
| 400 | const restoredChildren = await Promise.all( |
| 401 | children.map((child) => restoreExpandedState(child)) |
| 402 | ); |
| 403 | return { |
| 404 | ...node, |
| 405 | expanded: true, |
| 406 | loaded: true, |
| 407 | children: restoredChildren |
| 408 | }; |
| 409 | } else if (node.type === 'folder' && node.children) { |
| 410 | const restoredChildren = await Promise.all( |
| 411 | node.children.map((child) => restoreExpandedState(child)) |
| 412 | ); |
| 413 | return { |
| 414 | ...node, |
| 415 | children: restoredChildren |
| 416 | }; |
| 417 | } |
no test coverage detected