forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTree.vue
More file actions
181 lines (159 loc) · 5.06 KB
/
FileTree.vue
File metadata and controls
181 lines (159 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script setup lang="ts">
import type { FileChange } from '#shared/types'
interface DiffTreeNode {
name: string
path: string
type: 'file' | 'directory'
changeType?: 'added' | 'removed' | 'modified'
children?: DiffTreeNode[]
}
const props = defineProps<{
files: FileChange[]
selectedPath: string | null
// Internal props for recursion
treeNodes?: DiffTreeNode[]
depth?: number
}>()
const emit = defineEmits<{
select: [file: FileChange]
}>()
const depth = computed(() => props.depth ?? 0)
// Sort: directories first, then alphabetically
function sortTree(nodes: DiffTreeNode[]): DiffTreeNode[] {
return nodes
.map(n => ({
...n,
children: n.children ? sortTree(n.children) : undefined,
}))
.sort((a, b) => {
if (a.type !== b.type) return a.type === 'directory' ? -1 : 1
return a.name.localeCompare(b.name)
})
}
// Build tree structure from flat file list (only at root level)
function buildTree(files: FileChange[]): DiffTreeNode[] {
const root: DiffTreeNode[] = []
for (const file of files) {
const parts = file.path.split('/')
let current = root
for (let i = 0; i < parts.length; i++) {
const part = parts[i]!
const isFile = i === parts.length - 1
const path = parts.slice(0, i + 1).join('/')
let node = current.find(n => n.name === part)
if (!node) {
node = {
name: part,
path,
type: isFile ? 'file' : 'directory',
changeType: isFile ? file.type : undefined,
children: isFile ? undefined : [],
}
current.push(node)
}
if (!isFile) {
current = node.children!
}
}
}
return sortTree(root)
}
// Use provided tree nodes or build from files
const tree = computed(() => props.treeNodes ?? buildTree(props.files))
// Check if a node or any of its children is currently selected
function isNodeActive(node: DiffTreeNode): boolean {
if (props.selectedPath === node.path) return true
if (props.selectedPath?.startsWith(node.path + '/')) return true
return false
}
const expandedDirs = ref<Set<string>>(new Set())
function collectDirs(nodes: DiffTreeNode[]) {
for (const node of nodes) {
if (node.type === 'directory') {
expandedDirs.value.add(node.path)
if (node.children) collectDirs(node.children)
}
}
}
// Auto-expand all directories eagerly (runs on both SSR and client)
watchEffect(() => {
if (props.depth === undefined || props.depth === 0) {
collectDirs(tree.value)
}
})
function toggleDir(path: string) {
if (expandedDirs.value.has(path)) {
expandedDirs.value.delete(path)
} else {
expandedDirs.value.add(path)
}
}
function isExpanded(path: string): boolean {
return expandedDirs.value.has(path)
}
function getChangeIcon(type: 'added' | 'removed' | 'modified') {
switch (type) {
case 'added':
return 'i-lucide:file-plus text-green-500'
case 'removed':
return 'i-lucide:file-minus text-red-500'
case 'modified':
return 'i-lucide:file-diff text-yellow-500'
}
}
function handleFileClick(node: DiffTreeNode) {
const file = props.files.find(f => f.path === node.path)
if (file) emit('select', file)
}
</script>
<template>
<ul class="list-none m-0 p-0" :class="depth === 0 ? 'py-2' : ''">
<li v-for="node in tree" :key="node.path">
<!-- Directory -->
<template v-if="node.type === 'directory'">
<button
type="button"
class="w-full flex items-center gap-1.5 py-1.5 px-3 text-start font-mono text-sm transition-colors hover:bg-bg-muted"
:class="isNodeActive(node) ? 'text-fg' : 'text-fg-muted'"
:style="{ paddingLeft: `${depth * 12 + 12}px` }"
@click="toggleDir(node.path)"
>
<span
class="w-4 h-4 shrink-0 transition-transform"
:class="[isExpanded(node.path) ? 'i-lucide:chevron-down' : 'i-lucide:chevron-right']"
/>
<span
class="w-4 h-4 shrink-0"
:class="
isExpanded(node.path)
? 'i-lucide:folder-open text-yellow-500'
: 'i-lucide:folder text-yellow-600'
"
/>
<span class="truncate">{{ node.name }}</span>
</button>
<FileTree
v-if="isExpanded(node.path) && node.children"
:files="files"
:tree-nodes="node.children"
:selected-path="selectedPath"
:depth="depth + 1"
@select="emit('select', $event)"
/>
</template>
<!-- File -->
<template v-else>
<button
type="button"
class="w-full flex items-center gap-1.5 py-1.5 px-3 font-mono text-sm transition-colors hover:bg-bg-muted text-start"
:class="selectedPath === node.path ? 'bg-bg-muted text-fg' : 'text-fg-muted'"
:style="{ paddingLeft: `${depth * 12 + 32}px` }"
@click="handleFileClick(node)"
>
<span class="w-4 h-4 shrink-0" :class="getChangeIcon(node.changeType!)" />
<span class="truncate">{{ node.name }}</span>
</button>
</template>
</li>
</ul>
</template>