Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import build from "https://esm.sh/build";
const ret = await build({
dependencies: {
"@codemirror/view": "^6",
"@codemirror/autocomplete": "^6",
"@codemirror/lang-javascript": "^6",
"@codemirror/lint": "^6",
"@codemirror/state": "^6",
"@typescript/vfs": "^1.5.0",
"codemirror": "^6",
"typescript": "^5.2.2",
"@valtown/codemirror-ts": "1.0.0",
},
source: `
export { EditorView, basicSetup } from "codemirror";
export { javascript } from "@codemirror/lang-javascript";
export { autocompletion } from "@codemirror/autocomplete";
export {
createDefaultMapFromCDN,
createSystem,
createVirtualTypeScriptEnvironment,
} from "@typescript/vfs";
export { ScriptElementKind, ModuleDetectionKind } from "typescript";
export {
tsLinter,
tsHover,
tsAutocomplete,
tsSync,
} from "@valtown/codemirror-ts";
`,
});
const {
autocompletion,
basicSetup,
createDefaultMapFromCDN,
createSystem,
createVirtualTypeScriptEnvironment,
ModuleDetectionKind,
EditorView,
javascript,
tsAutocomplete,
tsHover,
tsLinter,
tsSync,
} = await import(ret.url + "?bundle-deps");
import ts from "https://esm.sh/typescript@5.4.5";
for (const textarea of document.querySelectorAll("form textarea.for-codemirror")) {
const form = textarea.closest("form");
const submitButton = form.querySelector("button[type=\"submit\"], input[type=\"submit\"]");
textarea.setAttribute("hidden", "true");
const parent = textarea.parentElement;
const newDiv = document.createElement("div");
parent.insertBefore(newDiv, textarea);
const compilerOpts = {
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
jsx: ts.JsxEmit.React,
lib: ["esnext", "dom"],
moduleDetection: ModuleDetectionKind.Force,
strict: true,
allowJs: true,
};
const fsMap = await createDefaultMapFromCDN(
compilerOpts,
"5.2.2",
true,
ts,
);
const system = createSystem(fsMap);
const env = createVirtualTypeScriptEnvironment(system, [], ts, compilerOpts);
const path = "index.ts";
const editor = new EditorView({
doc: textarea.value,
extensions: [
basicSetup,
javascript({
typescript: true,
jsx: true,
}),
tsSync({ env, path }),
tsLinter({ env, path }),
autocompletion({
override: [tsAutocomplete({ env, path })],
}),
tsHover({
env,
path,
}),
],
parent: newDiv,
});
const syncEditor = (e) => {
textarea.value = editor.state.sliceDoc();
console.log(textarea.value);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 29, 2024