-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
dialog.ts
49 lines (44 loc) · 1.54 KB
/
dialog.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
import { App, SuggestModal } from "obsidian";
export const askString = (app: App, title: string, placeholder: string, initialText: string): Promise<string | false> => {
return new Promise((res) => {
const popover = new PopoverSelectString(app, title, placeholder, initialText, (result) => res(result));
popover.open();
});
};
export class PopoverSelectString extends SuggestModal<string> {
app: App;
callback?: (e: string | false) => void = () => { };
title = "";
getSuggestions(query: string): string[] | Promise<string[]> {
return [query];
}
renderSuggestion(value: string, el: HTMLElement) {
el.createDiv({ text: `${this.title}${value}` });
}
onChooseSuggestion(item: string, evt: MouseEvent | KeyboardEvent) {
this.callback?.(item);
this.callback = undefined;
}
constructor(app: App, title: string, placeholder: string | null, initialText: string, callback: (e: string | false) => void) {
super(app);
this.app = app;
this.title = title;
this.setPlaceholder(placeholder ?? ">");
this.callback = callback;
setTimeout(() => {
this.inputEl.value = initialText;
// this.inputEl.onchange();
})
const parent = this.containerEl.querySelector(".prompt");
if (parent) {
parent.addClass("override-input");
}
}
onClose(): void {
setTimeout(() => {
if (this.callback) {
this.callback(false);
}
}, 100);
}
}