freddyaboulton HF Staff commited on
Commit
a049641
·
verified ·
1 Parent(s): 7fd969e

Upload folder using huggingface_hub

Browse files
6.0.0/code/Example.svelte ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string | null;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+
6
+ function truncate_text(text: string | null, max_length = 60): string {
7
+ if (!text) return "";
8
+ const str = String(text);
9
+ if (str.length <= max_length) return str;
10
+ return str.slice(0, max_length) + "...";
11
+ }
12
+ </script>
13
+
14
+ <pre
15
+ class:table={type === "table"}
16
+ class:gallery={type === "gallery"}
17
+ class:selected>{truncate_text(value)}</pre>
18
+
19
+ <style>
20
+ pre {
21
+ text-align: left;
22
+ }
23
+ .gallery {
24
+ padding: var(--size-1) var(--size-2);
25
+ }
26
+ </style>
6.0.0/code/Index.svelte ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 language={gradio.props.language} value={gradio.props.value} />
73
+
74
+ <Code
75
+ bind:value={gradio.props.value}
76
+ language={gradio.props.language}
77
+ lines={gradio.props.lines}
78
+ max_lines={gradio.props.max_lines}
79
+ {dark_mode}
80
+ wrap_lines={gradio.props.wrap_lines}
81
+ show_line_numbers={gradio.props.show_line_numbers}
82
+ autocomplete={gradio.props.autocomplete}
83
+ readonly={!gradio.shared.interactive}
84
+ on:blur={() => gradio.dispatch("blur")}
85
+ on:focus={() => gradio.dispatch("focus")}
86
+ on:input={() => gradio.dispatch("input")}
87
+ />
88
+ {/if}
89
+ </Block>
6.0.0/code/package.json ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/code",
3
+ "version": "0.16.0",
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.43.4"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/gradio-app/gradio.git",
62
+ "directory": "js/code"
63
+ }
64
+ }
6.0.0/code/shared/Code.svelte ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, 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
+ export let class_names = "";
20
+ export let value = "";
21
+ export let dark_mode: boolean;
22
+ export let basic = true;
23
+ export let language: string;
24
+ export let lines = 5;
25
+ export let max_lines: number | null = null;
26
+ export let extensions: Extension[] = [];
27
+ export let use_tab = true;
28
+ export let readonly = false;
29
+ export let placeholder: string | HTMLElement | null | undefined = undefined;
30
+ export let wrap_lines = false;
31
+ export let show_line_numbers = true;
32
+ export let autocomplete = false;
33
+
34
+ const dispatch = createEventDispatcher<{
35
+ change: string;
36
+ blur: undefined;
37
+ focus: undefined;
38
+ input: undefined;
39
+ }>();
40
+ let lang_extension: Extension | undefined;
41
+ let element: HTMLDivElement;
42
+ let view: EditorView;
43
+
44
+ $: get_lang(language);
45
+
46
+ async function get_lang(val: string): Promise<void> {
47
+ const ext = await getLanguageExtension(val);
48
+ lang_extension = ext;
49
+ }
50
+
51
+ $: (reconfigure(), lang_extension, readonly);
52
+ $: set_doc(value);
53
+ $: update_lines();
54
+
55
+ function set_doc(new_doc: string): void {
56
+ if (view && new_doc !== view.state.doc.toString()) {
57
+ view.dispatch({
58
+ changes: {
59
+ from: 0,
60
+ to: view.state.doc.length,
61
+ insert: new_doc
62
+ }
63
+ });
64
+ }
65
+ }
66
+
67
+ function update_lines(): void {
68
+ if (view) {
69
+ view.requestMeasure({ read: resize });
70
+ }
71
+ }
72
+
73
+ function create_editor_view(): EditorView {
74
+ const editorView = new EditorView({
75
+ parent: element,
76
+ state: create_editor_state(value)
77
+ });
78
+ editorView.dom.addEventListener("focus", handle_focus, true);
79
+ editorView.dom.addEventListener("blur", handle_blur, true);
80
+ return editorView;
81
+ }
82
+
83
+ function handle_focus(): void {
84
+ dispatch("focus");
85
+ }
86
+
87
+ function handle_blur(): void {
88
+ dispatch("blur");
89
+ }
90
+
91
+ function getGutterLineHeight(_view: EditorView): string | null {
92
+ let elements = _view.dom.querySelectorAll<HTMLElement>(".cm-gutterElement");
93
+ if (elements.length === 0) {
94
+ return null;
95
+ }
96
+ for (var i = 0; i < elements.length; i++) {
97
+ let node = elements[i];
98
+ let height = getComputedStyle(node)?.height ?? "0px";
99
+ if (height != "0px") {
100
+ return height;
101
+ }
102
+ }
103
+ return null;
104
+ }
105
+
106
+ function resize(_view: EditorView): any {
107
+ let scroller = _view.dom.querySelector<HTMLElement>(".cm-scroller");
108
+ if (!scroller) {
109
+ return null;
110
+ }
111
+ const lineHeight = getGutterLineHeight(_view);
112
+ if (!lineHeight) {
113
+ return null;
114
+ }
115
+
116
+ const minLines = lines == 1 ? 1 : lines + 1;
117
+ scroller.style.minHeight = `calc(${lineHeight} * ${minLines})`;
118
+ if (max_lines)
119
+ scroller.style.maxHeight = `calc(${lineHeight} * ${max_lines + 1})`;
120
+ }
121
+
122
+ import { Transaction } from "@codemirror/state";
123
+
124
+ function is_user_input(update: ViewUpdate): boolean {
125
+ return update.transactions.some(
126
+ (tr) => tr.annotation(Transaction.userEvent) != null
127
+ );
128
+ }
129
+
130
+ function handle_change(vu: ViewUpdate): void {
131
+ if (!vu.docChanged) return;
132
+
133
+ const doc = vu.state.doc;
134
+ const text = doc.toString();
135
+ value = text;
136
+
137
+ const user_change = is_user_input(vu);
138
+ if (user_change) {
139
+ dispatch("change", text);
140
+ dispatch("input");
141
+ } else {
142
+ dispatch("change", text);
143
+ }
144
+
145
+ view.requestMeasure({ read: resize });
146
+ }
147
+
148
+ function get_extensions(): Extension[] {
149
+ const stateExtensions = [
150
+ ...get_base_extensions(
151
+ basic,
152
+ use_tab,
153
+ placeholder,
154
+ readonly,
155
+ lang_extension,
156
+ show_line_numbers
157
+ ),
158
+ FontTheme,
159
+ ...get_theme(),
160
+ ...extensions
161
+ ];
162
+ return stateExtensions;
163
+ }
164
+
165
+ const FontTheme = EditorView.theme({
166
+ "&": {
167
+ fontSize: "var(--text-sm)",
168
+ backgroundColor: "var(--border-color-secondary)"
169
+ },
170
+ ".cm-content": {
171
+ paddingTop: "5px",
172
+ paddingBottom: "5px",
173
+ color: "var(--body-text-color)",
174
+ fontFamily: "var(--font-mono)",
175
+ minHeight: "100%"
176
+ },
177
+ ".cm-gutterElement": {
178
+ marginRight: "var(--spacing-xs)"
179
+ },
180
+ ".cm-gutters": {
181
+ marginRight: "1px",
182
+ borderRight: "1px solid var(--border-color-primary)",
183
+ backgroundColor: "var(--block-background-fill);",
184
+ color: "var(--body-text-color-subdued)"
185
+ },
186
+ ".cm-focused": {
187
+ outline: "none"
188
+ },
189
+ ".cm-scroller": {
190
+ height: "auto"
191
+ },
192
+ ".cm-cursor": {
193
+ borderLeftColor: "var(--body-text-color)"
194
+ }
195
+ });
196
+
197
+ const AutocompleteTheme = EditorView.theme({
198
+ ".cm-tooltip-autocomplete": {
199
+ "& > ul": {
200
+ backgroundColor: "var(--background-fill-primary)",
201
+ color: "var(--body-text-color)"
202
+ },
203
+ "& > ul > li[aria-selected]": {
204
+ backgroundColor: "var(--color-accent-soft)",
205
+ color: "var(--body-text-color)"
206
+ }
207
+ }
208
+ });
209
+
210
+ function create_editor_state(_value: string | null | undefined): EditorState {
211
+ return EditorState.create({
212
+ doc: _value ?? undefined,
213
+ extensions: get_extensions()
214
+ });
215
+ }
216
+
217
+ function get_base_extensions(
218
+ basic: boolean,
219
+ use_tab: boolean,
220
+ placeholder: string | HTMLElement | null | undefined,
221
+ readonly: boolean,
222
+ lang: Extension | null | undefined,
223
+ show_line_numbers: boolean
224
+ ): Extension[] {
225
+ const extensions: Extension[] = [
226
+ EditorView.editable.of(!readonly),
227
+ EditorState.readOnly.of(readonly),
228
+ EditorView.contentAttributes.of({ "aria-label": "Code input container" })
229
+ ];
230
+
231
+ if (basic) {
232
+ extensions.push(basicSetup);
233
+ }
234
+ if (use_tab) {
235
+ extensions.push(
236
+ keymap.of([{ key: "Tab", run: acceptCompletion }, indentWithTab])
237
+ );
238
+ }
239
+ if (placeholder) {
240
+ extensions.push(placeholderExt(placeholder));
241
+ }
242
+ if (lang) {
243
+ extensions.push(lang);
244
+ }
245
+ if (show_line_numbers) {
246
+ extensions.push(lineNumbers());
247
+ }
248
+ if (autocomplete) {
249
+ extensions.push(autocompletion());
250
+ extensions.push(AutocompleteTheme);
251
+ }
252
+
253
+ extensions.push(EditorView.updateListener.of(handle_change));
254
+ if (wrap_lines) {
255
+ extensions.push(EditorView.lineWrapping);
256
+ }
257
+
258
+ return extensions;
259
+ }
260
+
261
+ function get_theme(): Extension[] {
262
+ const extensions: Extension[] = [];
263
+
264
+ if (dark_mode) {
265
+ extensions.push(basicDark);
266
+ } else {
267
+ extensions.push(basicLight);
268
+ }
269
+ return extensions;
270
+ }
271
+
272
+ function reconfigure(): void {
273
+ view?.dispatch({
274
+ effects: StateEffect.reconfigure.of(get_extensions())
275
+ });
276
+ }
277
+
278
+ onMount(() => {
279
+ view = create_editor_view();
280
+ return () => view?.destroy();
281
+ });
282
+ </script>
283
+
284
+ <div class="wrap">
285
+ <div class="codemirror-wrapper {class_names}" bind:this={element} />
286
+ </div>
287
+
288
+ <style>
289
+ .wrap {
290
+ display: flex;
291
+ flex-direction: column;
292
+ flex-grow: 1;
293
+ margin: 0;
294
+ padding: 0;
295
+ height: 100%;
296
+ }
297
+ .codemirror-wrapper {
298
+ flex-grow: 1;
299
+ overflow: auto;
300
+ }
301
+
302
+ :global(.cm-editor) {
303
+ height: 100%;
304
+ }
305
+
306
+ /* Dunno why this doesn't work through the theme API -- don't remove*/
307
+ :global(.cm-selectionBackground) {
308
+ background-color: #b9d2ff30 !important;
309
+ }
310
+
311
+ :global(.cm-focused) {
312
+ outline: none !important;
313
+ }
314
+ </style>
6.0.0/code/shared/Copy.svelte ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onDestroy } from "svelte";
3
+ import { Copy, Check } from "@gradio/icons";
4
+ import { IconButton } from "@gradio/atoms";
5
+
6
+ let copied = false;
7
+ export let value: string;
8
+ let timer: NodeJS.Timeout;
9
+
10
+ function copy_feedback(): void {
11
+ copied = true;
12
+ if (timer) clearTimeout(timer);
13
+ timer = setTimeout(() => {
14
+ copied = false;
15
+ }, 2000);
16
+ }
17
+
18
+ async function handle_copy(): Promise<void> {
19
+ if ("clipboard" in navigator) {
20
+ await navigator.clipboard.writeText(value);
21
+ copy_feedback();
22
+ }
23
+ }
24
+
25
+ onDestroy(() => {
26
+ if (timer) clearTimeout(timer);
27
+ });
28
+ </script>
29
+
30
+ <IconButton Icon={copied ? Check : Copy} on:click={handle_copy} />
6.0.0/code/shared/Download.svelte ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ export let value: string;
8
+ export let language: string;
9
+
10
+ $: ext = get_ext_for_type(language);
11
+
12
+ function get_ext_for_type(type: string): string {
13
+ const exts: Record<string, string> = {
14
+ py: "py",
15
+ python: "py",
16
+ md: "md",
17
+ markdown: "md",
18
+ json: "json",
19
+ html: "html",
20
+ css: "css",
21
+ js: "js",
22
+ javascript: "js",
23
+ ts: "ts",
24
+ typescript: "ts",
25
+ yaml: "yaml",
26
+ yml: "yml",
27
+ dockerfile: "dockerfile",
28
+ sh: "sh",
29
+ shell: "sh",
30
+ r: "r",
31
+ c: "c",
32
+ cpp: "cpp",
33
+ latex: "tex"
34
+ };
35
+
36
+ return exts[type] || "txt";
37
+ }
38
+
39
+ let copied = false;
40
+ let timer: NodeJS.Timeout;
41
+
42
+ function copy_feedback(): void {
43
+ copied = true;
44
+ if (timer) clearTimeout(timer);
45
+ timer = setTimeout(() => {
46
+ copied = false;
47
+ }, 2000);
48
+ }
49
+
50
+ $: download_value = URL.createObjectURL(new Blob([value]));
51
+
52
+ onDestroy(() => {
53
+ if (timer) clearTimeout(timer);
54
+ });
55
+ </script>
56
+
57
+ <DownloadLink
58
+ download="file.{ext}"
59
+ href={download_value}
60
+ on:click={copy_feedback}
61
+ >
62
+ <IconButton Icon={copied ? Check : Download} />
63
+ </DownloadLink>
6.0.0/code/shared/Widgets.svelte ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Copy from "./Copy.svelte";
3
+ import Download from "./Download.svelte";
4
+ import { IconButtonWrapper } from "@gradio/atoms";
5
+
6
+ export let value: string;
7
+ export let language: string;
8
+ </script>
9
+
10
+ <IconButtonWrapper>
11
+ <Download {value} {language} />
12
+ <Copy {value} />
13
+ </IconButtonWrapper>
6.0.0/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.0.0/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.0.0/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.0.0/code/types.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export interface CodeProps {
2
+ value: string;
3
+ language: string;
4
+ max_lines: number;
5
+ wrap_lines: boolean;
6
+ show_line_numbers: boolean;
7
+ autocomplete: boolean;
8
+ lines: number;
9
+ }
10
+
11
+ export interface CodeEvents {
12
+ change: any;
13
+ input: any;
14
+ focus: any;
15
+ blur: any;
16
+ clear_status: any;
17
+ }