gradio-pr-bot commited on
Commit
09aaecf
·
verified ·
1 Parent(s): cdca5f7

Upload folder using huggingface_hub

Browse files
5.49.1/dropdown/Example.svelte ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string | string[] | null;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ export let choices: [string, string | number][];
6
+
7
+ let value_array = value ? (Array.isArray(value) ? value : [value]) : [];
8
+ let names = value_array
9
+ .map(
10
+ (val) =>
11
+ (
12
+ choices.find((pair) => pair[1] === val) as
13
+ | [string, string | number]
14
+ | undefined
15
+ )?.[0]
16
+ )
17
+ .filter((name) => name !== undefined);
18
+ let names_string = names.join(", ");
19
+ </script>
20
+
21
+ <div
22
+ class:table={type === "table"}
23
+ class:gallery={type === "gallery"}
24
+ class:selected
25
+ >
26
+ {names_string}
27
+ </div>
28
+
29
+ <style>
30
+ .gallery {
31
+ padding: var(--size-1) var(--size-2);
32
+ }
33
+ </style>
5.49.1/dropdown/Index.svelte ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseDropdown } from "./shared/Dropdown.svelte";
3
+ export { default as BaseDropdownOptions } from "./shared/DropdownOptions.svelte";
4
+ export { default as BaseMultiselect } from "./shared/Multiselect.svelte";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import type { Gradio, KeyUpData, SelectData } from "@gradio/utils";
10
+ import Multiselect from "./shared/Multiselect.svelte";
11
+ import Dropdown from "./shared/Dropdown.svelte";
12
+ import { Block } from "@gradio/atoms";
13
+ import { StatusTracker } from "@gradio/statustracker";
14
+ import type { LoadingStatus } from "@gradio/statustracker";
15
+
16
+ type Item = string | number;
17
+
18
+ export let label = "Dropdown";
19
+ export let info: string | undefined = undefined;
20
+ export let elem_id = "";
21
+ export let elem_classes: string[] = [];
22
+ export let visible: boolean | "hidden" = true;
23
+ export let multiselect = false;
24
+ export let value: Item | Item[] | undefined = multiselect ? [] : undefined;
25
+ export let value_is_output = false;
26
+ export let max_choices: number | null = null;
27
+ export let choices: [string, Item][];
28
+ export let show_label: boolean;
29
+ export let filterable: boolean;
30
+ export let container = true;
31
+ export let scale: number | null = null;
32
+ export let min_width: number | undefined = undefined;
33
+ export let loading_status: LoadingStatus;
34
+ export let allow_custom_value = false;
35
+ export let gradio: Gradio<{
36
+ change: never;
37
+ input: never;
38
+ select: SelectData;
39
+ blur: never;
40
+ focus: never;
41
+ key_up: KeyUpData;
42
+ clear_status: LoadingStatus;
43
+ }>;
44
+ export let interactive: boolean;
45
+ </script>
46
+
47
+ <Block
48
+ {visible}
49
+ {elem_id}
50
+ {elem_classes}
51
+ padding={container}
52
+ allow_overflow={false}
53
+ {scale}
54
+ {min_width}
55
+ >
56
+ <StatusTracker
57
+ autoscroll={gradio.autoscroll}
58
+ i18n={gradio.i18n}
59
+ {...loading_status}
60
+ on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
61
+ />
62
+
63
+ {#if multiselect}
64
+ <Multiselect
65
+ bind:value
66
+ bind:value_is_output
67
+ {choices}
68
+ {max_choices}
69
+ {label}
70
+ {info}
71
+ {show_label}
72
+ {allow_custom_value}
73
+ {filterable}
74
+ {container}
75
+ i18n={gradio.i18n}
76
+ on:change={() => gradio.dispatch("change")}
77
+ on:input={() => gradio.dispatch("input")}
78
+ on:select={(e) => gradio.dispatch("select", e.detail)}
79
+ on:blur={() => gradio.dispatch("blur")}
80
+ on:focus={() => gradio.dispatch("focus")}
81
+ on:key_up={() => gradio.dispatch("key_up")}
82
+ disabled={!interactive}
83
+ />
84
+ {:else}
85
+ <Dropdown
86
+ bind:value
87
+ bind:value_is_output
88
+ {choices}
89
+ {label}
90
+ {info}
91
+ {show_label}
92
+ {filterable}
93
+ {allow_custom_value}
94
+ {container}
95
+ on:change={() => gradio.dispatch("change")}
96
+ on:input={() => gradio.dispatch("input")}
97
+ on:select={(e) => gradio.dispatch("select", e.detail)}
98
+ on:blur={() => gradio.dispatch("blur")}
99
+ on:focus={() => gradio.dispatch("focus")}
100
+ on:key_up={(e) => gradio.dispatch("key_up", e.detail)}
101
+ disabled={!interactive}
102
+ />
103
+ {/if}
104
+ </Block>
5.49.1/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.10.5",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./example": {
17
+ "gradio": "./Example.svelte",
18
+ "svelte": "./dist/Example.svelte",
19
+ "types": "./dist/Example.svelte.d.ts"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "dependencies": {
24
+ "@gradio/atoms": "workspace:^",
25
+ "@gradio/icons": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/utils": "workspace:^"
28
+ },
29
+ "devDependencies": {
30
+ "@gradio/preview": "workspace:^"
31
+ },
32
+ "peerDependencies": {
33
+ "svelte": "^4.0.0"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/gradio-app/gradio.git",
38
+ "directory": "js/dropdown"
39
+ }
40
+ }
5.49.1/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import DropdownOptions from "./DropdownOptions.svelte";
3
+ import { createEventDispatcher, afterUpdate } from "svelte";
4
+ import { BlockTitle } from "@gradio/atoms";
5
+ import { DropdownArrow } from "@gradio/icons";
6
+ import type { SelectData, KeyUpData } from "@gradio/utils";
7
+ import { handle_filter, handle_change, handle_shared_keys } from "./utils";
8
+
9
+ type Item = string | number;
10
+
11
+ export let label: string;
12
+ export let info: string | undefined = undefined;
13
+ export let value: Item | Item[] | undefined = undefined;
14
+ let old_value: typeof value = undefined;
15
+ export let value_is_output = false;
16
+ export let choices: [string, Item][];
17
+ let old_choices: typeof choices;
18
+ export let disabled = false;
19
+ export let show_label: boolean;
20
+ export let container = true;
21
+ export let allow_custom_value = false;
22
+ export let filterable = true;
23
+
24
+ let filter_input: HTMLElement;
25
+
26
+ let show_options = false;
27
+ let choices_names: string[];
28
+ let choices_values: (string | number)[];
29
+ let input_text = "";
30
+ let old_input_text = "";
31
+ let initialized = false;
32
+
33
+ // All of these are indices with respect to the choices array
34
+ let filtered_indices: number[] = [];
35
+ let active_index: number | null = null;
36
+ // selected_index is null if allow_custom_value is true and the input_text is not in choices_names
37
+ let selected_index: number | null = null;
38
+ let old_selected_index: number | null;
39
+
40
+ const dispatch = createEventDispatcher<{
41
+ change: string | undefined;
42
+ input: undefined;
43
+ select: SelectData;
44
+ blur: undefined;
45
+ focus: undefined;
46
+ key_up: KeyUpData;
47
+ }>();
48
+
49
+ // Setting the initial value of the dropdown
50
+ if (value) {
51
+ old_selected_index = choices.map((c) => c[1]).indexOf(value as string);
52
+ selected_index = old_selected_index;
53
+ if (selected_index === -1) {
54
+ old_value = value;
55
+ selected_index = null;
56
+ } else {
57
+ [input_text, old_value] = choices[selected_index];
58
+ old_input_text = input_text;
59
+ }
60
+ set_input_text();
61
+ }
62
+
63
+ $: {
64
+ if (
65
+ selected_index !== old_selected_index &&
66
+ selected_index !== null &&
67
+ initialized
68
+ ) {
69
+ [input_text, value] = choices[selected_index];
70
+ old_selected_index = selected_index;
71
+ dispatch("select", {
72
+ index: selected_index,
73
+ value: choices_values[selected_index],
74
+ selected: true
75
+ });
76
+ }
77
+ }
78
+
79
+ $: if (JSON.stringify(old_value ?? "") !== JSON.stringify(value ?? "")) {
80
+ set_input_text();
81
+ handle_change(dispatch, value, value_is_output);
82
+ old_value = value;
83
+ }
84
+
85
+ function set_choice_names_values(): void {
86
+ choices_names = choices.map((c) => c[0]);
87
+ choices_values = choices.map((c) => c[1]);
88
+ }
89
+
90
+ $: choices, set_choice_names_values();
91
+
92
+ const is_browser = typeof window !== "undefined";
93
+
94
+ $: {
95
+ if (choices !== old_choices) {
96
+ if (!allow_custom_value) {
97
+ set_input_text();
98
+ }
99
+ old_choices = choices;
100
+ filtered_indices = handle_filter(choices, input_text);
101
+ if (!allow_custom_value && filtered_indices.length > 0) {
102
+ active_index = filtered_indices[0];
103
+ }
104
+ if (is_browser && filter_input === document.activeElement) {
105
+ show_options = true;
106
+ }
107
+ }
108
+ }
109
+
110
+ $: {
111
+ if (input_text !== old_input_text) {
112
+ filtered_indices = handle_filter(choices, input_text);
113
+ old_input_text = input_text;
114
+ if (!allow_custom_value && filtered_indices.length > 0) {
115
+ active_index = filtered_indices[0];
116
+ }
117
+ }
118
+ }
119
+
120
+ function set_input_text(): void {
121
+ set_choice_names_values();
122
+ if (value === undefined || (Array.isArray(value) && value.length === 0)) {
123
+ input_text = "";
124
+ selected_index = null;
125
+ } else if (choices_values.includes(value as string)) {
126
+ input_text = choices_names[choices_values.indexOf(value as string)];
127
+ selected_index = choices_values.indexOf(value as string);
128
+ } else if (allow_custom_value) {
129
+ input_text = value as string;
130
+ selected_index = null;
131
+ } else {
132
+ input_text = "";
133
+ selected_index = null;
134
+ }
135
+ old_selected_index = selected_index;
136
+ }
137
+
138
+ function handle_option_selected(e: any): void {
139
+ selected_index = parseInt(e.detail.target.dataset.index);
140
+ if (isNaN(selected_index)) {
141
+ // This is the case when the user clicks on the scrollbar
142
+ selected_index = null;
143
+ return;
144
+ }
145
+ show_options = false;
146
+ active_index = null;
147
+ filter_input.blur();
148
+ }
149
+
150
+ function handle_focus(e: FocusEvent): void {
151
+ filtered_indices = choices.map((_, i) => i);
152
+ show_options = true;
153
+ dispatch("focus");
154
+ }
155
+
156
+ function handle_blur(): void {
157
+ if (!allow_custom_value) {
158
+ input_text = choices_names[choices_values.indexOf(value as string)];
159
+ } else {
160
+ value = input_text;
161
+ }
162
+ show_options = false;
163
+ active_index = null;
164
+ dispatch("blur");
165
+ }
166
+
167
+ function handle_key_down(e: KeyboardEvent): void {
168
+ [show_options, active_index] = handle_shared_keys(
169
+ e,
170
+ active_index,
171
+ filtered_indices
172
+ );
173
+ if (e.key === "Enter") {
174
+ if (active_index !== null) {
175
+ selected_index = active_index;
176
+ show_options = false;
177
+ filter_input.blur();
178
+ active_index = null;
179
+ } else if (choices_names.includes(input_text)) {
180
+ selected_index = choices_names.indexOf(input_text);
181
+ show_options = false;
182
+ active_index = null;
183
+ filter_input.blur();
184
+ } else if (allow_custom_value) {
185
+ value = input_text;
186
+ selected_index = null;
187
+ show_options = false;
188
+ active_index = null;
189
+ filter_input.blur();
190
+ }
191
+ }
192
+ }
193
+
194
+ afterUpdate(() => {
195
+ value_is_output = false;
196
+ initialized = true;
197
+ });
198
+ </script>
199
+
200
+ <div class:container>
201
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
202
+
203
+ <div class="wrap">
204
+ <div class="wrap-inner" class:show_options>
205
+ <div class="secondary-wrap">
206
+ <input
207
+ role="listbox"
208
+ aria-controls="dropdown-options"
209
+ aria-expanded={show_options}
210
+ aria-label={label}
211
+ class="border-none"
212
+ class:subdued={!choices_names.includes(input_text) &&
213
+ !allow_custom_value}
214
+ {disabled}
215
+ autocomplete="off"
216
+ bind:value={input_text}
217
+ bind:this={filter_input}
218
+ on:keydown={handle_key_down}
219
+ on:keyup={(e) =>
220
+ dispatch("key_up", {
221
+ key: e.key,
222
+ input_value: input_text
223
+ })}
224
+ on:blur={handle_blur}
225
+ on:focus={handle_focus}
226
+ readonly={!filterable}
227
+ />
228
+ {#if !disabled}
229
+ <div class="icon-wrap">
230
+ <DropdownArrow />
231
+ </div>
232
+ {/if}
233
+ </div>
234
+ </div>
235
+ <DropdownOptions
236
+ {show_options}
237
+ {choices}
238
+ {filtered_indices}
239
+ {disabled}
240
+ selected_indices={selected_index === null ? [] : [selected_index]}
241
+ {active_index}
242
+ on:change={handle_option_selected}
243
+ />
244
+ </div>
245
+ </div>
246
+
247
+ <style>
248
+ .icon-wrap {
249
+ position: absolute;
250
+ top: 50%;
251
+ transform: translateY(-50%);
252
+ right: var(--size-5);
253
+ color: var(--body-text-color);
254
+ width: var(--size-5);
255
+ pointer-events: none;
256
+ }
257
+ .container {
258
+ height: 100%;
259
+ }
260
+ .container .wrap {
261
+ box-shadow: var(--input-shadow);
262
+ border: var(--input-border-width) solid var(--border-color-primary);
263
+ }
264
+
265
+ .wrap {
266
+ position: relative;
267
+ border-radius: var(--input-radius);
268
+ background: var(--input-background-fill);
269
+ }
270
+
271
+ .wrap:focus-within {
272
+ box-shadow: var(--input-shadow-focus);
273
+ border-color: var(--input-border-color-focus);
274
+ background: var(--input-background-fill-focus);
275
+ }
276
+
277
+ .wrap-inner {
278
+ display: flex;
279
+ position: relative;
280
+ flex-wrap: wrap;
281
+ align-items: center;
282
+ gap: var(--checkbox-label-gap);
283
+ padding: var(--checkbox-label-padding);
284
+ height: 100%;
285
+ }
286
+ .secondary-wrap {
287
+ display: flex;
288
+ flex: 1 1 0%;
289
+ align-items: center;
290
+ border: none;
291
+ min-width: min-content;
292
+ height: 100%;
293
+ }
294
+
295
+ input {
296
+ margin: var(--spacing-sm);
297
+ outline: none;
298
+ border: none;
299
+ background: inherit;
300
+ width: var(--size-full);
301
+ color: var(--body-text-color);
302
+ font-size: var(--input-text-size);
303
+ height: 100%;
304
+ }
305
+
306
+ input:disabled {
307
+ -webkit-text-fill-color: var(--body-text-color);
308
+ -webkit-opacity: 1;
309
+ opacity: 1;
310
+ cursor: not-allowed;
311
+ }
312
+
313
+ .subdued {
314
+ color: var(--body-text-color-subdued);
315
+ }
316
+
317
+ input[readonly] {
318
+ cursor: pointer;
319
+ }
320
+ </style>
5.49.1/dropdown/shared/DropdownOptions.svelte ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { fly } from "svelte/transition";
3
+ import { createEventDispatcher } from "svelte";
4
+ export let choices: [string, string | number][];
5
+ export let filtered_indices: number[];
6
+ export let show_options = false;
7
+ export let disabled = false;
8
+ export let selected_indices: (string | number)[] = [];
9
+ export let active_index: number | null = null;
10
+ export let remember_scroll = false;
11
+ export let offset_from_top = 0;
12
+ export let from_top = false;
13
+
14
+ let distance_from_top: number;
15
+ let distance_from_bottom: number;
16
+ let input_height: number;
17
+ let input_width: number;
18
+ let refElement: HTMLDivElement;
19
+ let listElement: HTMLUListElement;
20
+ let top: string | null, bottom: string | null, max_height: number;
21
+ let innerHeight: number;
22
+ let list_scroll_y = 0;
23
+
24
+ function calculate_window_distance(): void {
25
+ const { top: ref_top, bottom: ref_bottom } =
26
+ refElement.getBoundingClientRect();
27
+ if (from_top) {
28
+ distance_from_top = offset_from_top;
29
+ } else {
30
+ distance_from_top = ref_top;
31
+ }
32
+ distance_from_bottom = innerHeight - ref_bottom;
33
+ }
34
+
35
+ let scroll_timeout: NodeJS.Timeout | null = null;
36
+ function scroll_listener(): void {
37
+ if (!show_options) return;
38
+ if (scroll_timeout !== null) {
39
+ clearTimeout(scroll_timeout);
40
+ }
41
+
42
+ scroll_timeout = setTimeout(() => {
43
+ calculate_window_distance();
44
+ scroll_timeout = null;
45
+ }, 10);
46
+ }
47
+
48
+ function restore_last_scroll(): void {
49
+ listElement?.scrollTo?.(0, list_scroll_y);
50
+ }
51
+
52
+ $: {
53
+ if (show_options && refElement) {
54
+ if (remember_scroll) {
55
+ restore_last_scroll();
56
+ } else {
57
+ if (listElement && selected_indices.length > 0) {
58
+ let elements = listElement.querySelectorAll("li");
59
+ for (const element of Array.from(elements)) {
60
+ if (
61
+ element.getAttribute("data-index") ===
62
+ selected_indices[0].toString()
63
+ ) {
64
+ listElement?.scrollTo?.(0, (element as HTMLLIElement).offsetTop);
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ }
70
+ calculate_window_distance();
71
+ const rect = refElement.parentElement?.getBoundingClientRect();
72
+ input_height = rect?.height || 0;
73
+ input_width = rect?.width || 0;
74
+ }
75
+ if (distance_from_bottom > distance_from_top || from_top) {
76
+ top = `${distance_from_top}px`;
77
+ max_height = distance_from_bottom;
78
+ bottom = null;
79
+ } else {
80
+ bottom = `${distance_from_bottom + input_height}px`;
81
+ max_height = distance_from_top - input_height;
82
+ top = null;
83
+ }
84
+ }
85
+
86
+ const dispatch = createEventDispatcher();
87
+ </script>
88
+
89
+ <svelte:window on:scroll={scroll_listener} bind:innerHeight />
90
+
91
+ <div class="reference" bind:this={refElement} />
92
+ {#if show_options && !disabled}
93
+ <ul
94
+ class="options"
95
+ transition:fly={{ duration: 200, y: 5 }}
96
+ on:mousedown|preventDefault={(e) => dispatch("change", e)}
97
+ on:scroll={(e) => (list_scroll_y = e.currentTarget.scrollTop)}
98
+ style:top
99
+ style:bottom
100
+ style:max-height={`calc(${max_height}px - var(--window-padding))`}
101
+ style:width={input_width + "px"}
102
+ bind:this={listElement}
103
+ role="listbox"
104
+ >
105
+ {#each filtered_indices as index}
106
+ <li
107
+ class="item"
108
+ class:selected={selected_indices.includes(index)}
109
+ class:active={index === active_index}
110
+ class:bg-gray-100={index === active_index}
111
+ class:dark:bg-gray-600={index === active_index}
112
+ style:width={input_width + "px"}
113
+ data-index={index}
114
+ aria-label={choices[index][0]}
115
+ data-testid="dropdown-option"
116
+ role="option"
117
+ aria-selected={selected_indices.includes(index)}
118
+ >
119
+ <span class:hide={!selected_indices.includes(index)} class="inner-item">
120
+
121
+ </span>
122
+ {choices[index][0]}
123
+ </li>
124
+ {/each}
125
+ </ul>
126
+ {/if}
127
+
128
+ <style>
129
+ .options {
130
+ --window-padding: var(--size-8);
131
+ position: fixed;
132
+ z-index: var(--layer-top);
133
+ margin-left: 0;
134
+ box-shadow: var(--shadow-drop-lg);
135
+ border-radius: var(--container-radius);
136
+ background: var(--background-fill-primary);
137
+ min-width: fit-content;
138
+ max-width: inherit;
139
+ overflow: auto;
140
+ color: var(--body-text-color);
141
+ list-style: none;
142
+ }
143
+
144
+ .item {
145
+ display: flex;
146
+ cursor: pointer;
147
+ padding: var(--size-2);
148
+ word-break: break-word;
149
+ }
150
+
151
+ .item:hover,
152
+ .active {
153
+ background: var(--background-fill-secondary);
154
+ }
155
+
156
+ .inner-item {
157
+ padding-right: var(--size-1);
158
+ }
159
+
160
+ .hide {
161
+ visibility: hidden;
162
+ }
163
+ </style>
5.49.1/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,421 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { afterUpdate, createEventDispatcher } from "svelte";
3
+ import { _ } from "svelte-i18n";
4
+ import { BlockTitle } from "@gradio/atoms";
5
+ import { Remove, DropdownArrow } from "@gradio/icons";
6
+ import type { KeyUpData, SelectData, I18nFormatter } from "@gradio/utils";
7
+ import DropdownOptions from "./DropdownOptions.svelte";
8
+ import { handle_filter, handle_change, handle_shared_keys } from "./utils";
9
+
10
+ type Item = string | number;
11
+
12
+ export let label: string;
13
+ export let info: string | undefined = undefined;
14
+ export let value: Item | Item[] | undefined = [];
15
+ let old_value: typeof value = [];
16
+ export let value_is_output = false;
17
+ export let max_choices: number | null = null;
18
+ export let choices: [string, Item][];
19
+ let old_choices: typeof choices;
20
+ export let disabled = false;
21
+ export let show_label: boolean;
22
+ export let container = true;
23
+ export let allow_custom_value = false;
24
+ export let filterable = true;
25
+ export let i18n: I18nFormatter;
26
+
27
+ let filter_input: HTMLElement;
28
+ let input_text = "";
29
+ let old_input_text = "";
30
+ let show_options = false;
31
+ let choices_names: string[];
32
+ let choices_values: (string | number)[];
33
+
34
+ // All of these are indices with respect to the choices array
35
+ let filtered_indices: number[] = [];
36
+ let active_index: number | null = null;
37
+ // selected_index consists of indices from choices or strings if allow_custom_value is true and user types in a custom value
38
+ let selected_indices: (number | string)[] = [];
39
+ let old_selected_index: (number | string)[] = [];
40
+
41
+ const dispatch = createEventDispatcher<{
42
+ change: string | string[] | undefined;
43
+ input: undefined;
44
+ select: SelectData;
45
+ blur: undefined;
46
+ focus: undefined;
47
+ key_up: KeyUpData;
48
+ }>();
49
+
50
+ // Setting the initial value of the multiselect dropdown
51
+ if (Array.isArray(value)) {
52
+ value.forEach((element) => {
53
+ const index = choices.map((c) => c[1]).indexOf(element);
54
+ if (index !== -1) {
55
+ selected_indices.push(index);
56
+ } else {
57
+ selected_indices.push(element);
58
+ }
59
+ });
60
+ }
61
+
62
+ $: {
63
+ choices_names = choices.map((c) => c[0]);
64
+ choices_values = choices.map((c) => c[1]);
65
+ }
66
+
67
+ $: {
68
+ if (choices !== old_choices || input_text !== old_input_text) {
69
+ filtered_indices = handle_filter(choices, input_text);
70
+ old_choices = choices;
71
+ old_input_text = input_text;
72
+ if (!allow_custom_value) {
73
+ active_index = filtered_indices[0];
74
+ }
75
+ }
76
+ }
77
+
78
+ $: {
79
+ if (JSON.stringify(value) != JSON.stringify(old_value)) {
80
+ handle_change(dispatch, value, value_is_output);
81
+ old_value = Array.isArray(value) ? value.slice() : value;
82
+ }
83
+ }
84
+
85
+ $: {
86
+ if (
87
+ JSON.stringify(selected_indices) != JSON.stringify(old_selected_index)
88
+ ) {
89
+ value = selected_indices.map((index) =>
90
+ typeof index === "number" ? choices_values[index] : index
91
+ );
92
+ old_selected_index = selected_indices.slice();
93
+ }
94
+ }
95
+
96
+ function handle_blur(): void {
97
+ if (!allow_custom_value) {
98
+ input_text = "";
99
+ }
100
+
101
+ if (allow_custom_value && input_text !== "") {
102
+ add_selected_choice(input_text);
103
+ input_text = "";
104
+ }
105
+
106
+ show_options = false;
107
+ active_index = null;
108
+ dispatch("blur");
109
+ }
110
+
111
+ function remove_selected_choice(option_index: number | string): void {
112
+ selected_indices = selected_indices.filter((v) => v !== option_index);
113
+ dispatch("select", {
114
+ index: typeof option_index === "number" ? option_index : -1,
115
+ value:
116
+ typeof option_index === "number"
117
+ ? choices_values[option_index]
118
+ : option_index,
119
+ selected: false
120
+ });
121
+ }
122
+
123
+ function add_selected_choice(option_index: number | string): void {
124
+ if (max_choices === null || selected_indices.length < max_choices) {
125
+ selected_indices = [...selected_indices, option_index];
126
+ dispatch("select", {
127
+ index: typeof option_index === "number" ? option_index : -1,
128
+ value:
129
+ typeof option_index === "number"
130
+ ? choices_values[option_index]
131
+ : option_index,
132
+ selected: true
133
+ });
134
+ }
135
+ if (selected_indices.length === max_choices) {
136
+ show_options = false;
137
+ active_index = null;
138
+ filter_input.blur();
139
+ }
140
+ }
141
+
142
+ function handle_option_selected(e: any): void {
143
+ const option_index = parseInt(e.detail.target.dataset.index);
144
+ add_or_remove_index(option_index);
145
+ }
146
+
147
+ function add_or_remove_index(option_index: number): void {
148
+ if (selected_indices.includes(option_index)) {
149
+ remove_selected_choice(option_index);
150
+ } else {
151
+ add_selected_choice(option_index);
152
+ }
153
+ input_text = "";
154
+ }
155
+
156
+ function remove_all(e: any): void {
157
+ selected_indices = [];
158
+ input_text = "";
159
+ e.preventDefault();
160
+ }
161
+
162
+ function handle_focus(e: FocusEvent): void {
163
+ filtered_indices = choices.map((_, i) => i);
164
+ if (max_choices === null || selected_indices.length < max_choices) {
165
+ show_options = true;
166
+ }
167
+ dispatch("focus");
168
+ }
169
+
170
+ function handle_key_down(e: KeyboardEvent): void {
171
+ [show_options, active_index] = handle_shared_keys(
172
+ e,
173
+ active_index,
174
+ filtered_indices
175
+ );
176
+ if (e.key === "Enter") {
177
+ if (active_index !== null) {
178
+ add_or_remove_index(active_index);
179
+ } else {
180
+ if (allow_custom_value) {
181
+ add_selected_choice(input_text);
182
+ input_text = "";
183
+ }
184
+ }
185
+ }
186
+ if (e.key === "Backspace" && input_text === "") {
187
+ selected_indices = [...selected_indices.slice(0, -1)];
188
+ }
189
+ if (selected_indices.length === max_choices) {
190
+ show_options = false;
191
+ active_index = null;
192
+ }
193
+ }
194
+
195
+ function set_selected_indices(): void {
196
+ if (value === undefined) {
197
+ selected_indices = [];
198
+ } else if (Array.isArray(value)) {
199
+ selected_indices = value
200
+ .map((v) => {
201
+ const index = choices_values.indexOf(v);
202
+ if (index !== -1) {
203
+ return index;
204
+ }
205
+ if (allow_custom_value) {
206
+ return v;
207
+ }
208
+ // Instead of returning null, skip this iteration
209
+ return undefined;
210
+ })
211
+ .filter((val): val is string | number => val !== undefined);
212
+ }
213
+ }
214
+
215
+ $: value, set_selected_indices();
216
+
217
+ afterUpdate(() => {
218
+ value_is_output = false;
219
+ });
220
+ </script>
221
+
222
+ <label class:container>
223
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
224
+
225
+ <div class="wrap">
226
+ <div class="wrap-inner" class:show_options>
227
+ {#each selected_indices as s}
228
+ <div class="token">
229
+ <span>
230
+ {#if typeof s === "number"}
231
+ {choices_names[s]}
232
+ {:else}
233
+ {s}
234
+ {/if}
235
+ </span>
236
+ {#if !disabled}
237
+ <div
238
+ class="token-remove"
239
+ on:click|preventDefault={() => remove_selected_choice(s)}
240
+ on:keydown={(event) => {
241
+ if (event.key === "Enter") {
242
+ remove_selected_choice(s);
243
+ }
244
+ }}
245
+ role="button"
246
+ tabindex="0"
247
+ title={i18n("common.remove") + " " + s}
248
+ >
249
+ <Remove />
250
+ </div>
251
+ {/if}
252
+ </div>
253
+ {/each}
254
+ <div class="secondary-wrap">
255
+ <input
256
+ class="border-none"
257
+ class:subdued={(!choices_names.includes(input_text) &&
258
+ !allow_custom_value) ||
259
+ selected_indices.length === max_choices}
260
+ {disabled}
261
+ autocomplete="off"
262
+ bind:value={input_text}
263
+ bind:this={filter_input}
264
+ on:keydown={handle_key_down}
265
+ on:keyup={(e) =>
266
+ dispatch("key_up", {
267
+ key: e.key,
268
+ input_value: input_text
269
+ })}
270
+ on:blur={handle_blur}
271
+ on:focus={handle_focus}
272
+ readonly={!filterable}
273
+ />
274
+
275
+ {#if !disabled}
276
+ {#if selected_indices.length > 0}
277
+ <div
278
+ role="button"
279
+ tabindex="0"
280
+ class="token-remove remove-all"
281
+ title={i18n("common.clear")}
282
+ on:click={remove_all}
283
+ on:keydown={(event) => {
284
+ if (event.key === "Enter") {
285
+ remove_all(event);
286
+ }
287
+ }}
288
+ >
289
+ <Remove />
290
+ </div>
291
+ {/if}
292
+ <span class="icon-wrap"> <DropdownArrow /></span>
293
+ {/if}
294
+ </div>
295
+ </div>
296
+ <DropdownOptions
297
+ {show_options}
298
+ {choices}
299
+ {filtered_indices}
300
+ {disabled}
301
+ {selected_indices}
302
+ {active_index}
303
+ remember_scroll={true}
304
+ on:change={handle_option_selected}
305
+ />
306
+ </div>
307
+ </label>
308
+
309
+ <style>
310
+ .icon-wrap {
311
+ color: var(--body-text-color);
312
+ margin-right: var(--size-2);
313
+ width: var(--size-5);
314
+ }
315
+ label:not(.container),
316
+ label:not(.container) .wrap,
317
+ label:not(.container) .wrap-inner,
318
+ label:not(.container) .secondary-wrap,
319
+ label:not(.container) .token,
320
+ label:not(.container) input {
321
+ height: 100%;
322
+ }
323
+ .container .wrap {
324
+ box-shadow: var(--input-shadow);
325
+ border: var(--input-border-width) solid var(--border-color-primary);
326
+ }
327
+
328
+ .wrap {
329
+ position: relative;
330
+ border-radius: var(--input-radius);
331
+ background: var(--input-background-fill);
332
+ }
333
+
334
+ .wrap:focus-within {
335
+ box-shadow: var(--input-shadow-focus);
336
+ border-color: var(--input-border-color-focus);
337
+ }
338
+
339
+ .wrap-inner {
340
+ display: flex;
341
+ position: relative;
342
+ flex-wrap: wrap;
343
+ align-items: center;
344
+ gap: var(--checkbox-label-gap);
345
+ padding: var(--checkbox-label-padding);
346
+ }
347
+
348
+ .token {
349
+ display: flex;
350
+ align-items: center;
351
+ transition: var(--button-transition);
352
+ cursor: pointer;
353
+ box-shadow: var(--checkbox-label-shadow);
354
+ border: var(--checkbox-label-border-width) solid
355
+ var(--checkbox-label-border-color);
356
+ border-radius: var(--button-small-radius);
357
+ background: var(--checkbox-label-background-fill);
358
+ padding: var(--checkbox-label-padding);
359
+ color: var(--checkbox-label-text-color);
360
+ font-weight: var(--checkbox-label-text-weight);
361
+ font-size: var(--checkbox-label-text-size);
362
+ line-height: var(--line-md);
363
+ word-break: break-word;
364
+ }
365
+
366
+ .token > * + * {
367
+ margin-left: var(--size-2);
368
+ }
369
+
370
+ .token-remove {
371
+ fill: var(--body-text-color);
372
+ display: flex;
373
+ justify-content: center;
374
+ align-items: center;
375
+ cursor: pointer;
376
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
377
+ border-radius: var(--radius-full);
378
+ background: var(--background-fill-primary);
379
+ padding: var(--size-0-5);
380
+ width: 16px;
381
+ height: 16px;
382
+ flex-shrink: 0;
383
+ }
384
+
385
+ .secondary-wrap {
386
+ display: flex;
387
+ flex: 1 1 0%;
388
+ align-items: center;
389
+ border: none;
390
+ min-width: min-content;
391
+ }
392
+
393
+ input {
394
+ margin: var(--spacing-sm);
395
+ outline: none;
396
+ border: none;
397
+ background: inherit;
398
+ width: var(--size-full);
399
+ color: var(--body-text-color);
400
+ font-size: var(--input-text-size);
401
+ }
402
+
403
+ input:disabled {
404
+ -webkit-text-fill-color: var(--body-text-color);
405
+ -webkit-opacity: 1;
406
+ opacity: 1;
407
+ cursor: not-allowed;
408
+ }
409
+
410
+ .remove-all {
411
+ margin-left: var(--size-1);
412
+ width: 20px;
413
+ height: 20px;
414
+ }
415
+ .subdued {
416
+ color: var(--body-text-color-subdued);
417
+ }
418
+ input[readonly] {
419
+ cursor: pointer;
420
+ }
421
+ </style>
5.49.1/dropdown/shared/utils.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function positive_mod(n: number, m: number): number {
2
+ return ((n % m) + m) % m;
3
+ }
4
+
5
+ export function handle_filter(
6
+ choices: [string, string | number][],
7
+ input_text: string
8
+ ): number[] {
9
+ return choices.reduce((filtered_indices, o, index) => {
10
+ if (
11
+ input_text ? o[0].toLowerCase().includes(input_text.toLowerCase()) : true
12
+ ) {
13
+ filtered_indices.push(index);
14
+ }
15
+ return filtered_indices;
16
+ }, [] as number[]);
17
+ }
18
+
19
+ export function handle_change(
20
+ dispatch: any,
21
+ value: string | number | (string | number)[] | undefined,
22
+ value_is_output: boolean
23
+ ): void {
24
+ dispatch("change", value);
25
+ if (!value_is_output) {
26
+ dispatch("input");
27
+ }
28
+ }
29
+
30
+ export function handle_shared_keys(
31
+ e: KeyboardEvent,
32
+ active_index: number | null,
33
+ filtered_indices: number[]
34
+ ): [boolean, number | null] {
35
+ if (e.key === "Escape") {
36
+ return [false, active_index];
37
+ }
38
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
39
+ if (filtered_indices.length > 0) {
40
+ if (active_index === null) {
41
+ active_index =
42
+ e.key === "ArrowDown"
43
+ ? filtered_indices[0]
44
+ : filtered_indices[filtered_indices.length - 1];
45
+ } else {
46
+ const index_in_filtered = filtered_indices.indexOf(active_index);
47
+ const increment = e.key === "ArrowUp" ? -1 : 1;
48
+ active_index =
49
+ filtered_indices[
50
+ positive_mod(index_in_filtered + increment, filtered_indices.length)
51
+ ];
52
+ }
53
+ }
54
+ }
55
+ return [true, active_index];
56
+ }