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