Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: optimize template search
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 6, 2023
1 parent 3aecfbc commit 42fe774
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
47 changes: 47 additions & 0 deletions src/components/SingleTemplateEdit.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script setup lang="ts">
import { NAutoComplete } from 'naive-ui'
import { computed } from 'vue'
import type { UseProducer } from '@/persis'
import { imgIndex } from '@/data/image'
const props = defineProps<{
value: string
edit: UseProducer<string>
}>()
const options = computed(() => {
const lowerSearch = props.value.toLowerCase().split(/ +/)
if (lowerSearch.length === 0) {
return []
}
return imgIndex.value
.map(name => ({
name,
type: lowerSearch
.map(key => (name.toLowerCase().indexOf(key) !== -1 ? 0 : 1) as number)
.reduce((a, b) => a + b, 0)
}))
.filter(({ type }) => type < 2)
.sort((a, b) => a.type - b.type)
.map(x => ({
label: x.name,
value: x.name
}))
})
</script>

<template>
<NAutoComplete
:value="value"
@update:value="
(v: string) => {
edit(() => v)
}
"
:input-props="{
autocomplete: 'disabled'
}"
:options="options"
placeholder="template"
></NAutoComplete>
</template>
7 changes: 2 additions & 5 deletions src/components/TemplateEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SingleArrayButton from './SingleArrayButton.vue'
import FloatInput from './FloatInput.vue'
import ImageHover from './ImageHover.vue'
import { type UseProducer, updateEdit } from '@/persis'
import SingleTemplateEdit from './SingleTemplateEdit.vue'
type TTemp = string | string[] | null
type TThre = number | number[] | null
Expand Down Expand Up @@ -101,11 +102,7 @@ const fixThre = (v: number) => {
>
<template #edit="{ value, edit, index }">
<div class="flex gap-2">
<NInput
:value="value"
@update:value="v => updateEdit(edit, v)"
placeholder="template"
></NInput>
<SingleTemplateEdit :value="value" :edit="edit"></SingleTemplateEdit>
<FloatInput
v-if="!isThresholdSingle"
:nullable="false"
Expand Down
13 changes: 13 additions & 0 deletions src/data/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { computed } from 'vue'
import { fs } from './fs'
import { Util } from '@/fs'

export const imgIndex = computed(() => {
const res: string[] = []
fs.now().value?.enumFile((dir, entry) => {
if (entry.name.endsWith('.png') && entry.ref) {
res.push(Util.path2zip(Util.pathjoin(dir, entry.name)))
}
})
return res
})

0 comments on commit 42fe774

Please sign in to comment.