2021-07-19 16:27:38 +08:00
|
|
|
<template>
|
2021-08-23 19:37:43 +08:00
|
|
|
<div class="one-search">
|
|
|
|
<veui-search-box
|
|
|
|
v-model="query"
|
|
|
|
class="input"
|
|
|
|
:placeholder="searchPlaceholder"
|
|
|
|
@input="handleInput"
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
ref="docsearch"
|
|
|
|
class="docsearch"
|
|
|
|
/>
|
|
|
|
</div>
|
2021-07-19 16:27:38 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-08-23 19:37:43 +08:00
|
|
|
import { SearchBox } from 'veui'
|
|
|
|
import { createElement } from 'preact'
|
2021-07-19 16:27:38 +08:00
|
|
|
import i18n from '../common/i18n'
|
|
|
|
|
|
|
|
function isSpecialClick (event) {
|
|
|
|
return (
|
|
|
|
event.button === 1 ||
|
|
|
|
event.altKey ||
|
|
|
|
event.ctrlKey ||
|
|
|
|
event.metaKey ||
|
|
|
|
event.shiftKey
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default {
|
|
|
|
name: 'one-search',
|
2021-08-23 19:37:43 +08:00
|
|
|
components: {
|
|
|
|
'veui-search-box': SearchBox
|
|
|
|
},
|
2021-07-19 16:27:38 +08:00
|
|
|
mixins: [i18n],
|
2021-08-23 19:37:43 +08:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
query: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
searchPlaceholder () {
|
|
|
|
return this.locale === 'zh-Hans' ? '⌘ K | 搜索…' : '⌘ K | Search...'
|
|
|
|
}
|
|
|
|
},
|
2021-07-19 16:27:38 +08:00
|
|
|
watch: {
|
|
|
|
locale (val) {
|
|
|
|
this.update(val)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted () {
|
|
|
|
this.initialize(this.locale)
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getRelativePath (absoluteUrl) {
|
|
|
|
const { pathname, hash } = new URL(absoluteUrl)
|
|
|
|
return `${pathname}/${hash}`
|
|
|
|
},
|
|
|
|
initialize (locale) {
|
|
|
|
Promise.all([
|
|
|
|
import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
|
|
|
|
import(/* webpackChunkName: "docsearch" */ '@docsearch/css')
|
|
|
|
]).then(([docsearch]) => {
|
|
|
|
docsearch = docsearch.default
|
|
|
|
docsearch({
|
|
|
|
apiKey: '21cdc123c620ec4bb81259c32e90c08f',
|
|
|
|
indexName: 'veui',
|
2021-08-23 19:37:43 +08:00
|
|
|
container: this.$refs.docsearch,
|
2021-07-19 16:27:38 +08:00
|
|
|
searchParameters: {
|
|
|
|
facetFilters: [`lang:${locale}`]
|
|
|
|
},
|
|
|
|
navigator: {
|
2021-08-24 14:50:43 +08:00
|
|
|
navigate: ({ itemUrl }) => {
|
2021-07-19 16:27:38 +08:00
|
|
|
const { pathname: hitPathname } = new URL(
|
2021-08-24 14:50:43 +08:00
|
|
|
window.location.origin + itemUrl
|
2021-07-19 16:27:38 +08:00
|
|
|
)
|
|
|
|
if (this.$router.history.current.path === hitPathname) {
|
2021-08-24 14:50:43 +08:00
|
|
|
window.location.assign(window.location.origin + itemUrl)
|
2021-07-19 16:27:38 +08:00
|
|
|
} else {
|
2021-08-24 14:50:43 +08:00
|
|
|
this.$router.push(itemUrl)
|
2021-07-19 16:27:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
transformItems: items => {
|
|
|
|
return items.map(item => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
url: this.getRelativePath(item.url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
hitComponent: ({ hit, children }) => {
|
2021-08-23 19:37:43 +08:00
|
|
|
return createElement(
|
|
|
|
'a',
|
|
|
|
{
|
2021-07-19 16:27:38 +08:00
|
|
|
href: hit.url,
|
|
|
|
onClick: event => {
|
|
|
|
if (isSpecialClick(event)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// We rely on the native link scrolling when user is
|
|
|
|
// already on the right anchor because Vue Router doesn't
|
|
|
|
// support duplicated history entries.
|
|
|
|
if (this.$router.history.current.fullPath === hit.url) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const { pathname: hitPathname } = new URL(
|
|
|
|
window.location.origin + hit.url
|
|
|
|
)
|
|
|
|
// If the hits goes to another page, we prevent the native link behavior
|
|
|
|
// to leverage the Vue Router loading feature.
|
|
|
|
if (this.$router.history.current.path !== hitPathname) {
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
this.$router.push(hit.url)
|
|
|
|
},
|
|
|
|
children
|
|
|
|
}
|
2021-08-23 19:37:43 +08:00
|
|
|
)
|
2021-07-19 16:27:38 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
update (locale) {
|
2021-08-23 19:37:43 +08:00
|
|
|
this.$refs.docsearch.innerHTML = ''
|
2021-07-19 16:27:38 +08:00
|
|
|
this.initialize(locale)
|
2021-08-23 19:37:43 +08:00
|
|
|
},
|
|
|
|
handleInput (value) {
|
|
|
|
if (!value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.teleportQuery(value)
|
|
|
|
},
|
|
|
|
teleportQuery (value) {
|
|
|
|
this.query = ''
|
|
|
|
const docsearchButton = this.$refs.docsearch.querySelector('.DocSearch')
|
|
|
|
docsearchButton.click()
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
const docsearchInput = document.getElementById('docsearch-input')
|
|
|
|
docsearchInput.value = value
|
|
|
|
docsearchInput.dispatchEvent(new Event('input'))
|
|
|
|
})
|
2021-07-19 16:27:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
2021-08-23 19:37:43 +08:00
|
|
|
|
|
|
|
<style lang="stylus" scoped>
|
|
|
|
.input
|
|
|
|
width auto
|
|
|
|
|
|
|
|
.docsearch
|
|
|
|
display none
|
|
|
|
</style>
|