-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
209 lines (191 loc) · 5.74 KB
/
editor.js
File metadata and controls
209 lines (191 loc) · 5.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Configure Monaco’s AMD loader
require.config({
paths: {
vs: "https://unpkg.com/monaco-editor@0.52.2/min/vs"
}
});
const isMobile = /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
const code =
`package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}`;
const codeExamples = document.getElementById("codeExamples");
const examples = [
{
title: "Hello, World!",
code: code
},
{
fileName: "sieve_of_eratosthenes.go",
title: "Sieve of Eratosthenes",
},
{
fileName: "types.go",
title: "Exploring Go's types",
},
];
async function loadAllExamples() {
const promises = examples.slice(1).map(e => loadExample(e));
await Promise.all(promises);
examples.forEach(e => {
const button = document.createElement("button");
button.className = "executionBtn exampleBtn";
button.textContent = e.title;
button.addEventListener("click", async () => {
if (window.editor && e.code) {
window.editor.setValue(e.code);
cleanBreakpoints();
window.showMessage(true, `Loaded example: ${e.title}`);
window.toggleInfoSection();
}
});
codeExamples.appendChild(button);
});
}
async function loadExample(example) {
const response = await fetch(`code_examples/${example.fileName}`);
if (response.ok) {
example.code = await response.text();
}
}
loadAllExamples();
let breakpoints = {};
window.getBreakpointValues = () => {
return Object.values(breakpoints).filter(bp => bp != null);
}
window.getBreakpointLineNumbers = () => {
return window.getBreakpointValues().map(bp => bp.lineNumber);
}
let decorations = [];
window.highlightedLineNumber = null;
const DEBUG_BREAKPOINT = "debugBreakpoint";
const INVALID_DEBUG_BREAKPOINT = "invalidDebugBreakpoint";
const DEBUG_HIGHLIGHT_LINE = "debugHighlightLine";
function getInitialCode() {
const encoded = window.location.hash.slice(1);
return encoded ? base64UrlDecode(encoded) : code;
}
require(["vs/editor/editor.main"], function () {
const initialCode = getInitialCode();
const mobileOpts = {
scrollBeyondLastLine: 10,
scrollBeyondLastColumn: 10,
minimap: { enabled: false },
smoothScrolling: true,
scrollbar: {
vertical: "hidden",
horizontal: "hidden",
useShadows: false,
verticalScrollbarSize: 8,
},
fontSize: 14,
lineHeight: 24,
dragAndDrop: true,
renderWhitespace: "none",
}
const editor = monaco.editor.create(document.getElementById("editor"), {
value: initialCode,
language: "go",
theme: "vs-dark",
automaticLayout: true,
glyphMargin: true,
scrollbar: {
horizontal: "hidden",
},
...(isMobile ? mobileOpts : {})
});
window.editor = editor;
window.breakpoints = breakpoints;
editor.onMouseDown(e => {
if (e.target.type !== monaco.editor.MouseTargetType.GUTTER_LINE_NUMBERS &&
e.target.type !== monaco.editor.MouseTargetType.GUTTER_GLYPH_MARGIN) {
return;
}
const line = e.target.position.lineNumber;
if (breakpoints[line] != null) {
breakpoints[line] = null;
} else {
breakpoints[line] = {
lineNumber: line,
valid: true
};
}
setDecorations();
setYaegiBreakpoints(getBreakpointLineNumbers());
});
});
window.updateBreakpoints = function (event) {
if (breakpoints == null || Object.keys(breakpoints).length <= 0) {
return;
}
if (event == null || event.length <= 0) {
return;
}
event.forEach(bpInfo => {
if (breakpoints[bpInfo.lineNumber] != null) {
breakpoints[bpInfo.lineNumber] = {
lineNumber: bpInfo.lineNumber,
position: bpInfo.position,
valid: bpInfo.valid
}
}
});
setDecorations();
};
const setDecorations = () => {
const bpDecorations = window.getBreakpointValues().map(bp => {
const glyphMarginClassName = bp.valid !== true ?
INVALID_DEBUG_BREAKPOINT : DEBUG_BREAKPOINT
return {
range: new monaco.Range(bp.lineNumber, 1, bp.lineNumber, 1),
options: {
isWholeLine: true,
glyphMarginClassName: glyphMarginClassName
}
};
});
const newDecorations = [...bpDecorations];
if (window.highlightedLineNumber != null) {
newDecorations.push({
range: new monaco.Range(window.highlightedLineNumber, 1, window.highlightedLineNumber, 1),
options: {
isWholeLine: true,
className: DEBUG_HIGHLIGHT_LINE,
marginClassName: DEBUG_HIGHLIGHT_LINE
}
});
}
decorations = window.editor.deltaDecorations(
decorations,
newDecorations
);
};
window.setDecorations = setDecorations;
function cleanBreakpoints() {
breakpoints = {};
window.breakpoints = breakpoints;
setDecorations();
setYaegiBreakpoints(getBreakpointLineNumbers());
}
function encode(str) {
return btoa(unescape(encodeURIComponent(str)));
}
function decode(str) {
return decodeURIComponent(escape(atob(str)));
}
function base64UrlEncode(str) {
return encode(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
window.base64UrlEncode = base64UrlEncode;
function base64UrlDecode(str) {
str = str
.replace(/-/g, '+')
.replace(/_/g, '/');
while (str.length % 4) str += '=';
return decode(str);
}