-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (218 loc) · 5.53 KB
/
index.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
function fetchExample(exampleId) {
return fetch(`examples/${exampleId}`)
.then(res => res.ok ? res.text() : null);
}
// TODO use window.location.hash instead of document.URL
function url2src(url) {
let n = url.lastIndexOf("#");
if(n == -1) {
return "";
}
return url.substr(n + 1);
}
function makeTitle(name) {
console.log(`Planck.js Playground - ${name}`);
return `Planck.js Playground - ${name}`;
}
let nav = document.getElementsByTagName('NAV')[0];
let typingPaths = [
'./node_modules/planck-js/lib/index.d.ts',
'./node_modules/planck-js/lib/collision/index.d.ts',
'./node_modules/planck-js/lib/common/index.d.ts',
'./node_modules/planck-js/lib/joint/index.d.ts',
'./node_modules/planck-js/lib/shape/index.d.ts',
'./node_modules/planck-js/testbed/index.d.ts'
];
// This is used to check whether a src got requested before another fetch fulfilled.
let pendingSrc = "";
function initialLoad() {
let value, link, url;
let src = url2src(document.URL);
if(!src) {
link = nav.getElementsByTagName('A')[0];
url = link.href;
src = url2src(url);
} else {
url = document.URL;
let links = nav.getElementsByTagName('A');
for(let l of links) {
if(l.href.toLowerCase() === url.toLowerCase()) {
link = l;
break;
}
}
}
if(link) {
// history.state !== null can happen in Firefox when reloading
history.replaceState(history.state, makeTitle(link.innerText), url);
link.parentElement.parentElement.previousElementSibling.classList.add("open");
}
pendingSrc = src;
if(history.state && history.state.code) {
value = Promise.resolve(history.state.code);
} else {
value = fetchExample(src);
}
value.then(value => {
handleCodeLoaded(src, value);
if(history.state && history.state.view) {
editor.restoreViewState(history.state.view);
}
});
}
initialLoad();
// validation settings
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: false,
});
// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
noLib: true,
target: monaco.languages.typescript.ScriptTarget.ES6,
allowNonTsExtensions: true,
});
let editorContainer = document.getElementById('editor');
var editor = monaco.editor.create(editorContainer, {
language: 'javascript',
minimap: {enabled: false},
readOnly: true,
// Batman style
theme: 'vs-dark',
});
editor.addAction({
id: "runCode",
label: "Run Code",
keybindings: [
monaco.KeyCode.F5,
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_B,
],
run(editor) {
view.reset();
view.addCode(editor.getValue());
},
});
editor.addAction({
id: "toggleLayout",
label: "Toggle Layout",
run() {
document.body.classList.toggle('horizontal');
updateLayout();
},
});
window.onresize = updateLayout;
function updateLayout() {
editor.layout({
width: editorContainer.clientWidth,
height: editorContainer.clientHeight,
});
}
// Load Typescript definitions
typingPaths.forEach(path => {
fetch(path)
.then(res => res.text())
.then(lib => {
monaco.languages.typescript.javascriptDefaults.addExtraLib(lib, path);
});
});
let view = {
iframe: document.getElementById("view"),
errorContainer: document.getElementById("errorContainer"),
iframeLoaded: false,
userScriptPending: null,
reset() {
this.removeErrors();
this.iframe.contentWindow.location.reload();
this.iframeLoaded = false;
},
addCode(code) {
let userScript = document.createElement("script");
userScript.innerHTML = code;
if(this.iframeLoaded) {
this.iframe.contentWindow.document.body.append(userScript);
} else {
this.userScriptPending = userScript;
}
},
errorsOnScreen: 0,
showError(msg) {
if(this.errorsOnScreen < 5) {
let panel = document.createElement("DIV");
panel.innerHTML = msg;
this.errorContainer.append(panel);
this.errorsOnScreen++;
}
console.error(msg);
},
removeErrors() {
let child = this.errorContainer.firstChild;
while(child) {
this.errorContainer.removeChild(child);
child = this.errorContainer.firstChild;
}
this.errorsOnScreen = 0;
},
}
view.iframeLoaded = view.iframe.contentDocument.readyState === "complete";
view.iframe.onload = () => {
view.iframeLoaded = true;
view.iframe.contentWindow.onerror = view.showError.bind(view);
if(view.userScriptPending) {
view.iframe.contentWindow.document.body.append(view.userScriptPending);
view.userScriptPending = null;
}
}
function handleCodeLoaded(src, code) {
if(src !== pendingSrc) {
return;
}
pendingSrc = "";
if(code === null) {
view.showError(`Unable to load ${src}`);
return;
}
view.addCode(code);
editor.setValue(code);
editor.updateOptions({readOnly: false});
}
nav.onclick = e => {
if(e.target.parentElement == nav) {
e.target.classList.toggle("open");
} else if(e.target.tagName == "A") {
let url = e.target.href;
if(url == document.URL | !url) {
return false;
}
let src = url2src(url);
pendingSrc = src;
fetchExample(src).then(code => {
handleCodeLoaded(src, code);
if(src === pendingSrc) {
saveState(code);
}
});
saveState(editor.getValue());
history.pushState(null, makeTitle(e.target.innerText), url);
view.reset();
editor.setValue("");
editor.updateOptions({readOnly: true});
return false;
}
}
window.onbeforeunload = e => {
saveState(editor.getValue());
}
window.onpopstate = e => {
view.reset();
editor.setValue("");
editor.updateOptions({readOnly: true});
initialLoad();
editor.focus();
}
function saveState(code) {
history.replaceState({
code,
view: editor.saveViewState(),
}, "", document.URL);
}
// BUG: does not save state when going back/forwards in history