@ternarytech-io/gears-ui 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/TFileExplorer.vue +622 -0
- package/src/components/TTree.vue +7 -3
- package/src/index.ts +4 -1
- package/src/types.ts +59 -0
package/package.json
CHANGED
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A file explorer over a data provider (ExplorerProvider: the Studio's server, an app's API, or
|
|
3
|
+
// anything): the drives and their folders in a tree, the open folder's contents as a list or as
|
|
4
|
+
// cards, the path as breadcrumbs, and a toolbar. v-model:location is where it is (a drive, a
|
|
5
|
+
// folder, the folders on the way), v-model:selection the chosen items' ids, v-model:view the list
|
|
6
|
+
// or the cards. Selection works with the mouse (Ctrl, Shift) and the keyboard (arrows, Shift,
|
|
7
|
+
// Ctrl+A); Enter opens a folder (a file emits `open`), F2 renames in place, Delete deletes (after
|
|
8
|
+
// asking), Ctrl+X and Ctrl+V move, Backspace goes up. Items drag onto folders (in the list or the
|
|
9
|
+
// tree) to move; files dropped from the computer upload, a queue with progress. The context menu
|
|
10
|
+
// has the standard actions and `menu`'s own. The `info` slot is a side pane for the selection.
|
|
11
|
+
// What the provider can't do is left out.
|
|
12
|
+
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
|
13
|
+
import TBreadcrumbs from './TBreadcrumbs.vue'
|
|
14
|
+
import TButton from './TButton.vue'
|
|
15
|
+
import TContextMenu from './TContextMenu.vue'
|
|
16
|
+
import TEmpty from './TEmpty.vue'
|
|
17
|
+
import TIcon from './TIcon.vue'
|
|
18
|
+
import TModal from './TModal.vue'
|
|
19
|
+
import TProgress from './TProgress.vue'
|
|
20
|
+
import TSpinner from './TSpinner.vue'
|
|
21
|
+
import TSplitter from './TSplitter.vue'
|
|
22
|
+
import TTree from './TTree.vue'
|
|
23
|
+
import type { BreadcrumbItem, ContextMenuItem, ExplorerDrive, ExplorerItem, ExplorerLocation, ExplorerProvider, TreeNode } from '../types'
|
|
24
|
+
|
|
25
|
+
const props = withDefaults(defineProps<{
|
|
26
|
+
provider: ExplorerProvider
|
|
27
|
+
/** More context menu groups for the items chosen (none for the folder's background). */
|
|
28
|
+
menu?: (items: ExplorerItem[], location: ExplorerLocation) => ContextMenuItem[][]
|
|
29
|
+
/** Remembers the tree's width per browser under this key. */
|
|
30
|
+
storageKey?: string
|
|
31
|
+
/** The explorer's accessible name. */
|
|
32
|
+
label?: string
|
|
33
|
+
}>(), { label: 'Files' })
|
|
34
|
+
|
|
35
|
+
const location = defineModel<ExplorerLocation | null>('location', { default: null })
|
|
36
|
+
const selection = defineModel<string[]>('selection', { default: () => [] })
|
|
37
|
+
const view = defineModel<'list' | 'grid'>('view', { default: 'list' })
|
|
38
|
+
const emit = defineEmits<{ open: [item: ExplorerItem, location: ExplorerLocation]; changed: [location: ExplorerLocation] }>()
|
|
39
|
+
defineSlots<{
|
|
40
|
+
info?(scope: { items: ExplorerItem[]; location: ExplorerLocation | null }): unknown
|
|
41
|
+
toolbar?(scope: { items: ExplorerItem[]; location: ExplorerLocation | null }): unknown
|
|
42
|
+
}>()
|
|
43
|
+
|
|
44
|
+
const drives = ref<ExplorerDrive[]>([])
|
|
45
|
+
const items = ref<ExplorerItem[]>([])
|
|
46
|
+
const cursor = ref<string | null>(null)
|
|
47
|
+
const loading = ref(false)
|
|
48
|
+
const problem = ref('')
|
|
49
|
+
const drive = computed(() => drives.value.find(d => d.id === location.value?.drive) ?? null)
|
|
50
|
+
const writable = computed(() => !!drive.value && !drive.value.readOnly && !drive.value.unavailable)
|
|
51
|
+
const chosen = computed(() => items.value.filter(i => selection.value.includes(i.id)))
|
|
52
|
+
|
|
53
|
+
// ---- The tree: drives, then folders, each node's id "drive|folder" ("drive|" for its top) ----
|
|
54
|
+
const tree = ref<InstanceType<typeof TTree>>()
|
|
55
|
+
const treeSelected = ref<string | null>(null)
|
|
56
|
+
const treeExpanded = ref<string[]>([])
|
|
57
|
+
// Each folder node's place, to work out the path when it's chosen in the tree.
|
|
58
|
+
const places = new Map<string, { drive: string; folder: string | null; name: string; parent: string | null }>()
|
|
59
|
+
const nodeId = (driveId: string, folder: string | null) => `${driveId}|${folder ?? ''}`
|
|
60
|
+
const treeNodes = computed<TreeNode[]>(() => drives.value.map(d => ({
|
|
61
|
+
id: nodeId(d.id, null), label: d.name, icon: d.icon ?? 'server-2', leaf: !!d.unavailable, disabled: false
|
|
62
|
+
})))
|
|
63
|
+
|
|
64
|
+
async function loadFolders(node: TreeNode): Promise<TreeNode[]> {
|
|
65
|
+
const place = places.get(node.id) ?? { drive: node.id.split('|')[0]!, folder: null }
|
|
66
|
+
const found: TreeNode[] = []
|
|
67
|
+
let next: string | null = null
|
|
68
|
+
// Folders come first: read pages until a file shows up.
|
|
69
|
+
do {
|
|
70
|
+
const page = await props.provider.list(place.drive, place.folder, next)
|
|
71
|
+
const folders = page.items.filter(i => i.folder)
|
|
72
|
+
for (const f of folders) {
|
|
73
|
+
const id = nodeId(place.drive, f.id)
|
|
74
|
+
places.set(id, { drive: place.drive, folder: f.id, name: f.name, parent: node.id })
|
|
75
|
+
found.push({ id, label: f.name, icon: 'folder', openIcon: 'folder-open' })
|
|
76
|
+
}
|
|
77
|
+
next = folders.length === page.items.length ? page.cursor : null
|
|
78
|
+
} while (next)
|
|
79
|
+
return found
|
|
80
|
+
}
|
|
81
|
+
function pathTo(id: string): { id: string; name: string }[] {
|
|
82
|
+
const path: { id: string; name: string }[] = []
|
|
83
|
+
for (let at = places.get(id); at && at.folder; at = at.parent ? places.get(at.parent) : undefined)
|
|
84
|
+
path.unshift({ id: at.folder, name: at.name })
|
|
85
|
+
return path
|
|
86
|
+
}
|
|
87
|
+
function treeChose(node: TreeNode) {
|
|
88
|
+
const [driveId] = node.id.split('|')
|
|
89
|
+
const place = places.get(node.id)
|
|
90
|
+
go({ drive: driveId!, folder: place?.folder ?? null, path: place ? pathTo(node.id) : [] })
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---- Where it is ----
|
|
94
|
+
function go(to: ExplorerLocation) {
|
|
95
|
+
location.value = to
|
|
96
|
+
}
|
|
97
|
+
async function load(more = false) {
|
|
98
|
+
const at = location.value
|
|
99
|
+
if (!at || !drive.value || drive.value.unavailable) {
|
|
100
|
+
items.value = []
|
|
101
|
+
cursor.value = null
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
loading.value = true
|
|
105
|
+
problem.value = ''
|
|
106
|
+
try {
|
|
107
|
+
const page = await props.provider.list(at.drive, at.folder, more ? cursor.value : null)
|
|
108
|
+
if (location.value !== at) return
|
|
109
|
+
items.value = more ? [...items.value, ...page.items] : page.items
|
|
110
|
+
cursor.value = page.cursor
|
|
111
|
+
if (!more) selection.value = selection.value.filter(id => items.value.some(i => i.id === id))
|
|
112
|
+
} catch (e) {
|
|
113
|
+
problem.value = e instanceof Error ? e.message : 'The folder couldn\'t be read.'
|
|
114
|
+
} finally {
|
|
115
|
+
loading.value = false
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
watch(location, async at => {
|
|
119
|
+
await load()
|
|
120
|
+
if (!at) return
|
|
121
|
+
treeSelected.value = nodeId(at.drive, at.folder)
|
|
122
|
+
const ids = [nodeId(at.drive, null), ...at.path.map(p => nodeId(at.drive, p.id))]
|
|
123
|
+
if (ids.length > 1) await tree.value?.reveal(ids)
|
|
124
|
+
}, { deep: true })
|
|
125
|
+
|
|
126
|
+
onMounted(async () => {
|
|
127
|
+
try {
|
|
128
|
+
drives.value = await props.provider.drives()
|
|
129
|
+
} catch (e) {
|
|
130
|
+
problem.value = e instanceof Error ? e.message : 'The drives couldn\'t be read.'
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
if (!location.value || !drives.value.some(d => d.id === location.value!.drive)) {
|
|
134
|
+
const first = drives.value.find(d => !d.unavailable) ?? drives.value[0]
|
|
135
|
+
if (first) go({ drive: first.id, folder: null, path: [] })
|
|
136
|
+
} else await load()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
const crumbs = computed<BreadcrumbItem[]>(() => {
|
|
140
|
+
const at = location.value
|
|
141
|
+
if (!at || !drive.value) return []
|
|
142
|
+
return [
|
|
143
|
+
{ label: drive.value.name, icon: drive.value.icon ?? 'server-2', onSelect: () => go({ drive: at.drive, folder: null, path: [] }) },
|
|
144
|
+
...at.path.map((p, i) => ({ label: p.name, onSelect: () => go({ drive: at.drive, folder: p.id, path: at.path.slice(0, i + 1) }) }))
|
|
145
|
+
]
|
|
146
|
+
})
|
|
147
|
+
function openItem(item: ExplorerItem) {
|
|
148
|
+
const at = location.value
|
|
149
|
+
if (!at) return
|
|
150
|
+
if (item.folder) go({ drive: at.drive, folder: item.id, path: [...at.path, { id: item.id, name: item.name }] })
|
|
151
|
+
else emit('open', item, at)
|
|
152
|
+
}
|
|
153
|
+
function up() {
|
|
154
|
+
const at = location.value
|
|
155
|
+
if (!at || !at.path.length) return
|
|
156
|
+
const path = at.path.slice(0, -1)
|
|
157
|
+
go({ drive: at.drive, folder: path.at(-1)?.id ?? null, path })
|
|
158
|
+
}
|
|
159
|
+
/** After a change: the list, and the tree's node for this folder. */
|
|
160
|
+
async function changed() {
|
|
161
|
+
await load()
|
|
162
|
+
const at = location.value
|
|
163
|
+
if (!at) return
|
|
164
|
+
await tree.value?.reload(nodeId(at.drive, at.folder))
|
|
165
|
+
emit('changed', at)
|
|
166
|
+
}
|
|
167
|
+
async function attempt(what: string, work: () => Promise<unknown>) {
|
|
168
|
+
problem.value = ''
|
|
169
|
+
try {
|
|
170
|
+
await work()
|
|
171
|
+
} catch (e) {
|
|
172
|
+
problem.value = e instanceof Error ? e.message : `${what} didn't work.`
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ---- Selection ----
|
|
177
|
+
const focus = ref<string | null>(null)
|
|
178
|
+
let anchor: string | null = null
|
|
179
|
+
function click(event: MouseEvent, item: ExplorerItem) {
|
|
180
|
+
const ids = items.value.map(i => i.id)
|
|
181
|
+
if (event.shiftKey && anchor) {
|
|
182
|
+
const [a, b] = [ids.indexOf(anchor), ids.indexOf(item.id)].sort((x, y) => x - y)
|
|
183
|
+
selection.value = ids.slice(a, b + 1)
|
|
184
|
+
} else if (event.ctrlKey || event.metaKey) {
|
|
185
|
+
selection.value = selection.value.includes(item.id) ? selection.value.filter(id => id !== item.id) : [...selection.value, item.id]
|
|
186
|
+
anchor = item.id
|
|
187
|
+
} else {
|
|
188
|
+
selection.value = [item.id]
|
|
189
|
+
anchor = item.id
|
|
190
|
+
}
|
|
191
|
+
focus.value = item.id
|
|
192
|
+
}
|
|
193
|
+
function key(event: KeyboardEvent) {
|
|
194
|
+
if (renaming.value) return
|
|
195
|
+
const list = items.value
|
|
196
|
+
const at = list.findIndex(i => i.id === focus.value)
|
|
197
|
+
const step = (by: number) => {
|
|
198
|
+
const next = list[Math.max(0, Math.min(list.length - 1, at + by))]
|
|
199
|
+
if (!next) return
|
|
200
|
+
focus.value = next.id
|
|
201
|
+
if (event.shiftKey && anchor) {
|
|
202
|
+
const ids = list.map(i => i.id)
|
|
203
|
+
const [a, b] = [ids.indexOf(anchor), ids.indexOf(next.id)].sort((x, y) => x - y)
|
|
204
|
+
selection.value = ids.slice(a, b + 1)
|
|
205
|
+
} else {
|
|
206
|
+
selection.value = [next.id]
|
|
207
|
+
anchor = next.id
|
|
208
|
+
}
|
|
209
|
+
document.getElementById(rowId(next.id))?.scrollIntoView({ block: 'nearest' })
|
|
210
|
+
}
|
|
211
|
+
const ctrl = event.ctrlKey || event.metaKey
|
|
212
|
+
if (event.key === 'ArrowDown' || (view.value === 'grid' && event.key === 'ArrowRight')) step(1)
|
|
213
|
+
else if (event.key === 'ArrowUp' || (view.value === 'grid' && event.key === 'ArrowLeft')) step(-1)
|
|
214
|
+
else if (event.key === 'Home') step(-list.length)
|
|
215
|
+
else if (event.key === 'End') step(list.length)
|
|
216
|
+
else if (ctrl && event.key.toLowerCase() === 'a') selection.value = list.map(i => i.id)
|
|
217
|
+
else if (event.key === 'Enter' && chosen.value.length === 1) openItem(chosen.value[0]!)
|
|
218
|
+
else if (event.key === 'Backspace') up()
|
|
219
|
+
else if (event.key === 'F2' && chosen.value.length === 1 && canRename(chosen.value[0]!)) startRename(chosen.value[0]!)
|
|
220
|
+
else if (event.key === 'Delete' && chosen.value.length && canDelete(chosen.value)) askDelete(chosen.value)
|
|
221
|
+
else if (ctrl && event.key.toLowerCase() === 'x' && chosen.value.length && canMove(chosen.value)) cut(chosen.value)
|
|
222
|
+
else if (ctrl && event.key.toLowerCase() === 'v' && clipboard.value) void paste(location.value!.folder)
|
|
223
|
+
else if (event.key === 'Escape') selection.value = []
|
|
224
|
+
else return
|
|
225
|
+
event.preventDefault()
|
|
226
|
+
}
|
|
227
|
+
const rowId = (id: string) => `t-explorer-${uid}-${id}`
|
|
228
|
+
const uid = Math.random().toString(36).slice(2, 8)
|
|
229
|
+
|
|
230
|
+
// ---- Actions ----
|
|
231
|
+
const canRename = (item: ExplorerItem) => !!props.provider.rename && writable.value && item.can?.rename !== false
|
|
232
|
+
const canMove = (list: ExplorerItem[]) => !!props.provider.move && writable.value && list.every(i => i.can?.move !== false)
|
|
233
|
+
const canDelete = (list: ExplorerItem[]) => !!props.provider.remove && writable.value && list.every(i => i.can?.delete !== false)
|
|
234
|
+
|
|
235
|
+
const renaming = ref<string | null>(null)
|
|
236
|
+
const newName = ref('')
|
|
237
|
+
function startRename(item: ExplorerItem) {
|
|
238
|
+
renaming.value = item.id
|
|
239
|
+
newName.value = item.name
|
|
240
|
+
}
|
|
241
|
+
async function finishRename(item: ExplorerItem) {
|
|
242
|
+
const name = newName.value.trim()
|
|
243
|
+
renaming.value = null
|
|
244
|
+
if (!name || name === item.name || !location.value) return
|
|
245
|
+
await attempt('Renaming', async () => {
|
|
246
|
+
await props.provider.rename!(location.value!.drive, item, name)
|
|
247
|
+
await changed()
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const clipboard = ref<{ drive: string; items: ExplorerItem[] } | null>(null)
|
|
252
|
+
function cut(list: ExplorerItem[]) {
|
|
253
|
+
clipboard.value = { drive: location.value!.drive, items: [...list] }
|
|
254
|
+
}
|
|
255
|
+
async function paste(folder: string | null) {
|
|
256
|
+
const held = clipboard.value
|
|
257
|
+
const at = location.value
|
|
258
|
+
if (!held || !at) return
|
|
259
|
+
if (held.drive !== at.drive) {
|
|
260
|
+
problem.value = 'Items move within their drive here.'
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
clipboard.value = null
|
|
264
|
+
await moveTo(held.items, folder)
|
|
265
|
+
}
|
|
266
|
+
async function moveTo(list: ExplorerItem[], folder: string | null) {
|
|
267
|
+
const at = location.value
|
|
268
|
+
if (!at || !list.length) return
|
|
269
|
+
// A folder can't go into itself.
|
|
270
|
+
const moving = list.filter(i => i.id !== folder)
|
|
271
|
+
if (!moving.length) return
|
|
272
|
+
await attempt('Moving', async () => {
|
|
273
|
+
await props.provider.move!(at.drive, moving, folder)
|
|
274
|
+
await changed()
|
|
275
|
+
await tree.value?.reload(nodeId(at.drive, folder))
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const deleting = ref<ExplorerItem[] | null>(null)
|
|
280
|
+
const deleteOpen = computed({ get: () => !!deleting.value, set: open => { if (!open) deleting.value = null } })
|
|
281
|
+
function askDelete(list: ExplorerItem[]) {
|
|
282
|
+
deleting.value = [...list]
|
|
283
|
+
}
|
|
284
|
+
async function confirmDelete() {
|
|
285
|
+
const list = deleting.value
|
|
286
|
+
deleting.value = null
|
|
287
|
+
if (!list || !location.value) return
|
|
288
|
+
await attempt('Deleting', async () => {
|
|
289
|
+
await props.provider.remove!(location.value!.drive, list)
|
|
290
|
+
selection.value = []
|
|
291
|
+
await changed()
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const creating = ref(false)
|
|
296
|
+
const folderName = ref('')
|
|
297
|
+
async function createFolder() {
|
|
298
|
+
const name = folderName.value.trim()
|
|
299
|
+
creating.value = false
|
|
300
|
+
folderName.value = ''
|
|
301
|
+
if (!name || !location.value) return
|
|
302
|
+
await attempt('Making the folder', async () => {
|
|
303
|
+
await props.provider.createFolder!(location.value!.drive, location.value!.folder, name)
|
|
304
|
+
await changed()
|
|
305
|
+
})
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// ---- Uploads: one at a time, each with its progress, cancelable ----
|
|
309
|
+
interface Upload { id: number; name: string; size: number; sent: number; state: 'waiting' | 'sending' | 'done' | 'failed' | 'canceled'; error?: string; abort: AbortController; drive: string; folder: string | null; file: File }
|
|
310
|
+
const uploads = reactive<Upload[]>([])
|
|
311
|
+
let uploadIds = 0
|
|
312
|
+
let uploading = false
|
|
313
|
+
const picker = ref<HTMLInputElement>()
|
|
314
|
+
function queue(files: File[]) {
|
|
315
|
+
const at = location.value
|
|
316
|
+
if (!at || !props.provider.upload || !writable.value) return
|
|
317
|
+
for (const file of files)
|
|
318
|
+
uploads.push({ id: ++uploadIds, name: file.name, size: file.size, sent: 0, state: 'waiting', abort: new AbortController(), drive: at.drive, folder: at.folder, file })
|
|
319
|
+
void run()
|
|
320
|
+
}
|
|
321
|
+
async function run() {
|
|
322
|
+
if (uploading) return
|
|
323
|
+
uploading = true
|
|
324
|
+
try {
|
|
325
|
+
for (let next = uploads.find(u => u.state === 'waiting'); next; next = uploads.find(u => u.state === 'waiting')) {
|
|
326
|
+
const upload = next
|
|
327
|
+
upload.state = 'sending'
|
|
328
|
+
try {
|
|
329
|
+
await props.provider.upload!(upload.drive, upload.folder, upload.file, sent => { upload.sent = sent }, upload.abort.signal)
|
|
330
|
+
upload.state = 'done'
|
|
331
|
+
upload.sent = upload.size
|
|
332
|
+
if (location.value?.drive === upload.drive && location.value.folder === upload.folder) await load()
|
|
333
|
+
} catch (e) {
|
|
334
|
+
if (upload.abort.signal.aborted) upload.state = 'canceled'
|
|
335
|
+
else {
|
|
336
|
+
upload.state = 'failed'
|
|
337
|
+
upload.error = e instanceof Error ? e.message : 'It couldn\'t be uploaded.'
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
} finally {
|
|
342
|
+
uploading = false
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
function cancel(upload: Upload) {
|
|
346
|
+
if (upload.state === 'waiting') upload.state = 'canceled'
|
|
347
|
+
else if (upload.state === 'sending') upload.abort.abort()
|
|
348
|
+
}
|
|
349
|
+
function clearUploads() {
|
|
350
|
+
for (let i = uploads.length - 1; i >= 0; i--)
|
|
351
|
+
if (uploads[i]!.state !== 'waiting' && uploads[i]!.state !== 'sending') uploads.splice(i, 1)
|
|
352
|
+
}
|
|
353
|
+
const picked = (event: Event) => {
|
|
354
|
+
const input = event.target as HTMLInputElement
|
|
355
|
+
queue([...(input.files ?? [])])
|
|
356
|
+
input.value = ''
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ---- Dragging: items onto folders (list or tree) to move; files from the computer to upload ----
|
|
360
|
+
const dragType = 'application/x-t-explorer'
|
|
361
|
+
const over = ref<string | null>(null)
|
|
362
|
+
let dragged: ExplorerItem[] = []
|
|
363
|
+
function dragStart(event: DragEvent, item: ExplorerItem) {
|
|
364
|
+
// The model updates only once the parent has it, so the items are worked out here, not read back.
|
|
365
|
+
dragged = selection.value.includes(item.id) ? [...chosen.value] : [item]
|
|
366
|
+
if (!selection.value.includes(item.id)) selection.value = [item.id]
|
|
367
|
+
if (!canMove(dragged)) {
|
|
368
|
+
event.preventDefault()
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
event.dataTransfer?.setData(dragType, uid)
|
|
372
|
+
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move'
|
|
373
|
+
}
|
|
374
|
+
const carriesItems = (event: DragEvent) => event.dataTransfer?.types.includes(dragType) ?? false
|
|
375
|
+
const carriesFiles = (event: DragEvent) => event.dataTransfer?.types.includes('Files') ?? false
|
|
376
|
+
function rowDragOver(event: DragEvent, item: ExplorerItem) {
|
|
377
|
+
if (item.folder && carriesItems(event) && !dragged.some(d => d.id === item.id)) {
|
|
378
|
+
event.preventDefault()
|
|
379
|
+
over.value = item.id
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
async function rowDrop(event: DragEvent, item: ExplorerItem) {
|
|
383
|
+
over.value = null
|
|
384
|
+
if (!item.folder || !carriesItems(event)) return
|
|
385
|
+
event.preventDefault()
|
|
386
|
+
event.stopPropagation()
|
|
387
|
+
await moveTo(dragged, item.id)
|
|
388
|
+
}
|
|
389
|
+
const areaOver = ref(false)
|
|
390
|
+
function areaDragOver(event: DragEvent) {
|
|
391
|
+
if (carriesFiles(event) && writable.value && props.provider.upload) {
|
|
392
|
+
event.preventDefault()
|
|
393
|
+
areaOver.value = true
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function areaDrop(event: DragEvent) {
|
|
397
|
+
areaOver.value = false
|
|
398
|
+
if (!carriesFiles(event)) return
|
|
399
|
+
event.preventDefault()
|
|
400
|
+
queue([...(event.dataTransfer?.files ?? [])])
|
|
401
|
+
}
|
|
402
|
+
const treeDroppable = (node: TreeNode, event: DragEvent) =>
|
|
403
|
+
carriesItems(event) && node.id.startsWith(`${location.value?.drive}|`) && !dragged.some(d => nodeId(location.value!.drive, d.id) === node.id)
|
|
404
|
+
async function treeDrop({ node }: { node: TreeNode; event: DragEvent }) {
|
|
405
|
+
await moveTo(dragged, places.get(node.id)?.folder ?? null)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// ---- The context menu ----
|
|
409
|
+
function menuFor(event: MouseEvent | KeyboardEvent): ContextMenuItem[][] {
|
|
410
|
+
const row = (event.target as HTMLElement).closest<HTMLElement>('[data-item]')
|
|
411
|
+
const at = location.value
|
|
412
|
+
if (!at) return []
|
|
413
|
+
if (row) {
|
|
414
|
+
const item = items.value.find(i => i.id === row.dataset.item)
|
|
415
|
+
// A row outside the selection becomes it; the model updates only once the parent has it, so the list is worked out here.
|
|
416
|
+
const list = !item ? chosen.value : selection.value.includes(item.id) ? chosen.value : [item]
|
|
417
|
+
if (item && !selection.value.includes(item.id)) selection.value = [item.id]
|
|
418
|
+
const one = list.length === 1 ? list[0]! : null
|
|
419
|
+
const groups: ContextMenuItem[][] = [[
|
|
420
|
+
...(one ? [{ label: 'Open', icon: one.folder ? 'folder-open' : 'external-link', kbd: 'Enter', onSelect: () => openItem(one) }] : []),
|
|
421
|
+
...(one && !one.folder && props.provider.href ? [{ label: 'Download', icon: 'download', onSelect: () => download(one) }] : [])
|
|
422
|
+
], [
|
|
423
|
+
...(props.provider.rename && one ? [{ label: 'Rename', icon: 'pencil', kbd: 'F2', disabled: !canRename(one), reason: reasonFor(one, 'rename'), onSelect: () => startRename(one) }] : []),
|
|
424
|
+
...(props.provider.move ? [{ label: 'Cut', icon: 'scissors', kbd: 'Ctrl+X', disabled: !canMove(list), reason: list.length === 1 ? reasonFor(list[0]!, 'move') : undefined, onSelect: () => cut(list) }] : []),
|
|
425
|
+
...(clipboard.value && one?.folder ? [{ label: 'Paste into', icon: 'clipboard', onSelect: () => void paste(one.id) }] : [])
|
|
426
|
+
], ...(props.menu?.(list, at) ?? []), [
|
|
427
|
+
...(props.provider.remove ? [{ label: 'Delete', icon: 'trash', color: 'error' as const, kbd: 'Del', disabled: !canDelete(list), reason: list.length === 1 ? reasonFor(list[0]!, 'delete') : undefined, onSelect: () => askDelete(list) }] : [])
|
|
428
|
+
]]
|
|
429
|
+
return groups.filter(g => g.length)
|
|
430
|
+
}
|
|
431
|
+
// The folder's background.
|
|
432
|
+
selection.value = []
|
|
433
|
+
return [[
|
|
434
|
+
...(props.provider.createFolder ? [{ label: 'New folder', icon: 'folder-plus', disabled: !writable.value, onSelect: () => { creating.value = true } }] : []),
|
|
435
|
+
...(props.provider.upload ? [{ label: 'Upload files…', icon: 'upload', disabled: !writable.value, onSelect: () => picker.value?.click() }] : []),
|
|
436
|
+
...(clipboard.value ? [{ label: 'Paste', icon: 'clipboard', kbd: 'Ctrl+V', onSelect: () => void paste(at.folder) }] : [])
|
|
437
|
+
], [{ label: 'Refresh', icon: 'refresh', onSelect: () => void load() }]].filter(g => g.length)
|
|
438
|
+
}
|
|
439
|
+
function reasonFor(item: ExplorerItem, what: 'rename' | 'move' | 'delete') {
|
|
440
|
+
if (drive.value?.readOnly) return 'The drive is read-only'
|
|
441
|
+
return item.can?.[what] === false ? 'You may not' : undefined
|
|
442
|
+
}
|
|
443
|
+
function download(item: ExplorerItem) {
|
|
444
|
+
const href = props.provider.href?.(location.value!.drive, item)
|
|
445
|
+
if (!href) return
|
|
446
|
+
const a = document.createElement('a')
|
|
447
|
+
a.href = href
|
|
448
|
+
a.download = item.name
|
|
449
|
+
a.click()
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// ---- Showing things ----
|
|
453
|
+
function sizeOf(bytes?: number | null) {
|
|
454
|
+
if (bytes == null) return ''
|
|
455
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
456
|
+
let value = bytes
|
|
457
|
+
let unit = 0
|
|
458
|
+
while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit++ }
|
|
459
|
+
return `${value.toFixed(value < 10 && unit > 0 ? 1 : 0)} ${units[unit]}`
|
|
460
|
+
}
|
|
461
|
+
function dateOf(value?: string | null) {
|
|
462
|
+
return value ? new Date(value).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' }) : ''
|
|
463
|
+
}
|
|
464
|
+
function iconOf(item: ExplorerItem) {
|
|
465
|
+
if (item.icon) return item.icon
|
|
466
|
+
if (item.folder) return 'folder'
|
|
467
|
+
const type = item.mimeType ?? ''
|
|
468
|
+
if (type.startsWith('image/')) return 'photo'
|
|
469
|
+
if (type === 'application/pdf') return 'file-type-pdf'
|
|
470
|
+
if (type.startsWith('video/')) return 'movie'
|
|
471
|
+
if (type.startsWith('audio/')) return 'music'
|
|
472
|
+
if (type.includes('zip')) return 'file-zip'
|
|
473
|
+
if (type.includes('sheet') || type.includes('csv')) return 'file-spreadsheet'
|
|
474
|
+
if (type.startsWith('text/')) return 'file-text'
|
|
475
|
+
return 'file'
|
|
476
|
+
}
|
|
477
|
+
const infoShown = ref(true)
|
|
478
|
+
</script>
|
|
479
|
+
|
|
480
|
+
<template>
|
|
481
|
+
<div class="t-explorer" :aria-label="label" role="region">
|
|
482
|
+
<TSplitter :default-size="24" :min-start="12" :min-end="40" :storage-key="storageKey ? `${storageKey}:tree` : undefined" class="t-explorer-split">
|
|
483
|
+
<template #start>
|
|
484
|
+
<div class="t-explorer-tree">
|
|
485
|
+
<TTree ref="tree" v-model:selected="treeSelected" v-model:expanded="treeExpanded" :nodes="treeNodes" :load="loadFolders"
|
|
486
|
+
:droppable="treeDroppable" size="sm" :label="`${label}: folders`" @select="treeChose" @drop="treeDrop" />
|
|
487
|
+
</div>
|
|
488
|
+
</template>
|
|
489
|
+
<template #end>
|
|
490
|
+
<div class="t-explorer-main">
|
|
491
|
+
<div class="t-explorer-bar">
|
|
492
|
+
<TBreadcrumbs :items="crumbs" :max="5" size="sm" class="flex-fill min-w-0" />
|
|
493
|
+
<slot name="toolbar" :items="chosen" :location="location" />
|
|
494
|
+
<TButton v-if="provider.createFolder" size="sm" variant="ghost" color="neutral" icon="folder-plus" :disabled="!writable" title="New folder"
|
|
495
|
+
aria-label="New folder" @click="creating = true" />
|
|
496
|
+
<TButton v-if="provider.upload" size="sm" variant="ghost" color="neutral" icon="upload" label="Upload" :disabled="!writable" @click="picker?.click()" />
|
|
497
|
+
<input ref="picker" type="file" multiple class="d-none" @change="picked" />
|
|
498
|
+
<div class="btn-group" role="group" aria-label="View">
|
|
499
|
+
<TButton size="sm" :variant="view === 'list' ? 'soft' : 'ghost'" color="neutral" icon="list" aria-label="List" @click="view = 'list'" />
|
|
500
|
+
<TButton size="sm" :variant="view === 'grid' ? 'soft' : 'ghost'" color="neutral" icon="layout-grid" aria-label="Cards" @click="view = 'grid'" />
|
|
501
|
+
</div>
|
|
502
|
+
<TButton v-if="$slots.info" size="sm" variant="ghost" color="neutral" :icon="infoShown ? 'layout-sidebar-right-collapse' : 'layout-sidebar-right-expand'"
|
|
503
|
+
:aria-label="infoShown ? 'Hide details' : 'Show details'" @click="infoShown = !infoShown" />
|
|
504
|
+
</div>
|
|
505
|
+
<div v-if="problem" class="alert alert-danger py-2 px-3 m-2 mb-0 small d-flex align-items-center gap-2" role="alert">
|
|
506
|
+
<TIcon name="alert-circle" /> <span class="flex-fill">{{ problem }}</span>
|
|
507
|
+
<button type="button" class="btn-close" aria-label="Dismiss" @click="problem = ''" />
|
|
508
|
+
</div>
|
|
509
|
+
<div class="t-explorer-body">
|
|
510
|
+
<!-- The menu's area is inside: TContextMenu has two roots (the area, the menu on the body), so the explorer's scoped styles stay on its own element. -->
|
|
511
|
+
<div :class="['t-explorer-items', { over: areaOver }]" tabindex="0" :aria-label="`${label}: contents`"
|
|
512
|
+
@keydown="key" @dragover="areaDragOver" @dragleave="areaOver = false" @drop="areaDrop">
|
|
513
|
+
<TContextMenu :items="menuFor" style="min-height: 100%">
|
|
514
|
+
<div v-if="drive?.unavailable" class="p-4"><TEmpty icon="plug-connected-x" :title="`${drive.name} can't be opened here`" :description="drive.unavailable" /></div>
|
|
515
|
+
<div v-else-if="loading && !items.length" class="p-4 text-center"><TSpinner /></div>
|
|
516
|
+
<div v-else-if="!items.length && !creating" class="p-4">
|
|
517
|
+
<TEmpty icon="folder-open" title="This folder is empty" :description="writable && provider.upload ? 'Drop files here, or use Upload.' : undefined" />
|
|
518
|
+
</div>
|
|
519
|
+
<template v-else>
|
|
520
|
+
<div v-if="creating" class="t-explorer-new">
|
|
521
|
+
<TIcon name="folder-plus" />
|
|
522
|
+
<input v-model="folderName" class="form-control form-control-sm" placeholder="New folder's name" aria-label="New folder's name" autofocus
|
|
523
|
+
@keydown.enter.stop="createFolder" @keydown.esc.stop="creating = false; folderName = ''" @blur="folderName.trim() ? createFolder() : (creating = false)" />
|
|
524
|
+
</div>
|
|
525
|
+
<table v-if="view === 'list'" class="table table-sm table-hover t-explorer-table mb-0">
|
|
526
|
+
<thead><tr><th>Name</th><th class="d-none d-md-table-cell">Kind</th><th class="text-end">Size</th><th class="d-none d-lg-table-cell">Changed</th></tr></thead>
|
|
527
|
+
<tbody>
|
|
528
|
+
<tr v-for="item in items" :id="rowId(item.id)" :key="item.id" :data-item="item.id" :draggable="canMove([item]) || undefined"
|
|
529
|
+
:class="{ selected: selection.includes(item.id), focused: focus === item.id, over: over === item.id, cut: clipboard?.items.some(c => c.id === item.id) }"
|
|
530
|
+
:aria-selected="selection.includes(item.id)" @click="click($event, item)" @dblclick="openItem(item)"
|
|
531
|
+
@dragstart="dragStart($event, item)" @dragover="rowDragOver($event, item)" @dragleave="over = null" @drop="rowDrop($event, item)">
|
|
532
|
+
<td class="t-explorer-name">
|
|
533
|
+
<TIcon :name="iconOf(item)" :class="item.folder ? 'text-primary' : 'text-secondary'" />
|
|
534
|
+
<input v-if="renaming === item.id" v-model="newName" class="form-control form-control-sm" :aria-label="`New name for ${item.name}`" autofocus
|
|
535
|
+
@click.stop @keydown.enter.stop="finishRename(item)" @keydown.esc.stop="renaming = null" @blur="finishRename(item)" />
|
|
536
|
+
<span v-else class="text-truncate">{{ item.name }}</span>
|
|
537
|
+
</td>
|
|
538
|
+
<td class="d-none d-md-table-cell text-secondary">{{ item.folder ? 'Folder' : item.kind ?? '' }}</td>
|
|
539
|
+
<td class="text-end text-secondary text-nowrap">{{ item.folder ? '' : sizeOf(item.size) }}</td>
|
|
540
|
+
<td class="d-none d-lg-table-cell text-secondary text-nowrap">{{ dateOf(item.modifiedAt) }}</td>
|
|
541
|
+
</tr>
|
|
542
|
+
</tbody>
|
|
543
|
+
</table>
|
|
544
|
+
<div v-else class="t-explorer-grid">
|
|
545
|
+
<div v-for="item in items" :id="rowId(item.id)" :key="item.id" :data-item="item.id" :draggable="canMove([item]) || undefined"
|
|
546
|
+
:class="['t-explorer-card', { selected: selection.includes(item.id), focused: focus === item.id, over: over === item.id, cut: clipboard?.items.some(c => c.id === item.id) }]"
|
|
547
|
+
:aria-selected="selection.includes(item.id)" @click="click($event, item)" @dblclick="openItem(item)"
|
|
548
|
+
@dragstart="dragStart($event, item)" @dragover="rowDragOver($event, item)" @dragleave="over = null" @drop="rowDrop($event, item)">
|
|
549
|
+
<TIcon :name="iconOf(item)" :class="['t-explorer-card-icon', item.folder ? 'text-primary' : 'text-secondary']" />
|
|
550
|
+
<input v-if="renaming === item.id" v-model="newName" class="form-control form-control-sm" :aria-label="`New name for ${item.name}`" autofocus
|
|
551
|
+
@click.stop @keydown.enter.stop="finishRename(item)" @keydown.esc.stop="renaming = null" @blur="finishRename(item)" />
|
|
552
|
+
<span v-else class="t-explorer-card-name">{{ item.name }}</span>
|
|
553
|
+
<span class="small text-secondary">{{ item.folder ? 'Folder' : sizeOf(item.size) }}</span>
|
|
554
|
+
</div>
|
|
555
|
+
</div>
|
|
556
|
+
<div v-if="cursor" class="p-2 text-center">
|
|
557
|
+
<TButton size="sm" variant="ghost" color="neutral" label="Show more" :loading="loading" @click="load(true)" />
|
|
558
|
+
</div>
|
|
559
|
+
</template>
|
|
560
|
+
</TContextMenu>
|
|
561
|
+
</div>
|
|
562
|
+
<aside v-if="$slots.info && infoShown" class="t-explorer-info" :aria-label="`${label}: details`">
|
|
563
|
+
<slot name="info" :items="chosen" :location="location" />
|
|
564
|
+
</aside>
|
|
565
|
+
</div>
|
|
566
|
+
<div v-if="uploads.length" class="t-explorer-uploads" aria-label="Uploads">
|
|
567
|
+
<div class="d-flex align-items-center gap-2 mb-1">
|
|
568
|
+
<span class="small fw-medium flex-fill">Uploads</span>
|
|
569
|
+
<TButton size="sm" variant="ghost" color="neutral" label="Clear finished" @click="clearUploads" />
|
|
570
|
+
</div>
|
|
571
|
+
<div v-for="u in uploads" :key="u.id" class="t-explorer-upload">
|
|
572
|
+
<TIcon :name="u.state === 'done' ? 'circle-check' : u.state === 'failed' ? 'alert-circle' : u.state === 'canceled' ? 'circle-x' : 'upload'"
|
|
573
|
+
:class="u.state === 'done' ? 'text-success' : u.state === 'failed' ? 'text-danger' : 'text-secondary'" />
|
|
574
|
+
<span class="text-truncate small" style="min-width: 6rem; max-width: 16rem">{{ u.name }}</span>
|
|
575
|
+
<TProgress v-if="u.state === 'sending' || u.state === 'waiting'" :value="u.state === 'waiting' ? 0 : u.sent" :max="u.size || 1" size="sm" class="flex-fill" :label="`${u.name} sent`" />
|
|
576
|
+
<span v-else class="small text-secondary flex-fill text-truncate">{{ u.state === 'failed' ? u.error : u.state === 'canceled' ? 'Canceled' : sizeOf(u.size) }}</span>
|
|
577
|
+
<TButton v-if="u.state === 'waiting' || u.state === 'sending'" size="sm" variant="ghost" color="neutral" icon="x" :aria-label="`Cancel ${u.name}`" @click="cancel(u)" />
|
|
578
|
+
</div>
|
|
579
|
+
</div>
|
|
580
|
+
</div>
|
|
581
|
+
</template>
|
|
582
|
+
</TSplitter>
|
|
583
|
+
<TModal v-model:open="deleteOpen" title="Delete?" size="sm">
|
|
584
|
+
<p class="mb-0">
|
|
585
|
+
{{ deleting?.length === 1 ? `Delete ${deleting[0]!.name}?` : `Delete these ${deleting?.length} items?` }}
|
|
586
|
+
</p>
|
|
587
|
+
<template #footer>
|
|
588
|
+
<TButton color="neutral" variant="ghost" label="Cancel" @click="deleting = null" />
|
|
589
|
+
<TButton color="error" icon="trash" label="Delete" @click="confirmDelete" />
|
|
590
|
+
</template>
|
|
591
|
+
</TModal>
|
|
592
|
+
</div>
|
|
593
|
+
</template>
|
|
594
|
+
|
|
595
|
+
<style scoped>
|
|
596
|
+
.t-explorer { display: flex; flex-direction: column; height: 100%; min-height: 24rem; border: var(--tblr-border-width) solid var(--tblr-border-color); border-radius: var(--tblr-border-radius); background: var(--tblr-bg-surface); overflow: hidden; }
|
|
597
|
+
.t-explorer-split { flex: 1 1 auto; min-height: 0; }
|
|
598
|
+
.t-explorer-tree { height: 100%; overflow: auto; padding: .375rem; border-inline-end: var(--tblr-border-width) solid var(--tblr-border-color); }
|
|
599
|
+
.t-explorer-main { display: flex; flex-direction: column; height: 100%; min-width: 0; }
|
|
600
|
+
.t-explorer-bar { display: flex; align-items: center; gap: .25rem; padding: .375rem .5rem; border-bottom: var(--tblr-border-width) solid var(--tblr-border-color); min-width: 0; }
|
|
601
|
+
.t-explorer-body { display: flex; flex: 1 1 auto; min-height: 0; }
|
|
602
|
+
.t-explorer-items { flex: 1 1 auto; overflow: auto; min-width: 0; outline: none; }
|
|
603
|
+
.t-explorer-items.over { box-shadow: inset 0 0 0 2px var(--tblr-primary); background: var(--tblr-primary-lt); }
|
|
604
|
+
.t-explorer-table th { position: sticky; top: 0; background: var(--tblr-bg-surface); z-index: 1; font-weight: var(--tblr-font-weight-medium, 500); }
|
|
605
|
+
.t-explorer-table tr { cursor: default; user-select: none; }
|
|
606
|
+
.t-explorer-table tr.selected td { background: var(--tblr-primary-lt); }
|
|
607
|
+
.t-explorer-table tr.focused td:first-child { box-shadow: inset 2px 0 0 var(--tblr-primary); }
|
|
608
|
+
.t-explorer-table tr.over td { background: var(--tblr-primary-lt); box-shadow: inset 0 1px 0 var(--tblr-primary), inset 0 -1px 0 var(--tblr-primary); }
|
|
609
|
+
.t-explorer-table tr.cut, .t-explorer-card.cut { opacity: .5; }
|
|
610
|
+
.t-explorer-name { display: flex; align-items: center; gap: .5rem; min-width: 0; max-width: 32rem; }
|
|
611
|
+
.t-explorer-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); gap: .5rem; padding: .5rem; }
|
|
612
|
+
.t-explorer-card { display: flex; flex-direction: column; align-items: center; gap: .25rem; padding: .75rem .5rem; border-radius: var(--tblr-border-radius); border: 1px solid transparent; cursor: default; user-select: none; text-align: center; min-width: 0; }
|
|
613
|
+
.t-explorer-card:hover { background: var(--tblr-bg-surface-secondary); }
|
|
614
|
+
.t-explorer-card.selected { background: var(--tblr-primary-lt); border-color: var(--tblr-primary); }
|
|
615
|
+
.t-explorer-card.over { border-color: var(--tblr-primary); }
|
|
616
|
+
.t-explorer-card-icon { font-size: 2rem; line-height: 1; display: block; height: 2rem; }
|
|
617
|
+
.t-explorer-card-name { font-size: .8125rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
|
|
618
|
+
.t-explorer-new { display: flex; align-items: center; gap: .5rem; padding: .375rem .75rem; }
|
|
619
|
+
.t-explorer-info { width: 18rem; flex: none; overflow: auto; border-inline-start: var(--tblr-border-width) solid var(--tblr-border-color); }
|
|
620
|
+
.t-explorer-uploads { border-top: var(--tblr-border-width) solid var(--tblr-border-color); padding: .5rem .75rem; max-height: 11rem; overflow: auto; }
|
|
621
|
+
.t-explorer-upload { display: flex; align-items: center; gap: .5rem; padding-block: .125rem; }
|
|
622
|
+
</style>
|
package/src/components/TTree.vue
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// goes up, Home and End, Enter or Space chooses, and typing a letter jumps to the next node it
|
|
7
7
|
// starts. Nodes can take drops (`droppable` says which, for a dragover) and be dragged
|
|
8
8
|
// (`draggable`), for moving things into folders. The `label` slot draws a row's text, `actions`
|
|
9
|
-
// its trailing buttons. `reload(id)` reads a node's children again; `reveal(ids)` opens a path
|
|
9
|
+
// its trailing buttons. `reload(id)` reads a node's children again; `reveal(ids)` opens a path
|
|
10
|
+
// and marks its last node chosen (without `select`, which is for a person's choice).
|
|
10
11
|
import { computed, nextTick, reactive, ref } from 'vue'
|
|
11
12
|
import TIcon from './TIcon.vue'
|
|
12
13
|
import TSpinner from './TSpinner.vue'
|
|
@@ -107,13 +108,16 @@ async function reload(id?: string) {
|
|
|
107
108
|
if (row) await fetch(row.node, true)
|
|
108
109
|
else loaded.delete(id)
|
|
109
110
|
}
|
|
110
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* Opens each node of a path, top first, loading as it goes, and marks the last as chosen. It
|
|
113
|
+
* doesn't emit `select`: that's for a person's choice, and a reveal follows one made elsewhere.
|
|
114
|
+
*/
|
|
111
115
|
async function reveal(ids: string[]) {
|
|
112
116
|
for (const [i, id] of ids.entries()) {
|
|
113
117
|
const row = rows.value.find(r => r.node.id === id)
|
|
114
118
|
if (!row) return
|
|
115
119
|
if (i < ids.length - 1) await openNode(row.node)
|
|
116
|
-
else
|
|
120
|
+
else selected.value = row.node.id
|
|
117
121
|
}
|
|
118
122
|
}
|
|
119
123
|
defineExpose({ reload, reveal })
|
package/src/index.ts
CHANGED
|
@@ -28,6 +28,7 @@ import TDropdown from './components/TDropdown.vue'
|
|
|
28
28
|
import TBreadcrumbs from './components/TBreadcrumbs.vue'
|
|
29
29
|
import TContextMenu from './components/TContextMenu.vue'
|
|
30
30
|
import TTree from './components/TTree.vue'
|
|
31
|
+
import TFileExplorer from './components/TFileExplorer.vue'
|
|
31
32
|
import TEditableTable from './components/TEditableTable.vue'
|
|
32
33
|
import TEmpty from './components/TEmpty.vue'
|
|
33
34
|
import TFieldGroup from './components/TFieldGroup.vue'
|
|
@@ -105,6 +106,7 @@ export {
|
|
|
105
106
|
TBreadcrumbs,
|
|
106
107
|
TContextMenu,
|
|
107
108
|
TTree,
|
|
109
|
+
TFileExplorer,
|
|
108
110
|
TEditableTable,
|
|
109
111
|
TEmpty,
|
|
110
112
|
TFieldGroup,
|
|
@@ -170,7 +172,7 @@ export { useToast, type Toast, type ToastPosition } from './composables/useToast
|
|
|
170
172
|
const components = {
|
|
171
173
|
TAccentPicker, TAdvancedTable, TAlert, TAppLayout, TAuthLayout, TAvatar, TBadge, TButton, TButtonGroup, TCard,
|
|
172
174
|
TCheckbox, TCheckboxTree, TCodeBlock, TColorModeButton, TCommandPalette, TContainerPicker, TCopyButton, TDataGrid, TDataGridItem, TDensityPicker,
|
|
173
|
-
TDropdown, TBreadcrumbs, TContextMenu, TTree, TEditableTable, TEmpty, TFieldGroup, TFileUpload, TFontPicker, TFormField, TIcon, TIconPicker, TImageUpload,
|
|
175
|
+
TDropdown, TBreadcrumbs, TContextMenu, TTree, TFileExplorer, TEditableTable, TEmpty, TFieldGroup, TFileUpload, TFontPicker, TFormField, TIcon, TIconPicker, TImageUpload,
|
|
174
176
|
TInput, TInputNumber, TInputTags, TLightbox, TListBox, TLogView, TMethodBadge, TModal, TNav, TNavPositionPicker, TPage, TPagination,
|
|
175
177
|
TPanelList, TProgress, TRadioGroup, TRadiusPicker, TRating, TScaledFrame, TSearchButton, TSectionLayout,
|
|
176
178
|
TSelect, TSelectMenu, TSeparator, TShell, TSkeleton, TSlideover, TSparkline, TSpinner, TSplitButton, TSplitter, TStatCard,
|
|
@@ -210,6 +212,7 @@ declare module 'vue' {
|
|
|
210
212
|
TBreadcrumbs: typeof TBreadcrumbs
|
|
211
213
|
TContextMenu: typeof TContextMenu
|
|
212
214
|
TTree: typeof TTree
|
|
215
|
+
TFileExplorer: typeof TFileExplorer
|
|
213
216
|
TEditableTable: typeof TEditableTable
|
|
214
217
|
TEmpty: typeof TEmpty
|
|
215
218
|
TFieldGroup: typeof TFieldGroup
|
package/src/types.ts
CHANGED
|
@@ -72,6 +72,65 @@ export interface TreeNode {
|
|
|
72
72
|
data?: unknown
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/** A drive of a TFileExplorer: a top-level place files are kept (App files, a share, a bucket). */
|
|
76
|
+
export interface ExplorerDrive {
|
|
77
|
+
id: string
|
|
78
|
+
name: string
|
|
79
|
+
icon?: string
|
|
80
|
+
/** Nothing can be added, renamed, moved or deleted in it. */
|
|
81
|
+
readOnly?: boolean
|
|
82
|
+
/** Why it can't be opened here; shown instead of its contents. */
|
|
83
|
+
unavailable?: string | null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A file or folder in a TFileExplorer. `can` says what the person may do with it (all, when unset). */
|
|
87
|
+
export interface ExplorerItem {
|
|
88
|
+
id: string
|
|
89
|
+
name: string
|
|
90
|
+
folder: boolean
|
|
91
|
+
size?: number | null
|
|
92
|
+
/** When it was last changed or uploaded (ISO). */
|
|
93
|
+
modifiedAt?: string | null
|
|
94
|
+
mimeType?: string | null
|
|
95
|
+
/** What the explorer shows as its kind: a file type's name, say. */
|
|
96
|
+
kind?: string | null
|
|
97
|
+
icon?: string
|
|
98
|
+
can?: { rename?: boolean; move?: boolean; delete?: boolean }
|
|
99
|
+
/** Anything of the provider's: the explorer only passes it back. */
|
|
100
|
+
data?: unknown
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Where a TFileExplorer is: a drive, a folder in it (null for its top), and the folders on the way. */
|
|
104
|
+
export interface ExplorerLocation {
|
|
105
|
+
drive: string
|
|
106
|
+
folder: string | null
|
|
107
|
+
path: { id: string; name: string }[]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A page of a folder's contents: folders first, then files; `cursor` reads the next page (null for none). */
|
|
111
|
+
export interface ExplorerPage {
|
|
112
|
+
items: ExplorerItem[]
|
|
113
|
+
cursor: string | null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* What a TFileExplorer works over: the Studio's server, an app's API, or anything else. Only
|
|
118
|
+
* `drives` and `list` are needed; what's missing is left out of the menus and the toolbar.
|
|
119
|
+
*/
|
|
120
|
+
export interface ExplorerProvider {
|
|
121
|
+
drives(): Promise<ExplorerDrive[]>
|
|
122
|
+
/** A folder's contents (null for the drive's top), a page at a time. */
|
|
123
|
+
list(drive: string, folder: string | null, cursor?: string | null): Promise<ExplorerPage>
|
|
124
|
+
createFolder?(drive: string, parent: string | null, name: string): Promise<ExplorerItem>
|
|
125
|
+
rename?(drive: string, item: ExplorerItem, name: string): Promise<ExplorerItem>
|
|
126
|
+
/** Moves items into a folder of the same drive (null for its top). */
|
|
127
|
+
move?(drive: string, items: ExplorerItem[], folder: string | null): Promise<void>
|
|
128
|
+
remove?(drive: string, items: ExplorerItem[]): Promise<void>
|
|
129
|
+
upload?(drive: string, folder: string | null, file: File, progress: (sent: number) => void, signal: AbortSignal): Promise<ExplorerItem>
|
|
130
|
+
/** Where a file downloads from. */
|
|
131
|
+
href?(drive: string, item: ExplorerItem): string
|
|
132
|
+
}
|
|
133
|
+
|
|
75
134
|
/** A button of a TButtonGroup: a label and/or an icon (give an icon-only one a tooltip). */
|
|
76
135
|
export interface ButtonGroupItem {
|
|
77
136
|
label?: string
|