Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…1254) Co-authored-by: meteorlxy <meteor.lxy@foxmail.com>
- Loading branch information
1 parent
346c6e7
commit f5d5b11
Showing
8 changed files
with
155 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useDocsearchHotkeyListener.js' | ||
export * from './useDocsearchShim.js' |
16 changes: 16 additions & 0 deletions
16
ecosystem/plugin-docsearch/src/client/composables/useDocsearchHotkeyListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { onMounted, onUnmounted } from 'vue' | ||
|
||
/** | ||
* Add hotkey listener, remove it after triggered | ||
*/ | ||
export const useDocsearchHotkeyListener = (callback: () => void): void => { | ||
const hotkeyListener = (event: KeyboardEvent): void => { | ||
if (event.key === 'k' && (event.ctrlKey || event.metaKey)) { | ||
event.preventDefault() | ||
window.removeEventListener('keydown', hotkeyListener) | ||
callback() | ||
} | ||
} | ||
onMounted(() => window.addEventListener('keydown', hotkeyListener)) | ||
onUnmounted(() => window.removeEventListener('keydown', hotkeyListener)) | ||
} |
18 changes: 18 additions & 0 deletions
18
ecosystem/plugin-docsearch/src/client/utils/getFacetFilters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { isArray } from '@vuepress/shared' | ||
import type { DocsearchOptions } from '../../shared/index.js' | ||
|
||
type FacetFilters = | ||
Required<DocsearchOptions>['searchParameters']['facetFilters'] | ||
|
||
/** | ||
* Get facet filters for current lang | ||
*/ | ||
export const getFacetFilters = ( | ||
rawFacetFilters: FacetFilters = [], | ||
lang: string | ||
): FacetFilters => [ | ||
`lang:${lang}`, | ||
...((isArray(rawFacetFilters) | ||
? rawFacetFilters | ||
: [rawFacetFilters]) as string[]), | ||
] |
14 changes: 14 additions & 0 deletions
14
ecosystem/plugin-docsearch/src/client/utils/getSearchButtonTemplate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { DocSearchTranslations } from '@docsearch/react' | ||
|
||
/** | ||
* Get the search button template | ||
* | ||
* Use the same content as in @docsearch/js | ||
* | ||
* TODO: the meta key text should also be dynamic | ||
*/ | ||
export const getSearchButtonTemplate = ({ | ||
buttonText = 'Search', | ||
buttonAriaLabel = buttonText, | ||
}: DocSearchTranslations['button'] = {}): string => | ||
`<button type="button" class="DocSearch DocSearch-Button" aria-label="${buttonAriaLabel}"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">${buttonText}</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"><svg width="15" height="15" class="DocSearch-Control-Key-Icon"><path d="M4.505 4.496h2M5.505 5.496v5M8.216 4.496l.055 5.993M10 7.5c.333.333.5.667.5 1v2M12.326 4.5v5.996M8.384 4.496c1.674 0 2.116 0 2.116 1.5s-.442 1.5-2.116 1.5M3.205 9.303c-.09.448-.277 1.21-1.241 1.203C1 10.5.5 9.513.5 8V7c0-1.57.5-2.5 1.464-2.494.964.006 1.134.598 1.24 1.342M12.553 10.5h1.953" stroke-width="1.2" stroke="currentColor" fill="none" stroke-linecap="square"></path></svg></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './getFacetFilters.js' | ||
export * from './getSearchButtonTemplate.js' | ||
export * from './pollToOpenDocsearch.js' | ||
export * from './preconnectToAlgolia.js' |
15 changes: 15 additions & 0 deletions
15
ecosystem/plugin-docsearch/src/client/utils/pollToOpenDocsearch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const POLL_INTERVAL = 16 | ||
|
||
/** | ||
* Programmatically open the docsearch modal | ||
*/ | ||
export const pollToOpenDocsearch = (): void => { | ||
if (document.querySelector('.DocSearch-Modal')) return | ||
const e = new Event('keydown') as { | ||
-readonly [P in keyof KeyboardEvent]: KeyboardEvent[P] | ||
} | ||
e.key = 'k' | ||
e.metaKey = true | ||
window.dispatchEvent(e) | ||
setTimeout(pollToOpenDocsearch, POLL_INTERVAL) | ||
} |
16 changes: 16 additions & 0 deletions
16
ecosystem/plugin-docsearch/src/client/utils/preconnectToAlgolia.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Preconnect to Algolia's API | ||
*/ | ||
export const preconnectToAlgolia = (appId: string): void => { | ||
const id = 'algolia-preconnect' | ||
const rIC = window.requestIdleCallback || setTimeout | ||
rIC(() => { | ||
if (document.head.querySelector(`#${id}`)) return | ||
const preconnect = document.createElement('link') | ||
preconnect.id = id | ||
preconnect.rel = 'preconnect' | ||
preconnect.href = `https://${appId}-dsn.algolia.net` | ||
preconnect.crossOrigin = '' | ||
document.head.appendChild(preconnect) | ||
}) | ||
} |