3
3
</template >
4
4
5
5
<script >
6
+ import docsearch from ' @docsearch/js'
7
+ import ' @docsearch/css/dist/style.css'
8
+
6
9
function isSpecialClick (event ) {
7
10
return (
8
11
event .button === 1 ||
@@ -32,98 +35,71 @@ export default {
32
35
33
36
methods: {
34
37
getRelativePath (absoluteUrl ) {
35
- const { pathname , hash } = new URL (absoluteUrl)
38
+ const {pathname , hash } = new URL (absoluteUrl)
36
39
return pathname .replace (this .$site .base , ' /' ) + hash
37
40
},
38
41
initialize (userOptions , lang ) {
39
- Promise .all ([
40
- import (/* webpackChunkName: "docsearch" */ ' @docsearch/js' ),
41
- import (/* webpackChunkName: "docsearch" */ ' @docsearch/css' )
42
- ]).then (([docsearch ]) => {
43
- docsearch = docsearch .default
44
-
45
- docsearch (
46
- Object .assign (
47
- {
48
- placeholder: this .$site .themeConfig .searchPlaceholder
42
+ const router = this .$router
43
+ docsearch (
44
+ {
45
+ placeholder: this .$site .themeConfig .searchPlaceholder ,
46
+ ... userOptions,
47
+ container: ' #docsearch' ,
48
+ // #697 Make DocSearch work well in i18n mode.
49
+ searchParameters: Object .assign (
50
+ {},
51
+ lang && {
52
+ facetFilters: [` lang:${ lang} ` ].concat (
53
+ userOptions .facetFilters || []
54
+ )
49
55
},
50
- userOptions,
51
- {
52
- container: ' #docsearch' ,
53
- // #697 Make DocSearch work well in i18n mode.
54
- searchParameters: Object .assign (
55
- {},
56
- // lang && {
57
- // facetFilters: [`lang:${lang}`].concat(
58
- // userOptions.facetFilters || []
59
- // )
60
- // },
61
- userOptions .searchParameters
62
- ),
63
- navigator : {
64
- navigate : ({ suggestionUrl }) => {
65
- const { pathname: hitPathname } = new URL (
66
- window .location .origin + suggestionUrl
67
- )
56
+ userOptions .searchParameters
57
+ ),
58
+ navigator : {
59
+ navigate : ({suggestionUrl}) => {
60
+ const {pathname: hitPathname } = new URL (
61
+ window .location .origin + suggestionUrl
62
+ )
68
63
69
- // Vue Router doesn't handle same-page navigation so we use
70
- // the native browser location API for anchor navigation.
71
- if (this .$router .history .current .path === hitPathname) {
72
- window .location .assign (
73
- window .location .origin + suggestionUrl
74
- )
75
- } else {
76
- this .$router .push (suggestionUrl)
64
+ // Vue Router doesn't handle same-page navigation so we use
65
+ // the native browser location API for anchor navigation.
66
+ if (router .history .current .path === hitPathname) {
67
+ window .location .assign (
68
+ window .location .origin + suggestionUrl
69
+ )
70
+ } else {
71
+ router .push (suggestionUrl)
72
+ }
73
+ }
74
+ },
75
+ transformItems : items => {
76
+ return items .map (item => {
77
+ return Object .assign ({}, item, {
78
+ url: this .getRelativePath (item .url )
79
+ })
80
+ })
81
+ },
82
+ hitComponent : ({ hit, children }) =>
83
+ ({
84
+ type: ' a' ,
85
+ ref: undefined ,
86
+ constructor : undefined,
87
+ key: undefined,
88
+ props: {
89
+ href: hit.url,
90
+ // handle `onClick` by `router.push`
91
+ onClick: (event ) => {
92
+ if (isSpecialClick (event )) {
93
+ return
77
94
}
78
- }
95
+ event .preventDefault ()
96
+ router .push (hit .url )
97
+ },
98
+ children,
79
99
},
80
- transformItems : items => {
81
- return items .map (item => {
82
- return Object .assign ({}, item, {
83
- url: this .getRelativePath (item .url )
84
- })
85
- })
86
- },
87
- hitComponent : ({ hit, children }) => {
88
- return {
89
- type: ' a' ,
90
- ref: undefined ,
91
- constructor : undefined,
92
- key: undefined,
93
- props: {
94
- href: hit.url,
95
- onClick: event => {
96
- if (isSpecialClick (event )) {
97
- return
98
- }
99
-
100
- // We rely on the native link scrolling when user is
101
- // already on the right anchor because Vue Router doesn't
102
- // support duplicated history entries.
103
- if (this .$router .history .current .fullPath === hit .url ) {
104
- return
105
- }
106
-
107
- const { pathname: hitPathname } = new URL (
108
- window .location .origin + hit .url
109
- )
110
-
111
- // If the hits goes to another page, we prevent the native link behavior
112
- // to leverage the Vue Router loading feature.
113
- if (this .$router .history .current .path !== hitPathname) {
114
- event .preventDefault ()
115
- }
116
-
117
- this .$router .push (hit .url )
118
- },
119
- children
120
- }
121
- }
122
- }
123
- }
124
- )
125
- )
126
- })
100
+ __v: null ,
101
+ })
102
+ })
127
103
},
128
104
129
105
update (options , lang ) {
@@ -139,4 +115,4 @@ export default {
139
115
--docsearch-highlight-color : var (--docsearch-primary-color );
140
116
--docsearch-searchbox-shadow : inset 0 0 0 2px var (--docsearch-primary-color );
141
117
}
142
- </style >
118
+ </style >
0 commit comments