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
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, tick } from "svelte";
3
+ import { BlockTitle, IconButton, IconButtonWrapper } from "@gradio/atoms";
4
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
5
+ import { Copy, Check, Send, Plus, Trash } from "@gradio/icons";
6
+ import { fade } from "svelte/transition";
7
+ import { BaseDropdown, BaseDropdownOptions } from "@gradio/dropdown";
8
+ import { Gradio } from "@gradio/utils";
9
+ import type { DialogueLine } from "./utils";
10
+ import Switch from "./Switch.svelte";
11
+ import type { DialogueEvents, DialogueProps } from "./types";
12
+
13
+ const props = $props();
14
+
15
+ const gradio: Gradio<DialogueEvents, DialogueProps> = props.gradio;
16
+
17
+ let checked = $state(false);
18
+ let disabled = $derived(!gradio.shared.interactive);
19
+
20
+ let dialogue_lines: DialogueLine[] = $state([]);
21
+
22
+ $effect(() => {
23
+ if (
24
+ gradio.props.value &&
25
+ gradio.props.value.length &&
26
+ typeof gradio.props.value !== "string"
27
+ ) {
28
+ dialogue_lines = [...gradio.props.value];
29
+ } else if (gradio.props.value && typeof gradio.props.value !== "string") {
30
+ dialogue_lines = [
31
+ {
32
+ speaker: `${gradio.props.speakers.length ? gradio.props.speakers[0] : ""}`,
33
+ text: ""
34
+ }
35
+ ];
36
+ } else if (typeof gradio.props.value === "string") {
37
+ textbox_value = gradio.props.value;
38
+ checked = true;
39
+ }
40
+ });
41
+
42
+ let buttons = $derived(gradio.props.buttons || ["copy"]);
43
+ let on_custom_button_click = (id: number) => {
44
+ gradio.dispatch("custom_button_click", { id });
45
+ };
46
+
47
+ let dialogue_container_element: HTMLDivElement;
48
+
49
+ let showTagMenu = $state(false);
50
+ let currentLineIndex = $state(-1);
51
+ let selectedOptionIndex = $state(0);
52
+ let filtered_tags: string[] = $state([]);
53
+ let input_elements: (HTMLInputElement | HTMLTextAreaElement)[] = $state([]);
54
+
55
+ let textarea_element: HTMLTextAreaElement;
56
+ let offset_from_top = $state(0);
57
+ let copied = $state(false);
58
+ let timer: any;
59
+ let textbox_value = $state("");
60
+ let hoveredSpeaker: string | null = $state(null);
61
+ let is_unformatting = $state(false);
62
+ let is_formatting = $state(false);
63
+
64
+ const defaultColorNames = [
65
+ "red",
66
+ "green",
67
+ "blue",
68
+ "yellow",
69
+ "purple",
70
+ "teal",
71
+ "orange",
72
+ "cyan",
73
+ "lime",
74
+ "pink"
75
+ ];
76
+
77
+ const colorNameToHex: Record<string, string> = {
78
+ red: "rgba(254, 202, 202, 0.7)",
79
+ green: "rgba(209, 250, 229, 0.7)",
80
+ blue: "rgba(219, 234, 254, 0.7)",
81
+ yellow: "rgba(254, 243, 199, 0.7)",
82
+ purple: "rgba(233, 213, 255, 0.7)",
83
+ teal: "rgba(204, 251, 241, 0.7)",
84
+ orange: "rgba(254, 215, 170, 0.7)",
85
+ cyan: "rgba(207, 250, 254, 0.7)",
86
+ lime: "rgba(217, 249, 157, 0.7)",
87
+ pink: "rgba(252, 231, 243, 0.7)"
88
+ };
89
+
90
+ let speakerColors: Record<string, string> = $derived.by(() => {
91
+ let _speakerColors: Record<string, string> = {};
92
+ if (gradio.props.color_map) {
93
+ _speakerColors = { ...gradio.props.color_map };
94
+ } else {
95
+ _speakerColors = {};
96
+ gradio.props.speakers.forEach((speaker, index) => {
97
+ const colorName = defaultColorNames[index % defaultColorNames.length];
98
+ _speakerColors[speaker] = colorNameToHex[colorName];
99
+ });
100
+ }
101
+ return _speakerColors;
102
+ });
103
+
104
+ function add_line(index: number): void {
105
+ const newSpeaker =
106
+ gradio.props.speakers.length > 0 ? gradio.props.speakers[0] : "";
107
+ dialogue_lines = [
108
+ ...dialogue_lines.slice(0, index + 1),
109
+ { speaker: newSpeaker, text: "" },
110
+ ...dialogue_lines.slice(index + 1)
111
+ ];
112
+
113
+ tick().then(() => {
114
+ if (input_elements[index + 1]) {
115
+ input_elements[index + 1].focus();
116
+ }
117
+ });
118
+ gradio.props.value = [...dialogue_lines];
119
+ }
120
+
121
+ function delete_line(index: number): void {
122
+ dialogue_lines = [
123
+ ...dialogue_lines.slice(0, index),
124
+ ...dialogue_lines.slice(index + 1)
125
+ ];
126
+ gradio.props.value = [...dialogue_lines];
127
+ }
128
+
129
+ function update_line(
130
+ index: number,
131
+ key: keyof DialogueLine,
132
+ value: string
133
+ ): void {
134
+ dialogue_lines[index][key] = value;
135
+ dialogue_lines = [...dialogue_lines];
136
+ gradio.props.value = [...dialogue_lines];
137
+ }
138
+
139
+ function handle_input(event: Event, index: number): void {
140
+ const input = (event.target as HTMLInputElement) || HTMLTextAreaElement;
141
+ if (input && !input_elements[index]) {
142
+ input_elements[index] = input;
143
+ }
144
+
145
+ const cursor_position = input.selectionStart || 0;
146
+ const text = input.value;
147
+ let show_menu = false;
148
+ let position_reference_index = -1;
149
+
150
+ if (text[cursor_position - 1] === ":") {
151
+ currentLineIndex = index;
152
+ position_reference_index = cursor_position;
153
+ const search_text = get_tag_search_text(text, cursor_position);
154
+ filtered_tags = gradio.props.tags.filter(
155
+ (tag) =>
156
+ search_text === "" ||
157
+ tag.toLowerCase().includes(search_text.toLowerCase())
158
+ );
159
+ show_menu = filtered_tags.length > 0;
160
+ selectedOptionIndex = 0;
161
+ } else {
162
+ const lastColonIndex = text.lastIndexOf(":", cursor_position - 1);
163
+ if (
164
+ lastColonIndex >= 0 &&
165
+ !text.substring(lastColonIndex + 1, cursor_position).includes(" ")
166
+ ) {
167
+ currentLineIndex = index;
168
+ position_reference_index = lastColonIndex + 1; // Position menu relative to the start of the potential tag
169
+ const searchText = text.substring(lastColonIndex + 1, cursor_position);
170
+ filtered_tags = gradio.props.tags.filter(
171
+ (tag) =>
172
+ searchText === "" ||
173
+ tag.toLowerCase().includes(searchText.toLowerCase())
174
+ );
175
+ show_menu = filtered_tags.length > 0;
176
+ selectedOptionIndex = 0;
177
+ }
178
+ }
179
+
180
+ if (show_menu && position_reference_index !== -1) {
181
+ showTagMenu = true;
182
+ const input_rect = input.getBoundingClientRect();
183
+ // Position menu below the current input by calculating the distance from the top of the container
184
+ // and use 1.5 times the input height.
185
+ if (dialogue_container_element) {
186
+ const container_rect =
187
+ dialogue_container_element.getBoundingClientRect();
188
+ offset_from_top =
189
+ container_rect.top + input_rect.height * (index + 1.5);
190
+ }
191
+ } else {
192
+ showTagMenu = false;
193
+ }
194
+ gradio.dispatch("input");
195
+ }
196
+
197
+ function get_tag_search_text(text: string, cursorPosition: number): string {
198
+ const lastColonIndex = text.lastIndexOf(":", cursorPosition - 1);
199
+ if (lastColonIndex >= 0) {
200
+ return text.substring(lastColonIndex + 1, cursorPosition);
201
+ }
202
+ return "";
203
+ }
204
+
205
+ async function insert_selected_tag(): Promise<void> {
206
+ const tag = filtered_tags[selectedOptionIndex];
207
+ if (tag) {
208
+ let text;
209
+ let currentInput;
210
+ if (checked) {
211
+ currentInput = textarea_element;
212
+ text = textbox_value;
213
+ } else {
214
+ currentInput = input_elements[currentLineIndex];
215
+ text = dialogue_lines[currentLineIndex].text;
216
+ }
217
+ const cursorPosition = currentInput?.selectionStart || 0;
218
+ const lastColonIndex = text.lastIndexOf(":", cursorPosition - 1);
219
+ if (lastColonIndex >= 0) {
220
+ const beforeColon = text.substring(0, lastColonIndex);
221
+ const afterCursor = text.substring(cursorPosition);
222
+
223
+ if (checked) {
224
+ // plain text mode: don't filter speaker tags
225
+ const newText = `${beforeColon}${tag} ${afterCursor}`;
226
+ textbox_value = newText;
227
+ if (gradio.props.speakers.length === 0) {
228
+ gradio.props.value = newText;
229
+ } else {
230
+ gradio.props.value = await gradio.shared.server.unformat({
231
+ text: newText
232
+ });
233
+ }
234
+
235
+ tick().then(() => {
236
+ if (textarea_element) {
237
+ const newCursorPosition = beforeColon.length + tag.length + 1;
238
+ textarea_element.setSelectionRange(
239
+ newCursorPosition,
240
+ newCursorPosition
241
+ );
242
+ textarea_element.focus();
243
+ }
244
+ });
245
+ } else {
246
+ // dialogue line mode
247
+ const filteredBeforeColon = beforeColon
248
+ .replace(/\[S\d+\]/g, "")
249
+ .trim();
250
+ const newText = `${filteredBeforeColon}${tag} ${afterCursor}`;
251
+ update_line(currentLineIndex, "text", newText);
252
+
253
+ tick().then(() => {
254
+ const updatedInput = input_elements[currentLineIndex];
255
+ if (updatedInput) {
256
+ const newCursorPosition =
257
+ filteredBeforeColon.length + tag.length + 1;
258
+ updatedInput.setSelectionRange(
259
+ newCursorPosition,
260
+ newCursorPosition
261
+ );
262
+ updatedInput.focus();
263
+ }
264
+ });
265
+ }
266
+ }
267
+
268
+ showTagMenu = false;
269
+ selectedOptionIndex = 0;
270
+ }
271
+ }
272
+
273
+ async function insert_tag(index: any): Promise<void> {
274
+ index = parseInt(index);
275
+ const tag = gradio.props.tags[index];
276
+ if (tag) {
277
+ let text;
278
+ let currentInput;
279
+ if (checked) {
280
+ currentInput = textarea_element;
281
+ text = textbox_value;
282
+ } else {
283
+ currentInput = input_elements[currentLineIndex];
284
+ text = dialogue_lines[currentLineIndex].text;
285
+ }
286
+ const cursorPosition = currentInput?.selectionStart || 0;
287
+ const lastColonIndex = text.lastIndexOf(":", cursorPosition - 1);
288
+ if (lastColonIndex >= 0) {
289
+ const beforeColon = text.substring(0, lastColonIndex);
290
+ const afterCursor = text.substring(cursorPosition);
291
+
292
+ if (checked) {
293
+ // plain text mode: don't filter speaker tags
294
+ const newText = `${beforeColon}${tag} ${afterCursor}`;
295
+ textbox_value = newText;
296
+ if (gradio.props.speakers.length === 0) {
297
+ gradio.props.value = newText;
298
+ } else {
299
+ gradio.props.value = await gradio.shared.server.unformat({
300
+ text: newText
301
+ });
302
+ }
303
+
304
+ tick().then(() => {
305
+ if (textarea_element) {
306
+ const newCursorPosition = beforeColon.length + tag.length + 1;
307
+ textarea_element.setSelectionRange(
308
+ newCursorPosition,
309
+ newCursorPosition
310
+ );
311
+ textarea_element.focus();
312
+ }
313
+ });
314
+ } else {
315
+ // dialogue line mode
316
+ const filteredBeforeColon = beforeColon
317
+ .replace(/\[S\d+\]/g, "")
318
+ .trim();
319
+ const newText = `${filteredBeforeColon}${tag} ${afterCursor}`;
320
+ update_line(currentLineIndex, "text", newText);
321
+
322
+ tick().then(() => {
323
+ const updatedInput = input_elements[currentLineIndex];
324
+ if (updatedInput) {
325
+ const newCursorPosition =
326
+ filteredBeforeColon.length + tag.length + 1;
327
+ updatedInput.setSelectionRange(
328
+ newCursorPosition,
329
+ newCursorPosition
330
+ );
331
+ updatedInput.focus();
332
+ }
333
+ });
334
+ }
335
+ }
336
+
337
+ showTagMenu = false;
338
+ selectedOptionIndex = 0;
339
+ }
340
+ }
341
+
342
+ function handle_click_outside(event: MouseEvent): void {
343
+ if (showTagMenu) {
344
+ const target = event.target as Node;
345
+ const tagMenu = document.querySelector(
346
+ '[data-testid="dialogue-tag-menu"]'
347
+ );
348
+ if (tagMenu && !tagMenu.contains(target)) {
349
+ showTagMenu = false;
350
+ }
351
+ }
352
+ }
353
+
354
+ async function value_to_string(
355
+ value: DialogueLine[] | string
356
+ ): Promise<string> {
357
+ if (typeof value === "string") {
358
+ return value;
359
+ }
360
+ return await gradio.shared.server.format(value);
361
+ }
362
+
363
+ async function handle_copy(): Promise<void> {
364
+ if ("clipboard" in navigator) {
365
+ const text = await value_to_string(gradio.props.value);
366
+ await navigator.clipboard.writeText(text);
367
+ copy_feedback();
368
+ }
369
+ }
370
+
371
+ function copy_feedback(): void {
372
+ copied = true;
373
+ if (timer) clearTimeout(timer);
374
+ timer = setTimeout(() => {
375
+ copied = false;
376
+ }, 1000);
377
+ }
378
+
379
+ async function handle_submit(): Promise<void> {
380
+ if (checked) {
381
+ gradio.props.value = await gradio.shared.server.unformat({
382
+ text: textbox_value
383
+ });
384
+ }
385
+ gradio.dispatch("submit");
386
+ }
387
+
388
+ onMount(async () => {
389
+ if (typeof gradio.props.value === "string") {
390
+ textbox_value = gradio.props.value;
391
+ } else if (gradio.props.value && gradio.props.value.length > 0) {
392
+ const formatted = await value_to_string(gradio.props.value);
393
+ textbox_value = formatted;
394
+ } else {
395
+ textbox_value = "";
396
+ }
397
+ });
398
+ </script>
399
+
400
+ <svelte:window on:click={handle_click_outside} />
401
+
402
+ <div class:container={gradio.shared.container}>
403
+ {#if gradio.shared.show_label && (buttons.some((btn) => typeof btn === "string" && btn === "copy") || buttons.some((btn) => typeof btn !== "string"))}
404
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
405
+ {#if buttons.some((btn) => typeof btn === "string" && btn === "copy")}
406
+ <IconButton
407
+ Icon={copied ? Check : Copy}
408
+ onclick={handle_copy}
409
+ label={copied ? "Copied" : "Copy"}
410
+ />
411
+ {/if}
412
+ </IconButtonWrapper>
413
+ {/if}
414
+
415
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
416
+ >{gradio.shared.label || "Dialogue"}</BlockTitle
417
+ >
418
+ {#if gradio.props.ui_mode === "both"}
419
+ <div
420
+ class="switch-container top-switch"
421
+ class:switch-disabled={is_formatting || is_unformatting}
422
+ >
423
+ <Switch
424
+ label="Plain Text"
425
+ bind:checked
426
+ disabled={is_formatting || is_unformatting}
427
+ on:click={async (e) => {
428
+ if (!e.detail.checked) {
429
+ is_unformatting = true;
430
+ try {
431
+ gradio.props.value = await gradio.shared.server.unformat({
432
+ text: textbox_value
433
+ });
434
+ dialogue_lines = [...(gradio.props.value as DialogueLine[])];
435
+ } finally {
436
+ is_unformatting = false;
437
+ }
438
+ } else {
439
+ is_formatting = true;
440
+ try {
441
+ textbox_value = await value_to_string(dialogue_lines);
442
+ } finally {
443
+ is_formatting = false;
444
+ }
445
+ }
446
+ }}
447
+ />
448
+ </div>
449
+ {/if}
450
+ {#if !checked && gradio.props.ui_mode !== "text"}
451
+ <div
452
+ class="dialogue-container"
453
+ data-testid="dialogue-container"
454
+ bind:this={dialogue_container_element}
455
+ class:loading={is_unformatting}
456
+ >
457
+ {#if is_unformatting}
458
+ <div class="loading-overlay" transition:fade={{ duration: 200 }}>
459
+ <div class="loading-spinner"></div>
460
+ <div class="loading-text">Converting to dialogue format...</div>
461
+ </div>
462
+ {/if}
463
+ {#each dialogue_lines as line, i}
464
+ <div
465
+ class="dialogue-line"
466
+ style="--speaker-bg-color: {disabled &&
467
+ (hoveredSpeaker === null || hoveredSpeaker === line.speaker)
468
+ ? speakerColors[line.speaker] || 'transparent'
469
+ : 'transparent'}"
470
+ >
471
+ <div
472
+ class="speaker-column"
473
+ role="button"
474
+ tabindex="0"
475
+ on:mouseenter={() => disabled && (hoveredSpeaker = line.speaker)}
476
+ on:mouseleave={() => disabled && (hoveredSpeaker = null)}
477
+ >
478
+ {#if disabled}
479
+ <textarea
480
+ bind:value={line.speaker}
481
+ {disabled}
482
+ rows="1"
483
+ readonly
484
+ />
485
+ {:else}
486
+ <BaseDropdown
487
+ label=""
488
+ show_label={false}
489
+ container={true}
490
+ interactive={true}
491
+ value={line.speaker}
492
+ choices={gradio.props.speakers.map((s) => [s, s])}
493
+ on_change={(val) => update_line(i, "speaker", val as string)}
494
+ />
495
+ {/if}
496
+ </div>
497
+ <div class="text-column">
498
+ <div class="input-container">
499
+ <textarea
500
+ bind:value={line.text}
501
+ placeholder={gradio.props.placeholder}
502
+ {disabled}
503
+ on:input={(event) => handle_input(event, i)}
504
+ on:focus={(event) => handle_input(event, i)}
505
+ on:keydown={(event) => {
506
+ if (event.key === "Escape" && showTagMenu) {
507
+ showTagMenu = false;
508
+ selectedOptionIndex = 0;
509
+ event.preventDefault();
510
+ } else if (showTagMenu && currentLineIndex === i) {
511
+ if (event.key === "ArrowDown") {
512
+ selectedOptionIndex = Math.min(
513
+ selectedOptionIndex + 1,
514
+ filtered_tags.length - 1
515
+ );
516
+ event.preventDefault();
517
+ } else if (event.key === "ArrowUp") {
518
+ selectedOptionIndex = Math.max(
519
+ selectedOptionIndex - 1,
520
+ 0
521
+ );
522
+ event.preventDefault();
523
+ } else if (event.key === "Enter") {
524
+ if (filtered_tags[selectedOptionIndex]) {
525
+ insert_selected_tag();
526
+ }
527
+ event.preventDefault();
528
+ }
529
+ }
530
+ }}
531
+ bind:this={input_elements[i]}
532
+ rows="1"
533
+ ></textarea>
534
+ {#if showTagMenu && currentLineIndex === i}
535
+ <div
536
+ data-testid="dialogue-tag-menu"
537
+ class="tag-menu"
538
+ transition:fade={{ duration: 100 }}
539
+ >
540
+ <BaseDropdownOptions
541
+ choices={gradio.props.tags.map((s, i) => [s, i])}
542
+ filtered_indices={filtered_tags.map((s) =>
543
+ gradio.props.tags.indexOf(s)
544
+ )}
545
+ active_index={filtered_tags.map((s) =>
546
+ gradio.props.tags.indexOf(s)
547
+ )[selectedOptionIndex]}
548
+ show_options={true}
549
+ onchange={(e) => insert_tag(e)}
550
+ {offset_from_top}
551
+ from_top={true}
552
+ />
553
+ </div>
554
+ {/if}
555
+ </div>
556
+ </div>
557
+ {#if gradio.props.max_lines == undefined || (gradio.props.max_lines && i < gradio.props.max_lines - 1)}
558
+ <div class:action-column={i == 0} class:hidden={disabled}>
559
+ <button
560
+ class="add-button"
561
+ data-testid="dialogue-add-button-{i}"
562
+ on:click={() => add_line(i)}
563
+ aria-label="Add new line"
564
+ {disabled}
565
+ >
566
+ <Plus />
567
+ </button>
568
+ </div>
569
+ {/if}
570
+ <div class="action-column" class:hidden={disabled || i == 0}>
571
+ <button
572
+ class="delete-button"
573
+ data-testid="dialogue-delete-button-{i}"
574
+ on:click={() => delete_line(i)}
575
+ aria-label="Remove current line"
576
+ {disabled}
577
+ >
578
+ <Trash />
579
+ </button>
580
+ </div>
581
+ </div>
582
+ {/each}
583
+ </div>
584
+ {:else if checked || gradio.props.ui_mode !== "dialogue"}
585
+ <div class="textarea-container" class:loading={is_formatting}>
586
+ {#if is_formatting}
587
+ <div class="loading-overlay" transition:fade={{ duration: 200 }}>
588
+ <div class="loading-spinner"></div>
589
+ <div class="loading-text">Converting to plain text...</div>
590
+ </div>
591
+ {/if}
592
+ <textarea
593
+ data-testid="textbox"
594
+ bind:value={textbox_value}
595
+ placeholder={gradio.props.placeholder}
596
+ rows={5}
597
+ {disabled}
598
+ on:input={(event) => {
599
+ handle_input(event, 0);
600
+ gradio.props.value = textbox_value;
601
+ }}
602
+ on:focus={(event) => handle_input(event, 0)}
603
+ on:keydown={(event) => {
604
+ if (event.key === "Escape" && showTagMenu) {
605
+ showTagMenu = false;
606
+ selectedOptionIndex = 0;
607
+ event.preventDefault();
608
+ } else if (showTagMenu) {
609
+ if (event.key === "ArrowDown") {
610
+ selectedOptionIndex = Math.min(
611
+ selectedOptionIndex + 1,
612
+ filtered_tags.length - 1
613
+ );
614
+ event.preventDefault();
615
+ } else if (event.key === "ArrowUp") {
616
+ selectedOptionIndex = Math.max(selectedOptionIndex - 1, 0);
617
+ event.preventDefault();
618
+ } else if (event.key === "Enter") {
619
+ if (filtered_tags[selectedOptionIndex]) {
620
+ insert_selected_tag();
621
+ }
622
+ event.preventDefault();
623
+ }
624
+ }
625
+ }}
626
+ bind:this={textarea_element}
627
+ />
628
+ {#if showTagMenu}
629
+ <div
630
+ data-testid="dialogue-tag-menu"
631
+ class="tag-menu-plain-text"
632
+ transition:fade={{ duration: 100 }}
633
+ >
634
+ <BaseDropdownOptions
635
+ choices={gradio.props.tags.map((s, i) => [s, i])}
636
+ filtered_indices={filtered_tags.map((s) =>
637
+ gradio.props.tags.indexOf(s)
638
+ )}
639
+ active_index={filtered_tags.map((s) =>
640
+ gradio.props.tags.indexOf(s)
641
+ )[selectedOptionIndex]}
642
+ show_options={true}
643
+ on:change={(e) => insert_tag(e)}
644
+ />
645
+ </div>
646
+ {/if}
647
+ </div>
648
+ {/if}
649
+
650
+ {#if gradio.props.submit_btn && !disabled}
651
+ <div class="submit-container">
652
+ <button
653
+ class="submit-button"
654
+ data-testid="dialogue-submit-button"
655
+ on:click={handle_submit}
656
+ {disabled}
657
+ >
658
+ {#if typeof gradio.props.submit_btn === "string"}
659
+ {gradio.props.submit_btn}
660
+ {:else}
661
+ <Send />
662
+ {/if}
663
+ </button>
664
+ </div>
665
+ {/if}
666
+ </div>
667
+
668
+ <style>
669
+ div.container {
670
+ display: block;
671
+ width: 100%;
672
+ }
673
+
674
+ .input-container {
675
+ display: flex;
676
+ position: relative;
677
+ align-items: flex-end;
678
+ }
679
+
680
+ .dialogue-container {
681
+ border: none;
682
+ border-radius: var(--input-radius);
683
+ background: var(--input-background-fill);
684
+ padding: var(--spacing-md);
685
+ margin-bottom: var(--spacing-sm);
686
+ position: relative;
687
+ }
688
+
689
+ .dialogue-line {
690
+ display: flex;
691
+ align-items: center;
692
+ margin-bottom: var(--spacing-sm);
693
+ }
694
+
695
+ .dialogue-line:has(.action-column.hidden) .text-column {
696
+ margin-right: 0;
697
+ }
698
+
699
+ .speaker-column {
700
+ flex: 0 0 150px;
701
+ margin-right: var(--spacing-sm);
702
+ display: flex;
703
+ align-items: center;
704
+ }
705
+
706
+ .speaker-column textarea {
707
+ background: var(--speaker-bg-color);
708
+ transition: background-color 0.2s ease;
709
+ border: 1px solid var(--border-color-primary);
710
+ border-radius: var(--radius-sm);
711
+ padding: var(--input-padding);
712
+ color: var(--body-text-color);
713
+ font-weight: var(--input-text-weight);
714
+ font-size: var(--input-text-size);
715
+ line-height: var(--line-sm);
716
+ resize: none;
717
+ width: 100%;
718
+ box-sizing: border-box;
719
+ height: auto;
720
+ min-height: 30px;
721
+ max-height: none;
722
+ margin-top: 0px;
723
+ margin-bottom: 0px;
724
+ z-index: 1;
725
+ display: block;
726
+ position: relative;
727
+ white-space: pre-wrap;
728
+ word-wrap: break-word;
729
+ overflow-wrap: break-word;
730
+ overflow: hidden;
731
+ }
732
+
733
+ .text-column {
734
+ flex: 1;
735
+ margin-right: var(--spacing-sm);
736
+ }
737
+
738
+ .text-column textarea {
739
+ width: 100%;
740
+ padding: var(--spacing-sm);
741
+ border: 1px solid var(--border-color-primary);
742
+ border-radius: var(--radius-sm);
743
+ color: var(--body-text-color);
744
+ background: var(--speaker-bg-color);
745
+ transition: background-color 0.2s ease;
746
+ height: auto;
747
+ min-height: 30px;
748
+ max-height: none;
749
+ flex-grow: 1;
750
+ margin-top: 0px;
751
+ margin-bottom: 0px;
752
+ resize: none;
753
+ z-index: 1;
754
+ display: block;
755
+ position: relative;
756
+ padding: var(--input-padding);
757
+ width: 100%;
758
+ color: var(--body-text-color);
759
+ font-weight: var(--input-text-weight);
760
+ font-size: var(--input-text-size);
761
+ line-height: var(--line-sm);
762
+ white-space: pre-wrap;
763
+ word-wrap: break-word;
764
+ overflow-wrap: break-word;
765
+ overflow: hidden;
766
+ box-sizing: border-box;
767
+ }
768
+
769
+ .text-column textarea {
770
+ color: var(--body-text-color);
771
+ }
772
+
773
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .text-column textarea,
774
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .speaker-column textarea {
775
+ color: black;
776
+ }
777
+
778
+ textarea {
779
+ flex-grow: 1;
780
+ outline: none !important;
781
+ margin-top: 0px;
782
+ margin-bottom: 0px;
783
+ resize: none;
784
+ z-index: 1;
785
+ display: block;
786
+ position: relative;
787
+ outline: none !important;
788
+ background: var(--input-background-fill);
789
+ padding: var(--input-padding);
790
+ width: 100%;
791
+ color: var(--body-text-color);
792
+ font-weight: var(--input-text-weight);
793
+ font-size: var(--input-text-size);
794
+ line-height: var(--line-sm);
795
+ box-shadow: var(--input-shadow);
796
+ border: var(--input-border-width) solid var(--input-border-color);
797
+ border-radius: var(--input-radius);
798
+ }
799
+
800
+ textarea:disabled {
801
+ -webkit-opacity: 1;
802
+ opacity: 1;
803
+ }
804
+
805
+ textarea:focus {
806
+ box-shadow: var(--input-shadow-focus);
807
+ border-color: var(--input-border-color-focus);
808
+ background: var(--input-background-fill-focus);
809
+ }
810
+
811
+ textarea::placeholder {
812
+ color: var(--input-placeholder-color);
813
+ }
814
+
815
+ .action-column {
816
+ display: flex;
817
+ justify-content: center;
818
+ }
819
+
820
+ .add-button {
821
+ display: flex;
822
+ justify-content: center;
823
+ align-items: center;
824
+ width: 25px;
825
+ height: 25px;
826
+ border: none;
827
+ background: transparent;
828
+ cursor: pointer;
829
+ }
830
+
831
+ .delete-button {
832
+ display: flex;
833
+ justify-content: center;
834
+ align-items: center;
835
+ width: 15px;
836
+ height: 15px;
837
+ }
838
+
839
+ .add-button:hover {
840
+ color: var(--color-accent);
841
+ }
842
+
843
+ .switch-container {
844
+ display: flex;
845
+ justify-content: flex-start;
846
+ transition: opacity 0.2s ease-in-out;
847
+ }
848
+
849
+ .switch-container.switch-disabled {
850
+ opacity: 0.6;
851
+ pointer-events: none;
852
+ }
853
+
854
+ .switch-container.top-switch {
855
+ margin-bottom: var(--spacing-sm);
856
+ justify-content: flex-end;
857
+ }
858
+
859
+ .submit-container {
860
+ display: flex;
861
+ justify-content: flex-end;
862
+ margin-top: var(--spacing-sm);
863
+ }
864
+
865
+ .submit-container button[disabled] {
866
+ cursor: not-allowed;
867
+ }
868
+
869
+ .submit-button {
870
+ border: none;
871
+ text-align: center;
872
+ text-decoration: none;
873
+ font-size: 14px;
874
+ cursor: pointer;
875
+ border-radius: 15px;
876
+ min-width: 30px;
877
+ height: 30px;
878
+ flex-shrink: 0;
879
+ display: flex;
880
+ justify-content: center;
881
+ align-items: center;
882
+ background: var(--button-secondary-background-fill);
883
+ color: var(--button-secondary-text-color);
884
+ }
885
+
886
+ .submit-button:hover {
887
+ background: var(--button-secondary-background-fill-hover);
888
+ }
889
+
890
+ .submit-button:active {
891
+ box-shadow: var(--button-shadow-active);
892
+ }
893
+
894
+ .submit-button :global(svg) {
895
+ height: 22px;
896
+ width: 22px;
897
+ }
898
+
899
+ .tag-menu {
900
+ position: absolute;
901
+ width: 100%;
902
+ top: 100%;
903
+ left: 0;
904
+ }
905
+
906
+ .tag-menu-plain-text {
907
+ position: relative;
908
+ width: 100%;
909
+ }
910
+
911
+ .tag-menu-plain-text :global(.options) {
912
+ position: static !important;
913
+ width: 100% !important;
914
+ max-height: none !important;
915
+ top: auto !important;
916
+ bottom: auto !important;
917
+ }
918
+
919
+ .hidden {
920
+ display: none;
921
+ }
922
+
923
+ .loading-overlay {
924
+ position: absolute;
925
+ top: 0;
926
+ left: 0;
927
+ right: 0;
928
+ bottom: 0;
929
+ background: var(--input-background-fill);
930
+ opacity: 0.95;
931
+ backdrop-filter: blur(2px);
932
+ display: flex;
933
+ flex-direction: column;
934
+ align-items: center;
935
+ justify-content: center;
936
+ z-index: 10;
937
+ border-radius: var(--input-radius);
938
+ }
939
+
940
+ .loading-spinner {
941
+ width: 24px;
942
+ height: 24px;
943
+ border: 2px solid var(--border-color-primary);
944
+ border-top: 2px solid var(--color-accent);
945
+ border-radius: 50%;
946
+ animation: spin 1s linear infinite;
947
+ margin-bottom: var(--spacing-sm);
948
+ }
949
+
950
+ @keyframes spin {
951
+ 0% {
952
+ transform: rotate(0deg);
953
+ }
954
+ 100% {
955
+ transform: rotate(360deg);
956
+ }
957
+ }
958
+
959
+ .loading-text {
960
+ color: var(--body-text-color);
961
+ font-size: var(--text-sm);
962
+ font-weight: 500;
963
+ opacity: 0.8;
964
+ }
965
+
966
+ .dialogue-container.loading,
967
+ .textarea-container.loading {
968
+ position: relative;
969
+ opacity: 0.7;
970
+ transition: opacity 0.3s ease-in-out;
971
+ }
972
+
973
+ .dialogue-container,
974
+ .textarea-container {
975
+ transition: opacity 0.3s ease-in-out;
976
+ }
977
+ </style>
6.13.1/dialogue/Example.svelte ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ </script>
6
+
7
+ <div
8
+ class:table={type === "table"}
9
+ class:gallery={type === "gallery"}
10
+ class:selected
11
+ >
12
+ {value}
13
+ </div>
14
+
15
+ <style>
16
+ .gallery {
17
+ padding: var(--size-1) var(--size-2);
18
+ }
19
+ </style>
6.13.1/dialogue/Index.svelte ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script context="module" lang="ts">
4
+ export { default as BaseDialogue } from "./Dialogue.svelte";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import { Gradio } from "@gradio/utils";
10
+ import type { DialogueEvents, DialogueProps } from "./types";
11
+ import Dialogue from "./Dialogue.svelte";
12
+ import { Block } from "@gradio/atoms";
13
+ import { StatusTracker } from "@gradio/statustracker";
14
+
15
+ const props = $props();
16
+ const gradio = new Gradio<DialogueEvents, DialogueProps>(props);
17
+
18
+ let mounted = $state(false);
19
+ let old_value = gradio.props.value;
20
+
21
+ $effect(() => {
22
+ if (!mounted) {
23
+ // @ts-ignore
24
+ old_value = gradio.props.value;
25
+ mounted = true;
26
+ }
27
+ // @ts-ignore
28
+ if (JSON.stringify(old_value) != JSON.stringify(gradio.props.value)) {
29
+ // @ts-ignore
30
+ old_value = gradio.props.value;
31
+ // @ts-ignore
32
+ gradio.dispatch("change");
33
+ }
34
+ });
35
+ </script>
36
+
37
+ <Block
38
+ visible={gradio.shared.visible}
39
+ elem_id={gradio.shared.elem_id}
40
+ elem_classes={gradio.shared.elem_classes}
41
+ scale={gradio.shared.scale}
42
+ min_width={gradio.shared.min_width}
43
+ allow_overflow={true}
44
+ padding={gradio.shared.container}
45
+ >
46
+ {#if gradio.shared.loading_status}
47
+ <StatusTracker
48
+ autoscroll={gradio.shared.autoscroll}
49
+ i18n={gradio.i18n}
50
+ {...gradio.shared.loading_status}
51
+ on_clear_status={() =>
52
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
53
+ />
54
+ {/if}
55
+ <Dialogue {gradio} />
56
+ </Block>
6.13.1/dialogue/Switch.svelte ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ // based on https://svelte.dev/playground/d65a4e9f0ae74d1eb1b08d13e428af32?version=3.35.0
3
+
4
+ import { createEventDispatcher } from "svelte";
5
+ export let label;
6
+ export let checked = false;
7
+ export let disabled = false;
8
+ const dispatch = createEventDispatcher();
9
+ // @ts-ignore
10
+ function handleClick(event: Event): void {
11
+ const target = event.target;
12
+ // @ts-ignore
13
+ const state = target.getAttribute("aria-checked");
14
+ checked = state === "true" ? false : true;
15
+ dispatch("click", { checked });
16
+ }
17
+ </script>
18
+
19
+ <div class="s s--slider" style="font-size:var(--font-size-sm)px">
20
+ <button
21
+ role="switch"
22
+ aria-checked={checked}
23
+ on:click={handleClick}
24
+ {disabled}
25
+ >
26
+ </button>
27
+ <span>{label}</span>
28
+ </div>
29
+
30
+ <style>
31
+ .s--slider {
32
+ display: flex;
33
+ align-items: center;
34
+ }
35
+
36
+ .s--slider button {
37
+ width: 3em;
38
+ height: 1.6em;
39
+ position: relative;
40
+ margin: 0 0.5em 0 0;
41
+ background: var(--button-secondary-background-fill);
42
+ border: none;
43
+ }
44
+
45
+ .s--slider button[disabled] {
46
+ cursor: not-allowed;
47
+ }
48
+
49
+ .s--slider button::before {
50
+ content: "";
51
+ position: absolute;
52
+ width: 1.3em;
53
+ height: 1.3em;
54
+ background: #fff;
55
+ top: 0.13em;
56
+ right: 1.5em;
57
+ transition: transform 0.3s;
58
+ }
59
+
60
+ .s--slider button[aria-checked="true"] {
61
+ background-color: var(--color-accent);
62
+ }
63
+
64
+ .s--slider button[aria-checked="true"]::before {
65
+ transform: translateX(1.3em);
66
+ transition: transform 0.3s;
67
+ }
68
+
69
+ .s--slider button:focus {
70
+ box-shadow: 0 0px 0px 1px var(--color-accent);
71
+ }
72
+
73
+ /* Slider Design Option */
74
+ .s--slider button {
75
+ border-radius: 1.5em;
76
+ }
77
+
78
+ .s--slider button::before {
79
+ border-radius: 100%;
80
+ }
81
+
82
+ .s--slider button:focus {
83
+ box-shadow: 0 0px 8px var(--accent-color);
84
+ border-radius: 1.5em;
85
+ }
86
+ </style>
6.13.1/dialogue/main.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import { default as Index } from "./Index.svelte";
2
+ export default Index;
6.13.1/dialogue/package.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dialogue",
3
+ "version": "0.3.9",
4
+ "description": "Gradio dialogue component",
5
+ "type": "module",
6
+ "author": "Gradio",
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
+ "@gradio/dropdown": "workspace:^",
29
+ "@gradio/textbox": "workspace:^"
30
+ },
31
+ "devDependencies": {
32
+ "@gradio/preview": "workspace:^"
33
+ },
34
+ "peerDependencies": {
35
+ "svelte": "^5.48.0"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/gradio-app/gradio.git",
40
+ "directory": "js/dialogue"
41
+ }
42
+ }
6.13.1/dialogue/types.ts ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { CustomButton } from "@gradio/utils";
2
+
3
+ export interface DialogueProps {
4
+ value: { speaker: string; text: string }[] | string;
5
+ buttons: (string | CustomButton)[];
6
+ ui_mode: "dialogue" | "text" | "both";
7
+ max_lines: number;
8
+ tags: string[];
9
+ separator: string;
10
+ color_map: { [key: string]: string };
11
+ speakers: string[];
12
+ info: string;
13
+ placeholder: string;
14
+ submit_btn: string | boolean;
15
+ }
16
+
17
+ export interface DialogueEvents {
18
+ change: never;
19
+ input: never;
20
+ submit: never;
21
+ clear_status: any;
22
+ custom_button_click: { id: number };
23
+ }
6.13.1/dialogue/utils.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ export interface DialogueLine {
2
+ speaker: string;
3
+ text: string;
4
+ }