forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFileTree.vue
More file actions
84 lines (77 loc) · 2.62 KB
/
CodeFileTree.vue
File metadata and controls
84 lines (77 loc) · 2.62 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
<script setup lang="ts">
import type { PackageFileTree } from '#shared/types'
import { getFileIcon } from '~/utils/file-icons'
const props = defineProps<{
tree: PackageFileTree[]
currentPath: string
baseUrl: string
depth?: number
}>()
const depth = computed(() => props.depth ?? 0)
// Check if a node or any of its children is currently selected
function isNodeActive(node: PackageFileTree): boolean {
if (props.currentPath === node.path) return true
if (props.currentPath.startsWith(node.path + '/')) return true
return false
}
const { toggleDir, isExpanded, autoExpandAncestors } = useFileTreeState(props.baseUrl)
// Auto-expand directories in the current path
watch(
() => props.currentPath,
path => {
if (path) {
autoExpandAncestors(path)
}
},
{ immediate: true },
)
</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-carbon:chevron-down' : 'i-carbon:chevron-right']"
/>
<span
class="w-4 h-4 shrink-0"
:class="
isExpanded(node.path)
? 'i-carbon:folder-open text-yellow-500'
: 'i-carbon:folder text-yellow-600'
"
/>
<span class="truncate">{{ node.name }}</span>
</button>
<CodeFileTree
v-if="isExpanded(node.path) && node.children"
:tree="node.children"
:current-path="currentPath"
:base-url="baseUrl"
:depth="depth + 1"
/>
</template>
<!-- File -->
<template v-else>
<NuxtLink
:to="`${baseUrl}/${node.path}`"
class="flex items-center gap-1.5 py-1.5 px-3 font-mono text-sm transition-colors hover:bg-bg-muted"
:class="currentPath === node.path ? 'bg-bg-muted text-fg' : 'text-fg-muted'"
:style="{ paddingLeft: `${depth * 12 + 32}px` }"
>
<span class="w-4 h-4 shrink-0" :class="getFileIcon(node.name)" />
<span class="truncate">{{ node.name }}</span>
</NuxtLink>
</template>
</li>
</ul>
</template>