1
1
<template >
2
- <form
3
- id =" search-form"
4
- class =" algolia-search-wrapper search-box"
5
- role =" search"
6
- >
7
- <input
8
- id =" algolia-search-input"
9
- class =" search-query"
10
- :placeholder =" placeholder"
11
- >
12
- </form >
2
+ <div id =" docsearch" ></div >
13
3
</template >
14
4
15
5
<script >
6
+ function isSpecialClick (event ) {
7
+ return (
8
+ event .button === 1 ||
9
+ event .altKey ||
10
+ event .ctrlKey ||
11
+ event .metaKey ||
12
+ event .shiftKey
13
+ )
14
+ }
15
+
16
16
export default {
17
17
name: ' AlgoliaSearchBox' ,
18
18
19
19
props: [' options' ],
20
20
21
- data () {
22
- return {
23
- placeholder: undefined
24
- }
25
- },
26
-
27
21
watch: {
28
22
$lang (newValue) {
29
23
this .update (this .options , newValue)
@@ -36,135 +30,120 @@ export default {
36
30
37
31
mounted () {
38
32
this .initialize (this .options , this .$lang )
39
- this .placeholder = this .$site .themeConfig .searchPlaceholder || ' '
40
33
},
41
34
42
35
methods: {
36
+ getRelativePath (absoluteUrl ) {
37
+ const { pathname , hash } = new URL (absoluteUrl)
38
+ const url = pathname .replace (this .$site .base , ' /' ) + hash
39
+
40
+ return url
41
+ },
43
42
initialize (userOptions , lang ) {
44
43
Promise .all ([
45
- import (/* webpackChunkName: "docsearch" */ ' docsearch.js/dist/cdn/docsearch.min. js' ),
46
- import (/* webpackChunkName: "docsearch" */ ' docsearch.js/dist/cdn/docsearch.min. css' )
44
+ import (/* webpackChunkName: "docsearch" */ ' @ docsearch/ js' ),
45
+ import (/* webpackChunkName: "docsearch" */ ' @ docsearch/ css' )
47
46
]).then (([docsearch ]) => {
48
47
docsearch = docsearch .default
49
- const { algoliaOptions = {}} = userOptions
50
- docsearch (Object .assign (
51
- {},
52
- userOptions,
53
- {
54
- inputSelector: ' #algolia-search-input' ,
55
- // #697 Make docsearch work well at i18n mode.
56
- algoliaOptions: Object .assign ({
57
- ' facetFilters' : [` lang:${ lang} ` ].concat (algoliaOptions .facetFilters || [])
58
- }, algoliaOptions),
59
- handleSelected : (input , event , suggestion ) => {
60
- const { pathname , hash } = new URL (suggestion .url )
61
- const routepath = pathname .replace (this .$site .base , ' /' )
62
- this .$router .push (` ${ routepath}${ hash} ` )
48
+
49
+ docsearch (
50
+ Object .assign (
51
+ {
52
+ placeholder: this .$site .themeConfig .searchPlaceholder
53
+ },
54
+ userOptions,
55
+ {
56
+ container: ' #docsearch' ,
57
+ // #697 Make DocSearch work well in i18n mode.
58
+ searchParameters: Object .assign (
59
+ {},
60
+ // lang && {
61
+ // facetFilters: [`lang:${lang}`].concat(
62
+ // userOptions.facetFilters || []
63
+ // )
64
+ // },
65
+ userOptions .searchParameters
66
+ ),
67
+ navigator : {
68
+ navigate : ({ suggestionUrl }) => {
69
+ const { pathname: hitPathname } = new URL (
70
+ window .location .origin + suggestionUrl
71
+ )
72
+
73
+ // Vue Router doesn't handle same-page navigation so we use
74
+ // the native browser location API for anchor navigation.
75
+ if (this .$router .history .current .path === hitPathname) {
76
+ window .location .assign (
77
+ window .location .origin + suggestionUrl
78
+ )
79
+ } else {
80
+ this .$router .push (suggestionUrl)
81
+ }
82
+ }
83
+ },
84
+ transformItems : items => {
85
+ return items .map (item => {
86
+ return Object .assign ({}, item, {
87
+ url: this .getRelativePath (item .url )
88
+ })
89
+ })
90
+ },
91
+ hitComponent : ({ hit, children }) => {
92
+ return {
93
+ type: ' a' ,
94
+ ref: undefined ,
95
+ constructor : undefined,
96
+ key: undefined,
97
+ props: {
98
+ href: hit.url,
99
+ onClick: event => {
100
+ if (isSpecialClick (event )) {
101
+ return
102
+ }
103
+
104
+ // We rely on the native link scrolling when user is
105
+ // already on the right anchor because Vue Router doesn't
106
+ // support duplicated history entries.
107
+ if (this .$router .history .current .fullPath === hit .url ) {
108
+ return
109
+ }
110
+
111
+ const { pathname: hitPathname } = new URL (
112
+ window .location .origin + hit .url
113
+ )
114
+
115
+ // If the hits goes to another page, we prevent the native link behavior
116
+ // to leverage the Vue Router loading feature.
117
+ if (this .$router .history .current .path !== hitPathname) {
118
+ event .preventDefault ()
119
+ }
120
+
121
+ this .$router .push (hit .url )
122
+ },
123
+ children
124
+ }
125
+ }
126
+ }
63
127
}
64
- }
65
- ))
128
+ )
129
+ )
66
130
})
67
131
},
68
132
69
- update (options , lang ) {
70
- this .$el .innerHTML = ' <input id="algolia-search-input" class="search-query" >'
133
+ update (options , lang ) {
134
+ this .$el .innerHTML = ' <div id="docsearch"></div >'
71
135
this .initialize (options, lang)
72
136
}
73
137
}
74
138
}
75
139
</script >
76
140
77
- <style lang="stylus">
78
- .algolia-search-wrapper
79
- & > span
80
- vertical-align middle
81
- .algolia-autocomplete
82
- line-height normal
83
- .ds-dropdown-menu
84
- background-color #f f f
85
- border 1px solid #9 9 9
86
- border-radius 4px
87
- font-size 16px
88
- margin 6px 0 0
89
- padding 4px
90
- text-align left
91
- & :before
92
- border-color #9 9 9
93
- [class* = ds-dataset- ]
94
- border none
95
- padding 0
96
- .ds-suggestions
97
- margin-top 0
98
- .ds-suggestion
99
- border-bottom 1px solid $borderColor
100
- .algolia-docsearch-suggestion--highlight
101
- color #2c 81 5b
102
- .algolia-docsearch-suggestion
103
- border-color $borderColor
104
- padding 0
105
- .algolia-docsearch-suggestion--category-header
106
- padding 5px 10px
107
- margin-top 0
108
- background $accentColor
109
- color #f f f
110
- font-weight 600
111
- .algolia-docsearch-suggestion--highlight
112
- background rgba (255 , 255 , 255 , 0.6 )
113
- .algolia-docsearch-suggestion--wrapper
114
- padding 0
115
- .algolia-docsearch-suggestion--title
116
- font-weight 600
117
- margin-bottom 0
118
- color $textColor
119
- .algolia-docsearch-suggestion--subcategory-column
120
- vertical-align top
121
- padding 5px 7px 5px 5px
122
- border-color $borderColor
123
- background #f1 f3 f5
124
- & :after
125
- display none
126
- .algolia-docsearch-suggestion--subcategory-column-text
127
- color #5 5 5
128
- .algolia-docsearch-footer
129
- border-color $borderColor
130
- .ds-cursor .algolia-docsearch-suggestion--content
131
- background-color #e7 ed f3 !important
132
- color $textColor
133
-
134
- @media (min-width : $MQMobile)
135
- .algolia-search-wrapper
136
- .algolia-autocomplete
137
- .algolia-docsearch-suggestion
138
- .algolia-docsearch-suggestion--subcategory-column
139
- float none
140
- width 150px
141
- min-width 150px
142
- display table-cell
143
- .algolia-docsearch-suggestion--content
144
- float none
145
- display table-cell
146
- width 100%
147
- vertical-align top
148
- .ds-dropdown-menu
149
- min-width 515px !important
150
-
151
- @media (max-width : $MQMobile)
152
- .algolia-search-wrapper
153
- .ds-dropdown-menu
154
- min-width calc (100vw - 4rem ) !important
155
- max-width calc (100vw - 4rem ) !important
156
- .algolia-docsearch-suggestion--wrapper
157
- padding 5px 7px 5px 5px !important
158
- .algolia-docsearch-suggestion--subcategory-column
159
- padding 0 !important
160
- background white !important
161
- .algolia-docsearch-suggestion--subcategory-column-text :after
162
- content " > "
163
- font-size 10px
164
- line-height 14.4px
165
- display inline-block
166
- width 5px
167
- margin - 3px 3px 0
168
- vertical-align middle
141
+ <style lang="scss">
142
+ @import ' @theme/styles/_settings.scss' ;
169
143
144
+ .DocSearch {
145
+ --docsearch-primary-color : #{$green } ;
146
+ --docsearch-highlight-color : var (--docsearch-primary-color );
147
+ --docsearch-searchbox-shadow : inset 0 0 0 2px var (--docsearch-primary-color );
148
+ }
170
149
</style >
0 commit comments