|
| 1 | +import type { API } from 'jsEngine/api/API'; |
| 2 | +import { ButtonStyleType } from 'jsEngine/utils/Util'; |
| 3 | +import { SvelteModal } from 'jsEngine/api/prompts/SvelteModal'; |
| 4 | +import { mount } from 'svelte'; |
| 5 | +import ButtonModalComponent from 'jsEngine/api/prompts/ButtonModalComponent.svelte'; |
| 6 | +import { Suggester } from 'jsEngine/api/prompts/Suggester'; |
| 7 | + |
| 8 | +export interface ModalPromptOptions { |
| 9 | + /** |
| 10 | + * The title of the modal. |
| 11 | + */ |
| 12 | + title: string; |
| 13 | + /** |
| 14 | + * A list of CSS classes to apply to the modal. |
| 15 | + */ |
| 16 | + classes?: string[]; |
| 17 | +} |
| 18 | + |
| 19 | +export interface ButtonPromptOptions<T> extends ModalPromptOptions { |
| 20 | + /** |
| 21 | + * Text content to display in the modal. |
| 22 | + */ |
| 23 | + content?: string; |
| 24 | + /** |
| 25 | + * A list of buttons to display in the modal. |
| 26 | + */ |
| 27 | + buttons: { |
| 28 | + label: string; |
| 29 | + value: T; |
| 30 | + variant?: ButtonStyleType; |
| 31 | + }[]; |
| 32 | +} |
| 33 | + |
| 34 | +export interface ConfirmPromptOptions extends ModalPromptOptions { |
| 35 | + /** |
| 36 | + * Text content to display in the modal. |
| 37 | + */ |
| 38 | + content?: string; |
| 39 | +} |
| 40 | + |
| 41 | +export interface YesNoPromptOptions extends ModalPromptOptions { |
| 42 | + /** |
| 43 | + * Text content to display in the modal. |
| 44 | + */ |
| 45 | + content?: string; |
| 46 | +} |
| 47 | + |
| 48 | +export interface SuggesterPromptOptions<T> { |
| 49 | + placeholder?: string; |
| 50 | + options: SuggesterOption<T>[]; |
| 51 | +} |
| 52 | + |
| 53 | +export interface SuggesterOption<T> { |
| 54 | + value: T; |
| 55 | + label: string; |
| 56 | +} |
| 57 | + |
| 58 | +export class PromptAPI { |
| 59 | + readonly apiInstance: API; |
| 60 | + |
| 61 | + constructor(apiInstance: API) { |
| 62 | + this.apiInstance = apiInstance; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Prompts the user with a modal containing a list of buttons. |
| 67 | + * Returns the value of the button that was clicked, or undefined if the modal was closed. |
| 68 | + */ |
| 69 | + public button<T>(options: ButtonPromptOptions<T>): Promise<T | undefined> { |
| 70 | + return new Promise<T | undefined>((resolve, reject) => { |
| 71 | + try { |
| 72 | + new SvelteModal( |
| 73 | + this.apiInstance.app, |
| 74 | + options, |
| 75 | + (modal, targetEl) => { |
| 76 | + return mount(ButtonModalComponent, { |
| 77 | + target: targetEl, |
| 78 | + props: { |
| 79 | + options, |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any |
| 81 | + modal: modal as any, |
| 82 | + }, |
| 83 | + }); |
| 84 | + }, |
| 85 | + // we cast to narrow the type, so that it is inferred correctly |
| 86 | + resolve as (value: T | undefined) => void, |
| 87 | + ).open(); |
| 88 | + } catch (error) { |
| 89 | + reject(error); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Prompts the user with a confirm/cancel dialog. |
| 96 | + * Returns true if the user confirms, false if the user cancels or otherwise closes the modal. |
| 97 | + */ |
| 98 | + public async confirm(options: ConfirmPromptOptions): Promise<boolean> { |
| 99 | + return ( |
| 100 | + (await this.button<boolean>({ |
| 101 | + ...options, |
| 102 | + buttons: [ |
| 103 | + { |
| 104 | + label: 'Confirm', |
| 105 | + value: true, |
| 106 | + variant: ButtonStyleType.PRIMARY, |
| 107 | + }, |
| 108 | + { |
| 109 | + label: 'Cancel', |
| 110 | + value: false, |
| 111 | + variant: ButtonStyleType.DEFAULT, |
| 112 | + }, |
| 113 | + ], |
| 114 | + })) ?? false |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Prompts the user with a yes/no dialog. |
| 120 | + * Returns true if the user selects yes, false if the user selects no, and undefined if the user otherwise closes the modal. |
| 121 | + */ |
| 122 | + public async yesNo(options: YesNoPromptOptions): Promise<boolean | undefined> { |
| 123 | + return await this.button<boolean>({ |
| 124 | + ...options, |
| 125 | + buttons: [ |
| 126 | + { |
| 127 | + label: 'Yes', |
| 128 | + value: true, |
| 129 | + variant: ButtonStyleType.PRIMARY, |
| 130 | + }, |
| 131 | + { |
| 132 | + label: 'No', |
| 133 | + value: false, |
| 134 | + variant: ButtonStyleType.DEFAULT, |
| 135 | + }, |
| 136 | + ], |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Prompts the user with a fuzzy finder suggester dialog. |
| 142 | + * Returns the value of the selected option, or undefined if the user closes the modal. |
| 143 | + */ |
| 144 | + public suggester<T>(options: SuggesterPromptOptions<T>): Promise<T | undefined> { |
| 145 | + return new Promise<T | undefined>((resolve, reject) => { |
| 146 | + try { |
| 147 | + new Suggester<T>(this.apiInstance.app, options, resolve).open(); |
| 148 | + } catch (error) { |
| 149 | + reject(error); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | +} |
0 commit comments