generated from tpl/obsidian-sample-plugin
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import GoodreadsSearchSuggestion from "./GoodreadsSearchSuggestion.svelte";
|
|
import { type SearchResult } from "@data-sources/Goodreads";
|
|
import { App, SuggestModal } from "obsidian";
|
|
import { mount } from "svelte";
|
|
|
|
export class GoodreadsSearchSuggestModal extends SuggestModal<SearchResult> {
|
|
constructor(
|
|
app: App,
|
|
private readonly results: SearchResult[],
|
|
private readonly onChoose: (results: SearchResult) => void
|
|
) {
|
|
super(app);
|
|
}
|
|
|
|
getSuggestions(_query: string): SearchResult[] | Promise<SearchResult[]> {
|
|
return this.results;
|
|
}
|
|
|
|
renderSuggestion(searchResult: SearchResult, el: HTMLElement): void {
|
|
mount(GoodreadsSearchSuggestion, {
|
|
target: el,
|
|
props: { searchResult },
|
|
});
|
|
}
|
|
|
|
onChooseSuggestion(
|
|
item: SearchResult,
|
|
evt: MouseEvent | KeyboardEvent
|
|
): void {
|
|
this.onChoose(item);
|
|
}
|
|
|
|
static async createAndOpen(
|
|
app: App,
|
|
results: SearchResult[]
|
|
): Promise<SearchResult | undefined> {
|
|
return new Promise((resolve, reject) => {
|
|
const modal = new GoodreadsSearchSuggestModal(
|
|
app,
|
|
results,
|
|
(results) => {
|
|
modal.close();
|
|
resolve(results);
|
|
}
|
|
);
|
|
modal.open();
|
|
});
|
|
}
|
|
}
|