|
1 | | -import * as shiki from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' |
2 | | -import { shikiToMonaco } from 'https://cdn.jsdelivr.net/npm/@shikijs/[email protected]/+esm' |
3 | | -import * as monaco from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' |
4 | | - |
5 | | -const colorSchemeMetaTag = document.querySelector('meta[name="color-scheme"]'); |
6 | | -if (colorSchemeMetaTag) { |
7 | | - // Create a MutationObserver to watch for attribute changes |
8 | | - const observer = new MutationObserver(() => updateTheme()); |
9 | | - // Start observing the `content` attribute on the meta tag |
10 | | - observer.observe(colorSchemeMetaTag, { |
11 | | - attributes: true, // Only observe attribute changes |
12 | | - attributeFilter: ["content"], // Only watch the `content` attribute |
13 | | - }); |
14 | | -} |
15 | | - |
16 | | -setUpEditor().then(() => { |
17 | | - for (let element of document.getElementsByClassName('code-editor')) { |
18 | | - createEditor(element, null, true, true) |
19 | | - } |
20 | | - |
21 | | - for (let element of document.getElementsByClassName('playground-editor')) { |
22 | | - createEditor(element, null, true, false) |
23 | | - } |
24 | | - |
25 | | - // for (let element of document.getElementsByClassName('result-editor')) { |
26 | | - // createEditor(element, null, false, true) |
27 | | - // } |
28 | | -}) |
29 | | - |
30 | | -const DARK_THEME = 'slack-dark'; |
31 | | -const LIGHT_THEME = 'slack-ochin'; |
32 | | - |
33 | | -async function createEditor(element, overrideContent = null, doHighlight = true, editorReadOnly = true) { |
34 | | - let content = element.textContent.trim().replace(/^```|```$/g, '').trim(); |
35 | | - element.textContent = undefined; |
36 | | - if (overrideContent) { |
37 | | - content = overrideContent.trim(); |
38 | | - } |
39 | | - let language = "text"; |
40 | | - if (doHighlight) language = 'saki'; |
41 | | - let darkMode = document.querySelector('meta[name="color-scheme"]').getAttribute("content"); |
42 | | - |
43 | | - let editorConfig = { |
44 | | - value: content, |
45 | | - language: language, |
46 | | - theme: darkMode === 'dark' ? DARK_THEME : LIGHT_THEME, |
47 | | - automaticLayout: true, |
48 | | - minimap: { enabled: false }, // Disable minimap if desired |
49 | | - scrollBeyondLastLine: !editorReadOnly, // Prevent scrolling beyond the last line |
50 | | - scrollbar: { |
51 | | - vertical: editorReadOnly ? "hidden" : "visible", // Disables vertical scroll bar |
52 | | - alwaysConsumeMouseWheel: !editorReadOnly, |
53 | | - }, |
54 | | - cursorSmoothCaretAnimation: 'on', |
55 | | - readOnly: editorReadOnly, |
56 | | - cursorBlinking: editorReadOnly ? 'hidden' : 'smooth', |
57 | | - unicodeHighlight: { ambiguousCharacters: false }, |
58 | | - lineNumbersMinChars: 4, |
59 | | - lineDecorationsWidth: 0, |
60 | | - lineHeight: 1.5, |
61 | | - padding: { top: 10, bottom: 10 } |
62 | | - } |
63 | | - |
64 | | - if (editorReadOnly && content.split('\n').length < 100) { |
65 | | - editorConfig.renderLineHighlight = 'none'; |
66 | | - editorConfig.lineNumbers = 'off'; |
67 | | - editorConfig.glyphMargin = false; |
68 | | - editorConfig.folding = false; |
69 | | - editorConfig.lineDecorationsWidth = 20; |
70 | | - editorConfig.lineNumbersMinChars = 0; |
71 | | - editorConfig.overviewRulerLanes = 0; |
72 | | - } |
73 | | - |
74 | | - if (!doHighlight) { |
75 | | - editorConfig.wordWrap = 'on'; |
76 | | - } |
77 | | - |
78 | | - let codeEditor = await monaco.editor.create(element, editorConfig); |
79 | | - |
80 | | - if (editorReadOnly) { |
81 | | - const resizeEditor = () => { |
82 | | - const editorHeight = codeEditor.getContentHeight(); // minimum height |
83 | | - codeEditor.layout({ width: codeEditor.getLayoutInfo().width, height: editorHeight }); |
84 | | - }; |
85 | | - codeEditor.onDidContentSizeChange(resizeEditor); |
86 | | - } |
87 | | - |
88 | | - element.codeEditor = codeEditor; |
89 | | - updateTheme(); |
90 | | -} |
91 | | - |
92 | | -async function setUpEditor() { |
93 | | - const grammar = (await fetch('/assets/Saki.tmLanguage.json')).json(); |
94 | | - const langConf = (await fetch('/assets/language-configuration.json')).json(); |
95 | | - |
96 | | - let lightTheme = (await shiki.bundledThemes[LIGHT_THEME]()).default; |
97 | | - lightTheme.colors['editor.background'] = '#F8F8F8'; |
98 | | - |
99 | | - const highlighter = await shiki.createHighlighter({ |
100 | | - themes: [ |
101 | | - DARK_THEME, |
102 | | - lightTheme, |
103 | | - ], |
104 | | - langs: [ grammar ], |
105 | | - }); |
106 | | - monaco.languages.register({ id: 'saki' }); |
107 | | - monaco.languages.setLanguageConfiguration('saki', langConf); |
108 | | - await shikiToMonaco(highlighter, monaco); |
109 | | -} |
110 | | - |
111 | | -async function updateTheme() { |
112 | | - let darkMode = document.querySelector('meta[name="color-scheme"]').getAttribute("content"); |
113 | | - monaco.editor.setTheme(darkMode === 'dark' ? DARK_THEME : LIGHT_THEME); |
114 | | -} |
115 | | - |
116 | | -export async function runCodeInEditor(codeEditorElementId, resultEditorElementId, isEval = false) { |
117 | | - let codeEditorElement = document.getElementById(codeEditorElementId); |
118 | | - let resultEditorElement = document.getElementById(resultEditorElementId); |
119 | | - await createEditor(resultEditorElement, null, false, true); |
120 | | - let code = codeEditorElement.codeEditor.getValue(); |
121 | | - resultEditorElement.codeEditor.setValue("Running..."); |
122 | | - resultEditorElement.style.display = "flex"; |
123 | | - fetch("https://api.saki-lang.tech/v1/exec", { |
124 | | - method: "POST", |
125 | | - headers: { |
126 | | - "Content-Type": "application/json" |
127 | | - }, |
128 | | - body: JSON.stringify({ |
129 | | - sandbox: "saki", |
130 | | - command: "run", |
131 | | - files: { "": isEval ? "eval {\n" + code + "\n}" : code }, |
132 | | - }), |
133 | | - signal: AbortSignal.timeout(5000) |
134 | | - }).then(response => response.json()).then(data => { |
135 | | - resultEditorElement.codeEditor.setValue(data.stdout); |
136 | | - }).catch(error => { |
137 | | - resultEditorElement.codeEditor.setValue(error.message); |
138 | | - }); |
139 | | -} |
140 | | - |
141 | | -window.runCodeInEditor = runCodeInEditor; |
| 1 | +import * as shiki from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' |
| 2 | +import { shikiToMonaco } from 'https://cdn.jsdelivr.net/npm/@shikijs/[email protected]/+esm' |
| 3 | +import * as monaco from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm' |
| 4 | + |
| 5 | +const colorSchemeMetaTag = document.querySelector('meta[name="color-scheme"]'); |
| 6 | +if (colorSchemeMetaTag) { |
| 7 | + // Create a MutationObserver to watch for attribute changes |
| 8 | + const observer = new MutationObserver(() => updateTheme()); |
| 9 | + // Start observing the `content` attribute on the meta tag |
| 10 | + observer.observe(colorSchemeMetaTag, { |
| 11 | + attributes: true, // Only observe attribute changes |
| 12 | + attributeFilter: ["content"], // Only watch the `content` attribute |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +setUpEditor().then(() => { |
| 17 | + for (let element of document.getElementsByClassName('code-editor')) { |
| 18 | + createEditor(element, null, true, true) |
| 19 | + } |
| 20 | + |
| 21 | + for (let element of document.getElementsByClassName('playground-editor')) { |
| 22 | + createEditor(element, null, true, false) |
| 23 | + } |
| 24 | + |
| 25 | + // for (let element of document.getElementsByClassName('result-editor')) { |
| 26 | + // createEditor(element, null, false, true) |
| 27 | + // } |
| 28 | +}) |
| 29 | + |
| 30 | +const DARK_THEME = 'slack-dark'; |
| 31 | +const LIGHT_THEME = 'slack-ochin'; |
| 32 | + |
| 33 | +async function createEditor(element, overrideContent = null, doHighlight = true, editorReadOnly = true) { |
| 34 | + let content = element.textContent.trim().replace(/^```|```$/g, '').trim(); |
| 35 | + element.textContent = undefined; |
| 36 | + if (overrideContent) { |
| 37 | + content = overrideContent.trim(); |
| 38 | + } |
| 39 | + let language = "text"; |
| 40 | + if (doHighlight) language = 'saki'; |
| 41 | + let darkMode = document.querySelector('meta[name="color-scheme"]').getAttribute("content"); |
| 42 | + |
| 43 | + let editorConfig = { |
| 44 | + value: content, |
| 45 | + language: language, |
| 46 | + theme: darkMode === 'dark' ? DARK_THEME : LIGHT_THEME, |
| 47 | + automaticLayout: true, |
| 48 | + minimap: { enabled: false }, // Disable minimap if desired |
| 49 | + scrollBeyondLastLine: !editorReadOnly, // Prevent scrolling beyond the last line |
| 50 | + scrollbar: { |
| 51 | + vertical: editorReadOnly ? "hidden" : "visible", // Disables vertical scroll bar |
| 52 | + alwaysConsumeMouseWheel: !editorReadOnly, |
| 53 | + }, |
| 54 | + cursorSmoothCaretAnimation: 'on', |
| 55 | + readOnly: editorReadOnly, |
| 56 | + cursorBlinking: editorReadOnly ? 'hidden' : 'smooth', |
| 57 | + unicodeHighlight: { ambiguousCharacters: false }, |
| 58 | + lineNumbersMinChars: 4, |
| 59 | + lineDecorationsWidth: 0, |
| 60 | + lineHeight: 1.5, |
| 61 | + padding: { top: 10, bottom: 10 } |
| 62 | + } |
| 63 | + |
| 64 | + if (editorReadOnly && content.split('\n').length < 100) { |
| 65 | + editorConfig.renderLineHighlight = 'none'; |
| 66 | + editorConfig.lineNumbers = 'off'; |
| 67 | + editorConfig.glyphMargin = false; |
| 68 | + editorConfig.folding = false; |
| 69 | + editorConfig.lineDecorationsWidth = 20; |
| 70 | + editorConfig.lineNumbersMinChars = 0; |
| 71 | + editorConfig.overviewRulerLanes = 0; |
| 72 | + } |
| 73 | + |
| 74 | + if (!doHighlight) { |
| 75 | + editorConfig.wordWrap = 'on'; |
| 76 | + } |
| 77 | + |
| 78 | + let codeEditor = await monaco.editor.create(element, editorConfig); |
| 79 | + |
| 80 | + if (editorReadOnly) { |
| 81 | + const resizeEditor = () => { |
| 82 | + const editorHeight = codeEditor.getContentHeight(); // minimum height |
| 83 | + codeEditor.layout({ width: codeEditor.getLayoutInfo().width, height: editorHeight }); |
| 84 | + }; |
| 85 | + codeEditor.onDidContentSizeChange(resizeEditor); |
| 86 | + } |
| 87 | + |
| 88 | + element.codeEditor = codeEditor; |
| 89 | + updateTheme(); |
| 90 | +} |
| 91 | + |
| 92 | +async function setUpEditor() { |
| 93 | + const grammar = (await fetch('/assets/Saki.tmLanguage.json')).json(); |
| 94 | + const langConf = (await fetch('/assets/language-configuration.json')).json(); |
| 95 | + |
| 96 | + let lightTheme = (await shiki.bundledThemes[LIGHT_THEME]()).default; |
| 97 | + lightTheme.colors['editor.background'] = '#F8F8F8'; |
| 98 | + |
| 99 | + const highlighter = await shiki.createHighlighter({ |
| 100 | + themes: [ |
| 101 | + DARK_THEME, |
| 102 | + lightTheme, |
| 103 | + ], |
| 104 | + langs: [ grammar ], |
| 105 | + }); |
| 106 | + monaco.languages.register({ id: 'saki' }); |
| 107 | + monaco.languages.setLanguageConfiguration('saki', langConf); |
| 108 | + await shikiToMonaco(highlighter, monaco); |
| 109 | +} |
| 110 | + |
| 111 | +async function updateTheme() { |
| 112 | + let darkMode = document.querySelector('meta[name="color-scheme"]').getAttribute("content"); |
| 113 | + monaco.editor.setTheme(darkMode === 'dark' ? DARK_THEME : LIGHT_THEME); |
| 114 | +} |
| 115 | + |
| 116 | +export async function runCodeInEditor(codeEditorElementId, resultEditorElementId, isEval = false) { |
| 117 | + let codeEditorElement = document.getElementById(codeEditorElementId); |
| 118 | + let resultEditorElement = document.getElementById(resultEditorElementId); |
| 119 | + await createEditor(resultEditorElement, null, false, true); |
| 120 | + let code = codeEditorElement.codeEditor.getValue(); |
| 121 | + resultEditorElement.codeEditor.setValue("Running..."); |
| 122 | + resultEditorElement.style.display = "flex"; |
| 123 | + await runCode(code, resultEditorElement, 1, isEval); |
| 124 | +} |
| 125 | + |
| 126 | +async function runCode(code, resultEditor, retries, isEval = false) { |
| 127 | + fetch("https://api.saki-lang.tech/v1/exec", { |
| 128 | + method: "POST", |
| 129 | + headers: { |
| 130 | + "Content-Type": "application/json" |
| 131 | + }, |
| 132 | + body: JSON.stringify({ |
| 133 | + sandbox: "saki", |
| 134 | + command: "run", |
| 135 | + files: { "": isEval ? "eval {\n" + code + "\n}" : code }, |
| 136 | + }), |
| 137 | + signal: AbortSignal.timeout(7000) |
| 138 | + }).then(response => response.json()).then(data => { |
| 139 | + resultEditor.codeEditor.setValue(data.stdout); |
| 140 | + }).catch(error => { |
| 141 | + if (retries > 0) { |
| 142 | + runCode(code, resultEditor, retries - 1, isEval); |
| 143 | + } else { |
| 144 | + resultEditor.codeEditor.setValue("Failed to send execution request to the server, please retry later: " + error.message); |
| 145 | + } |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +window.runCodeInEditor = runCodeInEditor; |
0 commit comments