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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 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(index: any): Promise<void> {
283
+ index = parseInt(index);
284
+ const tag = gradio.props.tags[index];
285
+ if (tag) {
286
+ let text;
287
+ let currentInput;
288
+ if (checked) {
289
+ currentInput = textarea_element;
290
+ text = textbox_value;
291
+ } else {
292
+ currentInput = input_elements[currentLineIndex];
293
+ text = dialogue_lines[currentLineIndex].text;
294
+ }
295
+ const cursorPosition = currentInput?.selectionStart || 0;
296
+ const lastColonIndex = text.lastIndexOf(":", cursorPosition - 1);
297
+ if (lastColonIndex >= 0) {
298
+ const beforeColon = text.substring(0, lastColonIndex);
299
+ const afterCursor = text.substring(cursorPosition);
300
+
301
+ if (checked) {
302
+ // plain text mode: don't filter speaker tags
303
+ const newText = `${beforeColon}${tag} ${afterCursor}`;
304
+ textbox_value = newText;
305
+ if (gradio.props.speakers.length === 0) {
306
+ gradio.props.value = newText;
307
+ } else {
308
+ gradio.props.value = await gradio.shared.server.unformat({
309
+ text: newText
310
+ });
311
+ }
312
+
313
+ tick().then(() => {
314
+ if (textarea_element) {
315
+ const newCursorPosition = beforeColon.length + tag.length + 1;
316
+ textarea_element.setSelectionRange(
317
+ newCursorPosition,
318
+ newCursorPosition
319
+ );
320
+ textarea_element.focus();
321
+ }
322
+ });
323
+ } else {
324
+ // dialogue line mode
325
+ const filteredBeforeColon = beforeColon
326
+ .replace(/\[S\d+\]/g, "")
327
+ .trim();
328
+ const newText = `${filteredBeforeColon}${tag} ${afterCursor}`;
329
+ update_line(currentLineIndex, "text", newText);
330
+
331
+ tick().then(() => {
332
+ const updatedInput = input_elements[currentLineIndex];
333
+ if (updatedInput) {
334
+ const newCursorPosition =
335
+ filteredBeforeColon.length + tag.length + 1;
336
+ updatedInput.setSelectionRange(
337
+ newCursorPosition,
338
+ newCursorPosition
339
+ );
340
+ updatedInput.focus();
341
+ }
342
+ });
343
+ }
344
+ }
345
+
346
+ showTagMenu = false;
347
+ selectedOptionIndex = 0;
348
+ }
349
+ }
350
+
351
+ function handle_click_outside(event: MouseEvent): void {
352
+ if (showTagMenu) {
353
+ const target = event.target as Node;
354
+ const tagMenu = document.getElementById("tag-menu");
355
+ if (tagMenu && !tagMenu.contains(target)) {
356
+ showTagMenu = false;
357
+ }
358
+ }
359
+ }
360
+
361
+ async function value_to_string(
362
+ value: DialogueLine[] | string
363
+ ): Promise<string> {
364
+ if (typeof value === "string") {
365
+ return value;
366
+ }
367
+ return await gradio.shared.server.format(value);
368
+ }
369
+
370
+ async function handle_copy(): Promise<void> {
371
+ if ("clipboard" in navigator) {
372
+ const text = await value_to_string(gradio.props.value);
373
+ await navigator.clipboard.writeText(text);
374
+ copy_feedback();
375
+ }
376
+ }
377
+
378
+ function copy_feedback(): void {
379
+ copied = true;
380
+ if (timer) clearTimeout(timer);
381
+ timer = setTimeout(() => {
382
+ copied = false;
383
+ }, 1000);
384
+ }
385
+
386
+ async function handle_submit(): Promise<void> {
387
+ if (checked) {
388
+ gradio.props.value = await gradio.shared.server.unformat({
389
+ text: textbox_value
390
+ });
391
+ }
392
+ gradio.dispatch("submit");
393
+ }
394
+
395
+ onMount(async () => {
396
+ if (typeof gradio.props.value === "string") {
397
+ textbox_value = gradio.props.value;
398
+ } else if (gradio.props.value && gradio.props.value.length > 0) {
399
+ const formatted = await value_to_string(gradio.props.value);
400
+ textbox_value = formatted;
401
+ } else {
402
+ textbox_value = "";
403
+ }
404
+ });
405
+ </script>
406
+
407
+ <svelte:window on:click={handle_click_outside} />
408
+
409
+ <div class:container={gradio.shared.container}>
410
+ {#if gradio.shared.show_label && (buttons.some((btn) => typeof btn === "string" && btn === "copy") || buttons.some((btn) => typeof btn !== "string"))}
411
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
412
+ {#if buttons.some((btn) => typeof btn === "string" && btn === "copy")}
413
+ <IconButton
414
+ Icon={copied ? Check : Copy}
415
+ onclick={handle_copy}
416
+ label={copied ? "Copied" : "Copy"}
417
+ />
418
+ {/if}
419
+ </IconButtonWrapper>
420
+ {/if}
421
+
422
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
423
+ >{gradio.shared.label || "Dialogue"}</BlockTitle
424
+ >
425
+ {#if gradio.props.ui_mode === "both"}
426
+ <div
427
+ class="switch-container top-switch"
428
+ class:switch-disabled={is_formatting || is_unformatting}
429
+ >
430
+ <Switch
431
+ label="Plain Text"
432
+ bind:checked
433
+ disabled={is_formatting || is_unformatting}
434
+ on:click={async (e) => {
435
+ if (!e.detail.checked) {
436
+ is_unformatting = true;
437
+ try {
438
+ gradio.props.value = await gradio.shared.server.unformat({
439
+ text: textbox_value
440
+ });
441
+ dialogue_lines = [...(gradio.props.value as DialogueLine[])];
442
+ } finally {
443
+ is_unformatting = false;
444
+ }
445
+ } else {
446
+ is_formatting = true;
447
+ try {
448
+ textbox_value = await value_to_string(dialogue_lines);
449
+ } finally {
450
+ is_formatting = false;
451
+ }
452
+ }
453
+ }}
454
+ />
455
+ </div>
456
+ {/if}
457
+ {#if !checked && gradio.props.ui_mode !== "text"}
458
+ <div
459
+ class="dialogue-container"
460
+ bind:this={dialogue_container_element}
461
+ class:loading={is_unformatting}
462
+ >
463
+ {#if is_unformatting}
464
+ <div class="loading-overlay" transition:fade={{ duration: 200 }}>
465
+ <div class="loading-spinner"></div>
466
+ <div class="loading-text">Converting to dialogue format...</div>
467
+ </div>
468
+ {/if}
469
+ {#each dialogue_lines as line, i}
470
+ <div
471
+ class="dialogue-line"
472
+ style="--speaker-bg-color: {disabled &&
473
+ (hoveredSpeaker === null || hoveredSpeaker === line.speaker)
474
+ ? speakerColors[line.speaker] || 'transparent'
475
+ : 'transparent'}"
476
+ >
477
+ <div
478
+ class="speaker-column"
479
+ role="button"
480
+ tabindex="0"
481
+ on:mouseenter={() => disabled && (hoveredSpeaker = line.speaker)}
482
+ on:mouseleave={() => disabled && (hoveredSpeaker = null)}
483
+ >
484
+ {#if disabled}
485
+ <textarea
486
+ bind:value={line.speaker}
487
+ {disabled}
488
+ rows="1"
489
+ readonly
490
+ />
491
+ {:else}
492
+ <BaseDropdown
493
+ label=""
494
+ show_label={false}
495
+ container={true}
496
+ interactive={true}
497
+ value={line.speaker}
498
+ choices={gradio.props.speakers.map((s) => [s, s])}
499
+ on_change={(val) => update_line(i, "speaker", val as string)}
500
+ />
501
+ {/if}
502
+ </div>
503
+ <div class="text-column">
504
+ <div class="input-container">
505
+ <textarea
506
+ bind:value={line.text}
507
+ placeholder={gradio.props.placeholder}
508
+ {disabled}
509
+ on:input={(event) => handle_input(event, i)}
510
+ on:focus={(event) => handle_input(event, i)}
511
+ on:keydown={(event) => {
512
+ if (event.key === "Escape" && showTagMenu) {
513
+ showTagMenu = false;
514
+ selectedOptionIndex = 0;
515
+ event.preventDefault();
516
+ } else if (showTagMenu && currentLineIndex === i) {
517
+ if (event.key === "ArrowDown") {
518
+ selectedOptionIndex = Math.min(
519
+ selectedOptionIndex + 1,
520
+ filtered_tags.length - 1
521
+ );
522
+ event.preventDefault();
523
+ } else if (event.key === "ArrowUp") {
524
+ selectedOptionIndex = Math.max(
525
+ selectedOptionIndex - 1,
526
+ 0
527
+ );
528
+ event.preventDefault();
529
+ } else if (event.key === "Enter") {
530
+ if (filtered_tags[selectedOptionIndex]) {
531
+ insert_selected_tag();
532
+ }
533
+ event.preventDefault();
534
+ }
535
+ }
536
+ }}
537
+ bind:this={input_elements[i]}
538
+ rows="1"
539
+ ></textarea>
540
+ {#if showTagMenu && currentLineIndex === i}
541
+ <div
542
+ id="tag-menu"
543
+ class="tag-menu"
544
+ transition:fade={{ duration: 100 }}
545
+ >
546
+ <BaseDropdownOptions
547
+ choices={gradio.props.tags.map((s, i) => [s, i])}
548
+ filtered_indices={filtered_tags.map((s) =>
549
+ gradio.props.tags.indexOf(s)
550
+ )}
551
+ active_index={filtered_tags.map((s) =>
552
+ gradio.props.tags.indexOf(s)
553
+ )[selectedOptionIndex]}
554
+ show_options={true}
555
+ onchange={(e) => insert_tag(e)}
556
+ {offset_from_top}
557
+ from_top={true}
558
+ />
559
+ </div>
560
+ {/if}
561
+ </div>
562
+ </div>
563
+ {#if gradio.props.max_lines == undefined || (gradio.props.max_lines && i < gradio.props.max_lines - 1)}
564
+ <div class:action-column={i == 0} class:hidden={disabled}>
565
+ <button
566
+ class="add-button"
567
+ on:click={() => add_line(i)}
568
+ aria-label="Add new line"
569
+ {disabled}
570
+ >
571
+ <Plus />
572
+ </button>
573
+ </div>
574
+ {/if}
575
+ <div class="action-column" class:hidden={disabled || i == 0}>
576
+ <button
577
+ class="delete-button"
578
+ on:click={() => delete_line(i)}
579
+ aria-label="Remove current line"
580
+ {disabled}
581
+ >
582
+ <Trash />
583
+ </button>
584
+ </div>
585
+ </div>
586
+ {/each}
587
+ </div>
588
+ {:else if checked || gradio.props.ui_mode !== "dialogue"}
589
+ <div class="textarea-container" class:loading={is_formatting}>
590
+ {#if is_formatting}
591
+ <div class="loading-overlay" transition:fade={{ duration: 200 }}>
592
+ <div class="loading-spinner"></div>
593
+ <div class="loading-text">Converting to plain text...</div>
594
+ </div>
595
+ {/if}
596
+ <textarea
597
+ data-testid="textbox"
598
+ bind:value={textbox_value}
599
+ placeholder={gradio.props.placeholder}
600
+ rows={5}
601
+ {disabled}
602
+ on:input={(event) => {
603
+ handle_input(event, 0);
604
+ gradio.props.value = textbox_value;
605
+ }}
606
+ on:focus={(event) => handle_input(event, 0)}
607
+ on:keydown={(event) => {
608
+ if (event.key === "Escape" && showTagMenu) {
609
+ showTagMenu = false;
610
+ selectedOptionIndex = 0;
611
+ event.preventDefault();
612
+ } else if (showTagMenu) {
613
+ if (event.key === "ArrowDown") {
614
+ selectedOptionIndex = Math.min(
615
+ selectedOptionIndex + 1,
616
+ filtered_tags.length - 1
617
+ );
618
+ event.preventDefault();
619
+ } else if (event.key === "ArrowUp") {
620
+ selectedOptionIndex = Math.max(selectedOptionIndex - 1, 0);
621
+ event.preventDefault();
622
+ } else if (event.key === "Enter") {
623
+ if (filtered_tags[selectedOptionIndex]) {
624
+ insert_selected_tag();
625
+ }
626
+ event.preventDefault();
627
+ }
628
+ }
629
+ }}
630
+ bind:this={textarea_element}
631
+ />
632
+ {#if showTagMenu}
633
+ <div
634
+ id="tag-menu"
635
+ class="tag-menu-plain-text"
636
+ transition:fade={{ duration: 100 }}
637
+ >
638
+ <BaseDropdownOptions
639
+ choices={gradio.props.tags.map((s, i) => [s, i])}
640
+ filtered_indices={filtered_tags.map((s) =>
641
+ gradio.props.tags.indexOf(s)
642
+ )}
643
+ active_index={filtered_tags.map((s) =>
644
+ gradio.props.tags.indexOf(s)
645
+ )[selectedOptionIndex]}
646
+ show_options={true}
647
+ on:change={(e) => insert_tag(e)}
648
+ />
649
+ </div>
650
+ {/if}
651
+ </div>
652
+ {/if}
653
+
654
+ {#if gradio.props.submit_btn && !disabled}
655
+ <div class="submit-container">
656
+ <button class="submit-button" on:click={handle_submit} {disabled}>
657
+ {#if typeof gradio.props.submit_btn === "string"}
658
+ {gradio.props.submit_btn}
659
+ {:else}
660
+ <Send />
661
+ {/if}
662
+ </button>
663
+ </div>
664
+ {/if}
665
+ </div>
666
+
667
+ <style>
668
+ div.container {
669
+ display: block;
670
+ width: 100%;
671
+ }
672
+
673
+ .input-container {
674
+ display: flex;
675
+ position: relative;
676
+ align-items: flex-end;
677
+ }
678
+
679
+ .dialogue-container {
680
+ border: none;
681
+ border-radius: var(--input-radius);
682
+ background: var(--input-background-fill);
683
+ padding: var(--spacing-md);
684
+ margin-bottom: var(--spacing-sm);
685
+ position: relative;
686
+ }
687
+
688
+ .dialogue-line {
689
+ display: flex;
690
+ align-items: center;
691
+ margin-bottom: var(--spacing-sm);
692
+ }
693
+
694
+ .dialogue-line:has(.action-column.hidden) .text-column {
695
+ margin-right: 0;
696
+ }
697
+
698
+ .speaker-column {
699
+ flex: 0 0 150px;
700
+ margin-right: var(--spacing-sm);
701
+ display: flex;
702
+ align-items: center;
703
+ }
704
+
705
+ .speaker-column textarea {
706
+ background: var(--speaker-bg-color);
707
+ transition: background-color 0.2s ease;
708
+ border: 1px solid var(--border-color-primary);
709
+ border-radius: var(--radius-sm);
710
+ padding: var(--input-padding);
711
+ color: var(--body-text-color);
712
+ font-weight: var(--input-text-weight);
713
+ font-size: var(--input-text-size);
714
+ line-height: var(--line-sm);
715
+ resize: none;
716
+ width: 100%;
717
+ box-sizing: border-box;
718
+ height: auto;
719
+ min-height: 30px;
720
+ max-height: none;
721
+ margin-top: 0px;
722
+ margin-bottom: 0px;
723
+ z-index: 1;
724
+ display: block;
725
+ position: relative;
726
+ white-space: pre-wrap;
727
+ word-wrap: break-word;
728
+ overflow-wrap: break-word;
729
+ overflow: hidden;
730
+ }
731
+
732
+ .text-column {
733
+ flex: 1;
734
+ margin-right: var(--spacing-sm);
735
+ }
736
+
737
+ .text-column textarea {
738
+ width: 100%;
739
+ padding: var(--spacing-sm);
740
+ border: 1px solid var(--border-color-primary);
741
+ border-radius: var(--radius-sm);
742
+ color: var(--body-text-color);
743
+ background: var(--speaker-bg-color);
744
+ transition: background-color 0.2s ease;
745
+ height: auto;
746
+ min-height: 30px;
747
+ max-height: none;
748
+ flex-grow: 1;
749
+ margin-top: 0px;
750
+ margin-bottom: 0px;
751
+ resize: none;
752
+ z-index: 1;
753
+ display: block;
754
+ position: relative;
755
+ padding: var(--input-padding);
756
+ width: 100%;
757
+ color: var(--body-text-color);
758
+ font-weight: var(--input-text-weight);
759
+ font-size: var(--input-text-size);
760
+ line-height: var(--line-sm);
761
+ white-space: pre-wrap;
762
+ word-wrap: break-word;
763
+ overflow-wrap: break-word;
764
+ overflow: hidden;
765
+ box-sizing: border-box;
766
+ }
767
+
768
+ .text-column textarea {
769
+ color: var(--body-text-color);
770
+ }
771
+
772
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .text-column textarea,
773
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .speaker-column textarea {
774
+ color: black;
775
+ }
776
+
777
+ textarea {
778
+ flex-grow: 1;
779
+ outline: none !important;
780
+ margin-top: 0px;
781
+ margin-bottom: 0px;
782
+ resize: none;
783
+ z-index: 1;
784
+ display: block;
785
+ position: relative;
786
+ outline: none !important;
787
+ background: var(--input-background-fill);
788
+ padding: var(--input-padding);
789
+ width: 100%;
790
+ color: var(--body-text-color);
791
+ font-weight: var(--input-text-weight);
792
+ font-size: var(--input-text-size);
793
+ line-height: var(--line-sm);
794
+ box-shadow: var(--input-shadow);
795
+ border: var(--input-border-width) solid var(--input-border-color);
796
+ border-radius: var(--input-radius);
797
+ }
798
+
799
+ textarea:disabled {
800
+ -webkit-opacity: 1;
801
+ opacity: 1;
802
+ }
803
+
804
+ textarea:focus {
805
+ box-shadow: var(--input-shadow-focus);
806
+ border-color: var(--input-border-color-focus);
807
+ background: var(--input-background-fill-focus);
808
+ }
809
+
810
+ textarea::placeholder {
811
+ color: var(--input-placeholder-color);
812
+ }
813
+
814
+ .action-column {
815
+ display: flex;
816
+ justify-content: center;
817
+ }
818
+
819
+ .add-button {
820
+ display: flex;
821
+ justify-content: center;
822
+ align-items: center;
823
+ width: 25px;
824
+ height: 25px;
825
+ border: none;
826
+ background: transparent;
827
+ cursor: pointer;
828
+ }
829
+
830
+ .delete-button {
831
+ display: flex;
832
+ justify-content: center;
833
+ align-items: center;
834
+ width: 15px;
835
+ height: 15px;
836
+ }
837
+
838
+ .add-button:hover {
839
+ color: var(--color-accent);
840
+ }
841
+
842
+ .switch-container {
843
+ display: flex;
844
+ justify-content: flex-start;
845
+ transition: opacity 0.2s ease-in-out;
846
+ }
847
+
848
+ .switch-container.switch-disabled {
849
+ opacity: 0.6;
850
+ pointer-events: none;
851
+ }
852
+
853
+ .switch-container.top-switch {
854
+ margin-bottom: var(--spacing-sm);
855
+ justify-content: flex-end;
856
+ }
857
+
858
+ .submit-container {
859
+ display: flex;
860
+ justify-content: flex-end;
861
+ margin-top: var(--spacing-sm);
862
+ }
863
+
864
+ .submit-container button[disabled] {
865
+ cursor: not-allowed;
866
+ }
867
+
868
+ .submit-button {
869
+ border: none;
870
+ text-align: center;
871
+ text-decoration: none;
872
+ font-size: 14px;
873
+ cursor: pointer;
874
+ border-radius: 15px;
875
+ min-width: 30px;
876
+ height: 30px;
877
+ flex-shrink: 0;
878
+ display: flex;
879
+ justify-content: center;
880
+ align-items: center;
881
+ background: var(--button-secondary-background-fill);
882
+ color: var(--button-secondary-text-color);
883
+ }
884
+
885
+ .submit-button:hover {
886
+ background: var(--button-secondary-background-fill-hover);
887
+ }
888
+
889
+ .submit-button:active {
890
+ box-shadow: var(--button-shadow-active);
891
+ }
892
+
893
+ .submit-button :global(svg) {
894
+ height: 22px;
895
+ width: 22px;
896
+ }
897
+
898
+ .tag-menu {
899
+ position: absolute;
900
+ width: 100%;
901
+ top: 100%;
902
+ left: 0;
903
+ }
904
+
905
+ .tag-menu-plain-text {
906
+ position: relative;
907
+ width: 100%;
908
+ }
909
+
910
+ .tag-menu-plain-text :global(.options) {
911
+ position: static !important;
912
+ width: 100% !important;
913
+ max-height: none !important;
914
+ top: auto !important;
915
+ bottom: auto !important;
916
+ }
917
+
918
+ .hidden {
919
+ display: none;
920
+ }
921
+
922
+ .loading-overlay {
923
+ position: absolute;
924
+ top: 0;
925
+ left: 0;
926
+ right: 0;
927
+ bottom: 0;
928
+ background: var(--input-background-fill);
929
+ opacity: 0.95;
930
+ backdrop-filter: blur(2px);
931
+ display: flex;
932
+ flex-direction: column;
933
+ align-items: center;
934
+ justify-content: center;
935
+ z-index: 10;
936
+ border-radius: var(--input-radius);
937
+ }
938
+
939
+ .loading-spinner {
940
+ width: 24px;
941
+ height: 24px;
942
+ border: 2px solid var(--border-color-primary);
943
+ border-top: 2px solid var(--color-accent);
944
+ border-radius: 50%;
945
+ animation: spin 1s linear infinite;
946
+ margin-bottom: var(--spacing-sm);
947
+ }
948
+
949
+ @keyframes spin {
950
+ 0% {
951
+ transform: rotate(0deg);
952
+ }
953
+ 100% {
954
+ transform: rotate(360deg);
955
+ }
956
+ }
957
+
958
+ .loading-text {
959
+ color: var(--body-text-color);
960
+ font-size: var(--text-sm);
961
+ font-weight: 500;
962
+ opacity: 0.8;
963
+ }
964
+
965
+ .dialogue-container.loading,
966
+ .textarea-container.loading {
967
+ position: relative;
968
+ opacity: 0.7;
969
+ transition: opacity 0.3s ease-in-out;
970
+ }
971
+
972
+ .dialogue-container,
973
+ .textarea-container {
974
+ transition: opacity 0.3s ease-in-out;
975
+ }
976
+ </style>
6.5.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.5.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.5.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.5.0/dialogue/main.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import { default as Index } from "./Index.svelte";
2
+ export default Index;
6.5.0/dialogue/package.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dialogue",
3
+ "version": "0.3.2",
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.5.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.5.0/dialogue/utils.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ export interface DialogueLine {
2
+ speaker: string;
3
+ text: string;
4
+ }