|
1 | 1 | <script setup lang="ts">
|
2 |
| -import 'docsearch.js/dist/cdn/docsearch.min.css' |
3 |
| -import docsearch from 'docsearch.js/dist/cdn/docsearch.min.js' |
4 | 2 | import { useRoute, useRouter, useData } from 'vitepress'
|
5 | 3 | import { getCurrentInstance, onMounted, watch } from 'vue'
|
6 | 4 | const props = defineProps<{
|
@@ -57,32 +55,37 @@ watch(
|
57 | 55 | }
|
58 | 56 | )
|
59 | 57 | function initialize(userOptions: any) {
|
60 |
| - const { algoliaOptions = {}} = userOptions |
61 |
| - docsearch(Object.assign( |
62 |
| - {}, |
63 |
| - userOptions, |
64 |
| - { |
65 |
| - inputSelector: '#algolia-search-input', |
66 |
| - // #697 Make docsearch work well at i18n mode. |
67 |
| - algoliaOptions: { |
68 |
| - ...algoliaOptions, |
69 |
| - facetFilters: [`lang:${lang.value}`].concat(algoliaOptions.facetFilters || []) |
70 |
| - }, |
71 |
| - handleSelected: (input, event, suggestion) => { |
72 |
| - const { pathname, hash } = new URL(suggestion.url) |
| 58 | + Promise.all([ |
| 59 | + import('docsearch.js/dist/cdn/docsearch.min.js'), |
| 60 | + import('docsearch.js/dist/cdn/docsearch.min.css') |
| 61 | + ]).then(([docsearch]) => { |
| 62 | + const { algoliaOptions = {}} = userOptions |
| 63 | + docsearch.default(Object.assign( |
| 64 | + {}, |
| 65 | + userOptions, |
| 66 | + { |
| 67 | + inputSelector: '#algolia-search-input', |
| 68 | + // #697 Make docsearch work well at i18n mode. |
| 69 | + algoliaOptions: { |
| 70 | + ...algoliaOptions, |
| 71 | + facetFilters: [`lang:${lang.value}`].concat(algoliaOptions.facetFilters || []) |
| 72 | + }, |
| 73 | + handleSelected: (input, event, suggestion) => { |
| 74 | + const { pathname, hash } = new URL(suggestion.url) |
73 | 75 |
|
74 |
| - // Router doesn't handle same-page navigation so we use the native |
75 |
| - // browser location API for anchor navigation |
76 |
| - if (route.path === pathname) { |
77 |
| - window.location.assign(suggestion.url) |
78 |
| - } else { |
79 |
| - const routepath = pathname.replace(site.base, '/') |
80 |
| - const _hash = decodeURIComponent(hash) |
81 |
| - router.go(`${routepath}${_hash}`) |
| 76 | + // Router doesn't handle same-page navigation so we use the native |
| 77 | + // browser location API for anchor navigation |
| 78 | + if (route.path === pathname) { |
| 79 | + window.location.assign(suggestion.url) |
| 80 | + } else { |
| 81 | + const routepath = pathname.replace(site.base, '/') |
| 82 | + const _hash = decodeURIComponent(hash) |
| 83 | + router.go(`${routepath}${_hash}`) |
| 84 | + } |
82 | 85 | }
|
83 |
| - } |
84 |
| - }) |
85 |
| - ) |
| 86 | + }) |
| 87 | + ) |
| 88 | + }) |
86 | 89 | }
|
87 | 90 | </script>
|
88 | 91 |
|
|
0 commit comments