feat: basic keyboard shortcuts (#319)

This commit is contained in:
Hartmut 2023-03-07 20:32:21 +01:00 committed by GitHub
parent 69c1bd8b6a
commit c4d8137186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 270 additions and 4 deletions

View file

@ -18,6 +18,7 @@ export const isFirstVisit = useLocalStorage(STORAGE_KEY_FIRST_VISIT, !process.mo
export const isSigninDialogOpen = ref(false)
export const isPublishDialogOpen = ref(false)
export const isKeyboardShortcutsDialogOpen = ref(false)
export const isMediaPreviewOpen = ref(false)
export const isEditHistoryDialogOpen = ref(false)
export const isPreviewHelpOpen = ref(isFirstVisit.value)
@ -139,3 +140,11 @@ export function openCommandPanel(isCommandMode = false) {
export function closeCommandPanel() {
isCommandPanelOpen.value = false
}
export function toggleKeyboardShortcuts() {
isKeyboardShortcutsDialogOpen.value = !isKeyboardShortcutsDialogOpen.value
}
export function closeKeyboardShortcuts() {
isKeyboardShortcutsDialogOpen.value = false
}

44
composables/magickeys.ts Normal file
View file

@ -0,0 +1,44 @@
import type { ComputedRef } from 'vue'
// TODO: consider to allow combinations similar to useMagicKeys using proxy?
// e.g. `const magicSequence = useMagicSequence()`
// `magicSequence['Shift+Ctrl+A']`
// `const { Ctrl_A_B } = useMagicSequence()`
/**
* source: inspired by https://github.com/vueuse/vueuse/issues/427#issuecomment-815619446
* @param keys ordered list of keys making up the sequence
*/
export function useMagicSequence(keys: string[]): ComputedRef<boolean> {
const magicKeys = useMagicKeys()
const success = ref(false)
const i = ref(0)
let down = false
watch(
() => magicKeys.current,
() => {
if (magicKeys[keys[i.value]].value && !down) {
down = true
i.value += 1
}
else if (i.value > 0 && !magicKeys[keys[i.value - 1]].value && down) {
down = false
}
else {
i.value = 0
down = false
success.value = false
}
if (i.value >= keys.length && !down) {
i.value = 0
down = false
success.value = true
}
}, {
deep: true,
})
return computed(() => success.value)
}