Large number of keyboard navigation and other fixes.

This commit is contained in:
Leo Vasanko 2023-11-18 10:15:13 -08:00
parent ebbd96bc94
commit e56cc47105
8 changed files with 94 additions and 82 deletions

View File

@ -62,23 +62,14 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
event.key === 'ArrowRight' || event.key === 'ArrowRight' ||
(c && event.code === 'Space') (c && event.code === 'Space')
) { ) {
event.preventDefault() if (!input) event.preventDefault()
} }
return return
} }
//console.log("key pressed", event) //console.log("key pressed", event)
// For up/down implement custom fast repeat /// Long if-else machina for all keys we handle here
let stride = 1 let arrow = ''
if (store.gallery) { if (event.key.startsWith("Arrow")) arrow = event.key.slice(5).toLowerCase()
const grid = document.querySelector('.gallery') as HTMLElement
stride = getComputedStyle(grid).gridTemplateColumns.split(' ').length
}
else if (event.altKey) stride *= 10
// Long if-else machina for all keys we handle here
if (event.key === 'ArrowUp') vert = stride * (keyup ? 0 : -1)
else if (event.key === 'ArrowDown') vert = stride * (keyup ? 0 : 1)
else if (store.gallery && event.key === 'ArrowLeft') vert = keyup ? 0 : -1
else if (store.gallery && event.key === 'ArrowRight') vert = keyup ? 0 : 1
// Find: process on keydown so that we can bypass the built-in search hotkey // Find: process on keydown so that we can bypass the built-in search hotkey
else if (!keyup && event.key === 'f' && (event.ctrlKey || event.metaKey)) { else if (!keyup && event.key === 'f' && (event.ctrlKey || event.metaKey)) {
headerMain.value!.toggleSearchInput() headerMain.value!.toggleSearchInput()
@ -87,12 +78,16 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
else if (keyup && !input && event.key === '/') { else if (keyup && !input && event.key === '/') {
headerMain.value!.toggleSearchInput() headerMain.value!.toggleSearchInput()
} }
// Globally close search on Escape // Globally close search, clear errors on Escape
else if (keyup && event.key === 'Escape') { else if (keyup && event.key === 'Escape') {
store.error = '' store.error = ''
headerMain.value!.closeSearch(event) headerMain.value!.closeSearch(event)
store.focusBreadcrumb()
} }
// Select all (toggle); keydown to prevent builtin else if (!input && keyup && event.key === 'Backspace') {
Router.back()
}
// Select all (toggle); keydown to precede and prevent builtin
else if (!keyup && event.key === 'a' && (event.ctrlKey || event.metaKey)) { else if (!keyup && event.key === 'a' && (event.ctrlKey || event.metaKey)) {
fileExplorer.toggleSelectAll() fileExplorer.toggleSelectAll()
} }
@ -100,7 +95,7 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
else if (!input && keyup && event.key === 'g') { else if (!input && keyup && event.key === 'g') {
store.gallery = !store.gallery store.gallery = !store.gallery
} }
// Keys 1-3 to sort columns // Keys Backquote-1-2-3 to sort columns
else if ( else if (
!input && !input &&
keyup && keyup &&
@ -116,28 +111,26 @@ const globalShortcutHandler = (event: KeyboardEvent) => {
else if (c && event.code === 'Space') { else if (c && event.code === 'Space') {
if (keyup && !event.altKey && !event.ctrlKey) if (keyup && !event.altKey && !event.ctrlKey)
fileExplorer.cursorSelect() fileExplorer.cursorSelect()
} else return }
else return
/// We are handling this!
event.preventDefault() event.preventDefault()
if (!vert) {
if (timer) { if (timer) {
clearTimeout(timer) // Good for either timeout or interval clearTimeout(timer) // Good for either timeout or interval
timer = null timer = null
} }
return let f: any
switch (arrow) {
case 'up': f = () => fileExplorer.up(event); break
case 'down': f = () => fileExplorer.down(event); break
case 'left': f = () => fileExplorer.left(event); break
case 'right': f = () => fileExplorer.right(event); break
} }
if (!timer) { if (f && !keyup) {
// Initial move, then t0 delay until repeats at tr intervals // Initial move, then t0 delay until repeats at tr intervals
const select = event.shiftKey const t0 = 200, tr = event.altKey ? 20 : 100
fileExplorer.cursorMove(vert, select) f()
const t0 = 200, timer = setTimeout(() => { timer = setInterval(f, tr) }, t0 - tr)
tr = 30
timer = setTimeout(
() =>
(timer = setInterval(() => {
fileExplorer.cursorMove(vert, select)
}, tr)),
t0 - tr
)
} }
} }
onMounted(() => { onMounted(() => {

View File

@ -2,8 +2,8 @@
<nav <nav
class="breadcrumb" class="breadcrumb"
aria-label="Breadcrumb" aria-label="Breadcrumb"
@keyup.left.stop="move(-1)" @keydown.left.stop="move(-1)"
@keyup.right.stop="move(1)" @keydown.right.stop="move(1)"
@keyup.enter="move(0)" @keyup.enter="move(0)"
@focus=focusCurrent @focus=focusCurrent
tabindex=0 tabindex=0

View File

@ -49,15 +49,9 @@
<FileRenameInput :doc="doc" :rename="rename" :exit="() => {editing = null}" /> <FileRenameInput :doc="doc" :rename="rename" :exit="() => {editing = null}" />
</template> </template>
<template v-else> <template v-else>
<a <a :href=doc.url tabindex=-1 @contextmenu.stop @focus.stop="store.cursor = doc.key">
:href="doc.url" {{ doc.name }}
tabindex="-1" </a>
@contextmenu.prevent
@focus.stop="store.cursor = doc.key"
@keyup.left="router.back()"
@keyup.right.stop="ev => { if (doc.dir) (ev.target as HTMLElement).click() }"
>{{ doc.name }}</a
>
<button tabindex=-1 v-if="store.cursor == doc.key" class="rename-button" @click="() => (editing = doc)">🖊</button> <button tabindex=-1 v-if="store.cursor == doc.key" class="rename-button" @click="() => (editing = doc)">🖊</button>
</template> </template>
</td> </td>
@ -151,9 +145,17 @@ defineExpose({
} else { } else {
store.selected.add(key) store.selected.add(key)
} }
this.cursorMove(1) this.cursorMove(1, null)
}, },
cursorMove(d: number, select = false) { up(ev: KeyboardEvent) { this.cursorMove(-1, ev) },
down(ev: KeyboardEvent) { this.cursorMove(1, ev) },
left(ev: KeyboardEvent) { router.back() },
right(ev: KeyboardEvent) {
const a = document.querySelector(`#file-${store.cursor} a`) as HTMLAnchorElement | null
if (a) a.click()
},
cursorMove(d: number, ev: KeyboardEvent | null) {
const select = !!ev?.shiftKey
// Move cursor up or down (keyboard navigation) // Move cursor up or down (keyboard navigation)
const docs = props.documents const docs = props.documents
if (docs.length === 0) { if (docs.length === 0) {

View File

@ -1,15 +1,13 @@
<template> <template>
<div v-if="props.documents.length || editing" class="gallery"> <div v-if="props.documents.length || editing" class="gallery" ref="gallery">
<template v-for="(doc, index) in documents" :key="doc.key"> <GalleryFigure v-for="(doc, index) in documents" :key="doc.key" :doc="doc" :index="index">
<GalleryFigure :doc="doc" :index="index">
<BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" v-if="showFolderBreadcrumb(index)" class="folder-change"/> <BreadCrumb :path="doc.loc ? doc.loc.split('/') : []" v-if="showFolderBreadcrumb(index)" class="folder-change"/>
</GalleryFigure> </GalleryFigure>
</template>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted } from 'vue' import { ref, computed, watchEffect, shallowRef, onMounted, onUnmounted, nextTick } from 'vue'
import { useMainStore } from '@/stores/main' import { useMainStore } from '@/stores/main'
import { Doc } from '@/repositories/Document' import { Doc } from '@/repositories/Document'
import FileRenameInput from './FileRenameInput.vue' import FileRenameInput from './FileRenameInput.vue'
@ -51,6 +49,11 @@ const rename = (doc: Doc, newName: string) => {
} }
doc.name = newName // We should get an update from watch but this is quicker doc.name = newName // We should get an update from watch but this is quicker
} }
const gallery = ref<HTMLElement>()
const columns = computed(() => {
if (!gallery.value) return 1
return getComputedStyle(gallery.value).gridTemplateColumns.split(' ').length
})
defineExpose({ defineExpose({
newFolder() { newFolder() {
const now = Math.floor(Date.now() / 1000) const now = Math.floor(Date.now() / 1000)
@ -85,9 +88,14 @@ defineExpose({
} else { } else {
store.selected.add(key) store.selected.add(key)
} }
this.cursorMove(1) this.cursorMove(1, null)
}, },
cursorMove(d: number, select = false) { up(ev: KeyboardEvent) { this.cursorMove(-columns.value, ev) },
down(ev: KeyboardEvent) { this.cursorMove(columns.value, ev) },
left(ev: KeyboardEvent) { this.cursorMove(-1, ev) },
right(ev: KeyboardEvent) { this.cursorMove(1, ev) },
cursorMove(d: number, ev: KeyboardEvent | null) {
const select = !!ev?.shiftKey
// Move cursor up or down (keyboard navigation) // Move cursor up or down (keyboard navigation)
const docs = props.documents const docs = props.documents
if (docs.length === 0) { if (docs.length === 0) {
@ -98,8 +106,15 @@ defineExpose({
const mod = (a: number, b: number) => ((a % b) + b) % b const mod = (a: number, b: number) => ((a % b) + b) % b
const increment = (i: number, d: number) => mod(i + d, N + 1) const increment = (i: number, d: number) => mod(i + d, N + 1)
const index = const index =
store.cursor ? docs.findIndex(doc => doc.key === store.cursor) : docs.length store.cursor ? docs.findIndex(doc => doc.key === store.cursor) : N
const moveto = increment(index, d) let moveto
if (index === N) moveto = d > 0 ? 0 : N - 1
else {
moveto = increment(index, d)
// Wrapping either end, just land outside the list
if (Math.abs(d) >= N || Math.sign(d) !== Math.sign(moveto - index)) moveto = N
}
console.log("Gallery cursorMove", d, index, moveto, moveto - index)
store.cursor = docs[moveto]?.key ?? '' store.cursor = docs[moveto]?.key ?? ''
const tr = store.cursor ? document.getElementById(`file-${store.cursor}`) : '' const tr = store.cursor ? document.getElementById(`file-${store.cursor}`) : ''
if (select) { if (select) {
@ -134,10 +149,8 @@ watchEffect(() => {
if (store.cursor && store.cursor !== editing.value?.key) editing.value = null if (store.cursor && store.cursor !== editing.value?.key) editing.value = null
if (editing.value) store.cursor = editing.value.key if (editing.value) store.cursor = editing.value.key
if (store.cursor) { if (store.cursor) {
const a = document.querySelector( const a = document.querySelector(`#file-${store.cursor}`) as HTMLAnchorElement | null
`#file-${store.cursor} a` if (a) { a.focus(); a.scrollIntoView({ block: 'center', behavior: 'smooth' }) }
) as HTMLAnchorElement | null
if (a) a.focus()
} }
}) })
watchEffect(() => { watchEffect(() => {

View File

@ -1,19 +1,17 @@
<template> <template>
<a <a :id="`file-${doc.key}`" :href=doc.url tabindex=-1
:id="`file-${doc.key}`"
:href="doc.url"
tabindex=0
:class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key }" :class="{ file: !doc.dir, folder: doc.dir, cursor: store.cursor === doc.key }"
@contextmenu.prevent @contextmenu.stop
@focus.stop="store.cursor = doc.key" @focus.stop="store.cursor = doc.key"
@click="ev => { @click="ev => {
store.cursor = store.cursor === doc.key ? '' : doc.key console.log('Gallery click', doc.key, store.cursor, !!media)
if (media) { media.play(); ev.preventDefault() } if (m!.play()) ev.preventDefault()
store.cursor = doc.key
}" }"
> >
<figure> <figure>
<slot></slot> <slot></slot>
<MediaPreview ref=media :doc="doc" /> <MediaPreview ref=m :doc="doc" :tabindex=-1 />
<caption> <caption>
<label> <label>
<SelectBox :doc=doc /> <SelectBox :doc=doc />
@ -35,7 +33,7 @@ const props = defineProps<{
doc: Doc doc: Doc
index: number index: number
}>() }>()
const media = ref<typeof MediaPreview | null>(null) const m = ref<typeof MediaPreview | null>(null)
</script> </script>
<style scoped> <style scoped>

View File

@ -1,19 +1,19 @@
<template> <template>
<img v-if=doc.img :src=doc.url alt=""> <img v-if=doc.img :src=doc.url alt="">
<span v-else-if=doc.dir class="folder icon"></span> <span v-else-if=doc.dir class="folder icon"></span>
<video v-else-if=video() ref=media :src=doc.url controls preload=metadata @click.prevent>📄</video> <video ref=vid v-else-if=video() :src=doc.url controls preload=none @click.prevent>📄</video>
<audio v-else-if=audio() ref=media :src=doc.url controls preload=metadata @click.stop>📄</audio> <audio ref=aud v-else-if=audio() :src=doc.url controls preload=metadata @click.stop>📄</audio>
<embed v-else-if=embed() :src=doc.url type=text/plain @click.stop @scroll.prevent>
<span v-else-if=archive() class="archive icon"></span> <span v-else-if=archive() class="archive icon"></span>
<span v-else class="file icon" :class="`ext-${doc.ext}`"></span> <span v-else class="file icon" :class="`ext-${doc.ext}`"></span>
</template> </template>
<script setup lang=ts> <script setup lang=ts>
import { ref } from 'vue' import { compile, computed, ref } from 'vue'
import type { Doc } from '@/repositories/Document' import type { Doc } from '@/repositories/Document'
const media = ref<HTMLAudioElement | HTMLVideoElement | null>(null) const aud = ref<HTMLAudioElement | null>(null)
const vid = ref<HTMLVideoElement | null>(null)
const media = computed(() => aud.value || vid.value)
const props = defineProps<{ const props = defineProps<{
doc: Doc doc: Doc
}>() }>()
@ -21,9 +21,12 @@ const props = defineProps<{
defineExpose({ defineExpose({
play() { play() {
if (media.value) { if (media.value) {
media.value.play() if (media.value.paused) media.value.play()
} else media.value.pause()
return true
} }
return false
},
}) })

View File

@ -83,6 +83,9 @@ export const useMainStore = defineStore({
if (this.query) this.prefs.sortFiltered = name if (this.query) this.prefs.sortFiltered = name
else this.prefs.sortListing = name else this.prefs.sortListing = name
}, },
focusBreadcrumb() {
(document.querySelector('.breadcrumb') as HTMLAnchorElement).focus()
}
}, },
getters: { getters: {
sortOrder(): SortOrder { return this.query ? this.prefs.sortFiltered : this.prefs.sortListing }, sortOrder(): SortOrder { return this.query ? this.prefs.sortFiltered : this.prefs.sortListing },

View File

@ -4,7 +4,7 @@
<p v-if="!store.connected">No Connection</p> <p v-if="!store.connected">No Connection</p>
<p v-else-if="store.document.length === 0">Waiting for Files</p> <p v-else-if="store.document.length === 0">Waiting for Files</p>
<p v-else-if="store.query">No matches!</p> <p v-else-if="store.query">No matches!</p>
<p v-else-if="!store.document.find(doc => doc.loc.length + 1 === props.path.length && [...doc.loc, doc.name].join('/') === props.path.join('/'))">Folder not found.</p> <p v-else-if="!store.document.some(doc => `${doc.loc}/${doc.name}` === props.path.join('/'))">Folder not found.</p>
<p v-else>Empty folder</p> <p v-else>Empty folder</p>
</div> </div>
<Gallery <Gallery