Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 68 additions & 9 deletions app/components/Code/FileTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const props = defineProps<{
baseUrl: string
baseRoute: Pick<RouteNamedMap['code'], 'params'>
depth?: number
loadingDirs?: ReadonlySet<string>
}>()

const emit = defineEmits<{
expandDir: [path: string]
}>()

const depth = computed(() => props.depth ?? 0)
Expand Down Expand Up @@ -37,18 +42,63 @@ function getFileRoute(nodePath: string): RouteLocationRaw {

const { toggleDir, isExpanded, autoExpandAncestors } = useFileTreeState(props.baseUrl)

const scrollIntoView = () => {
const autoExpanded = new Set<string>()

function expandLoadedAncestors(path: string, force: boolean) {
let prefix = ''

for (const segment of path.split('/').filter(Boolean)) {
prefix = prefix ? `${prefix}/${segment}` : segment
const node = findTreeNode(props.tree, prefix)
if (!node) return
if (node.type !== 'directory') continue
if (node.children === undefined) return
if (!force && !isExpanded(prefix) && autoExpanded.has(prefix)) return

autoExpanded.add(prefix)
autoExpandAncestors(prefix)
}
}

function handleToggleDir(node: PackageFileTree) {
const willExpand = !isExpanded(node.path)
toggleDir(node.path)
if (willExpand && node.children === undefined) {
emit('expandDir', node.path)
}
}

let lastScrolledPath = ''

const scrollIntoView = (path: string) => {
const el = treeRoot.value?.querySelector<HTMLElement>('[aria-current="true"]')
el?.scrollIntoView({ block: 'center', behavior: 'smooth' })
if (el) {
lastScrolledPath = path
el.scrollIntoView({ block: 'center', behavior: 'smooth' })
}
}

// Auto-expand directories in the current path
watch(
() => props.currentPath,
path => {
() => [props.currentPath, props.tree] as const,
([path, nodes], previous) => {
if (depth.value === 0 && path) {
autoExpandAncestors(path)
nextTick(scrollIntoView)
expandLoadedAncestors(path, previous?.[0] !== path)
}

for (const node of nodes) {
if (
node.type === 'directory' &&
node.children === undefined &&
isExpanded(node.path) &&
!isNodeActive(node) &&
!props.loadingDirs?.has(node.path)
) {
emit('expandDir', node.path)
}
}

if (depth.value === 0 && path && lastScrolledPath !== path) {
nextTick(() => scrollIntoView(path))
}
},
{ immediate: true },
Expand All @@ -64,14 +114,21 @@ watch(
class="w-full justify-start! rounded-none! border-none! transition-[color,background-color]! duration-100!"
block
:aria-pressed="isNodeActive(node)"
:aria-busy="loadingDirs?.has(node.path) || undefined"
:aria-label="
isPossiblyUnnecessaryContent(node.name, 'directory')
? `${node.name} - ${$t('code.possibly_unnecessary')}`
: undefined
"
:style="{ paddingLeft: `${depth * 12 + 12}px` }"
@click="toggleDir(node.path)"
:classicon="isExpanded(node.path) ? 'i-lucide:chevron-down' : 'i-lucide:chevron-right'"
@click="handleToggleDir(node)"
:classicon="
loadingDirs?.has(node.path)
? 'i-svg-spinners:ring-resize'
: isExpanded(node.path)
? 'i-lucide:chevron-down'
: 'i-lucide:chevron-right'
"
>
<svg
class="size-[1em] me-1 shrink-0"
Expand Down Expand Up @@ -106,6 +163,8 @@ watch(
:base-url="baseUrl"
:base-route="baseRoute"
:depth="depth + 1"
:loading-dirs="loadingDirs"
@expand-dir="emit('expandDir', $event)"
/>
</template>

Expand Down
7 changes: 7 additions & 0 deletions app/components/Code/MobileTreeDrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ defineProps<{
currentPath: string
baseUrl: string
baseRoute: Pick<RouteNamedMap['code'], 'params'>
loadingDirs?: ReadonlySet<string>
}>()

const emit = defineEmits<{
expandDir: [path: string]
}>()

const isOpen = shallowRef(false)
Expand Down Expand Up @@ -74,6 +79,8 @@ defineExpose({
:current-path="currentPath"
:base-url="baseUrl"
:base-route="baseRoute"
:loading-dirs="loadingDirs"
@expand-dir="emit('expandDir', $event)"
/>
</aside>
</Transition>
Expand Down
5 changes: 5 additions & 0 deletions app/composables/useFileTreeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function useFileTreeState(baseUrl: string) {
}
}

function collapseDir(path: string) {
expanded.value.delete(path)
}

function isExpanded(path: string) {
return expanded.value.has(path)
}
Expand All @@ -25,6 +29,7 @@ export function useFileTreeState(baseUrl: string) {

return {
toggleDir,
collapseDir,
isExpanded,
autoExpandAncestors,
}
Expand Down
Loading
Loading