forked from swift502/Sketchbook
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodeEditor.js
More file actions
104 lines (90 loc) · 4.22 KB
/
codeEditor.js
File metadata and controls
104 lines (90 loc) · 4.22 KB
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
101
102
103
104
require.config({
paths: {
vs: 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.51.0/min/vs',
}
});
var codeEditor;
window.editorApp = new Vue({
el: '#editorApp',
data: {
showEditor: true,
},
mounted() {
this.initializeEditor();
// Add event listener for window resize
window.addEventListener('resize', this.resizeEditor);
},
methods: {
async initializeEditor() {
await new Promise(resolve => requestAnimationFrame(resolve));
require(['vs/editor/editor.main'], async ()=> {
let classNames = await (fetch('paths.txt').then(r => r.text()));
classNames = classNames.replaceAll("\\", "/").replaceAll("\r", "");
classNames = classNames.split('\n');
classNames.push("src/main/helpers/helpers.js");
classNames.push("src/utils.js");
const LoadClass = async (className) => {
let three = className.includes("node_modules/@types/three/");
if (!className.includes("build/types/") && !three && !className.includes("peerjs/dist") && !className.startsWith("src/") &&
//!className.includes("tween.d.ts") &&
!className.includes("sweetalert2.d.ts")) return;
const text = await fetch(className).then(response => response.text()).catch(e => {
originalConsoleError("update paths.txt", className, e);
return "";
});
let code = text.replace(/export |import .*?;/gs, "");
//code = code.replaceAll("interface","class");
//let code = text.match(/export (?:declare )?(class [\s\S]*)/)?.[1] || text;
// if(className.includes("GLTFLoader"))debugger;
if (three && !className.includes("examples"))
code = "declare namespace THREE {" + code + "} "
if(className.includes("FunctionLibrary"))
code = "declare namespace Utils {" + code + "} "
await monaco.languages.typescript.typescriptDefaults.addExtraLib(code, `file:///${className}`);
};
await Promise.all(classNames.map(LoadClass));
codeEditor = monaco.editor.create(document.getElementById('editorElement'), {
language: 'typescript',
theme: 'vs-dark',
//readOnly: globalThis.isMobile, // Make editor readonly if on mobile
// Add the following line to disable the F12 key override
// contextmenu: false,
});
// Add keyboard shortcut for running code (Ctrl+Enter)
codeEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {
this.runCode();
});
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ESNext,
module: monaco.languages.typescript.ModuleKind.ESNext,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
allowNonTsExtensions: true,
});
// Add Vue component for editor controls
this.toggleEditor();
});
},
toggleEditor() {
this.showEditor = !this.showEditor;
},
runCode() {
ResetState();
const code = codeEditor.getValue();
chat.variant.files[0].content = code.replaceAll("export {}","");
setTimeout(() => Eval(code), 100);
this.toggleEditor();
},
resizeEditor() {
if (codeEditor && this.showEditor) {
codeEditor.layout();
}
},
}
});
function SetCode(code) {
codeEditor.setValue("export {};" + replaceImports(code));
}
function replaceImports(code)
{
return code.replaceAll("export {};","").replace(/import .*?;/gs, "")
}