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 
 
 
 
 
 
 
1
+ <script 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
+ {@const dd_props = new Gradio({
486
+ shared_props: {
487
+ container: true,
488
+ show_label: false,
489
+ interactive: true,
490
+ label: ""
491
+ },
492
+ props: {
493
+ value: line.speaker,
494
+ choices: gradio.props.speakers.map((s) => [s, s])
495
+ }
496
+ })}
497
+ <BaseDropdown
498
+ on_change={() =>
499
+ update_line(i, "speaker", dd_props.props.value as string)}
500
+ gradio={dd_props}
501
+ />
502
+ {/if}
503
+ </div>
504
+ <div class="text-column">
505
+ <div class="input-container">
506
+ <textarea
507
+ bind:value={line.text}
508
+ placeholder={gradio.props.placeholder}
509
+ {disabled}
510
+ on:input={(event) => handle_input(event, i)}
511
+ on:focus={(event) => handle_input(event, i)}
512
+ on:keydown={(event) => {
513
+ if (event.key === "Escape" && showTagMenu) {
514
+ showTagMenu = false;
515
+ selectedOptionIndex = 0;
516
+ event.preventDefault();
517
+ } else if (showTagMenu && currentLineIndex === i) {
518
+ if (event.key === "ArrowDown") {
519
+ selectedOptionIndex = Math.min(
520
+ selectedOptionIndex + 1,
521
+ filtered_tags.length - 1
522
+ );
523
+ event.preventDefault();
524
+ } else if (event.key === "ArrowUp") {
525
+ selectedOptionIndex = Math.max(
526
+ selectedOptionIndex - 1,
527
+ 0
528
+ );
529
+ event.preventDefault();
530
+ } else if (event.key === "Enter") {
531
+ if (filtered_tags[selectedOptionIndex]) {
532
+ insert_selected_tag();
533
+ }
534
+ event.preventDefault();
535
+ }
536
+ }
537
+ }}
538
+ bind:this={input_elements[i]}
539
+ rows="1"
540
+ ></textarea>
541
+ {#if showTagMenu && currentLineIndex === i}
542
+ <div
543
+ id="tag-menu"
544
+ class="tag-menu"
545
+ transition:fade={{ duration: 100 }}
546
+ >
547
+ <BaseDropdownOptions
548
+ choices={gradio.props.tags.map((s, i) => [s, i])}
549
+ filtered_indices={filtered_tags.map((s) =>
550
+ gradio.props.tags.indexOf(s)
551
+ )}
552
+ active_index={filtered_tags.map((s) =>
553
+ gradio.props.tags.indexOf(s)
554
+ )[selectedOptionIndex]}
555
+ show_options={true}
556
+ on:change={(e) => insert_tag(e)}
557
+ {offset_from_top}
558
+ from_top={true}
559
+ />
560
+ </div>
561
+ {/if}
562
+ </div>
563
+ </div>
564
+ {#if gradio.props.max_lines == undefined || (gradio.props.max_lines && i < gradio.props.max_lines - 1)}
565
+ <div class:action-column={i == 0} class:hidden={disabled}>
566
+ <button
567
+ class="add-button"
568
+ on:click={() => add_line(i)}
569
+ aria-label="Add new line"
570
+ {disabled}
571
+ >
572
+ <Plus />
573
+ </button>
574
+ </div>
575
+ {/if}
576
+ <div class="action-column" class:hidden={disabled || i == 0}>
577
+ <button
578
+ class="delete-button"
579
+ on:click={() => delete_line(i)}
580
+ aria-label="Remove current line"
581
+ {disabled}
582
+ >
583
+ <Trash />
584
+ </button>
585
+ </div>
586
+ </div>
587
+ {/each}
588
+ </div>
589
+ {:else if checked || gradio.props.ui_mode !== "dialogue"}
590
+ <div class="textarea-container" class:loading={is_formatting}>
591
+ {#if is_formatting}
592
+ <div class="loading-overlay" transition:fade={{ duration: 200 }}>
593
+ <div class="loading-spinner"></div>
594
+ <div class="loading-text">Converting to plain text...</div>
595
+ </div>
596
+ {/if}
597
+ <textarea
598
+ data-testid="textbox"
599
+ bind:value={textbox_value}
600
+ placeholder={gradio.props.placeholder}
601
+ rows={5}
602
+ {disabled}
603
+ on:input={(event) => {
604
+ handle_input(event, 0);
605
+ gradio.props.value = textbox_value;
606
+ }}
607
+ on:focus={(event) => handle_input(event, 0)}
608
+ on:keydown={(event) => {
609
+ if (event.key === "Escape" && showTagMenu) {
610
+ showTagMenu = false;
611
+ selectedOptionIndex = 0;
612
+ event.preventDefault();
613
+ } else if (showTagMenu) {
614
+ if (event.key === "ArrowDown") {
615
+ selectedOptionIndex = Math.min(
616
+ selectedOptionIndex + 1,
617
+ filtered_tags.length - 1
618
+ );
619
+ event.preventDefault();
620
+ } else if (event.key === "ArrowUp") {
621
+ selectedOptionIndex = Math.max(selectedOptionIndex - 1, 0);
622
+ event.preventDefault();
623
+ } else if (event.key === "Enter") {
624
+ if (filtered_tags[selectedOptionIndex]) {
625
+ insert_selected_tag();
626
+ }
627
+ event.preventDefault();
628
+ }
629
+ }
630
+ }}
631
+ bind:this={textarea_element}
632
+ />
633
+ {#if showTagMenu}
634
+ <div
635
+ id="tag-menu"
636
+ class="tag-menu-plain-text"
637
+ transition:fade={{ duration: 100 }}
638
+ >
639
+ <BaseDropdownOptions
640
+ choices={gradio.props.tags.map((s, i) => [s, i])}
641
+ filtered_indices={filtered_tags.map((s) =>
642
+ gradio.props.tags.indexOf(s)
643
+ )}
644
+ active_index={filtered_tags.map((s) =>
645
+ gradio.props.tags.indexOf(s)
646
+ )[selectedOptionIndex]}
647
+ show_options={true}
648
+ on:change={(e) => insert_tag(e)}
649
+ />
650
+ </div>
651
+ {/if}
652
+ </div>
653
+ {/if}
654
+
655
+ {#if gradio.props.submit_btn && !disabled}
656
+ <div class="submit-container">
657
+ <button class="submit-button" on:click={handle_submit} {disabled}>
658
+ {#if typeof gradio.props.submit_btn === "string"}
659
+ {gradio.props.submit_btn}
660
+ {:else}
661
+ <Send />
662
+ {/if}
663
+ </button>
664
+ </div>
665
+ {/if}
666
+ </label>
667
+
668
+ <style>
669
+ label {
670
+ display: block;
671
+ width: 100%;
672
+ }
673
+
674
+ .input-container {
675
+ display: flex;
676
+ position: relative;
677
+ align-items: flex-end;
678
+ }
679
+
680
+ .dialogue-container {
681
+ border: none;
682
+ border-radius: var(--input-radius);
683
+ background: var(--input-background-fill);
684
+ padding: var(--spacing-md);
685
+ margin-bottom: var(--spacing-sm);
686
+ position: relative;
687
+ }
688
+
689
+ .dialogue-line {
690
+ display: flex;
691
+ align-items: center;
692
+ margin-bottom: var(--spacing-sm);
693
+ }
694
+
695
+ .dialogue-line:has(.action-column.hidden) .text-column {
696
+ margin-right: 0;
697
+ }
698
+
699
+ .speaker-column {
700
+ flex: 0 0 150px;
701
+ margin-right: var(--spacing-sm);
702
+ display: flex;
703
+ align-items: center;
704
+ }
705
+
706
+ .speaker-column textarea {
707
+ background: var(--speaker-bg-color);
708
+ transition: background-color 0.2s ease;
709
+ border: 1px solid var(--border-color-primary);
710
+ border-radius: var(--radius-sm);
711
+ padding: var(--input-padding);
712
+ color: var(--body-text-color);
713
+ font-weight: var(--input-text-weight);
714
+ font-size: var(--input-text-size);
715
+ line-height: var(--line-sm);
716
+ resize: none;
717
+ width: 100%;
718
+ box-sizing: border-box;
719
+ height: auto;
720
+ min-height: 30px;
721
+ max-height: none;
722
+ margin-top: 0px;
723
+ margin-bottom: 0px;
724
+ z-index: 1;
725
+ display: block;
726
+ position: relative;
727
+ white-space: pre-wrap;
728
+ word-wrap: break-word;
729
+ overflow-wrap: break-word;
730
+ overflow: hidden;
731
+ }
732
+
733
+ .text-column {
734
+ flex: 1;
735
+ margin-right: var(--spacing-sm);
736
+ }
737
+
738
+ .text-column textarea {
739
+ width: 100%;
740
+ padding: var(--spacing-sm);
741
+ border: 1px solid var(--border-color-primary);
742
+ border-radius: var(--radius-sm);
743
+ color: var(--body-text-color);
744
+ background: var(--speaker-bg-color);
745
+ transition: background-color 0.2s ease;
746
+ height: auto;
747
+ min-height: 30px;
748
+ max-height: none;
749
+ flex-grow: 1;
750
+ margin-top: 0px;
751
+ margin-bottom: 0px;
752
+ resize: none;
753
+ z-index: 1;
754
+ display: block;
755
+ position: relative;
756
+ padding: var(--input-padding);
757
+ width: 100%;
758
+ color: var(--body-text-color);
759
+ font-weight: var(--input-text-weight);
760
+ font-size: var(--input-text-size);
761
+ line-height: var(--line-sm);
762
+ white-space: pre-wrap;
763
+ word-wrap: break-word;
764
+ overflow-wrap: break-word;
765
+ overflow: hidden;
766
+ box-sizing: border-box;
767
+ }
768
+
769
+ .text-column textarea {
770
+ color: var(--body-text-color);
771
+ }
772
+
773
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .text-column textarea,
774
+ .dialogue-line[style*="--speaker-bg-color: rgba"] .speaker-column textarea {
775
+ color: black;
776
+ }
777
+
778
+ textarea {
779
+ flex-grow: 1;
780
+ outline: none !important;
781
+ margin-top: 0px;
782
+ margin-bottom: 0px;
783
+ resize: none;
784
+ z-index: 1;
785
+ display: block;
786
+ position: relative;
787
+ outline: none !important;
788
+ background: var(--input-background-fill);
789
+ padding: var(--input-padding);
790
+ width: 100%;
791
+ color: var(--body-text-color);
792
+ font-weight: var(--input-text-weight);
793
+ font-size: var(--input-text-size);
794
+ line-height: var(--line-sm);
795
+ box-shadow: var(--input-shadow);
796
+ border: var(--input-border-width) solid var(--input-border-color);
797
+ border-radius: var(--input-radius);
798
+ }
799
+
800
+ textarea:disabled {
801
+ -webkit-opacity: 1;
802
+ opacity: 1;
803
+ }
804
+
805
+ textarea:focus {
806
+ box-shadow: var(--input-shadow-focus);
807
+ border-color: var(--input-border-color-focus);
808
+ background: var(--input-background-fill-focus);
809
+ }
810
+
811
+ textarea::placeholder {
812
+ color: var(--input-placeholder-color);
813
+ }
814
+
815
+ .action-column {
816
+ display: flex;
817
+ justify-content: center;
818
+ }
819
+
820
+ .add-button {
821
+ display: flex;
822
+ justify-content: center;
823
+ align-items: center;
824
+ width: 25px;
825
+ height: 25px;
826
+ border: none;
827
+ background: transparent;
828
+ cursor: pointer;
829
+ }
830
+
831
+ .delete-button {
832
+ display: flex;
833
+ justify-content: center;
834
+ align-items: center;
835
+ width: 15px;
836
+ height: 15px;
837
+ }
838
+
839
+ .add-button:hover {
840
+ color: var(--color-accent);
841
+ }
842
+
843
+ .switch-container {
844
+ display: flex;
845
+ justify-content: flex-start;
846
+ transition: opacity 0.2s ease-in-out;
847
+ }
848
+
849
+ .switch-container.switch-disabled {
850
+ opacity: 0.6;
851
+ pointer-events: none;
852
+ }
853
+
854
+ .switch-container.top-switch {
855
+ margin-bottom: var(--spacing-sm);
856
+ justify-content: flex-end;
857
+ }
858
+
859
+ .submit-container {
860
+ display: flex;
861
+ justify-content: flex-end;
862
+ margin-top: var(--spacing-sm);
863
+ }
864
+
865
+ .submit-container button[disabled] {
866
+ cursor: not-allowed;
867
+ }
868
+
869
+ .submit-button {
870
+ border: none;
871
+ text-align: center;
872
+ text-decoration: none;
873
+ font-size: 14px;
874
+ cursor: pointer;
875
+ border-radius: 15px;
876
+ min-width: 30px;
877
+ height: 30px;
878
+ flex-shrink: 0;
879
+ display: flex;
880
+ justify-content: center;
881
+ align-items: center;
882
+ background: var(--button-secondary-background-fill);
883
+ color: var(--button-secondary-text-color);
884
+ }
885
+
886
+ .submit-button:hover {
887
+ background: var(--button-secondary-background-fill-hover);
888
+ }
889
+
890
+ .submit-button:active {
891
+ box-shadow: var(--button-shadow-active);
892
+ }
893
+
894
+ .submit-button :global(svg) {
895
+ height: 22px;
896
+ width: 22px;
897
+ }
898
+
899
+ .tag-menu {
900
+ position: absolute;
901
+ width: 100%;
902
+ top: 100%;
903
+ left: 0;
904
+ }
905
+
906
+ .tag-menu-plain-text {
907
+ position: relative;
908
+ width: 100%;
909
+ }
910
+
911
+ .tag-menu-plain-text :global(.options) {
912
+ position: static !important;
913
+ width: 100% !important;
914
+ max-height: none !important;
915
+ top: auto !important;
916
+ bottom: auto !important;
917
+ }
918
+
919
+ .hidden {
920
+ display: none;
921
+ }
922
+
923
+ .loading-overlay {
924
+ position: absolute;
925
+ top: 0;
926
+ left: 0;
927
+ right: 0;
928
+ bottom: 0;
929
+ background: var(--input-background-fill);
930
+ opacity: 0.95;
931
+ backdrop-filter: blur(2px);
932
+ display: flex;
933
+ flex-direction: column;
934
+ align-items: center;
935
+ justify-content: center;
936
+ z-index: 10;
937
+ border-radius: var(--input-radius);
938
+ }
939
+
940
+ .loading-spinner {
941
+ width: 24px;
942
+ height: 24px;
943
+ border: 2px solid var(--border-color-primary);
944
+ border-top: 2px solid var(--color-accent);
945
+ border-radius: 50%;
946
+ animation: spin 1s linear infinite;
947
+ margin-bottom: var(--spacing-sm);
948
+ }
949
+
950
+ @keyframes spin {
951
+ 0% {
952
+ transform: rotate(0deg);
953
+ }
954
+ 100% {
955
+ transform: rotate(360deg);
956
+ }
957
+ }
958
+
959
+ .loading-text {
960
+ color: var(--body-text-color);
961
+ font-size: var(--text-sm);
962
+ font-weight: 500;
963
+ opacity: 0.8;
964
+ }
965
+
966
+ .dialogue-container.loading,
967
+ .textarea-container.loading {
968
+ position: relative;
969
+ opacity: 0.7;
970
+ transition: opacity 0.3s ease-in-out;
971
+ }
972
+
973
+ .dialogue-container,
974
+ .textarea-container {
975
+ transition: opacity 0.3s ease-in-out;
976
+ }
977
+ </style>
6.0.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.0.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.0.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.0.0/dialogue/main.ts ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ import { default as Index } from "./Index.svelte";
2
+ export default Index;
6.0.0/dialogue/package.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dialogue",
3
+ "version": "0.2.4",
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.0.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.0.0/dialogue/utils.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ export interface DialogueLine {
2
+ speaker: string;
3
+ text: string;
4
+ }