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