Skip to content

Commit

Permalink
feat: 模糊搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiaa12 committed Aug 3, 2023
1 parent f15b77e commit 987bacf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -47,6 +47,7 @@
"eslint-config-prettier": "^8.9.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vue": "^9.16.1",
"fuse.js": "^6.6.2",
"gsap": "^3.12.2",
"medium-zoom": "^1.0.8",
"nes-vue": "^1.6.10",
Expand Down
16 changes: 12 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/pages/components/game/GameList.vue
Expand Up @@ -55,6 +55,8 @@

<script setup lang="ts">
import { computed, ref, watch } from "vue"
import Fuse from "fuse.js"
import { roms } from "../game"
import InnerLoading from "./InnerLoading.vue"
Expand All @@ -66,8 +68,12 @@ const props = defineProps<{
}>()
const emits = defineEmits(["select"])
const romsFuse = new Fuse(roms, {
keys: ["title"],
})
const filterRoms = computed(() => {
if (props.keyword) return roms.filter(i => i.title.includes(props.keyword))
if (props.keyword) return romsFuse.search(props.keyword).map(i => i.item)
return !props.curCategory
? roms
: roms.filter(i => i.type === props.curCategory)
Expand Down
15 changes: 11 additions & 4 deletions src/pages/components/software.vue
Expand Up @@ -61,6 +61,8 @@

<script setup lang="ts">
import { computed, ref } from "vue"
import Fuse from "fuse.js"
import SearchCategory from "./common/SearchCategory.vue"
import list from "./software"
Expand All @@ -73,13 +75,18 @@ const cate = ref()
const filterList = computed(() => {
const temp = JSON.parse(JSON.stringify(list))
return temp.filter((i: any) => {
if (cate.value) return i.title === cate.value
if (keyword.value)
i.children = i.children.filter((i: any) => {
if (i.title.includes(keyword.value) || i.desc?.includes(keyword.value))
return i
if (keyword.value) {
const listFuse = new Fuse(i.children, {
includeScore: true,
shouldSort: false,
keys: ["title", '"desc'],
})
i.children = listFuse.search(keyword.value).map(i => i.item)
}
return i
})
})
Expand Down

0 comments on commit 987bacf

Please sign in to comment.