Skip to content

Commit 39b2b9b

Browse files
authored
feat[Search]: route search supports pinyin (#2643)
1 parent 2559891 commit 39b2b9b

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"normalize.css": "7.0.0",
5959
"nprogress": "0.2.0",
6060
"path-to-regexp": "2.4.0",
61+
"pinyin": "2.9.0",
6162
"screenfull": "4.2.0",
6263
"showdown": "1.9.0",
6364
"sortablejs": "1.8.4",

src/components/HeaderSearch/index.vue

+27-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export default {
4141
},
4242
lang() {
4343
return this.$store.getters.language
44+
},
45+
supportPinyinSearch() {
46+
return this.$store.state.settings.supportPinyinSearch
4447
}
4548
},
4649
watch: {
@@ -51,6 +54,10 @@ export default {
5154
this.searchPool = this.generateRoutes(this.routes)
5255
},
5356
searchPool(list) {
57+
// Support pinyin search
58+
if (this.lang === 'zh' && this.supportPinyinSearch) {
59+
this.addPinyinField(list)
60+
}
5461
this.initFuse(list)
5562
},
5663
show(value) {
@@ -65,6 +72,23 @@ export default {
6572
this.searchPool = this.generateRoutes(this.routes)
6673
},
6774
methods: {
75+
async addPinyinField(list) {
76+
const { default: pinyin } = await import('pinyin')
77+
if (Array.isArray(list)) {
78+
list.forEach(element => {
79+
const title = element.title
80+
if (Array.isArray(title)) {
81+
title.forEach(v => {
82+
v = pinyin(v, {
83+
style: pinyin.STYLE_NORMAL
84+
}).join('')
85+
element.pinyinTitle = v
86+
})
87+
}
88+
})
89+
return list
90+
}
91+
},
6892
click() {
6993
this.show = !this.show
7094
if (this.show) {
@@ -95,6 +119,9 @@ export default {
95119
keys: [{
96120
name: 'title',
97121
weight: 0.7
122+
}, {
123+
name: 'pinyinTitle',
124+
weight: 0.3
98125
}, {
99126
name: 'path',
100127
weight: 0.3
@@ -105,29 +132,23 @@ export default {
105132
// And generate the internationalized title
106133
generateRoutes(routes, basePath = '/', prefixTitle = []) {
107134
let res = []
108-
109135
for (const router of routes) {
110136
// skip hidden router
111137
if (router.hidden) { continue }
112-
113138
const data = {
114139
path: path.resolve(basePath, router.path),
115140
title: [...prefixTitle]
116141
}
117-
118142
if (router.meta && router.meta.title) {
119143
// generate internationalized title
120144
const i18ntitle = i18n.t(`route.${router.meta.title}`)
121-
122145
data.title = [...data.title, i18ntitle]
123-
124146
if (router.redirect !== 'noRedirect') {
125147
// only push the routes with title
126148
// special case: need to exclude parent router without redirect
127149
res.push(data)
128150
}
129151
}
130-
131152
// recursive child routes
132153
if (router.children) {
133154
const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
@@ -152,13 +173,11 @@ export default {
152173
<style lang="scss" scoped>
153174
.header-search {
154175
font-size: 0 !important;
155-
156176
.search-icon {
157177
cursor: pointer;
158178
font-size: 18px;
159179
vertical-align: middle;
160180
}
161-
162181
.header-search-select {
163182
font-size: 18px;
164183
transition: width 0.2s;
@@ -168,7 +187,6 @@ export default {
168187
border-radius: 0;
169188
display: inline-block;
170189
vertical-align: middle;
171-
172190
/deep/ .el-input__inner {
173191
border-radius: 0;
174192
border: 0;
@@ -179,7 +197,6 @@ export default {
179197
vertical-align: middle;
180198
}
181199
}
182-
183200
&.show {
184201
.header-search-select {
185202
width: 210px;

src/layout/components/Settings/index.vue

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
/>
3131
</a>
3232

33+
<div v-if="lang === 'zh'" class="drawer-item">
34+
<span>菜单支持拼音搜索</span>
35+
<el-switch v-model="supportPinyinSearch" class="drawer-switch" />
36+
</div>
37+
3338
</div>
3439
</div>
3540
</template>
@@ -78,6 +83,20 @@ export default {
7883
value: val
7984
})
8085
}
86+
},
87+
supportPinyinSearch: {
88+
get() {
89+
return this.$store.state.settings.supportPinyinSearch
90+
},
91+
set(val) {
92+
this.$store.dispatch('settings/changeSetting', {
93+
key: 'supportPinyinSearch',
94+
value: val
95+
})
96+
}
97+
},
98+
lang() {
99+
return this.$store.getters.language
81100
}
82101
},
83102
methods: {

src/settings.js

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ module.exports = {
2525
*/
2626
sidebarLogo: false,
2727

28+
/**
29+
* @type {boolean} true | false
30+
* @description Whether support pinyin search in headerSearch
31+
* Bundle size minified 47.3kb,minified + gzipped 63kb
32+
*/
33+
supportPinyinSearch: true,
34+
2835
/**
2936
* @type {string | array} 'production' | ['production', 'development']
3037
* @description Need show err logs component.

src/store/modules/settings.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import variables from '@/styles/element-variables.scss'
22
import defaultSettings from '@/settings'
33

4-
const { showSettings, tagsView, fixedHeader, sidebarLogo } = defaultSettings
4+
const { showSettings, tagsView, fixedHeader, sidebarLogo, supportPinyinSearch } = defaultSettings
55

66
const state = {
77
theme: variables.theme,
8-
showSettings: showSettings,
9-
tagsView: tagsView,
10-
fixedHeader: fixedHeader,
11-
sidebarLogo: sidebarLogo
8+
showSettings,
9+
tagsView,
10+
fixedHeader,
11+
sidebarLogo,
12+
supportPinyinSearch
1213
}
1314

1415
const mutations = {

0 commit comments

Comments
 (0)