|
|
@ -1,5 +1,5 @@
|
|
|
|
<template>
|
|
|
|
<template>
|
|
|
|
<div id="docsearch"></div>
|
|
|
|
<div class="algolia-search-box" id="docsearch" />
|
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
<script setup lang="ts">
|
|
|
@ -7,6 +7,20 @@ import type { AlgoliaSearchOptions } from 'algoliasearch'
|
|
|
|
import { useRoute, useRouter } from 'vitepress'
|
|
|
|
import { useRoute, useRouter } from 'vitepress'
|
|
|
|
import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
|
|
|
|
import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { options } = defineProps<{
|
|
|
|
|
|
|
|
options: AlgoliaSearchOptions
|
|
|
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const vm = getCurrentInstance()
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => options, (value) => { update(value) })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
|
|
initialize(options)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function isSpecialClick(event: MouseEvent) {
|
|
|
|
function isSpecialClick(event: MouseEvent) {
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
event.button === 1 ||
|
|
|
|
event.button === 1 ||
|
|
|
@ -19,31 +33,13 @@ function isSpecialClick(event: MouseEvent) {
|
|
|
|
|
|
|
|
|
|
|
|
function getRelativePath(absoluteUrl: string) {
|
|
|
|
function getRelativePath(absoluteUrl: string) {
|
|
|
|
const { pathname, hash } = new URL(absoluteUrl)
|
|
|
|
const { pathname, hash } = new URL(absoluteUrl)
|
|
|
|
// const url = pathname.replace(this.$site.base, '/') + hash
|
|
|
|
|
|
|
|
const url = pathname + hash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return url
|
|
|
|
return pathname + hash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const { options } = defineProps<{
|
|
|
|
|
|
|
|
// Using a regular import breaks at runtime
|
|
|
|
|
|
|
|
options: AlgoliaSearchOptions
|
|
|
|
|
|
|
|
}>()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
const vm = getCurrentInstance()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
|
|
|
() => options,
|
|
|
|
|
|
|
|
(newValue) => {
|
|
|
|
|
|
|
|
update(newValue)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function update(options: any) {
|
|
|
|
function update(options: any) {
|
|
|
|
if (vm && vm.vnode.el) {
|
|
|
|
if (vm && vm.vnode.el) {
|
|
|
|
vm.vnode.el.innerHTML = '<div id="docsearch"></div>'
|
|
|
|
vm.vnode.el.innerHTML = '<div class="algolia-search-box" id="docsearch"></div>'
|
|
|
|
initialize(options)
|
|
|
|
initialize(options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -52,33 +48,21 @@ function initialize(userOptions: any) {
|
|
|
|
Promise.all([
|
|
|
|
Promise.all([
|
|
|
|
import('@docsearch/js'),
|
|
|
|
import('@docsearch/js'),
|
|
|
|
import('@docsearch/css')
|
|
|
|
import('@docsearch/css')
|
|
|
|
// import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
|
|
|
|
|
|
|
|
// Promise.resolve(docsearch),
|
|
|
|
|
|
|
|
// import(/* webpackChunkName: "docsearch" */ '@docsearch/css'),
|
|
|
|
|
|
|
|
]).then(([docsearch]) => {
|
|
|
|
]).then(([docsearch]) => {
|
|
|
|
docsearch = docsearch.default
|
|
|
|
docsearch.default(
|
|
|
|
|
|
|
|
|
|
|
|
docsearch(
|
|
|
|
|
|
|
|
Object.assign({}, userOptions, {
|
|
|
|
Object.assign({}, userOptions, {
|
|
|
|
container: '#docsearch',
|
|
|
|
container: '#docsearch',
|
|
|
|
// #697 Make DocSearch work well in i18n mode.
|
|
|
|
|
|
|
|
searchParameters: Object.assign(
|
|
|
|
searchParameters: Object.assign({}, userOptions.searchParameters),
|
|
|
|
{},
|
|
|
|
|
|
|
|
// lang && {
|
|
|
|
|
|
|
|
// facetFilters: [`lang:${lang}`].concat(
|
|
|
|
|
|
|
|
// userOptions.facetFilters || []
|
|
|
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
userOptions.searchParameters
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
navigator: {
|
|
|
|
navigator: {
|
|
|
|
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
|
|
|
|
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
|
|
|
|
const { pathname: hitPathname } = new URL(
|
|
|
|
const { pathname: hitPathname } = new URL(
|
|
|
|
window.location.origin + suggestionUrl
|
|
|
|
window.location.origin + suggestionUrl
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Vue Router doesn't handle same-page navigation so we use
|
|
|
|
// Router doesn't handle same-page navigation so we use the native
|
|
|
|
// the native browser location API for anchor navigation.
|
|
|
|
// browser location API for anchor navigation
|
|
|
|
if (route.path === hitPathname) {
|
|
|
|
if (route.path === hitPathname) {
|
|
|
|
window.location.assign(window.location.origin + suggestionUrl)
|
|
|
|
window.location.assign(window.location.origin + suggestionUrl)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -86,6 +70,7 @@ function initialize(userOptions: any) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
transformItems: (items) => {
|
|
|
|
transformItems: (items) => {
|
|
|
|
return items.map((item) => {
|
|
|
|
return items.map((item) => {
|
|
|
|
return Object.assign({}, item, {
|
|
|
|
return Object.assign({}, item, {
|
|
|
@ -93,6 +78,7 @@ function initialize(userOptions: any) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
hitComponent: ({ hit, children }) => {
|
|
|
|
hitComponent: ({ hit, children }) => {
|
|
|
|
const relativeHit = hit.url.startsWith('http')
|
|
|
|
const relativeHit = hit.url.startsWith('http')
|
|
|
|
? getRelativePath(hit.url as string)
|
|
|
|
? getRelativePath(hit.url as string)
|
|
|
@ -110,15 +96,15 @@ function initialize(userOptions: any) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We rely on the native link scrolling when user is
|
|
|
|
// we rely on the native link scrolling when user is already on
|
|
|
|
// already on the right anchor because Vue Router doesn't
|
|
|
|
// the right anchor because Router doesn't support duplicated
|
|
|
|
// support duplicated history entries.
|
|
|
|
// history entries
|
|
|
|
if (route.path === relativeHit) {
|
|
|
|
if (route.path === relativeHit) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the hits goes to another page, we prevent the native link behavior
|
|
|
|
// if the hits goes to another page, we prevent the native link
|
|
|
|
// to leverage the Vue Router loading feature.
|
|
|
|
// behavior to leverage the Router loading feature
|
|
|
|
if (route.path !== relativeHit) {
|
|
|
|
if (route.path !== relativeHit) {
|
|
|
|
event.preventDefault()
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -133,16 +119,37 @@ function initialize(userOptions: any) {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
|
|
initialize(options)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
<style>
|
|
|
|
|
|
|
|
.algolia-search-box {
|
|
|
|
|
|
|
|
padding-top: 1px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@media (min-width: 720px) {
|
|
|
|
|
|
|
|
.algolia-search-box {
|
|
|
|
|
|
|
|
padding-left: 8px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@media (min-width: 751px) {
|
|
|
|
|
|
|
|
.algolia-search-box {
|
|
|
|
|
|
|
|
padding-left: 8px;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.algolia-search-box .DocSearch-Button-Placeholder {
|
|
|
|
|
|
|
|
padding-left: 8px;
|
|
|
|
|
|
|
|
font-size: .9rem;
|
|
|
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.DocSearch {
|
|
|
|
.DocSearch {
|
|
|
|
--docsearch-primary-color: #42b983;
|
|
|
|
--docsearch-primary-color: #42b983;
|
|
|
|
--docsearch-highlight-color: var(--docsearch-primary-color);
|
|
|
|
--docsearch-highlight-color: var(--docsearch-primary-color);
|
|
|
|
--docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color);
|
|
|
|
--docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color);
|
|
|
|
|
|
|
|
--docsearch-text-color: var(--c-text-light);
|
|
|
|
|
|
|
|
--docsearch-muted-color: var(--c-text-lighter);
|
|
|
|
|
|
|
|
--docsearch-searchbox-background: #f2f2f2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</style>
|
|
|
|