55import type { Ref } from 'vue'
66import type { TreeNode } from '~/types/editoro'
77import { isImagePath , isMarkdownPath } from '~/utils/editoro-file'
8+ import { hasHiddenPathSegment } from '~/utils/editoro-path'
89
910type EditorStoreLike = {
1011 clearEditor : ( ) => void
@@ -14,6 +15,9 @@ type EditorStoreLike = {
1415type FileSelectionOptions = {
1516 selectedNode : Ref < TreeNode | undefined >
1617 activeFilePath : Ref < string >
18+ pinnedFilePaths : Ref < string [ ] >
19+ showHiddenEntries : Ref < boolean >
20+ setShowHiddenEntries : ( nextValue : boolean ) => void
1721 treeInitialized : Ref < boolean >
1822 editorStore : EditorStoreLike
1923 loadTree : ( preferPath ?: string ) => Promise < void >
@@ -36,6 +40,27 @@ export function useEditoroFileSelection(options: FileSelectionOptions) {
3640 return ''
3741 }
3842
43+ function getPinnedFallbackPath ( ) {
44+ return options . pinnedFilePaths . value [ 0 ] || ''
45+ }
46+
47+ function getInitialPreferredPath ( ) {
48+ return getFileQueryValue ( ) || getPinnedFallbackPath ( )
49+ }
50+
51+ async function openPath ( path : string ) {
52+ if ( ! path ) {
53+ return false
54+ }
55+
56+ if ( hasHiddenPathSegment ( path ) && ! options . showHiddenEntries . value ) {
57+ options . setShowHiddenEntries ( true )
58+ }
59+
60+ await options . loadTree ( path )
61+ return options . selectedNode . value ?. path === path
62+ }
63+
3964 async function syncRouteWithFile ( filePath ?: string ) {
4065 const current = getFileQueryValue ( )
4166 const next = filePath || ''
@@ -74,36 +99,36 @@ export function useEditoroFileSelection(options: FileSelectionOptions) {
7499
75100 async function initializeFromRouteOnMounted ( ) {
76101 const filePath = getFileQueryValue ( )
102+ const preferredPath = getInitialPreferredPath ( )
77103
78- async function ensureRouteFileSelected ( path : string ) {
104+ async function ensurePathSelected ( path : string ) {
79105 const selected = options . selectedNode . value
80- if ( selected ?. type === 'file' && selected . path === path ) {
106+ if ( selected ?. path === path ) {
81107 return true
82108 }
83109
84- await options . loadTree ( path )
85-
86- const nextSelected = options . selectedNode . value
87- return nextSelected ?. type === 'file' && nextSelected . path === path
110+ return await openPath ( path )
88111 }
89112
90113 if ( ! options . treeInitialized . value ) {
91- await options . loadTree ( filePath || undefined )
114+ await options . loadTree ( preferredPath || undefined )
92115
93- if ( filePath && ! ( await ensureRouteFileSelected ( filePath ) ) ) {
116+ if ( filePath && ! ( await ensurePathSelected ( filePath ) ) ) {
94117 await syncRouteWithFile ( undefined )
95118 }
96119
97120 return
98121 }
99122
100123 if ( filePath ) {
101- const isOpened = await ensureRouteFileSelected ( filePath )
124+ const isOpened = await ensurePathSelected ( filePath )
102125 if ( ! isOpened ) {
103126 await syncRouteWithFile ( undefined )
104127 options . editorStore . clearEditor ( )
105128 return
106129 }
130+ } else if ( preferredPath ) {
131+ await ensurePathSelected ( preferredPath )
107132 }
108133
109134 // Hydration case: selected node can already be restored from SSR state,
@@ -126,6 +151,8 @@ export function useEditoroFileSelection(options: FileSelectionOptions) {
126151
127152 return {
128153 getFileQueryValue,
154+ getInitialPreferredPath,
155+ openPath,
129156 syncRouteWithFile,
130157 handleSelectedNodeChange,
131158 initializeFromRouteOnMounted
0 commit comments