|
1 | 1 | <script setup lang="ts">
|
2 |
| -import type { DocSearchHit } from '@docsearch/react/dist/esm/types' |
3 | 2 | import type { AlgoliaSearchOptions } from '../types'
|
4 | 3 | import docsearch from '@docsearch/js'
|
5 |
| -import { onMounted } from 'vue' |
| 4 | +import { nextTick, onMounted, watch } from 'vue' |
6 | 5 | import { useI18n } from 'vue-i18n'
|
7 |
| -import { useRoute, useRouter } from 'vue-router' |
| 6 | +import { useRouter } from 'vue-router' |
8 | 7 | import { useAddonAlgoliaConfig } from '../client'
|
9 | 8 | import '@docsearch/css'
|
10 | 9 |
|
11 | 10 | const router = useRouter()
|
12 |
| -const route = useRoute() |
13 | 11 |
|
14 | 12 | const algolia = useAddonAlgoliaConfig()
|
15 | 13 |
|
16 | 14 | const { locale } = useI18n()
|
17 | 15 |
|
| 16 | +type DocSearchProps = Parameters<typeof docsearch>[0] |
| 17 | +
|
| 18 | +onMounted(update) |
| 19 | +watch(locale, update) |
| 20 | +
|
| 21 | +async function update() { |
| 22 | + await nextTick() |
| 23 | + const options = { |
| 24 | + ...algolia.value.options!, |
| 25 | + ...algolia.value.options?.locales?.[locale.value], |
| 26 | + } |
| 27 | +
|
| 28 | + // now only lang:en |
| 29 | + // const rawFacetFilters = options.searchParameters?.facetFilters ?? [] |
| 30 | + // const facetFilters = [ |
| 31 | + // ...(Array.isArray(rawFacetFilters) |
| 32 | + // ? rawFacetFilters |
| 33 | + // : [rawFacetFilters] |
| 34 | + // ).filter(f => !f.startsWith('lang:')), |
| 35 | + // `lang:${lang.value}`, |
| 36 | + // ] |
| 37 | +
|
| 38 | + initialize({ |
| 39 | + ...options, |
| 40 | + searchParameters: { |
| 41 | + ...options.searchParameters, |
| 42 | + // facetFilters, |
| 43 | + }, |
| 44 | + }) |
| 45 | +} |
| 46 | +
|
18 | 47 | onMounted(() => {
|
19 | 48 | const options = {
|
20 | 49 | ...algolia.value.options,
|
@@ -47,79 +76,33 @@ onMounted(() => {
|
47 | 76 | function initialize(userOptions: AlgoliaSearchOptions) {
|
48 | 77 | // note: multi-lang search support is removed since the theme
|
49 | 78 | // doesn't support multiple locales as of now.
|
50 |
| - const options = Object.assign({}, userOptions, { |
51 |
| - container: '#docsearch', |
52 |
| - navigator: { |
53 |
| - navigate({ itemUrl }: { itemUrl: string }) { |
54 |
| - const { pathname: hitPathname } = new URL( |
55 |
| - window.location.origin + itemUrl, |
56 |
| - ) |
57 |
| - // router doesn't handle same-page navigation so we use the native |
58 |
| - // browser location API for anchor navigation |
59 |
| - if (route.path === hitPathname) |
60 |
| - window.location.assign(window.location.origin + itemUrl) |
61 |
| -
|
62 |
| - else |
| 79 | + const options = Object.assign<object, AlgoliaSearchOptions, Partial<DocSearchProps>>( |
| 80 | + {}, |
| 81 | + userOptions, |
| 82 | + { |
| 83 | + container: '#docsearch', |
| 84 | +
|
| 85 | + navigator: { |
| 86 | + navigate({ itemUrl }) { |
63 | 87 | router.push(itemUrl)
|
| 88 | + }, |
64 | 89 | },
|
65 |
| - }, |
66 |
| - transformItems(items: DocSearchHit[]) { |
67 |
| - return items.map((item) => { |
68 |
| - return Object.assign({}, item, { |
69 |
| - url: getRelativePath(item.url), |
| 90 | + transformItems(items) { |
| 91 | + return items.map((item) => { |
| 92 | + return Object.assign({}, item, { |
| 93 | + url: getRelativePath(item.url), |
| 94 | + }) |
70 | 95 | })
|
71 |
| - }) |
72 |
| - }, |
73 |
| - hitComponent({ hit, children }: { hit: DocSearchHit, children: any }) { |
74 |
| - const relativeHit = hit.url.startsWith('http') |
75 |
| - ? getRelativePath(hit.url as string) |
76 |
| - : hit.url |
77 |
| - return { |
78 |
| - __v: null, |
79 |
| - type: 'a', |
80 |
| - ref: undefined, |
81 |
| - constructor: undefined, |
82 |
| - key: undefined, |
83 |
| - props: { |
84 |
| - href: hit.url, |
85 |
| - onClick(event: MouseEvent) { |
86 |
| - if (isSpecialClick(event)) |
87 |
| - return |
88 |
| -
|
89 |
| - // we rely on the native link scrolling when user is already on |
90 |
| - // the right anchor because Router doesn't support duplicated |
91 |
| - // history entries. |
92 |
| - if (route.path === relativeHit) |
93 |
| - return |
94 |
| -
|
95 |
| - // if the hits goes to another page, we prevent the native link |
96 |
| - // behavior to leverage the Router loading feature. |
97 |
| - if (route.path !== relativeHit) |
98 |
| - event.preventDefault() |
99 |
| -
|
100 |
| - router.push(relativeHit) |
101 |
| - }, |
102 |
| - children, |
103 |
| - }, |
104 |
| - } |
| 96 | + }, |
105 | 97 | },
|
106 |
| - }) |
107 |
| - docsearch(options) |
108 |
| -} |
| 98 | + ) as DocSearchProps |
109 | 99 |
|
110 |
| -function isSpecialClick(event: MouseEvent) { |
111 |
| - return ( |
112 |
| - event.button === 1 |
113 |
| - || event.altKey |
114 |
| - || event.ctrlKey |
115 |
| - || event.metaKey |
116 |
| - || event.shiftKey |
117 |
| - ) |
| 100 | + docsearch(options) |
118 | 101 | }
|
119 | 102 |
|
120 |
| -function getRelativePath(absoluteUrl: string) { |
121 |
| - const { pathname, hash } = new URL(absoluteUrl) |
122 |
| - return pathname + hash |
| 103 | +function getRelativePath(url: string) { |
| 104 | + const { pathname, hash } = new URL(url, location.origin) |
| 105 | + return pathname.replace(/\.html$/, '') + hash |
123 | 106 | }
|
124 | 107 | </script>
|
125 | 108 |
|
|
0 commit comments