miaopanel
1<script setup lang="ts">
2import { onMounted, ref } from 'vue'
3import { api } from '../api'
4import type { DirListing } from '../types'
5
6const props = withDefaults(
7 defineProps<{
8 title?: string
9 initial?: string
10 selectLabel?: string
11 }>(),
12 { title: '选择目录', initial: '/', selectLabel: '使用此目录' },
13)
14
15const emit = defineEmits<{ (e: 'close'): void; (e: 'select', path: string): void }>()
16
17const path = ref('/')
18const parent = ref('/')
19const dirs = ref<string[]>([])
20const loading = ref(false)
21const error = ref('')
22const jump = ref('')
23// Absolute path of the virtual root ("/" in the listing API), used to translate
24// site roots from the database and to return a real server path on select.
25const root = ref('')
26
27function base() {
28 return root.value.replace(/\\/g, '/').replace(/\/+$/, '')
29}
30
31function toVirtual(input: string) {
32 const p = input.replace(/\\/g, '/').trim()
33 const b = base()
34 if (!b || !p.startsWith('/')) return p
35 if (p === b) return '/'
36 if (p.startsWith(b + '/')) return p.slice(b.length)
37 return p
38}
39
40function absolute(p: string) {
41 const b = base()
42 if (!b) return p
43 return p === '/' ? b : b + p
44}
45
46async function load(target?: string) {
47 loading.value = true
48 error.value = ''
49 try {
50 const res = await api.get<DirListing>(
51 'api/v1/files?path=' + encodeURIComponent(target ?? path.value),
52 )
53 path.value = res.path
54 parent.value = res.parent
55 jump.value = res.path
56 if (res.root) root.value = res.root
57 dirs.value = (res.entries ?? [])
58 .filter((e) => e.dir)
59 .sort((a, b) => a.name.localeCompare(b.name))
60 .map((e) => e.name)
61 } catch (e) {
62 error.value = (e as Error).message
63 } finally {
64 loading.value = false
65 }
66}
67
68async function init() {
69 const initial = (props.initial || '/').trim() || '/'
70 await load(initial)
71 if (!error.value) return
72 // A stored site root is a real host path. If the backend could not map it,
73 // learn the virtual root once and retry with the translated path.
74 await load('/')
75 const translated = toVirtual(initial)
76 if (translated !== '/' && translated !== initial) await load(translated)
77}
78
79function enter(name: string) {
80 load(path.value === '/' ? `/${name}` : `${path.value}/${name}`)
81}
82
83function submitJump() {
84 const target = jump.value.trim()
85 if (target) load(toVirtual(target))
86}
87
88function choose() {
89 if (path.value === '/') return
90 emit('select', absolute(path.value))
91}
92
93onMounted(init)
94</script>
95
96<template>
97 <div class="overlay" @click.self="emit('close')">
98 <div class="modal picker" role="dialog" aria-modal="true">
99 <div class="modal-head">
100 <h3>{{ title }}</h3>
101 <button class="icon-btn" @click="emit('close')">×</button>
102 </div>
103
104 <div class="row picker-bar">
105 <button class="btn btn-ghost btn-sm" :disabled="path === '/'" @click="load(parent)">
106 上一级
107 </button>
108 <input
109 v-model="jump"
110 class="input mono"
111 style="flex: 1"
112 placeholder="/example"
113 @keyup.enter="submitJump"
114 />
115 <button class="btn btn-ghost btn-sm" @click="submitJump">前往</button>
116 </div>
117
118 <div class="dir-list">
119 <button v-for="d in dirs" :key="d" class="dir-row" @click="enter(d)">
120 <span class="dir-icon">▸</span>
121 <span class="mono">{{ d }}</span>
122 </button>
123 <div v-if="dirs.length === 0" class="empty">
124 {{ loading ? '加载中…' : '该目录下没有子目录' }}
125 </div>
126 </div>
127
128 <div v-if="error" class="alert">{{ error }}</div>
129 <p class="hint">
130 当前位置:<span class="mono">{{ path }}</span>
131 <template v-if="root">
132 (服务器路径 <span class="mono">{{ absolute(path) }}</span>)
133 </template>
134 </p>
135 <p class="hint">根目录为网站根,可直接粘贴服务器绝对路径跳转;请选择具体目录,不能选择根目录本身。</p>
136
137 <div class="row end">
138 <button class="btn btn-ghost" @click="emit('close')">取消</button>
139 <button
140 class="btn btn-primary"
141 :disabled="loading || path === '/'"
142 :title="path === '/' ? '请先进入一个具体目录' : ''"
143 @click="choose"
144 >
145 {{ selectLabel }}
146 </button>
147 </div>
148 </div>
149 </div>
150</template>
151
152<style scoped>
153.picker {
154 width: min(620px, 100%);
155}
156.picker-bar {
157 gap: 8px;
158 margin-bottom: 10px;
159}
160.dir-list {
161 max-height: 320px;
162 overflow: auto;
163 border: 1px solid var(--line);
164 border-radius: 10px;
165 background: #fbfefd;
166 padding: 6px;
167 margin-bottom: 10px;
168}
169.dir-row {
170 display: flex;
171 align-items: center;
172 gap: 8px;
173 width: 100%;
174 padding: 8px 10px;
175 border: none;
176 border-radius: 8px;
177 background: none;
178 text-align: left;
179 cursor: pointer;
180 font-size: 13px;
181}
182.dir-row:hover {
183 background: rgba(52, 211, 153, 0.1);
184 color: #0f766e;
185}
186.dir-icon {
187 color: #0ea5e9;
188}
189</style>