-
Notifications
You must be signed in to change notification settings - Fork 284
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
fix(icon): [icon] The icon search algorithm is optimized. #2563
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,17 +4,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<tiny-input class="search-input" v-model="searchName" clearable autofocus size="small"></tiny-input> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="svgs-wrapper"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-for="(nameList, groupName) in iconGroups" :key="groupName"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="group-name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-for="(nameList, groupName) in iconGroupsMap" :key="groupName"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-show="nameList.length" class="group-name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{ groupName }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<template v-for="name in nameList"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
v-if="searchName === '' || name.toLowerCase().includes(searchName.toLowerCase())" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:key="name" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class="svgs-item" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@click="click(name)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<template v-for="name in nameList" :key="name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="svgs-item" @click="click(name)"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<component :is="Svgs[name] && Svgs[name]()" class="svgs-icon"></component> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<span class="svgs-text">{{ name }}</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -25,7 +20,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</template> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<script setup> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { ref } from 'vue' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { ref, reactive, watch } from 'vue' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import Svgs from '@opentiny/vue-icon' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { TinyModal, TinyInput } from '@opentiny/vue' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { iconGroups } from './iconGroups.js' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -41,6 +36,7 @@ Object.keys(iconGroups).forEach((k) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const searchName = ref('') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let iconGroupsMap = reactive(iconGroups) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Fix reactive state management The current implementation has a reactivity issue. Direct reassignment to a reactive object (lines 52, 67) will break Vue's reactivity system. Refactor the reactive state management: -let iconGroupsMap = reactive(iconGroups)
+const iconGroupsMap = ref(iconGroups)
// In watch handler
-iconGroupsMap = iconGroups
+iconGroupsMap.value = iconGroups
-iconGroupsMap = result
+iconGroupsMap.value = result Also applies to: 52-52, 67-67 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function click(name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
window.navigator.clipboard.writeText(name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -49,6 +45,28 @@ function click(name) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: 'info' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
watch(searchName, (newVal) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. watch 必要性不大, 还没有模板中直接过滤显得直接。 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const keyWord = (newVal || '').trim().toLowerCase() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (keyWord === '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iconGroupsMap = iconGroups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 丢失响应性了 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const result = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (let groupName in iconGroups) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const nameList = iconGroups[groupName] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nameList.forEach((name) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (name.toLowerCase().includes(keyWord)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!result[groupName]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result[groupName] = [name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result[groupName] = [...result[groupName], name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iconGroupsMap = result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+49
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize search algorithm performance The current search implementation can be optimized:
Here's an optimized version: watch(searchName, (newVal) => {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
- iconGroupsMap = iconGroups
+ iconGroupsMap.value = iconGroups
} else {
const result = {}
for (let groupName in iconGroups) {
- const nameList = iconGroups[groupName]
- nameList.forEach((name) => {
- if (name.toLowerCase().includes(keyWord)) {
- if (!result[groupName]) {
- result[groupName] = [name]
- } else {
- result[groupName] = [...result[groupName], name]
- }
- }
- })
+ const filteredList = iconGroups[groupName].filter(name =>
+ name.toLowerCase().includes(keyWord)
+ )
+ if (filteredList.length) {
+ result[groupName] = filteredList
+ }
}
- iconGroupsMap = result
+ iconGroupsMap.value = result
}
}) This optimization:
📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<style scoped> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,17 +4,12 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<tiny-input class="search-input" v-model="searchName" clearable autofocus size="small"></tiny-input> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="svgs-wrapper"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-for="(nameList, groupName) in iconGroups" :key="groupName"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="group-name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-for="(nameList, groupName) in iconGroupsMap" :key="groupName"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div v-show="nameList.length" class="group-name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{{ groupName }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<template v-for="name in nameList"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
v-if="searchName === '' || name.toLowerCase().includes(searchName.toLowerCase())" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:key="name" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class="svgs-item" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@click="click(name)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<template v-for="name in nameList" :key="name"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div class="svgs-item" @click="click(name)"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<component :is="Svgs[name] && Svgs[name]()" class="svgs-icon"></component> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<span class="svgs-text">{{ name }}</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -47,7 +42,8 @@ export default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iconGroups, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Svgs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
searchName: '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
searchName: '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
iconGroupsMap: iconGroups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
methods: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -58,6 +54,29 @@ export default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: 'info' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
watch: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
searchName(newVal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const keyWord = (newVal || '').trim().toLowerCase() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (keyWord === '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.iconGroupsMap = iconGroups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const result = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (let groupName in iconGroups) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const nameList = iconGroups[groupName] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nameList.forEach((name) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (name.toLowerCase().includes(keyWord)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!result[groupName]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result[groupName] = [name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result[groupName] = [...result[groupName], name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.iconGroupsMap = result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+58
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize search performance and memory usage. While the search implementation is functional, there are several opportunities for optimization:
Consider these optimizations: watch: {
searchName(newVal) {
const keyWord = (newVal || '').trim().toLowerCase()
if (keyWord === '') {
this.iconGroupsMap = iconGroups
} else {
const result = {}
- for (let groupName in iconGroups) {
- const nameList = iconGroups[groupName]
- nameList.forEach((name) => {
- if (name.toLowerCase().includes(keyWord)) {
- if (!result[groupName]) {
- result[groupName] = [name]
- } else {
- result[groupName] = [...result[groupName], name]
- }
- }
- })
+ for (const [groupName, nameList] of Object.entries(iconGroups)) {
+ const filteredNames = nameList.filter(name =>
+ name.toLowerCase().includes(keyWord)
+ )
+ if (filteredNames.length) {
+ result[groupName] = filteredNames
+ }
}
this.iconGroupsMap = result
}
}
} This optimization:
📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
111111111