forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuickPickInput.svelte
352 lines (321 loc) · 11.5 KB
/
QuickPickInput.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<script lang="ts">
import { onDestroy, onMount, tick } from 'svelte';
import type { InputBoxOptions, QuickPickOptions } from './quickpick-input';
import Markdown from '/@/lib/markdown/Markdown.svelte';
const DEFAULT_PROMPT = "Press 'Enter' to confirm your input or 'Escape' to cancel";
let inputValue = '';
let placeHolder = '';
let prompt = '';
let markdownDescription = '';
let currentId = 0;
let title = '';
let multiline = false;
let validationEnabled = false;
let validationError = '';
let onSelectCallbackEnabled = false;
let display = false;
let mode: 'InputBox' | 'QuickPick';
let quickPickItems: { value: any; description: string; detail: string; checkbox: boolean }[] = [];
let quickPickFilteredItems: { value: any; description: string; detail: string; checkbox: boolean }[] = quickPickItems;
let quickPickSelectedIndex = 0;
let quickPickSelectedFilteredIndex = 0;
let quickPickCanPickMany = false;
let inputElement: HTMLInputElement | HTMLTextAreaElement = undefined;
let outerDiv: HTMLDivElement = undefined;
const showInputCallback = async (options?: InputBoxOptions) => {
mode = 'InputBox';
inputValue = options.value;
placeHolder = options.placeHolder;
title = options.title;
currentId = options.id;
if (options.prompt) {
prompt = `${options.prompt} (${DEFAULT_PROMPT})`;
} else {
prompt = DEFAULT_PROMPT;
}
markdownDescription = options.markdownDescription;
multiline = options.multiline;
validationEnabled = options.validate;
display = true;
await tick();
if (display && inputElement) {
inputElement.focus();
if (options.valueSelection) {
inputElement.setSelectionRange(options.valueSelection[0], options.valueSelection[1]);
}
}
};
const showQuickPickCallback = async (options?: QuickPickOptions) => {
mode = 'QuickPick';
placeHolder = options.placeHolder;
title = options.title;
currentId = options.id;
if (options.prompt) {
prompt = options.prompt;
}
quickPickItems = options.items.map(item => {
if (typeof item === 'string') {
return { value: item, description: '', detail: '', checkbox: false };
} else {
// if type is QuickPickItem use label field for the display
return { value: item.label, description: item.description, detail: item.detail, checkbox: false };
}
});
quickPickFilteredItems = quickPickItems;
if (options.canPickMany) {
quickPickCanPickMany = true;
}
if (options.onSelectCallback) {
onSelectCallbackEnabled = true;
// if there is one item, notify that focus will be on it
if (quickPickItems.length > 0) {
window.sendShowQuickPickOnSelect(currentId, 0);
}
}
display = true;
await tick();
if (display && inputElement) {
inputElement.focus();
}
};
onMount(() => {
// handle the showInputBox events
window.events?.receive('showInputBox:add', showInputCallback);
// handle the showInputBox events
window.events?.receive('showQuickPick:add', showQuickPickCallback);
});
async function onInputChange(event: any) {
// in case of quick pick, filter the items
if (mode === 'QuickPick') {
quickPickFilteredItems = quickPickItems.filter(item => item.value.includes(event.target.value));
quickPickSelectedFilteredIndex = 0;
if (quickPickFilteredItems.length > 0) {
quickPickSelectedIndex = quickPickItems.indexOf(quickPickFilteredItems[quickPickSelectedFilteredIndex]);
}
return;
}
// skip validation if it is not enabled
if (!validationEnabled) {
return;
}
validationError = undefined;
const value = event.target.value;
const result = await window.sendShowInputBoxValidate(currentId, value);
if (result !== undefined && result !== null && result) {
validationError = result.toString();
}
}
onDestroy(() => {
cleanup();
});
function cleanup() {
display = false;
inputValue = '';
placeHolder = '';
prompt = '';
validationEnabled = false;
onSelectCallbackEnabled = false;
quickPickCanPickMany = false;
quickPickSelectedFilteredIndex = 0;
quickPickSelectedIndex = 0;
}
function validateQuickPick() {
if (mode === 'InputBox') {
// needs to convert the index from the filtered index to the original index
const originalIndex = quickPickItems.indexOf(quickPickFilteredItems[quickPickSelectedIndex]);
window.sendShowQuickPickValues(currentId, [originalIndex]);
} else {
// grab all selected items
if (quickPickCanPickMany) {
const selectedItems = quickPickItems.filter(item => item.checkbox);
window.sendShowQuickPickValues(
currentId,
selectedItems.map(item => quickPickItems.indexOf(item)),
);
} else {
window.sendShowQuickPickValues(currentId, [quickPickSelectedIndex]);
}
}
cleanup();
}
function clickQuickPickItem(item: any, index: number) {
if (quickPickCanPickMany) {
// reset index as we clicked
quickPickSelectedFilteredIndex = -1;
item.checkbox = !item.checkbox;
quickPickItems = [...quickPickItems];
quickPickFilteredItems = [...quickPickFilteredItems];
} else {
// select the index from the cursor
quickPickSelectedIndex = quickPickItems.indexOf(quickPickFilteredItems[index]);
validateQuickPick();
}
}
function handleKeydown(e: KeyboardEvent) {
if (e.key === 'Escape') {
// In case of validating error, do not proceed
if (validationError) {
e.preventDefault();
return;
}
if (mode === 'QuickPick') {
window.sendShowQuickPickValues(currentId, []);
} else if (mode === 'InputBox') {
window.sendShowInputBoxValue(currentId, undefined, undefined);
}
cleanup();
e.preventDefault();
return;
} else if (e.key === 'Enter') {
if (mode === 'InputBox') {
// In case of validating error, do not proceed
if (validationError) {
e.preventDefault();
return;
}
window.sendShowInputBoxValue(currentId, inputValue, undefined);
cleanup();
e.preventDefault();
return;
} else if (mode === 'QuickPick') {
validateQuickPick();
e.preventDefault();
return;
}
} else if (e.key === ' ') {
if (mode === 'QuickPick') {
if (quickPickCanPickMany) {
// if space is pressed, toggle the item
const originalIndex = quickPickItems.indexOf(quickPickFilteredItems[quickPickSelectedFilteredIndex]);
quickPickItems[originalIndex].checkbox = !quickPickItems[originalIndex].checkbox;
quickPickFilteredItems = quickPickItems;
e.preventDefault();
return;
}
}
}
if (mode === 'QuickPick') {
if (e.key === 'ArrowDown') {
// if down key is pressed, move the index
quickPickSelectedFilteredIndex++;
if (quickPickSelectedFilteredIndex >= quickPickFilteredItems.length) {
quickPickSelectedFilteredIndex = 0;
}
quickPickSelectedIndex = quickPickItems.indexOf(quickPickFilteredItems[quickPickSelectedFilteredIndex]);
e.preventDefault();
if (onSelectCallbackEnabled) {
window.sendShowQuickPickOnSelect(currentId, quickPickSelectedIndex);
}
return;
} else if (e.key === 'ArrowUp') {
// if down key is pressed, move the index
quickPickSelectedFilteredIndex--;
if (quickPickSelectedFilteredIndex < 0) {
quickPickSelectedFilteredIndex = quickPickItems.length - 1;
}
quickPickSelectedIndex = quickPickItems.indexOf(quickPickFilteredItems[quickPickSelectedFilteredIndex]);
if (onSelectCallbackEnabled) {
window.sendShowQuickPickOnSelect(currentId, quickPickSelectedIndex);
}
e.preventDefault();
return;
}
}
}
function handleMousedown(e: MouseEvent) {
if (outerDiv && !e.defaultPrevented && e.target instanceof Node && !outerDiv.contains(e.target)) {
window.sendShowQuickPickValues(currentId, []);
cleanup();
}
}
</script>
<svelte:window on:keydown="{handleKeydown}" on:mousedown="{handleMousedown}"/>
{#if display}
<!-- Create overlay-->
<div class="fixed top-0 left-0 right-0 bottom-0 bg-black bg-opacity-60 h-full z-40"></div>
<div class="absolute m-auto left-0 right-0 z-50">
<div class="flex justify-center items-center mt-1">
<div bind:this={outerDiv}
class="bg-charcoal-800 w-[700px] {mode === 'InputBox'
? 'h-fit'
: ''} shadow-sm p-2 rounded shadow-zinc-700 text-sm">
{#if title}
<div
aria-label="title"
class="w-full bg-charcoal-600 rounded-sm text-center max-w-[700px] truncate cursor-default">
{title}
</div>
{/if}
<div class="w-full flex flex-row">
{#if multiline}
<textarea
bind:this="{inputElement}"
on:input="{event => onInputChange(event)}"
bind:value="{inputValue}"
class="px-1 w-full h-20 text-gray-400 bg-zinc-700 border {validationError
? 'border-red-700'
: 'border-charcoal-600'} focus:outline-none"
placeholder="{placeHolder}"></textarea>
{:else}
<input
bind:this="{inputElement}"
on:input="{event => onInputChange(event)}"
type="text"
bind:value="{inputValue}"
class="px-1 w-full text-gray-400 bg-zinc-700 border {validationError
? 'border-red-700'
: 'border-charcoal-600'} focus:outline-none"
placeholder="{placeHolder}" />
{/if}
{#if quickPickCanPickMany}
<button
on:click="{() => validateQuickPick()}"
class="text-gray-400 bg-violet-600 border border-charcoal-600 focus:outline-none px-1">OK</button>
{/if}
</div>
{#if mode === 'InputBox'}
{#if validationError}
<div class="text-gray-400 border border-red-700 relative w-full bg-red-700 px-1">{validationError}</div>
{:else}
<div class="relative text-gray-400 pt-2 px-1 h-7 overflow-y-auto">{prompt}</div>
{#if markdownDescription?.length > 0}
<div class="relative text-gray-400 pt-2 px-1 h-fit overflow-y-auto">
<Markdown>{markdownDescription}</Markdown>
</div>
{/if}
{/if}
{:else if mode === 'QuickPick'}
{#each quickPickFilteredItems as item, i}
<div
class="flex w-full flex-row {i === quickPickSelectedFilteredIndex
? 'bg-violet-500'
: 'hover:bg-charcoal-600'} ">
{#if quickPickCanPickMany}
<input type="checkbox" class="mx-1 outline-none" bind:checked="{item.checkbox}" />
{/if}
<button
on:click="{() => clickQuickPickItem(item, i)}"
class="text-gray-400 text-left relative my-1 w-full {i === quickPickSelectedFilteredIndex
? 'bg-violet-500'
: ''} px-1">
<div class="flex flex-col w-full">
<!-- first row is Value + optional description-->
<div class="flex flex-row w-full max-w-[700px] truncate">
<div class="font-bold">{item.value}</div>
{#if item.description}
<div class="text-gray-400 text-xs ml-2">{item.description}</div>
{/if}
</div>
<!-- second row is optional detail -->
{#if item.detail}
<div class="w-full max-w-[700px] truncate text-gray-400 text-xs">{item.detail}</div>
{/if}
</div>
</button>
</div>
{/each}
{/if}
</div>
</div>
</div>
{/if}