From 42b7bf33b8151d1d8a9d65a3bdc88b563e362e14 Mon Sep 17 00:00:00 2001 From: shellRaining Date: Sat, 29 Jun 2024 00:21:17 +0800 Subject: [PATCH 1/3] feat: add new key binding (ctrl-p and ctrl-n) to select adjacent search items for mac platform --- .../components/VPLocalSearchBox.vue | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/client/theme-default/components/VPLocalSearchBox.vue b/src/client/theme-default/components/VPLocalSearchBox.vue index 15a62efe01b3..e557a0fcdb55 100644 --- a/src/client/theme-default/components/VPLocalSearchBox.vue +++ b/src/client/theme-default/components/VPLocalSearchBox.vue @@ -299,26 +299,40 @@ 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 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) }) +const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0 +if (isMac) { + 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) => { @@ -540,9 +554,18 @@ function formMarkRegex(terms: Set) { + / + + ctrl-p + + and + / + + ctrl-n + {{ translate('modal.footer.navigateText') }} From c87f4aed55dcb272cc6df402b66b2a74bf463a1c Mon Sep 17 00:00:00 2001 From: shellRaining Date: Sat, 6 Jul 2024 20:20:11 +0800 Subject: [PATCH 2/3] docs: Fix the issue of incorrect inline link addresses in zh/guide/ssr-compat.md --- docs/zh/guide/ssr-compat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/guide/ssr-compat.md b/docs/zh/guide/ssr-compat.md index 0e980f4a9efd..59da3fab5879 100644 --- a/docs/zh/guide/ssr-compat.md +++ b/docs/zh/guide/ssr-compat.md @@ -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 From f7b4dc7fcf8d8e34c5560e3fdebf139c68135cfd Mon Sep 17 00:00:00 2001 From: shellRaining Date: Mon, 15 Jul 2024 11:19:21 +0800 Subject: [PATCH 3/3] refactor: use vitepress check-mac-os inline script to check whether is mac platform --- src/client/theme-default/components/VPLocalSearchBox.vue | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client/theme-default/components/VPLocalSearchBox.vue b/src/client/theme-default/components/VPLocalSearchBox.vue index e557a0fcdb55..010e32c768a7 100644 --- a/src/client/theme-default/components/VPLocalSearchBox.vue +++ b/src/client/theme-default/components/VPLocalSearchBox.vue @@ -316,8 +316,7 @@ onKeyStroke('ArrowDown', (event) => { goRelativeItem(1) }) -const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0 -if (isMac) { +if (document.documentElement.classList.contains('mac')) { onKeyStroke('n', (event) => { if (event.ctrlKey) { event.preventDefault()