Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-docsearch): open docsearch with slash key before run initi… #1323

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,10 +5,15 @@ import { useEventListener } from '@vueuse/core'
*/
export const useDocsearchHotkeyListener = (callback: () => void): void => {
const remove = useEventListener('keydown', (event) => {
if (event.key === 'k' && (event.ctrlKey || event.metaKey)) {
event.preventDefault()
callback()
remove()
const isHotKeyBind = event.key === 'k' && (event.ctrlKey || event.metaKey)
const isSlashKey = event.key === '/'

if (!isSlashKey && !isHotKeyBind) {
return
}

event.preventDefault()
callback()
remove()
Comment on lines +8 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I do not like the changed here, reversing the logic doesn't make sense, the handler is catching some keys here, so a positive judgement should be better

Suggested change
const isHotKeyBind = event.key === 'k' && (event.ctrlKey || event.metaKey)
const isSlashKey = event.key === '/'
if (!isSlashKey && !isHotKeyBind) {
return
}
event.preventDefault()
callback()
remove()
const isHotKeyBind = (event.key === 'k' && (event.ctrlKey || event.metaKey))
const isSlashKey = event.key === '/'
if (isSlashKey || isHotKeyBind) {
event.preventDefault()
callback()
remove()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point, but the "early return" is a very considerable and advantageous approach to make the logic clearer and more evident (indicated when talking about clean code). This function will not do anything else if the hotkeys are not triggered, so this way avoids the need to put the entire logic of the method inside the if.

})
}