Skip to content

Commit 848acd8

Browse files
committed
update prompt api
1 parent 6a29da1 commit 848acd8

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed

exampleVault/promptTest.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@
2222
// });
2323

2424

25-
const files = engine.query.files((file) => {
26-
return {
27-
label: file.name,
28-
value: file.pat,
29-
};
30-
});
25+
// const files = engine.query.files((file) => {
26+
// return {
27+
// label: file.name,
28+
// value: file.pat,
29+
// };
30+
// });
3131

32-
const ret = await engine.prompt.suggester({
33-
placeholder: 'Select a file',
34-
options: files,
32+
// const ret = await engine.prompt.suggester({
33+
// placeholder: 'Select a file',
34+
// options: files,
35+
// });
36+
37+
const ret = await engine.prompt.text({
38+
title: 'Input Something',
39+
content: 'What ever you want to type...',
3540
});
3641

42+
3743
console.log(ret);

jsEngine/api/PromptAPI.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { API } from 'jsEngine/api/API';
22
import { ButtonStyleType } from 'jsEngine/utils/Util';
33
import { SvelteModal } from 'jsEngine/api/prompts/SvelteModal';
44
import ButtonModalComponent from 'jsEngine/api/prompts/ButtonModalComponent.svelte';
5+
import InputModalComponent from 'jsEngine/api/prompts/InputModalComponent.svelte';
56
import { Suggester } from 'jsEngine/api/prompts/Suggester';
67
import { mount } from 'svelte';
78
import type { AnySvelteComponent } from 'jsEngine/utils/SvelteUtils';
@@ -56,6 +57,17 @@ export interface SuggesterOption<T> {
5657
label: string;
5758
}
5859

60+
export interface InputPromptOptions extends ModalPromptOptions {
61+
/**
62+
* Text content to display in the modal.
63+
*/
64+
content?: string;
65+
/**
66+
* The placeholder text for the input field.
67+
*/
68+
placeholder?: string;
69+
}
70+
5971
export class PromptAPI {
6072
readonly apiInstance: API;
6173

@@ -149,4 +161,88 @@ export class PromptAPI {
149161
}
150162
});
151163
}
164+
165+
/**
166+
* Prompts the user with a text input dialog.
167+
* Returns the value of the input field, or undefined if the user closes the modal.
168+
*/
169+
public text(options: InputPromptOptions): Promise<string | undefined> {
170+
return new Promise<string | undefined>((resolve, reject) => {
171+
try {
172+
new SvelteModal<AnySvelteComponent, unknown>(
173+
this.apiInstance.app,
174+
options,
175+
(modal, targetEl) => {
176+
return mount(InputModalComponent, {
177+
target: targetEl,
178+
props: {
179+
options,
180+
modal,
181+
inputType: 'text',
182+
},
183+
});
184+
},
185+
resolve as (value: unknown) => void,
186+
).open();
187+
} catch (error) {
188+
reject(error);
189+
}
190+
});
191+
}
192+
193+
/**
194+
* Prompts the user with a textarea input dialog.
195+
* Returns the value of the input field, or undefined if the user closes the modal.
196+
*/
197+
public textarea(options: InputPromptOptions): Promise<string | undefined> {
198+
return new Promise<string | undefined>((resolve, reject) => {
199+
try {
200+
new SvelteModal<AnySvelteComponent, unknown>(
201+
this.apiInstance.app,
202+
options,
203+
(modal, targetEl) => {
204+
return mount(InputModalComponent, {
205+
target: targetEl,
206+
props: {
207+
options,
208+
modal,
209+
inputType: 'textarea',
210+
},
211+
});
212+
},
213+
resolve as (value: unknown) => void,
214+
).open();
215+
} catch (error) {
216+
reject(error);
217+
}
218+
});
219+
}
220+
221+
/**
222+
* Prompts the user with a number input dialog.
223+
* Returns the value of the input field, or undefined if the user closes the modal.
224+
*/
225+
public number(options: InputPromptOptions): Promise<number | undefined> {
226+
return new Promise<number | undefined>((resolve, reject) => {
227+
try {
228+
new SvelteModal<AnySvelteComponent, unknown>(
229+
this.apiInstance.app,
230+
options,
231+
(modal, targetEl) => {
232+
return mount(InputModalComponent, {
233+
target: targetEl,
234+
props: {
235+
options,
236+
modal,
237+
inputType: 'number',
238+
},
239+
});
240+
},
241+
resolve as (value: unknown) => void,
242+
).open();
243+
} catch (error) {
244+
reject(error);
245+
}
246+
});
247+
}
152248
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script lang="ts">
2+
import Button from 'jsEngine/utils/Button.svelte';
3+
import type { InputPromptOptions } from '../PromptAPI';
4+
import type { SvelteModal } from './SvelteModal';
5+
import { ButtonStyleType } from 'jsEngine/utils/Util';
6+
7+
const {
8+
options,
9+
modal,
10+
inputType,
11+
}: {
12+
options: InputPromptOptions;
13+
modal: SvelteModal<any, unknown>;
14+
inputType: 'text' | 'number' | 'textarea';
15+
} = $props();
16+
17+
let value = $state();
18+
</script>
19+
20+
<p>{options.content}</p>
21+
22+
{#if inputType === 'textarea'}
23+
<textarea style="width: 100%; resize: vertical; height: 200px" bind:value={value} placeholder={options.placeholder ?? 'Text here...'}></textarea>
24+
{:else if inputType === 'text'}
25+
<input style="width: 100%;" type="text" bind:value={value} placeholder={options.placeholder ?? 'Text here...'} />
26+
{:else}
27+
<input style="width: 100%;" type="number" bind:value={value} placeholder={options.placeholder ?? 'Number here...'} />
28+
{/if}
29+
30+
<div class="modal-button-container">
31+
<Button variant={ButtonStyleType.PRIMARY} onclick={() => modal.submit(value)}>Submit</Button>
32+
<Button variant={ButtonStyleType.DEFAULT} onclick={() => modal.submit(undefined)}>Cancel</Button>
33+
</div>

0 commit comments

Comments
 (0)