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
97 lines (89 loc) · 2.85 KB
/
FileTree.vue
File metadata and controls
97 lines (89 loc) · 2.85 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
<script setup lang="ts">
import type { PackageFileTree } from '#shared/types'
import type { RouteLocationRaw } from 'vue-router'
import { getFileIcon } from '~/utils/file-icons'
const props = defineProps<{
tree: PackageFileTree[]
currentPath: string
baseUrl: string
/** Base path segments for the code route (e.g., ['nuxt', 'v', '4.2.0']) */
basePath: 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
}
// Build route object for a file path
function getFileRoute(nodePath: string): RouteLocationRaw {
const pathSegments = [...props.basePath, ...nodePath.split('/')]
return {
name: 'code',
params: { path: pathSegments as [string, ...string[]] },
}
}
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'">
<ButtonBase
class="w-full justify-start! rounded-none! border-none!"
block
:aria-pressed="isNodeActive(node)"
:style="{ paddingLeft: `${depth * 12 + 12}px` }"
@click="toggleDir(node.path)"
:classicon="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>
</ButtonBase>
<CodeFileTree
v-if="isExpanded(node.path) && node.children"
:tree="node.children"
:current-path="currentPath"
:base-url="baseUrl"
:base-path="basePath"
:depth="depth + 1"
/>
</template>
<!-- File -->
<template v-else>
<LinkBase
type="button"
:to="getFileRoute(node.path)"
:aria-current="currentPath === node.path"
class="w-full justify-start! rounded-none! border-none!"
:inline="false"
size="sm"
:style="{ paddingLeft: `${depth * 12 + 32}px` }"
:classicon="getFileIcon(node.name)"
>
<span class="truncate">{{ node.name }}</span>
</LinkBase>
</template>
</li>
</ul>
</template>