-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic keyboard shortcuts (#319)
- Loading branch information
1 parent
69c1bd8
commit c4d8137
Showing
11 changed files
with
270 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<script setup lang="ts"> | ||
const emit = defineEmits(['close']) | ||
const { t } = useI18n() | ||
/* TODOs: | ||
* - I18n | ||
*/ | ||
interface ShortcutDef { | ||
keys: string[] | ||
isSequence: boolean | ||
} | ||
interface ShortcutItem { | ||
description: string | ||
shortcut: ShortcutDef | ||
} | ||
interface ShortcutItemGroup { | ||
name: string | ||
items: ShortcutItem[] | ||
} | ||
const shortcutItemGroups: ShortcutItemGroup[] = [ | ||
{ | ||
name: t('magic_keys.groups.navigation.title'), | ||
items: [ | ||
{ | ||
description: t('magic_keys.groups.navigation.shortcut_help'), | ||
shortcut: { keys: ['?'], isSequence: false }, | ||
}, | ||
// { | ||
// description: t('magic_keys.groups.navigation.next_status'), | ||
// shortcut: { keys: ['j'], isSequence: false }, | ||
// }, | ||
// { | ||
// description: t('magic_keys.groups.navigation.previous_status'), | ||
// shortcut: { keys: ['k'], isSequence: false }, | ||
// }, | ||
{ | ||
description: t('magic_keys.groups.navigation.go_to_home'), | ||
shortcut: { keys: ['g', 'h'], isSequence: true }, | ||
}, | ||
{ | ||
description: t('magic_keys.groups.navigation.go_to_notifications'), | ||
shortcut: { keys: ['g', 'n'], isSequence: true }, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: t('magic_keys.groups.actions.title'), | ||
items: [ | ||
{ | ||
description: t('magic_keys.groups.actions.command_mode'), | ||
shortcut: { keys: ['cmd', '/'], isSequence: false }, | ||
}, | ||
{ | ||
description: t('magic_keys.groups.actions.compose'), | ||
shortcut: { keys: ['c'], isSequence: false }, | ||
}, | ||
{ | ||
description: t('magic_keys.groups.actions.favourite'), | ||
shortcut: { keys: ['f'], isSequence: false }, | ||
}, | ||
{ | ||
description: t('magic_keys.groups.actions.boost'), | ||
shortcut: { keys: ['b'], isSequence: false }, | ||
}, | ||
{ | ||
description: t('magic_keys.groups.actions.zen_mode'), | ||
shortcut: { keys: ['z'], isSequence: false }, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: t('magic_keys.groups.media.title'), | ||
items: [], | ||
}, | ||
] | ||
</script> | ||
|
||
<template> | ||
<div px-3 sm:px-5 py-2 sm:py-4 max-w-220 relative max-h-screen> | ||
<button btn-action-icon absolute top-1 sm:top-2 right-1 sm:right-2 m1 :aria-label="$t('modals.aria_label_close')" @click="emit('close')"> | ||
<div i-ri:close-fill /> | ||
</button> | ||
<h2 text-xl font-700 mb3> | ||
{{ $t('magic_keys.dialog_header') }} | ||
</h2> | ||
<div mb2 grid grid-cols-1 md:grid-cols-3 gap-y- md:gap-x-6 lg:gap-x-8> | ||
<div | ||
v-for="group in shortcutItemGroups" | ||
:key="group.name" | ||
> | ||
<h3 font-700 my-2 text-lg> | ||
{{ group.name }} | ||
</h3> | ||
<div | ||
v-for="item in group.items" | ||
:key="item.description" | ||
flex my-1 lg:my-2 justify-between place-items-center max-w-full text-base | ||
> | ||
<div mr-2 break-words overflow-hidden leading-4 h-full inline-block align-middle> | ||
{{ item.description }} | ||
</div> | ||
<div> | ||
<template | ||
v-for="(key, idx) in item.shortcut.keys" | ||
:key="idx" | ||
> | ||
<span v-if="idx !== 0" mx1 text-sm op80>{{ item.shortcut.isSequence ? $t('magic_keys.sequence_then') : '+' }}</span> | ||
<code class="px2 md:px1.5 lg:px2 lg:px2 py0 lg:py-0.5" rounded bg-code border="px $c-border-code" shadow-sm my1 font-mono font-600>{{ key }}</code> | ||
</template> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { RouteLocationRaw } from 'vue-router' | ||
import { useMagicSequence } from '~/composables/magickeys' | ||
|
||
export default defineNuxtPlugin(({ $scrollToTop }) => { | ||
const userSettings = useUserSettings() | ||
const keys = useMagicKeys() | ||
const router = useRouter() | ||
|
||
// disable shortcuts when focused on inputs (https://vueuse.org/core/usemagickeys/#conditionally-disable) | ||
const activeElement = useActiveElement() | ||
|
||
const notUsingInput = computed(() => | ||
activeElement.value?.tagName !== 'INPUT' | ||
&& activeElement.value?.tagName !== 'TEXTAREA' | ||
&& !activeElement.value?.isContentEditable, | ||
) | ||
const isAuthenticated = currentUser.value !== undefined | ||
|
||
const navigateTo = (to: string | RouteLocationRaw) => { | ||
closeKeyboardShortcuts() | ||
$scrollToTop() // is this really required? | ||
router.push(to) | ||
} | ||
|
||
whenever(logicAnd(notUsingInput, keys['?']), toggleKeyboardShortcuts) | ||
whenever(logicAnd(notUsingInput, keys.z), () => userSettings.value.zenMode = !userSettings.value.zenMode) | ||
|
||
const defaultPublishDialog = () => { | ||
const current = keys.current | ||
// exclusive 'c' - not apply in combination | ||
// TODO: bugfix -> create PR for vueuse, reset `current` ref on window focus|blur | ||
if (!current.has('shift') && !current.has('meta') && !current.has('control') && !current.has('alt')) { | ||
// TODO: is this the correct way of using openPublishDialog()? | ||
openPublishDialog('dialog', getDefaultDraft()) | ||
} | ||
} | ||
whenever(logicAnd(isAuthenticated, notUsingInput, keys.c), defaultPublishDialog) | ||
|
||
whenever(logicAnd(notUsingInput, useMagicSequence(['g', 'h'])), () => navigateTo('/home')) | ||
whenever(logicAnd(isAuthenticated, notUsingInput, useMagicSequence(['g', 'n'])), () => navigateTo('/notifications')) | ||
|
||
const toggleFavouriteActiveStatus = () => { | ||
// TODO: find a better solution than clicking buttons... | ||
document | ||
.querySelector<HTMLElement>('[aria-roledescription=status-details]') | ||
?.querySelector<HTMLElement>('button[aria-label=Favourite]') | ||
?.click() | ||
} | ||
whenever(logicAnd(isAuthenticated, notUsingInput, keys.f), toggleFavouriteActiveStatus) | ||
|
||
const toggleBoostActiveStatus = () => { | ||
// TODO: find a better solution than clicking buttons... | ||
document | ||
.querySelector<HTMLElement>('[aria-roledescription=status-details]') | ||
?.querySelector<HTMLElement>('button[aria-label=Boost]') | ||
?.click() | ||
} | ||
whenever(logicAnd(isAuthenticated, notUsingInput, keys.b), toggleBoostActiveStatus) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters