-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathFileTree.vue
More file actions
103 lines (95 loc) · 3.18 KB
/
FileTree.vue
File metadata and controls
103 lines (95 loc) · 3.18 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
<script setup lang="ts">
import type { RouteLocationRaw } from 'vue-router'
import type { RouteNamedMap } from 'vue-router/auto-routes'
import { ADDITIONAL_ICONS, getFileIcon } from '~/utils/file-icons'
const props = defineProps<{
tree: PackageFileTree[]
currentPath: string
baseUrl: string
baseRoute: Pick<RouteNamedMap['code'], 'params'>
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 {
return {
name: 'code',
params: {
org: props.baseRoute.params.org,
packageName: props.baseRoute.params.packageName,
version: props.baseRoute.params.version,
filePath: nodePath ?? '',
},
}
}
const { toggleDir, isExpanded, autoExpandAncestors } = useFileTreeState(props.baseUrl)
// Auto-expand directories in the current path
watch(
() => props.currentPath,
path => {
if (depth.value === 0 && 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-lucide:chevron-down' : 'i-lucide:chevron-right'"
>
<svg
class="size-[1em] me-1 shrink-0"
:class="isExpanded(node.path) ? 'text-yellow-500' : 'text-yellow-600'"
viewBox="0 0 16 16"
aria-hidden="true"
>
<use
:href="`/file-tree-sprite.svg#${isExpanded(node.path) ? ADDITIONAL_ICONS['folder-open'] : ADDITIONAL_ICONS['folder']}`"
/>
</svg>
<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-route="baseRoute"
:depth="depth + 1"
/>
</template>
<!-- File -->
<template v-else>
<LinkBase
variant="button-secondary"
:to="getFileRoute(node.path)"
:aria-current="currentPath === node.path"
class="w-full justify-start! rounded-none! border-none!"
block
:style="{ paddingLeft: `${depth * 12 + 32}px` }"
>
<svg class="size-[1em] me-1 shrink-0" viewBox="0 0 16 16" aria-hidden="true">
<use :href="`/file-tree-sprite.svg#${getFileIcon(node.name)}`" />
</svg>
<span class="truncate">{{ node.name }}</span>
</LinkBase>
</template>
</li>
</ul>
</template>