generated from RyotaUshio/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest.ts
50 lines (42 loc) · 1.78 KB
/
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
41
42
43
44
45
46
47
48
49
50
import MathJaxPreamblePlugin from 'main';
import { SerializedPreambles } from "manager";
import { AbstractInputSuggest, TFile, TFolder } from "obsidian";
abstract class AbstractPathSuggest<T extends { path: string }> extends AbstractInputSuggest<T> {
constructor(public plugin: MathJaxPreamblePlugin, textInputEl: HTMLInputElement | HTMLDivElement) {
super(plugin.app, textInputEl);
}
renderSuggestion({ path }: T, el: HTMLElement) {
el.setText(path);
}
selectSuggestion({ path }: T) {
// super.selectSuggestion calls the callbacks registered via onSelect
// (alternatively, we can do this.selectCb?.({ path }))
// @ts-ignore
super.selectSuggestion({ path });
this.setValue(path);
// @ts-ignore
this.suggestEl.hide();
}
}
export class PreambleSuggest extends AbstractPathSuggest<{ path: string }> {
constructor(plugin: MathJaxPreamblePlugin, textInputEl: HTMLInputElement | HTMLDivElement, private serialized: SerializedPreambles) {
super(plugin, textInputEl);
}
getSuggestions(query: string) {
return this.serialized.preambles.filter(preamble => {
return preamble.path.toLowerCase().includes(query.toLowerCase())
});
}
}
export class FileSuggest extends AbstractPathSuggest<TFile> {
getSuggestions(query: string) {
return this.plugin.app.vault.getFiles().filter(file => file.path.toLowerCase().includes(query.toLowerCase()));
}
}
export class FolderSuggest extends AbstractPathSuggest<TFolder> {
getSuggestions(query: string) {
return this.plugin.app.vault
.getAllLoadedFiles()
.filter((folder): folder is TFolder => folder instanceof TFolder && folder.path.toLowerCase().includes(query.toLowerCase()));
}
}