Skip to content

Commit a0eba0a

Browse files
feat: add fuzzy search
1 parent ae9eb6b commit a0eba0a

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"electron-store": "^8.0.1",
5757
"floating-vue": "^2.0.0-beta.17",
5858
"fs-extra": "^10.0.1",
59+
"fuse.js": "^7.0.0",
5960
"highlight.js": "^11.5.1",
6061
"i18next": "^21.8.14",
6162
"i18next-fs-backend": "^1.1.4",

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/components/editor/EditorCodemirror.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ const init = async () => {
189189
}
190190
})
191191
192-
if (snippetStore.searchQuery) {
193-
findAll(snippetStore.searchQuery)
192+
if (snippetStore.searchQueryEscaped) {
193+
findAll(snippetStore.searchQueryEscaped)
194194
}
195195
}
196196
@@ -303,8 +303,8 @@ watch(
303303
() => {
304304
setValue(props.modelValue)
305305
306-
if (snippetStore.searchQuery) {
307-
findAll(snippetStore.searchQuery)
306+
if (snippetStore.searchQueryEscaped) {
307+
findAll(snippetStore.searchQueryEscaped)
308308
}
309309
310310
clearHistory()
@@ -319,7 +319,7 @@ watch(
319319
)
320320
321321
watch(
322-
() => snippetStore.searchQuery,
322+
() => snippetStore.searchQueryEscaped,
323323
v => {
324324
if (v) {
325325
findAll(v)

src/renderer/store/snippets.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
} from '@shared/types/renderer/store/snippets'
1818
import { useTagStore } from './tags'
1919
import { useAppStore } from './app'
20-
import { uniqBy } from 'lodash'
20+
import Fuse from 'fuse.js'
2121

2222
export const useSnippetStore = defineStore('snippets', {
2323
state: (): State => ({
@@ -27,6 +27,7 @@ export const useSnippetStore = defineStore('snippets', {
2727
selectedMultiple: [],
2828
fragment: 0,
2929
searchQuery: undefined,
30+
searchQueryEscaped: undefined,
3031
sort: 'updatedAt',
3132
hideSubfolderSnippets: false,
3233
compactMode: false,
@@ -304,17 +305,33 @@ export const useSnippetStore = defineStore('snippets', {
304305
await this.deleteSnippetsByIds(ids)
305306
},
306307
search (query: string) {
307-
const byName = this.all.filter(i =>
308-
i.name.toLowerCase().includes(query.toLowerCase())
309-
)
308+
let q = query
309+
let isExactSearch = false
310310

311-
const byContent = this.all.filter(i => {
312-
return i.content.some(c =>
313-
c.value.toLowerCase().includes(query.toLowerCase())
314-
)
311+
// обрезка кавычек для точного поиска
312+
if (query.startsWith('"') && query.endsWith('"')) {
313+
isExactSearch = true
314+
q = query.slice(1, -1)
315+
}
316+
317+
this.searchQueryEscaped = q
318+
319+
if (!q) {
320+
this.setSnippetsByAlias('all')
321+
this.searchQueryEscaped = undefined
322+
return
323+
}
324+
325+
const fuse = new Fuse(this.all, {
326+
includeScore: true,
327+
threshold: isExactSearch ? 0 : 0.4,
328+
ignoreLocation: true,
329+
keys: ['name', 'content.value']
315330
})
316331

317-
this.snippets = uniqBy([...byName, ...byContent], 'id')
332+
const result = fuse.search(q)
333+
334+
this.snippets = result.map(i => i.item)
318335
this.selected = this.snippets[0]
319336
},
320337
setSort (sort: SnippetsSort) {

src/shared/types/renderer/store/snippets.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface State {
1111
selectedMultiple: Snippet[]
1212
fragment: number
1313
searchQuery?: string
14+
searchQueryEscaped?: string
1415
sort: SnippetsSort
1516
hideSubfolderSnippets: boolean
1617
compactMode: boolean

0 commit comments

Comments
 (0)