Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/Web/CommandPalette/register-palette.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useActiveElement, useMagicKeys, whenever } from '@vueuse/core'
import { logicAnd } from '@vueuse/math'
import { computed, type Ref, watchEffect } from 'vue'
import { logicAnd, logicOr } from '@vueuse/math'
import { computed, type Ref } from 'vue'

interface Options {
value: Ref<boolean>
}

/**
* Registers `/` and `Cmd+K` hotkeys, as well as a `toggleCommandPalette` function.
* Registers `/` and `Cmd+K`/`Ctrl+K` hotkeys, as well as a `toggleCommandPalette` function.
*/
export function registerPalette(options: Options) {
const { Meta_K, Slash } = useMagicKeys({
const { Meta_K, Ctrl_K, Slash } = useMagicKeys({
target: document.body,
passive: false,
onEventFired(e) {
Expand All @@ -19,12 +19,16 @@ export function registerPalette(options: Options) {
}

if (e.key === '/' && e.type === 'keydown') {
e.preventDefault()
e.preventDefault()
}

if (e.key === 'k' && e.type === 'keydown' && e.metaKey) {
if (e.key === 'k' && e.type === 'keydown' && e.metaKey /* && OS is MacOS */) {
e.preventDefault()
}
}

if (e.key === 'k' && e.type === 'keydown' && e.ctrlKey /* && OS is not MacOS */) {
e.preventDefault()
}
},
})

Expand All @@ -39,9 +43,17 @@ export function registerPalette(options: Options) {
element.addEventListener('click', toggleCommandPalette)
})

const activeElement = useActiveElement()
const notUsingInput = computed(() => !['INPUT', 'TEXTAREA'].includes(activeElement.value?.tagName ?? ''))

whenever(logicAnd(Meta_K, notUsingInput), () => options.value.value = !options.value.value)
const activeElement = useActiveElement({ triggerOnRemoval: true })
const notUsingInput = computed(() => !['INPUT', 'TEXTAREA'].includes(activeElement.value?.tagName ?? ''))
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#determining_the_modifier_key_for_the_users_platform
const isMacKbdLayout = navigator.platform.toLowerCase().startsWith('mac')

whenever(
logicOr(
logicAnd(Meta_K, notUsingInput, isMacKbdLayout),
logicAnd(Ctrl_K, notUsingInput, !isMacKbdLayout)
),
() => options.value.value = !options.value.value
)
whenever(logicAnd(Slash, notUsingInput), () => options.value.value = true)
}
3 changes: 1 addition & 2 deletions src/Web/x-search.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<x-icon name="tabler:search" class="size-4 shrink-0 text-(--ui-text-dimmed)"/>
<span class="truncate">Search docs, blog...</span>
<kbd class="ml-2 inline-flex items-center gap-0.5 rounded border border-(--ui-border) bg-(--ui-bg-muted) px-1.5 py-0.5 text-xs text-(--ui-text-dimmed) font-medium">
<x-icon name="tabler:command" class="size-3"/>
K
/
</kbd>
</button>
Loading