fix(theme-yun): cmd + k to search when first mounted, completes #567

This commit is contained in:
JasonXuDeveloper - 傑 2025-07-13 13:28:50 +10:00
parent 843fbcee31
commit 9f426f1c93
2 changed files with 36 additions and 1 deletions

View File

@ -16,7 +16,7 @@ function togglePopup() {
const algoliaRef = ref()
onMounted(() => {
// algolia has its own hotkey
// algolia has its own hotkey handling in YunAlgoliaSearch component
if (isFuse.value)
useHotKey('k', togglePopup)
})

View File

@ -1,6 +1,7 @@
<script lang="ts" setup>
import { isEmptyAddon } from 'valaxy'
import * as addonAlgolia from 'valaxy-addon-algolia'
import { onMounted, onUnmounted } from 'vue'
defineProps<{
open: boolean
@ -16,6 +17,40 @@ defineExpose({
load,
dispatchEvent,
})
function isEditingContent(event: KeyboardEvent): boolean {
const element = event.target as HTMLElement
const tagName = element.tagName
return (
element.isContentEditable
|| tagName === 'INPUT'
|| tagName === 'SELECT'
|| tagName === 'TEXTAREA'
)
}
onMounted(() => {
const handleSearchHotKey = (event: KeyboardEvent) => {
if (
(event.key?.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey))
|| (!isEditingContent(event) && event.key === '/')
) {
event.preventDefault()
load()
// eslint-disable-next-line ts/no-use-before-define
remove()
}
}
const remove = () => {
window.removeEventListener('keydown', handleSearchHotKey)
}
window.addEventListener('keydown', handleSearchHotKey)
onUnmounted(remove)
})
</script>
<template>