refactor: use script setup

pull/153/head
Eduardo San Martin Morote 5 years ago
parent 2c47fad2cd
commit 2ea51e7ee8

@ -2,12 +2,10 @@
<div id="docsearch"></div>
</template>
<script lang="ts">
<script setup lang="ts">
import { AlgoliaSearchOptions } from 'algoliasearch'
// import docsearch from '@docsearch/js'
// import '@docsearch/css'
import { useRoute, useRouter } from 'vitepress'
import { getCurrentInstance, onMounted, PropType, watch } from 'vue'
import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
function isSpecialClick(event: MouseEvent) {
return (
@ -27,126 +25,115 @@ function getRelativePath(absoluteUrl: string) {
return url
}
export default {
name: 'AlgoliaSearchBox',
props: {
options: {
type: Object as PropType<AlgoliaSearchOptions>,
required: true,
},
},
setup(props) {
const route = useRoute()
const router = useRouter()
const vm = getCurrentInstance()
watch(
() => props.options,
newValue => {
update(newValue)
}
)
const { options } = defineProps<{ options: AlgoliaSearchOptions }>()
const route = useRoute()
const router = useRouter()
const vm = getCurrentInstance()
watch(
() => options,
(newValue) => {
update(newValue)
}
)
function update(options: any) {
if (vm && vm.vnode.el) {
vm.vnode.el.innerHTML = '<div id="docsearch"></div>'
initialize(options)
}
}
function initialize(userOptions: any) {
Promise.all([
import('@docsearch/js'),
import('@docsearch/css')
// import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
// Promise.resolve(docsearch),
// import(/* webpackChunkName: "docsearch" */ '@docsearch/css'),
]).then(([docsearch]) => {
docsearch = docsearch.default
docsearch(
Object.assign({}, userOptions, {
container: '#docsearch',
// #697 Make DocSearch work well in i18n mode.
searchParameters: Object.assign(
{},
// lang && {
// facetFilters: [`lang:${lang}`].concat(
// userOptions.facetFilters || []
// )
// },
userOptions.searchParameters
),
navigator: {
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
const { pathname: hitPathname } = new URL(
window.location.origin + suggestionUrl
)
// Vue Router doesn't handle same-page navigation so we use
// the native browser location API for anchor navigation.
if (route.path === hitPathname) {
window.location.assign(window.location.origin + suggestionUrl)
} else {
router.go(suggestionUrl)
}
}
},
transformItems: (items) => {
return items.map((item) => {
return Object.assign({}, item, {
url: getRelativePath(item.url)
})
})
},
hitComponent: ({ hit, children }) => {
const relativeHit = hit.url.startsWith('http')
? getRelativePath(hit.url as string)
: hit.url
return {
type: 'a',
ref: undefined,
constructor: undefined,
key: undefined,
props: {
href: hit.url,
onClick: (event: MouseEvent) => {
if (isSpecialClick(event)) {
return
}
function update(options: any) {
if (vm && vm.vnode.el) {
vm.vnode.el.innerHTML = '<div id="docsearch"></div>'
initialize(options)
}
}
function initialize(userOptions: any) {
Promise.all([
import('@docsearch/js'),
import('@docsearch/css'),
// import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
// Promise.resolve(docsearch),
// import(/* webpackChunkName: "docsearch" */ '@docsearch/css'),
]).then(([docsearch]) => {
docsearch = docsearch.default
docsearch(
Object.assign({}, userOptions, {
container: '#docsearch',
// #697 Make DocSearch work well in i18n mode.
searchParameters: Object.assign(
{},
// lang && {
// facetFilters: [`lang:${lang}`].concat(
// userOptions.facetFilters || []
// )
// },
userOptions.searchParameters
),
navigator: {
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
const { pathname: hitPathname } = new URL(
window.location.origin + suggestionUrl
)
// Vue Router doesn't handle same-page navigation so we use
// the native browser location API for anchor navigation.
if (route.path === hitPathname) {
window.location.assign(window.location.origin + suggestionUrl)
} else {
router.go(suggestionUrl)
// 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 (route.path === relativeHit) {
return
}
// If the hits goes to another page, we prevent the native link behavior
// to leverage the Vue Router loading feature.
if (route.path !== relativeHit) {
event.preventDefault()
}
router.go(relativeHit)
},
},
transformItems: items => {
return items.map(item => {
return Object.assign({}, item, {
url: getRelativePath(item.url),
})
})
},
hitComponent: ({ hit, children }) => {
const relativeHit = hit.url.startsWith('http')
? getRelativePath(hit.url as string)
: hit.url
return {
type: 'a',
ref: undefined,
constructor: undefined,
key: undefined,
props: {
href: hit.url,
onClick: (event: MouseEvent) => {
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 (route.path === relativeHit) {
return
}
// If the hits goes to another page, we prevent the native link behavior
// to leverage the Vue Router loading feature.
if (route.path !== relativeHit) {
event.preventDefault()
}
router.go(relativeHit)
},
children,
},
}
},
})
)
children
}
}
}
})
}
onMounted(() => {
initialize(props.options)
})
},
)
})
}
onMounted(() => {
initialize(options)
})
</script>
<style>

Loading…
Cancel
Save