Skip to content

Commit a7d1498

Browse files
authored
fix: keep dirs expanded (#148)
1 parent ec10662 commit a7d1498

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

app/components/CodeFileTree.vue

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,18 @@ function isNodeActive(node: PackageFileTree): boolean {
1818
return false
1919
}
2020
21-
// State for expanded directories
22-
const expandedDirs = ref<Set<string>>(new Set())
21+
const { toggleDir, isExpanded, autoExpandAncestors } = useFileTreeState(props.baseUrl)
2322
2423
// Auto-expand directories in the current path
2524
watch(
2625
() => props.currentPath,
2726
path => {
28-
if (!path) return
29-
const parts = path.split('/')
30-
for (let i = 1; i <= parts.length; i++) {
31-
expandedDirs.value.add(parts.slice(0, i).join('/'))
27+
if (path) {
28+
autoExpandAncestors(path)
3229
}
3330
},
3431
{ immediate: true },
3532
)
36-
37-
function toggleDir(path: string) {
38-
if (expandedDirs.value.has(path)) {
39-
expandedDirs.value.delete(path)
40-
} else {
41-
expandedDirs.value.add(path)
42-
}
43-
}
44-
45-
function isExpanded(path: string): boolean {
46-
return expandedDirs.value.has(path)
47-
}
4833
</script>
4934

5035
<template>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function useFileTreeState(baseUrl: string) {
2+
const stateKey = computed(() => `npmx-file-tree${baseUrl}`)
3+
4+
const expanded = useState<Set<string>>(stateKey.value, () => new Set<string>())
5+
6+
function toggleDir(path: string) {
7+
if (expanded.value.has(path)) {
8+
expanded.value.delete(path)
9+
} else {
10+
expanded.value.add(path)
11+
}
12+
}
13+
14+
function isExpanded(path: string) {
15+
return expanded.value.has(path)
16+
}
17+
18+
function autoExpandAncestors(path: string) {
19+
if (!path) return
20+
const parts = path.split('/').filter(Boolean)
21+
let prefix = ''
22+
for (const part of parts) {
23+
prefix = prefix ? `${prefix}/${part}` : part
24+
expanded.value.add(prefix)
25+
}
26+
}
27+
28+
return {
29+
toggleDir,
30+
isExpanded,
31+
autoExpandAncestors,
32+
}
33+
}

0 commit comments

Comments
 (0)