-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder-suggest.ts
40 lines (35 loc) · 1.03 KB
/
folder-suggest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { TAbstractFile, TFolder } from "obsidian";
import fuzzysort from "fuzzysort";
import { highlightSearch } from "src/utils/misc";
import { TextInputSuggest } from "src/utils/suggest";
export class FolderSuggest extends TextInputSuggest<
Fuzzysort.KeyResult<TFolder>
> {
getSuggestions(inputStr: string): Fuzzysort.KeyResult<TFolder>[] {
const abstractFiles = this.app.vault.getAllLoadedFiles();
const folders: TFolder[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();
abstractFiles.forEach((folder: TAbstractFile) => {
if (
folder instanceof TFolder &&
folder.path.toLowerCase().contains(lowerCaseInputStr)
) {
folders.push(folder);
}
});
return fuzzysort.go(lowerCaseInputStr, abstractFiles, {
key: "path",
}) as any;
}
renderSuggestion(
file: Fuzzysort.KeyResult<TFolder>,
el: HTMLElement
): void {
highlightSearch(el, file);
}
selectSuggestion(file: Fuzzysort.KeyResult<TFolder>): void {
this.inputEl.value = file?.obj?.path;
this.inputEl.trigger("input");
this.close();
}
}