tweak(search): switch back to fuse.js

This commit is contained in:
ycnmhd 2025-04-16 20:16:35 +01:00
parent 98bd89c237
commit eb8eb0472e
No known key found for this signature in database
GPG key ID: E38ED8B2B6559A4E
4 changed files with 35 additions and 27 deletions

13
package-lock.json generated
View file

@ -1,16 +1,17 @@
{
"name": "lineage",
"version": "0.8.3",
"version": "0.8.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "lineage",
"version": "0.8.3",
"version": "0.8.4",
"license": "MIT",
"dependencies": {
"classnames": "2.5.1",
"diff": "5.2.0",
"fuse.js": "7.0.0",
"lucide-svelte": "0.344.0",
"monkey-around": "3.0.0",
"nanoid": "5.0.6",
@ -3555,6 +3556,14 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/fuse.js": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/fuse.js/-/fuse.js-7.0.0.tgz",
"integrity": "sha512-14F4hBIxqKvD4Zz/XjDc3y94mNZN6pRv3U13Udo0lNLCWRBUsrMv2xwcF/y/Z5sV6+FQW+/ow68cHpm4sunt8Q==",
"engines": {
"node": ">=10"
}
},
"node_modules/get-caller-file": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",

View file

@ -49,6 +49,7 @@
"dependencies": {
"classnames": "2.5.1",
"diff": "5.2.0",
"fuse.js": "7.0.0",
"lucide-svelte": "0.344.0",
"monkey-around": "3.0.0",
"nanoid": "5.0.6",

View file

@ -1,5 +1,4 @@
import { LineageView } from 'src/view/view';
import { debounce } from 'obsidian';
export const updateActiveNodeAfterSearch = (
view: LineageView,
@ -18,7 +17,7 @@ export const updateActiveNodeAfterSearch = (
}
};
export const updateSearchResults = debounce((view: LineageView) => {
export const updateSearchResults = (view: LineageView) => {
const viewState = view.viewStore.getValue();
const query = viewState.search.query;
@ -38,4 +37,4 @@ export const updateSearchResults = debounce((view: LineageView) => {
if (previousSearchResults !== newSearchResults) {
updateActiveNodeAfterSearch(view, Array.from(results.keys()));
}
}, 100);
};

View file

@ -1,52 +1,51 @@
import { LineageView } from 'src/view/view';
import {
prepareFuzzySearch,
prepareSimpleSearch,
SearchResult,
} from 'obsidian';
import Fuse, { FuseResult } from 'fuse.js';
type SearchItem = { id: string; content: string };
export type NodeSearchResult = SearchResult;
export type NodeSearchResult = FuseResult<SearchItem>;
export class DocumentSearch {
constructor(private view: LineageView) {}
private fuse: Fuse<SearchItem> | null;
#searchTriggeredMinimap: boolean;
private index: SearchItem[] | null = null;
private updateIndex = () => {
const documentState = this.view.documentStore.getValue();
this.index = [];
const viewState = this.view.viewStore.getValue();
const items: { id: string; content: string }[] = [];
for (const id of Object.keys(documentState.document.content)) {
const content = documentState.document.content[id]?.content;
if (content) {
this.index.push({
items.push({
id,
content,
});
}
}
this.fuse = new Fuse(items, {
keys: ['content'],
threshold: viewState.search.fuzzySearch ? 0.4 : 0,
shouldSort: true,
isCaseSensitive: false,
ignoreLocation: true,
});
};
resetIndex = () => {
this.index = null;
this.fuse = null;
};
search = (query: string) => {
if (!this.index || this.index.length === 0) {
if (!this.fuse) {
this.updateIndex();
}
const viewState = this.view.viewStore.getValue();
const searchUtil = viewState.search.fuzzySearch
? prepareFuzzySearch
: prepareSimpleSearch;
const results: Map<string, NodeSearchResult> = new Map();
for (const item of this.index!) {
const result = searchUtil(query)(item.content);
if (result) {
results.set(item.id, result);
}
const results = this.fuse!.search(query);
const map: Map<string, NodeSearchResult> = new Map();
for (const result of results) {
map.set(result.item.id, result);
}
return results;
return map;
};
get searchTriggeredMinimap() {