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

feat: add keybind(ctrl-p and ctrl-n) for mac platform #3997

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/zh/guide/ssr-compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (!import.meta.env.SSR) {
}
```

因为 [`Theme.enhanceApp`](/guide/custom-theme#theme-interface) 可以是异步的,所以可以有条件地导入并注册访问浏览器 API 的 Vue 插件:
因为 [`Theme.enhanceApp`](./custom-theme#theme-interface) 可以是异步的,所以可以有条件地导入并注册访问浏览器 API 的 Vue 插件:

```js
// .vitepress/theme/index.js
Expand Down
46 changes: 34 additions & 12 deletions src/client/theme-default/components/VPLocalSearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -299,26 +299,39 @@ function scrollToSelectedResult() {
})
}

onKeyStroke('ArrowUp', (event) => {
event.preventDefault()
selectedIndex.value--
if (selectedIndex.value < 0) {
selectedIndex.value = results.value.length - 1
}
function goRelativeItem(offset: number) {
const resNum = results.value.length
selectedIndex.value = (selectedIndex.value + resNum + (offset % resNum)) % resNum
Copy link
Contributor Author

Choose a reason for hiding this comment

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

when resNum == 0, the selectedIndex.value will be NAN, but this won't crash the script, so I don't add more judgement

disableMouseOver.value = true
scrollToSelectedResult()
}

onKeyStroke('ArrowUp', (event) => {
event.preventDefault()
goRelativeItem(-1)
})

onKeyStroke('ArrowDown', (event) => {
event.preventDefault()
selectedIndex.value++
if (selectedIndex.value >= results.value.length) {
selectedIndex.value = 0
}
disableMouseOver.value = true
scrollToSelectedResult()
goRelativeItem(1)
})

if (document.documentElement.classList.contains('mac')) {
onKeyStroke('n', (event) => {
if (event.ctrlKey) {
event.preventDefault()
goRelativeItem(1)
}
})

onKeyStroke('p', (event) => {
if (event.ctrlKey) {
event.preventDefault()
goRelativeItem(-1)
}
})
}

const router = useRouter()

onKeyStroke('Enter', (e) => {
Expand Down Expand Up @@ -540,9 +553,18 @@ function formMarkRegex(terms: Set<string>) {
<kbd :aria-label="translate('modal.footer.navigateUpKeyAriaLabel')">
<span class="vpi-arrow-up navigate-icon" />
</kbd>
<span>/</span>
<kbd :aria-label="translate('modal.footer.navigateDownKeyAriaLabel')">
<span class="navigate-icon">ctrl-p</span>
</kbd>
<span>and</span>
<kbd :aria-label="translate('modal.footer.navigateDownKeyAriaLabel')">
<span class="vpi-arrow-down navigate-icon" />
</kbd>
<span>/</span>
<kbd :aria-label="translate('modal.footer.navigateDownKeyAriaLabel')">
<span class="navigate-icon">ctrl-n</span>
</kbd>
{{ translate('modal.footer.navigateText') }}
</span>
<span>
Expand Down