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