mirror of
https://github.com/talwrii/plugin-repl.git
synced 2026-07-22 11:40:27 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { FuzzySuggestModal, App } from 'obsidian';
|
|
|
|
|
|
|
|
export function fuzzySelect(app: App, choices: Array<string>, prompt?: string) {
|
|
return new Promise((reject, resolve) => {
|
|
let selector = new FuzzySelector(app, prompt || "select:", choices, [reject, resolve])
|
|
selector.run()
|
|
})
|
|
}
|
|
|
|
class FuzzySelector extends FuzzySuggestModal<string> {
|
|
resolve: (_: any) => void
|
|
reject: (_: any) => void
|
|
choices: Array<string>
|
|
|
|
constructor(app: App, prompt: string, choices: Array<string>, callbacks: [resolv: (_: any) => void, reject: (_: any) => void]) {
|
|
const [resolve, reject] = callbacks
|
|
super(app);
|
|
this.setPlaceholder(prompt);
|
|
this.resolve = resolve
|
|
this.reject = reject
|
|
this.choices = choices
|
|
}
|
|
|
|
getItems(): string[] {
|
|
return this.choices;
|
|
}
|
|
|
|
getItemText(choice: string): string {
|
|
return choice
|
|
}
|
|
|
|
onChooseItem(item: string): void {
|
|
this.resolve(item)
|
|
}
|
|
|
|
run() {
|
|
try {
|
|
this.open()
|
|
} catch (e) {
|
|
this.reject(e)
|
|
}
|
|
}
|
|
|
|
}
|