gradio-pr-bot commited on
Commit
abbafc6
·
verified ·
1 Parent(s): 3da2382

Upload folder using huggingface_hub

Browse files
6.9.1/code/Example.svelte ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ interface Props {
3
+ value: string | null;
4
+ type: "gallery" | "table";
5
+ selected?: boolean;
6
+ }
7
+
8
+ let { value, type, selected = false }: Props = $props();
9
+
10
+ function truncate_text(text: string | null, max_length = 60): string {
11
+ if (!text) return "";
12
+ const str = String(text);
13
+ if (str.length <= max_length) return str;
14
+ return str.slice(0, max_length) + "...";
15
+ }
16
+ </script>
17
+
18
+ <pre
19
+ class:table={type === "table"}
20
+ class:gallery={type === "gallery"}
21
+ class:selected>{truncate_text(value)}</pre>
22
+
23
+ <style>
24
+ pre {
25
+ text-align: left;
26
+ }
27
+ .gallery {
28
+ padding: var(--size-1) var(--size-2);
29
+ }
30
+ </style>
6.9.1/code/Index.svelte ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseCode } from "./shared/Code.svelte";
3
+ export { default as BaseCopy } from "./shared/Copy.svelte";
4
+ export { default as BaseDownload } from "./shared/Download.svelte";
5
+ export { default as BaseWidget } from "./shared/Widgets.svelte";
6
+ export { default as BaseExample } from "./Example.svelte";
7
+ </script>
8
+
9
+ <script lang="ts">
10
+ import { Gradio } from "@gradio/utils";
11
+ import type { CodeProps, CodeEvents } from "./types";
12
+ import { StatusTracker } from "@gradio/statustracker";
13
+
14
+ import Code from "./shared/Code.svelte";
15
+ import Widget from "./shared/Widgets.svelte";
16
+ import { Block, BlockLabel, Empty } from "@gradio/atoms";
17
+ import { Code as CodeIcon } from "@gradio/icons";
18
+
19
+ const props = $props();
20
+ const gradio = new Gradio<CodeEvents, CodeProps>(props);
21
+
22
+ let dark_mode = gradio.shared.theme === "dark";
23
+
24
+ let label = $derived(gradio.shared.label || gradio.i18n("code.code"));
25
+ let old_value = $state(gradio.props.value);
26
+ let first_change = true;
27
+
28
+ $effect(() => {
29
+ if (first_change) {
30
+ first_change = false;
31
+ return;
32
+ }
33
+ if (old_value != gradio.props.value) {
34
+ old_value = gradio.props.value;
35
+ gradio.dispatch("change");
36
+ }
37
+ });
38
+ </script>
39
+
40
+ <Block
41
+ height={gradio.props.max_lines && "fit-content"}
42
+ variant={"solid"}
43
+ padding={false}
44
+ elem_id={gradio.shared.elem_id}
45
+ elem_classes={gradio.shared.elem_classes}
46
+ visible={gradio.shared.visible}
47
+ scale={gradio.shared.scale}
48
+ min_width={gradio.shared.min_width}
49
+ >
50
+ <StatusTracker
51
+ autoscroll={gradio.shared.autoscroll}
52
+ i18n={gradio.i18n}
53
+ {...gradio.shared.loading_status}
54
+ on_clear_status={() =>
55
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
56
+ />
57
+
58
+ {#if gradio.shared.show_label}
59
+ <BlockLabel
60
+ Icon={CodeIcon}
61
+ show_label={gradio.shared.show_label}
62
+ {label}
63
+ float={false}
64
+ />
65
+ {/if}
66
+
67
+ {#if !gradio.props.value && !gradio.shared.interactive}
68
+ <Empty unpadded_box={true} size="large">
69
+ <CodeIcon />
70
+ </Empty>
71
+ {:else}
72
+ <Widget
73
+ language={gradio.props.language}
74
+ value={gradio.props.value}
75
+ buttons={gradio.props.buttons ?? ["copy", "download"]}
76
+ on_custom_button_click={(id) => {
77
+ gradio.dispatch("custom_button_click", { id });
78
+ }}
79
+ />
80
+
81
+ <Code
82
+ bind:value={gradio.props.value}
83
+ language={gradio.props.language}
84
+ lines={gradio.props.lines}
85
+ max_lines={gradio.props.max_lines}
86
+ {dark_mode}
87
+ wrap_lines={gradio.props.wrap_lines}
88
+ show_line_numbers={gradio.props.show_line_numbers}
89
+ autocomplete={gradio.props.autocomplete}
90
+ readonly={!gradio.shared.interactive}
91
+ onblur={() => gradio.dispatch("blur")}
92
+ onfocus={() => gradio.dispatch("focus")}
93
+ oninput={() => gradio.dispatch("input")}
94
+ />
95
+ {/if}
96
+ </Block>
6.9.1/code/package.json ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/code",
3
+ "version": "0.17.4",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@codemirror/autocomplete": "^6.19.0",
11
+ "@codemirror/commands": "^6.9.0",
12
+ "@codemirror/lang-css": "^6.3.1",
13
+ "@codemirror/lang-html": "^6.4.11",
14
+ "@codemirror/lang-javascript": "^6.2.4",
15
+ "@codemirror/lang-json": "^6.0.2",
16
+ "@codemirror/lang-markdown": "^6.4.0",
17
+ "@codemirror/lang-python": "^6.2.1",
18
+ "@codemirror/language": "^6.11.3",
19
+ "@codemirror/legacy-modes": "^6.5.2",
20
+ "@codemirror/lint": "^6.9.0",
21
+ "@codemirror/search": "^6.5.11",
22
+ "@codemirror/state": "^6.5.2",
23
+ "@codemirror/view": "^6.38.5",
24
+ "@gradio/atoms": "workspace:^",
25
+ "@gradio/icons": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/upload": "workspace:^",
28
+ "@gradio/utils": "workspace:^",
29
+ "@lezer/common": "^1.2.3",
30
+ "@lezer/highlight": "^1.2.1",
31
+ "@lezer/markdown": "^1.4.3",
32
+ "cm6-theme-basic-dark": "^0.2.0",
33
+ "cm6-theme-basic-light": "^0.2.0",
34
+ "codemirror": "^6.0.2"
35
+ },
36
+ "main_changeset": true,
37
+ "main": "./Index.svelte",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/Index.svelte.d.ts",
41
+ "gradio": "./Index.svelte",
42
+ "svelte": "./dist/Index.svelte",
43
+ "default": "./dist/Index.svelte"
44
+ },
45
+ "./example": {
46
+ "types": "./dist/Example.svelte.d.ts",
47
+ "gradio": "./Example.svelte",
48
+ "svelte": "./dist/Example.svelte",
49
+ "default": "./dist/Example.svelte"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "devDependencies": {
54
+ "@gradio/preview": "workspace:^"
55
+ },
56
+ "peerDependencies": {
57
+ "svelte": "^5.48.0"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/gradio-app/gradio.git",
62
+ "directory": "js/code"
63
+ }
64
+ }
6.9.1/code/shared/Code.svelte ADDED
@@ -0,0 +1,345 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount } from "svelte";
3
+ import {
4
+ EditorView,
5
+ ViewUpdate,
6
+ keymap,
7
+ placeholder as placeholderExt,
8
+ lineNumbers
9
+ } from "@codemirror/view";
10
+ import { StateEffect, EditorState, type Extension } from "@codemirror/state";
11
+ import { indentWithTab } from "@codemirror/commands";
12
+ import { autocompletion, acceptCompletion } from "@codemirror/autocomplete";
13
+
14
+ import { basicDark } from "cm6-theme-basic-dark";
15
+ import { basicLight } from "cm6-theme-basic-light";
16
+ import { basicSetup } from "./extensions";
17
+ import { getLanguageExtension } from "./language";
18
+
19
+ interface Props {
20
+ class_names?: string;
21
+ value?: string;
22
+ dark_mode: boolean;
23
+ basic?: boolean;
24
+ language: string;
25
+ lines?: number;
26
+ max_lines?: number | null;
27
+ extensions?: Extension[];
28
+ use_tab?: boolean;
29
+ readonly?: boolean;
30
+ placeholder?: string | HTMLElement | null | undefined;
31
+ wrap_lines?: boolean;
32
+ show_line_numbers?: boolean;
33
+ autocomplete?: boolean;
34
+ onchange?: (value: string) => void;
35
+ onblur?: () => void;
36
+ onfocus?: () => void;
37
+ oninput?: () => void;
38
+ }
39
+
40
+ let {
41
+ class_names = "",
42
+ value = $bindable(),
43
+ dark_mode,
44
+ basic = true,
45
+ language,
46
+ lines = 5,
47
+ max_lines = null,
48
+ extensions = [],
49
+ use_tab = true,
50
+ readonly = false,
51
+ placeholder = undefined,
52
+ wrap_lines = false,
53
+ show_line_numbers = true,
54
+ autocomplete = false,
55
+ onchange,
56
+ onblur,
57
+ onfocus,
58
+ oninput
59
+ }: Props = $props();
60
+
61
+ let lang_extension: Extension | undefined = $state();
62
+ let element: HTMLDivElement;
63
+ let view: EditorView;
64
+
65
+ async function get_lang(val: string): Promise<void> {
66
+ const ext = await getLanguageExtension(val);
67
+ lang_extension = ext;
68
+ }
69
+
70
+ $effect(() => {
71
+ get_lang(language);
72
+ });
73
+
74
+ $effect(() => {
75
+ lang_extension;
76
+ readonly;
77
+ reconfigure();
78
+ });
79
+
80
+ $effect(() => {
81
+ set_doc(value);
82
+ });
83
+
84
+ update_lines();
85
+
86
+ function set_doc(new_doc: string): void {
87
+ if (view && new_doc !== view.state.doc.toString()) {
88
+ view.dispatch({
89
+ changes: {
90
+ from: 0,
91
+ to: view.state.doc.length,
92
+ insert: new_doc
93
+ }
94
+ });
95
+ }
96
+ }
97
+
98
+ function update_lines(): void {
99
+ if (view) {
100
+ view.requestMeasure({ read: resize });
101
+ }
102
+ }
103
+
104
+ function create_editor_view(): EditorView {
105
+ const editorView = new EditorView({
106
+ parent: element,
107
+ state: create_editor_state(value)
108
+ });
109
+ editorView.dom.addEventListener("focus", handle_focus, true);
110
+ editorView.dom.addEventListener("blur", handle_blur, true);
111
+ return editorView;
112
+ }
113
+
114
+ function handle_focus(): void {
115
+ onfocus?.();
116
+ }
117
+
118
+ function handle_blur(): void {
119
+ onblur?.();
120
+ }
121
+
122
+ function getGutterLineHeight(_view: EditorView): string | null {
123
+ let elements = _view.dom.querySelectorAll<HTMLElement>(".cm-gutterElement");
124
+ if (elements.length === 0) {
125
+ return null;
126
+ }
127
+ for (var i = 0; i < elements.length; i++) {
128
+ let node = elements[i];
129
+ let height = getComputedStyle(node)?.height ?? "0px";
130
+ if (height != "0px") {
131
+ return height;
132
+ }
133
+ }
134
+ return null;
135
+ }
136
+
137
+ function resize(_view: EditorView): any {
138
+ let scroller = _view.dom.querySelector<HTMLElement>(".cm-scroller");
139
+ if (!scroller) {
140
+ return null;
141
+ }
142
+ const lineHeight = getGutterLineHeight(_view);
143
+ if (!lineHeight) {
144
+ return null;
145
+ }
146
+
147
+ const minLines = lines == 1 ? 1 : lines + 1;
148
+ scroller.style.minHeight = `calc(${lineHeight} * ${minLines})`;
149
+ if (max_lines)
150
+ scroller.style.maxHeight = `calc(${lineHeight} * ${max_lines + 1})`;
151
+ }
152
+
153
+ import { Transaction } from "@codemirror/state";
154
+
155
+ function is_user_input(update: ViewUpdate): boolean {
156
+ return update.transactions.some(
157
+ (tr) => tr.annotation(Transaction.userEvent) != null
158
+ );
159
+ }
160
+
161
+ function handle_change(vu: ViewUpdate): void {
162
+ if (!vu.docChanged) return;
163
+
164
+ const doc = vu.state.doc;
165
+ const text = doc.toString();
166
+ value = text;
167
+
168
+ const user_change = is_user_input(vu);
169
+ if (user_change) {
170
+ onchange?.(text);
171
+ oninput?.();
172
+ } else {
173
+ onchange?.(text);
174
+ }
175
+
176
+ view.requestMeasure({ read: resize });
177
+ }
178
+
179
+ function get_extensions(): Extension[] {
180
+ const stateExtensions = [
181
+ ...get_base_extensions(
182
+ basic,
183
+ use_tab,
184
+ placeholder,
185
+ readonly,
186
+ lang_extension,
187
+ show_line_numbers
188
+ ),
189
+ FontTheme,
190
+ ...get_theme(),
191
+ ...extensions
192
+ ];
193
+ return stateExtensions;
194
+ }
195
+
196
+ const FontTheme = EditorView.theme({
197
+ "&": {
198
+ fontSize: "var(--text-sm)",
199
+ backgroundColor: "var(--border-color-secondary)"
200
+ },
201
+ ".cm-content": {
202
+ paddingTop: "5px",
203
+ paddingBottom: "5px",
204
+ color: "var(--body-text-color)",
205
+ fontFamily: "var(--font-mono)",
206
+ minHeight: "100%"
207
+ },
208
+ ".cm-gutterElement": {
209
+ marginRight: "var(--spacing-xs)"
210
+ },
211
+ ".cm-gutters": {
212
+ marginRight: "1px",
213
+ borderRight: "1px solid var(--border-color-primary)",
214
+ backgroundColor: "var(--block-background-fill);",
215
+ color: "var(--body-text-color-subdued)"
216
+ },
217
+ ".cm-focused": {
218
+ outline: "none"
219
+ },
220
+ ".cm-scroller": {
221
+ height: "auto"
222
+ },
223
+ ".cm-cursor": {
224
+ borderLeftColor: "var(--body-text-color)"
225
+ }
226
+ });
227
+
228
+ const AutocompleteTheme = EditorView.theme({
229
+ ".cm-tooltip-autocomplete": {
230
+ "& > ul": {
231
+ backgroundColor: "var(--background-fill-primary)",
232
+ color: "var(--body-text-color)"
233
+ },
234
+ "& > ul > li[aria-selected]": {
235
+ backgroundColor: "var(--color-accent-soft)",
236
+ color: "var(--body-text-color)"
237
+ }
238
+ }
239
+ });
240
+
241
+ function create_editor_state(_value: string | null | undefined): EditorState {
242
+ return EditorState.create({
243
+ doc: _value ?? undefined,
244
+ extensions: get_extensions()
245
+ });
246
+ }
247
+
248
+ function get_base_extensions(
249
+ basic: boolean,
250
+ use_tab: boolean,
251
+ placeholder: string | HTMLElement | null | undefined,
252
+ readonly: boolean,
253
+ lang: Extension | null | undefined,
254
+ show_line_numbers: boolean
255
+ ): Extension[] {
256
+ const extensions: Extension[] = [
257
+ EditorView.editable.of(!readonly),
258
+ EditorState.readOnly.of(readonly),
259
+ EditorView.contentAttributes.of({ "aria-label": "Code input container" })
260
+ ];
261
+
262
+ if (basic) {
263
+ extensions.push(basicSetup);
264
+ }
265
+ if (use_tab) {
266
+ extensions.push(
267
+ keymap.of([{ key: "Tab", run: acceptCompletion }, indentWithTab])
268
+ );
269
+ }
270
+ if (placeholder) {
271
+ extensions.push(placeholderExt(placeholder));
272
+ }
273
+ if (lang) {
274
+ extensions.push(lang);
275
+ }
276
+ if (show_line_numbers) {
277
+ extensions.push(lineNumbers());
278
+ }
279
+ if (autocomplete) {
280
+ extensions.push(autocompletion());
281
+ extensions.push(AutocompleteTheme);
282
+ }
283
+
284
+ extensions.push(EditorView.updateListener.of(handle_change));
285
+ if (wrap_lines) {
286
+ extensions.push(EditorView.lineWrapping);
287
+ }
288
+
289
+ return extensions;
290
+ }
291
+
292
+ function get_theme(): Extension[] {
293
+ const extensions: Extension[] = [];
294
+
295
+ if (dark_mode) {
296
+ extensions.push(basicDark);
297
+ } else {
298
+ extensions.push(basicLight);
299
+ }
300
+ return extensions;
301
+ }
302
+
303
+ function reconfigure(): void {
304
+ view?.dispatch({
305
+ effects: StateEffect.reconfigure.of(get_extensions())
306
+ });
307
+ }
308
+
309
+ onMount(() => {
310
+ view = create_editor_view();
311
+ return () => view?.destroy();
312
+ });
313
+ </script>
314
+
315
+ <div class="wrap">
316
+ <div class="codemirror-wrapper {class_names}" bind:this={element} />
317
+ </div>
318
+
319
+ <style>
320
+ .wrap {
321
+ display: flex;
322
+ flex-direction: column;
323
+ flex-grow: 1;
324
+ margin: 0;
325
+ padding: 0;
326
+ height: 100%;
327
+ }
328
+ .codemirror-wrapper {
329
+ flex-grow: 1;
330
+ overflow: auto;
331
+ }
332
+
333
+ :global(.cm-editor) {
334
+ height: 100%;
335
+ }
336
+
337
+ /* Dunno why this doesn't work through the theme API -- don't remove*/
338
+ :global(.cm-selectionBackground) {
339
+ background-color: #b9d2ff30 !important;
340
+ }
341
+
342
+ :global(.cm-focused) {
343
+ outline: none !important;
344
+ }
345
+ </style>
6.9.1/code/shared/Copy.svelte ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onDestroy } from "svelte";
3
+ import { Copy, Check } from "@gradio/icons";
4
+ import { IconButton } from "@gradio/atoms";
5
+
6
+ interface Props {
7
+ value: string;
8
+ }
9
+
10
+ let { value }: Props = $props();
11
+
12
+ let copied = $state(false);
13
+ let timer: NodeJS.Timeout;
14
+
15
+ function copy_feedback(): void {
16
+ copied = true;
17
+ if (timer) clearTimeout(timer);
18
+ timer = setTimeout(() => {
19
+ copied = false;
20
+ }, 2000);
21
+ }
22
+
23
+ async function handle_copy(): Promise<void> {
24
+ if ("clipboard" in navigator) {
25
+ await navigator.clipboard.writeText(value);
26
+ copy_feedback();
27
+ }
28
+ }
29
+
30
+ onDestroy(() => {
31
+ if (timer) clearTimeout(timer);
32
+ });
33
+ </script>
34
+
35
+ <IconButton Icon={copied ? Check : Copy} onclick={handle_copy} />
6.9.1/code/shared/Download.svelte ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onDestroy } from "svelte";
3
+ import { Download, Check } from "@gradio/icons";
4
+ import { DownloadLink } from "@gradio/atoms";
5
+ import { IconButton } from "@gradio/atoms";
6
+
7
+ interface Props {
8
+ value: string;
9
+ language: string;
10
+ }
11
+
12
+ let { value, language }: Props = $props();
13
+
14
+ let ext = $derived(get_ext_for_type(language));
15
+
16
+ function get_ext_for_type(type: string): string {
17
+ const exts: Record<string, string> = {
18
+ py: "py",
19
+ python: "py",
20
+ md: "md",
21
+ markdown: "md",
22
+ json: "json",
23
+ html: "html",
24
+ css: "css",
25
+ js: "js",
26
+ javascript: "js",
27
+ ts: "ts",
28
+ typescript: "ts",
29
+ yaml: "yaml",
30
+ yml: "yml",
31
+ dockerfile: "dockerfile",
32
+ sh: "sh",
33
+ shell: "sh",
34
+ r: "r",
35
+ c: "c",
36
+ cpp: "cpp",
37
+ latex: "tex"
38
+ };
39
+
40
+ return exts[type] || "txt";
41
+ }
42
+
43
+ let copied = $state(false);
44
+ let timer: NodeJS.Timeout;
45
+
46
+ function copy_feedback(): void {
47
+ copied = true;
48
+ if (timer) clearTimeout(timer);
49
+ timer = setTimeout(() => {
50
+ copied = false;
51
+ }, 2000);
52
+ }
53
+
54
+ let download_value = $derived(URL.createObjectURL(new Blob([value])));
55
+
56
+ onDestroy(() => {
57
+ if (timer) clearTimeout(timer);
58
+ });
59
+ </script>
60
+
61
+ <DownloadLink
62
+ download="file.{ext}"
63
+ href={download_value}
64
+ onclick={copy_feedback}
65
+ >
66
+ <IconButton Icon={copied ? Check : Download} />
67
+ </DownloadLink>
6.9.1/code/shared/Widgets.svelte ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Copy from "./Copy.svelte";
3
+ import Download from "./Download.svelte";
4
+ import { IconButtonWrapper } from "@gradio/atoms";
5
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
6
+
7
+ interface Props {
8
+ value: string;
9
+ language: string;
10
+ buttons?: (string | CustomButtonType)[] | null;
11
+ on_custom_button_click?: ((id: number) => void) | null;
12
+ }
13
+
14
+ let {
15
+ value,
16
+ language,
17
+ buttons = null,
18
+ on_custom_button_click = null
19
+ }: Props = $props();
20
+ </script>
21
+
22
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
23
+ {#if buttons?.some((btn) => typeof btn === "string" && btn === "download")}
24
+ <Download {value} {language} />
25
+ {/if}
26
+ {#if buttons?.some((btn) => typeof btn === "string" && btn === "copy")}
27
+ <Copy {value} />
28
+ {/if}
29
+ </IconButtonWrapper>
6.9.1/code/shared/extensions.ts ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Extension } from "@codemirror/state";
2
+ import {
3
+ lineNumbers,
4
+ highlightSpecialChars,
5
+ drawSelection,
6
+ rectangularSelection,
7
+ crosshairCursor,
8
+ keymap
9
+ } from "@codemirror/view";
10
+ export { EditorView } from "@codemirror/view";
11
+ import { EditorState } from "@codemirror/state";
12
+ import {
13
+ foldGutter,
14
+ indentOnInput,
15
+ syntaxHighlighting,
16
+ defaultHighlightStyle,
17
+ foldKeymap
18
+ } from "@codemirror/language";
19
+ import { history, defaultKeymap, historyKeymap } from "@codemirror/commands";
20
+ import {
21
+ closeBrackets,
22
+ closeBracketsKeymap,
23
+ completionKeymap
24
+ } from "@codemirror/autocomplete";
25
+ import { lintKeymap } from "@codemirror/lint";
26
+
27
+ export const basicSetup: Extension = /*@__PURE__*/ ((): Extension[] => [
28
+ highlightSpecialChars(),
29
+ history(),
30
+ foldGutter(),
31
+ drawSelection(),
32
+ EditorState.allowMultipleSelections.of(true),
33
+ indentOnInput(),
34
+ syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
35
+ closeBrackets(),
36
+ rectangularSelection(),
37
+ crosshairCursor(),
38
+
39
+ keymap.of([
40
+ ...closeBracketsKeymap,
41
+ ...defaultKeymap,
42
+ ...historyKeymap,
43
+ ...foldKeymap,
44
+ ...completionKeymap,
45
+ ...lintKeymap
46
+ ])
47
+ ])();
6.9.1/code/shared/frontmatter.ts ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ Element,
3
+ MarkdownExtension,
4
+ BlockContext,
5
+ Line
6
+ } from "@lezer/markdown";
7
+ import { parseMixed } from "@lezer/common";
8
+ import { yaml } from "@codemirror/legacy-modes/mode/yaml";
9
+ import { foldInside, foldNodeProp, StreamLanguage } from "@codemirror/language";
10
+ import { styleTags, tags } from "@lezer/highlight";
11
+
12
+ const frontMatterFence = /^---\s*$/m;
13
+
14
+ export const frontmatter: MarkdownExtension = {
15
+ defineNodes: [{ name: "Frontmatter", block: true }, "FrontmatterMark"],
16
+ props: [
17
+ styleTags({
18
+ Frontmatter: [tags.documentMeta, tags.monospace],
19
+ FrontmatterMark: tags.processingInstruction
20
+ }),
21
+ foldNodeProp.add({
22
+ Frontmatter: foldInside,
23
+ FrontmatterMark: () => null
24
+ })
25
+ ],
26
+ wrap: parseMixed((node) => {
27
+ const { parser } = StreamLanguage.define(yaml);
28
+ if (node.type.name === "Frontmatter") {
29
+ return {
30
+ parser,
31
+ overlay: [{ from: node.from + 4, to: node.to - 4 }]
32
+ };
33
+ }
34
+ return null;
35
+ }),
36
+ parseBlock: [
37
+ {
38
+ name: "Frontmatter",
39
+ before: "HorizontalRule",
40
+ parse: (cx: BlockContext, line: Line): boolean => {
41
+ let end: number | undefined = undefined;
42
+ const children = new Array<Element>();
43
+ if (cx.lineStart === 0 && frontMatterFence.test(line.text)) {
44
+ children.push(cx.elt("FrontmatterMark", 0, 4));
45
+ while (cx.nextLine()) {
46
+ if (frontMatterFence.test(line.text)) {
47
+ end = cx.lineStart + 4;
48
+ break;
49
+ }
50
+ }
51
+ if (end !== undefined) {
52
+ children.push(cx.elt("FrontmatterMark", end - 4, end));
53
+ cx.addElement(cx.elt("Frontmatter", 0, end, children));
54
+ }
55
+ return true;
56
+ }
57
+ return false;
58
+ }
59
+ }
60
+ ]
61
+ };
6.9.1/code/shared/language.ts ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Extension } from "@codemirror/state";
2
+ import { StreamLanguage } from "@codemirror/language";
3
+ import { _ } from "svelte-i18n";
4
+
5
+ const sql_dialects = [
6
+ "standardSQL",
7
+ "msSQL",
8
+ "mySQL",
9
+ "mariaDB",
10
+ "sqlite",
11
+ "cassandra",
12
+ "plSQL",
13
+ "hive",
14
+ "pgSQL",
15
+ "gql",
16
+ "gpSQL",
17
+ "sparkSQL",
18
+ "esper"
19
+ ] as const;
20
+
21
+ const lang_map: Record<string, (() => Promise<Extension>) | undefined> = {
22
+ python: () => import("@codemirror/lang-python").then((m) => m.python()),
23
+ c: () =>
24
+ import("@codemirror/legacy-modes/mode/clike").then((m) =>
25
+ StreamLanguage.define(m.c)
26
+ ),
27
+ cpp: () =>
28
+ import("@codemirror/legacy-modes/mode/clike").then((m) =>
29
+ StreamLanguage.define(m.cpp)
30
+ ),
31
+ markdown: async () => {
32
+ const [md, frontmatter] = await Promise.all([
33
+ import("@codemirror/lang-markdown"),
34
+ import("./frontmatter")
35
+ ]);
36
+ return md.markdown({ extensions: [frontmatter.frontmatter] });
37
+ },
38
+ latex: () =>
39
+ import("@codemirror/legacy-modes/mode/stex").then((m) =>
40
+ StreamLanguage.define(m.stex)
41
+ ),
42
+ json: () => import("@codemirror/lang-json").then((m) => m.json()),
43
+ html: () => import("@codemirror/lang-html").then((m) => m.html()),
44
+ css: () => import("@codemirror/lang-css").then((m) => m.css()),
45
+ javascript: () =>
46
+ import("@codemirror/lang-javascript").then((m) => m.javascript()),
47
+ jinja2: () =>
48
+ import("@codemirror/legacy-modes/mode/jinja2").then((m) =>
49
+ StreamLanguage.define(m.jinja2)
50
+ ),
51
+ typescript: () =>
52
+ import("@codemirror/lang-javascript").then((m) =>
53
+ m.javascript({ typescript: true })
54
+ ),
55
+ yaml: () =>
56
+ import("@codemirror/legacy-modes/mode/yaml").then((m) =>
57
+ StreamLanguage.define(m.yaml)
58
+ ),
59
+ dockerfile: () =>
60
+ import("@codemirror/legacy-modes/mode/dockerfile").then((m) =>
61
+ StreamLanguage.define(m.dockerFile)
62
+ ),
63
+ shell: () =>
64
+ import("@codemirror/legacy-modes/mode/shell").then((m) =>
65
+ StreamLanguage.define(m.shell)
66
+ ),
67
+ r: () =>
68
+ import("@codemirror/legacy-modes/mode/r").then((m) =>
69
+ StreamLanguage.define(m.r)
70
+ ),
71
+ sql: () =>
72
+ import("@codemirror/legacy-modes/mode/sql").then((m) =>
73
+ StreamLanguage.define(m.standardSQL)
74
+ ),
75
+ ...Object.fromEntries(
76
+ sql_dialects.map((dialect) => [
77
+ "sql-" + dialect,
78
+ () =>
79
+ import("@codemirror/legacy-modes/mode/sql").then((m) =>
80
+ StreamLanguage.define(m[dialect])
81
+ )
82
+ ])
83
+ )
84
+ } as const;
85
+
86
+ const alias_map: Record<string, string> = {
87
+ py: "python",
88
+ md: "markdown",
89
+ js: "javascript",
90
+ ts: "typescript",
91
+ sh: "shell"
92
+ };
93
+
94
+ export async function getLanguageExtension(
95
+ lang: string
96
+ ): Promise<Extension | undefined> {
97
+ const _lang = lang_map[lang] || lang_map[alias_map[lang]] || undefined;
98
+ if (_lang) {
99
+ return _lang();
100
+ }
101
+ return undefined;
102
+ }
6.9.1/code/types.ts ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { CustomButton } from "@gradio/utils";
2
+
3
+ export interface CodeProps {
4
+ value: string;
5
+ language: string;
6
+ max_lines: number;
7
+ wrap_lines: boolean;
8
+ show_line_numbers: boolean;
9
+ autocomplete: boolean;
10
+ lines: number;
11
+ buttons: (string | CustomButton)[] | null;
12
+ }
13
+
14
+ export interface CodeEvents {
15
+ change: any;
16
+ input: any;
17
+ focus: any;
18
+ blur: any;
19
+ clear_status: any;
20
+ custom_button_click: { id: number };
21
+ }