generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfolder-suggest.ts
37 lines (32 loc) · 1.04 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
import fuzzysort from "fuzzysort";
import { TAbstractFile, TFolder } from "obsidian";
import { highlightSearch } from "src/utils/highlight-search";
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.toLocaleLowerCase();
abstractFiles.forEach((folder: TAbstractFile) => {
if (
folder instanceof TFolder &&
folder.path.toLocaleLowerCase()?.contains(lowerCaseInputStr)
) {
folders.push(folder);
}
});
return fuzzysort.go(lowerCaseInputStr, folders, {
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();
}
}