Skip to content

Commit 8f23fe8

Browse files
committed
fix editor
1 parent f91f472 commit 8f23fe8

File tree

5 files changed

+99
-91
lines changed

5 files changed

+99
-91
lines changed

public/app.js

Lines changed: 61 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ <h1>Litecanvas</h1>
191191
</div>
192192
<div class="cm-container"></div>
193193
</div>
194-
<div class="game">
195-
<div id="frame-overlay" hidden style="display: none"></div>
196-
<iframe id="frame" frameborder="0"></iframe>
197-
</div>
194+
<div class="game"></div>
198195
</div>
199196
</body>
200197
</html>

public/stats.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

public/sw.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cacheName = "luizbills.litecanvas-editor-v1";
2-
const version = "2.45.0";
2+
const version = "2.45.1";
33

44
const precacheResources = [
55
"/",
@@ -9,7 +9,6 @@ const precacheResources = [
99
"/app.css",
1010
"/app.js",
1111
"/litecanvas.js",
12-
"/stats.js",
1312
"/prism/prism.css",
1413
"/prism/prism.js",
1514
"/prism/prism-typescript.js",

src/index.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ const stopButton = $("#stop");
6767
const shareButton = $("#share");
6868
const copyButton = $("#copy");
6969
const hideEditor = $("#hide-editor");
70-
/** @type {HTMLIFrameElement} */
71-
const iframe = $("#frame");
7270

7371
const smallScreen = innerWidth < 1024;
7472
const isMobile = navigator.userAgent.match(/android|iphone|ipad/i) !== null;
@@ -80,7 +78,7 @@ playButton.addEventListener("click", () => {
8078
show(stopButton);
8179
runCode();
8280
if (smallScreen) {
83-
iframe.focus();
81+
getIframe().focus();
8482
}
8583
});
8684

@@ -92,13 +90,13 @@ function stopGame(evt) {
9290
hide(game);
9391
show(playButton);
9492
hide(stopButton);
95-
iframe.contentDocument.body.innerHTML = "";
93+
getIframe().remove();
9694
}
9795

9896
shareButton.addEventListener("click", (evt) => {
9997
if (!navigator.clipboard) {
10098
return alert(
101-
"Your browser not support this feature. Consider installing Firefox or Chrome.",
99+
"Your browser not support this feature. Consider installing Firefox or Chrome."
102100
);
103101
}
104102
const code = window.codeEditor.state.doc.toString();
@@ -117,14 +115,14 @@ shareButton.addEventListener("click", (evt) => {
117115
(err) => {
118116
alert("Error: Unable to generate your shareable url!");
119117
console.error("Error on copying text to clipboard:", err);
120-
},
118+
}
121119
);
122120
});
123121

124122
copyButton.addEventListener("click", (evt) => {
125123
if (!navigator.clipboard) {
126124
return alert(
127-
"Your browser not support this feature. Consider installing Firefox or Chrome.",
125+
"Your browser not support this feature. Consider installing Firefox or Chrome."
128126
);
129127
}
130128
const code = window.codeEditor.state.doc.toString();
@@ -134,7 +132,7 @@ copyButton.addEventListener("click", (evt) => {
134132
(err) => {
135133
alert("Error: Unable to generate your shareable url!");
136134
console.error("Error on copying text to clipboard:", err);
137-
},
135+
}
138136
);
139137
});
140138

@@ -146,17 +144,36 @@ hideEditor.addEventListener("click", (evt) => {
146144
code.setAttribute("hidden", true);
147145
hideEditor.classList.add("active");
148146
}
149-
iframe.focus();
147+
getIframe().focus();
150148
});
151149

152150
function runCode() {
151+
const iframe = getIframe();
153152
iframe.src = "preview.html"; // reload the iframe
153+
if (!iframe.onload) {
154+
iframe.onload = loadCode;
155+
}
154156
}
155157

156-
iframe.addEventListener("load", () => {
158+
function loadCode() {
157159
const code = window.codeEditor.state.doc.toString();
158-
iframe.contentDocument.querySelector("#code").innerHTML = code;
159-
});
160+
getIframe().contentDocument.querySelector("#code").innerHTML = code;
161+
}
162+
163+
/**
164+
* @returns {HTMLIFrameElement}
165+
*/
166+
function getIframe() {
167+
const ID = "frame";
168+
let iframe = $("#" + ID);
169+
if (!iframe) {
170+
iframe = document.createElement("iframe");
171+
iframe.id = ID;
172+
iframe.setAttribute("frameborder", "0");
173+
game.appendChild(iframe);
174+
}
175+
return iframe;
176+
}
160177

161178
if (!smallScreen) {
162179
let updateTimeout = 0;
@@ -199,7 +216,7 @@ const state = EditorState.create({
199216
sourceType: "script",
200217
},
201218
rules: {},
202-
}),
219+
})
203220
),
204221
javascriptLanguage.data.of({
205222
autocomplete: customCompletions,
@@ -225,11 +242,11 @@ window.codeEditor = new EditorView({
225242
window.addEventListener("click", (evt) => {
226243
if (evt.target === playButton) return;
227244
if (evt.target === hideEditor) return;
228-
iframe.blur();
245+
getIframe().blur();
229246
});
230247

231248
window.addEventListener("blur", (evt) => {
232-
iframe.blur();
249+
getIframe().blur();
233250
});
234251

235252
function compressString(str) {
@@ -253,16 +270,16 @@ function decompressString(str) {
253270
new Uint8Array(
254271
atob(str)
255272
.split("")
256-
.map((c) => c.charCodeAt(0)),
273+
.map((c) => c.charCodeAt(0))
257274
),
258-
{ to: "string" },
275+
{ to: "string" }
259276
);
260277
console.log("Playground url decoded successfully!");
261278
break;
262279
} catch (e) {
263280
console.error(
264281
`Failed decode the playground url (${attempts + 1}/2). Error:`,
265-
e,
282+
e
266283
);
267284
console.log("Trying to decode again (fixing some characters)...");
268285
code = null;
@@ -288,7 +305,7 @@ if (config.autosave) {
288305
function saveCode() {
289306
localStorage.setItem(
290307
"litecanvas_code",
291-
window.codeEditor.state.doc.toString(),
308+
window.codeEditor.state.doc.toString()
292309
);
293310
}
294311

0 commit comments

Comments
 (0)