generated from tpl/obsidian-sample-plugin
39 lines
874 B
TypeScript
39 lines
874 B
TypeScript
import { App, SuggestModal } from "obsidian";
|
|
|
|
export class TextSuggestModal extends SuggestModal<string> {
|
|
constructor(
|
|
app: App,
|
|
private readonly items: string[],
|
|
private readonly onChoose: (value: string) => void
|
|
) {
|
|
super(app);
|
|
}
|
|
|
|
getSuggestions(query: string): string[] | Promise<string[]> {
|
|
return this.items.filter((item) =>
|
|
item.toLowerCase().includes(query.toLowerCase())
|
|
);
|
|
}
|
|
|
|
renderSuggestion(value: string, el: HTMLElement): void {
|
|
el.setText(value);
|
|
}
|
|
|
|
onChooseSuggestion(item: string, evt: MouseEvent | KeyboardEvent): void {
|
|
this.onChoose(item);
|
|
}
|
|
|
|
static async createAndOpen(
|
|
app: App,
|
|
results: string[]
|
|
): Promise<string | undefined> {
|
|
return new Promise((resolve, reject) => {
|
|
const modal = new TextSuggestModal(app, results, (results) => {
|
|
modal.close();
|
|
resolve(results);
|
|
});
|
|
modal.open();
|
|
});
|
|
}
|
|
}
|