From 85c3a812da69e970fdcb059c5784ba94d785493c Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 25 Jul 2020 19:36:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=88=91=E7=9A=84=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=AD=8C=E6=9B=B2=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=9B=B8=E4=BC=BC=E5=BA=A6=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/material/SearchList.vue | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/renderer/components/material/SearchList.vue b/src/renderer/components/material/SearchList.vue index e1021b26..4e55a03a 100644 --- a/src/renderer/components/material/SearchList.vue +++ b/src/renderer/components/material/SearchList.vue @@ -28,6 +28,68 @@ div(:class="$style.container" ref="dom_container" v-show="isShow") import { clipboardReadText, debounce, scrollTo } from '../../utils' let canceleFn + +// https://blog.csdn.net/xcxy2015/article/details/77164126#comments +const similar = (a, b) => { + if (!a || !b) return 0 + if (a.length > b.length) { // 保证 a <= b + let t = b + b = a + a = t + } + let al = a.length + let bl = b.length + let mp = [] // 一个表 + let i, j, ai, lt, tmp // ai:字符串a的第i个字符。 lt:左上角的值。 tmp:暂存新的值。 + for (i = 0; i <= bl; i++) mp[i] = i + for (i = 1; i <= al; i++) { + ai = a.charAt(i - 1) + lt = mp[0] + mp[0] = mp[0] + 1 + for (j = 1; j <= bl; j++) { + tmp = Math.min(mp[j] + 1, mp[j - 1] + 1, lt + (ai == b.charAt(j - 1) ? 0 : 1)) + lt = mp[j] + mp[j] = tmp + } + } + return 1 - (mp[bl] / bl) +} + +const sortInsert = (arr, data) => { + let key = data.num + let left = 0 + let right = arr.length - 1 + + while (left <= right) { + let middle = parseInt((left + right) / 2) + if (key == arr[middle]) { + left = middle + break + } else if (key < arr[middle].num) { + right = middle - 1 + } else { + left = middle + 1 + } + } + while (left > 0) { + if (arr[left - 1].num != key) break + left-- + } + + arr.splice(left, 0, data) +} + +const handleSortList = (list, keyword) => { + let arr = [] + for (const item of list) { + sortInsert(arr, { + num: similar(keyword, `${item.name} ${item.singer} ${item.albumName || ''}`), + data: item, + }) + } + return arr.map(item => item.data).reverse() +} + export default { props: { placeholder: { @@ -181,7 +243,7 @@ export default { for (const item of this.list) { if (rxp.test(`${item.name}${item.singer}${item.albumName ? item.albumName : ''}`)) list.push(item) } - this.resultList = list + this.resultList = handleSortList(list, this.text) }, }, }