Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[Search]: route search supports pinyin #2643

Merged
merged 5 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"normalize.css": "7.0.0",
"nprogress": "0.2.0",
"path-to-regexp": "2.4.0",
"pinyin": "^2.9.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写死版本号

"screenfull": "4.2.0",
"showdown": "1.9.0",
"sortablejs": "1.8.4",
Expand Down
37 changes: 27 additions & 10 deletions src/components/HeaderSearch/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export default {
},
lang() {
return this.$store.getters.language
},
supportPinyinSearch() {
return this.$store.state.settings.supportPinyinSearch
}
},
watch: {
Expand All @@ -51,6 +54,10 @@ export default {
this.searchPool = this.generateRoutes(this.routes)
},
searchPool(list) {
// Support pinyin search
if (this.lang === 'zh' && this.supportPinyinSearch) {
this.addPinyinField(list)
}
this.initFuse(list)
},
show(value) {
Expand All @@ -65,6 +72,23 @@ export default {
this.searchPool = this.generateRoutes(this.routes)
},
methods: {
addPinyinField(list) {
const pinyin = require('pinyin')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Array.isArray(list)) {
list.forEach(element => {
const title = element.title
if (Array.isArray(title)) {
title.forEach(v => {
v = pinyin(v, {
style: pinyin.STYLE_NORMAL
}).join('')
element.pinyinTitle = v
})
}
})
return list
}
},
click() {
this.show = !this.show
if (this.show) {
Expand Down Expand Up @@ -95,6 +119,9 @@ export default {
keys: [{
name: 'title',
weight: 0.7
}, {
name: 'pinyinTitle',
weight: 0.3
}, {
name: 'path',
weight: 0.3
Expand All @@ -105,29 +132,23 @@ export default {
// And generate the internationalized title
generateRoutes(routes, basePath = '/', prefixTitle = []) {
let res = []

for (const router of routes) {
// skip hidden router
if (router.hidden) { continue }

const data = {
path: path.resolve(basePath, router.path),
title: [...prefixTitle]
}

if (router.meta && router.meta.title) {
// generate internationalized title
const i18ntitle = i18n.t(`route.${router.meta.title}`)

data.title = [...data.title, i18ntitle]

if (router.redirect !== 'noRedirect') {
// only push the routes with title
// special case: need to exclude parent router without redirect
res.push(data)
}
}

// recursive child routes
if (router.children) {
const tempRoutes = this.generateRoutes(router.children, data.path, data.title)
Expand All @@ -152,13 +173,11 @@ export default {
<style lang="scss" scoped>
.header-search {
font-size: 0 !important;

.search-icon {
cursor: pointer;
font-size: 18px;
vertical-align: middle;
}

.header-search-select {
font-size: 18px;
transition: width 0.2s;
Expand All @@ -168,7 +187,6 @@ export default {
border-radius: 0;
display: inline-block;
vertical-align: middle;

/deep/ .el-input__inner {
border-radius: 0;
border: 0;
Expand All @@ -179,7 +197,6 @@ export default {
vertical-align: middle;
}
}

&.show {
.header-search-select {
width: 210px;
Expand Down
19 changes: 19 additions & 0 deletions src/layout/components/Settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<el-switch v-model="sidebarLogo" class="drawer-switch" />
</div>

<div v-if="lang === 'zh'" class="drawer-item">
<span>菜单支持拼音搜索</span>
<el-switch v-model="supportPinyinSearch" class="drawer-switch" />
</div>

</div>
</div>
</template>
Expand Down Expand Up @@ -68,6 +73,20 @@ export default {
value: val
})
}
},
supportPinyinSearch: {
get() {
return this.$store.state.settings.supportPinyinSearch
},
set(val) {
this.$store.dispatch('settings/changeSetting', {
key: 'supportPinyinSearch',
value: val
})
}
},
lang() {
return this.$store.getters.language
}
},
methods: {
Expand Down
7 changes: 7 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ module.exports = {
*/
sidebarLogo: false,

/**
* @type {boolean} true | false
* @description Whether support pinyin search in headerSearch
* Bundle size minified 47.3kb,minified + gzipped 63kb
*/
supportPinyinSearch: true,

/**
* @type {string | array} 'production' | ['production', 'development']
* @description Need show err logs component.
Expand Down
11 changes: 6 additions & 5 deletions src/store/modules/settings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import variables from '@/styles/element-variables.scss'
import defaultSettings from '@/settings'

const { showSettings, tagsView, fixedHeader, sidebarLogo } = defaultSettings
const { showSettings, tagsView, fixedHeader, sidebarLogo, supportPinyinSearch } = defaultSettings

const state = {
theme: variables.theme,
showSettings: showSettings,
tagsView: tagsView,
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo
showSettings,
tagsView,
fixedHeader,
sidebarLogo,
supportPinyinSearch
}

const mutations = {
Expand Down