@@ -67,8 +67,6 @@ const stopButton = $("#stop");
6767const shareButton = $ ( "#share" ) ;
6868const copyButton = $ ( "#copy" ) ;
6969const hideEditor = $ ( "#hide-editor" ) ;
70- /** @type {HTMLIFrameElement } */
71- const iframe = $ ( "#frame" ) ;
7270
7371const smallScreen = innerWidth < 1024 ;
7472const isMobile = navigator . userAgent . match ( / a n d r o i d | i p h o n e | i p a d / 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
9896shareButton . 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
124122copyButton . 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
152150function 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
161178if ( ! 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({
225242window . addEventListener ( "click" , ( evt ) => {
226243 if ( evt . target === playButton ) return ;
227244 if ( evt . target === hideEditor ) return ;
228- iframe . blur ( ) ;
245+ getIframe ( ) . blur ( ) ;
229246} ) ;
230247
231248window . addEventListener ( "blur" , ( evt ) => {
232- iframe . blur ( ) ;
249+ getIframe ( ) . blur ( ) ;
233250} ) ;
234251
235252function 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) {
288305function 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