|
1 | 1 | import { LineageView } from 'src/view/view'; |
2 | | -import { FuseResult } from 'fuse.js'; |
| 2 | +import { debounce } from 'obsidian'; |
3 | 3 |
|
4 | | -const updateActiveNodeAfterSearch = ( |
| 4 | +export const updateActiveNodeAfterSearch = ( |
5 | 5 | view: LineageView, |
6 | | - results: FuseResult<{ id: string; content: string }>[], |
| 6 | + results: string[], |
7 | 7 | ) => { |
| 8 | + const activeNode = view.viewStore.getValue().document.activeNode; |
8 | 9 | const shouldUpdateActiveNode = |
9 | | - results.length > 0 && |
10 | | - !results.find( |
11 | | - (r) => r.item.id === view.viewStore.getValue().document.activeNode, |
12 | | - ); |
| 10 | + results.length > 0 && !results.find((r) => r === activeNode); |
13 | 11 | if (shouldUpdateActiveNode) { |
14 | 12 | view.viewStore.dispatch({ |
15 | 13 | type: 'view/set-active-node/search', |
16 | 14 | payload: { |
17 | | - id: results[0].item.id, |
| 15 | + id: results[0], |
18 | 16 | }, |
19 | 17 | }); |
20 | 18 | } |
21 | 19 | }; |
22 | 20 |
|
23 | | -export const updateSearchResults = (view: LineageView) => { |
| 21 | +export const updateSearchResults = debounce((view: LineageView) => { |
24 | 22 | const viewState = view.viewStore.getValue(); |
25 | 23 |
|
26 | 24 | const query = viewState.search.query; |
27 | 25 | if (!query) return; |
28 | | - const search = view.documentSearch.search(query); |
29 | | - const results = search.map((r) => r.item.id); |
| 26 | + const results = view.documentSearch.search(query); |
30 | 27 | view.viewStore.dispatch({ |
31 | 28 | type: 'SEARCH/SET_RESULTS', |
32 | 29 | payload: { |
33 | 30 | results: results, |
34 | 31 | }, |
35 | 32 | }); |
36 | | - // needed in cases where the structure of the document is updated while showing search results (dnd/moving branches) |
37 | | - // in these cases, unless search results have changed, active node should be maintained |
| 33 | + |
38 | 34 | const newSearchResults = Array.from(results).sort().join(''); |
39 | | - const previousSearchResults = Array.from(viewState.search.results) |
| 35 | + const previousSearchResults = Array.from(viewState.search.results.keys()) |
40 | 36 | .sort() |
41 | 37 | .join(''); |
42 | 38 | if (previousSearchResults !== newSearchResults) { |
43 | | - updateActiveNodeAfterSearch(view, search); |
| 39 | + updateActiveNodeAfterSearch(view, Array.from(results.keys())); |
44 | 40 | } |
45 | | -}; |
| 41 | +}, 350); |
0 commit comments