@@ -154,10 +154,19 @@ export const setupPlayground = (
154
154
} , 300 )
155
155
} )
156
156
157
+ // If you set this to true, then the next time the playground would
158
+ // have set the user's hash it would be skipped - used for setting
159
+ // the text in examples
160
+ let suppressNextTextChangeForHashChange = false
161
+
157
162
// Sets the URL and storage of the sandbox string
158
163
const playgroundDebouncedMainFunction = ( ) => {
159
164
const alwaysUpdateURL = ! localStorage . getItem ( "disable-save-on-type" )
160
165
if ( alwaysUpdateURL ) {
166
+ if ( suppressNextTextChangeForHashChange ) {
167
+ suppressNextTextChangeForHashChange = false
168
+ return
169
+ }
161
170
const newURL = sandbox . createURLQueryWithCompilerOptions ( sandbox )
162
171
window . history . replaceState ( { } , "" , newURL )
163
172
}
@@ -177,6 +186,9 @@ export const setupPlayground = (
177
186
if ( model && plugin . modelChangedDebounce ) plugin . modelChangedDebounce ( sandbox , model , container )
178
187
} )
179
188
189
+ const skipInitiallySettingHash = document . location . hash && document . location . hash . includes ( "example/" )
190
+ if ( ! skipInitiallySettingHash ) playgroundDebouncedMainFunction ( )
191
+
180
192
// Setup working with the existing UI, once it's loaded
181
193
182
194
// Versions of TypeScript
@@ -274,6 +286,7 @@ export const setupPlayground = (
274
286
}
275
287
}
276
288
}
289
+ return false
277
290
}
278
291
} )
279
292
@@ -293,22 +306,31 @@ export const setupPlayground = (
293
306
}
294
307
}
295
308
296
- // Set up some key commands
297
- sandbox . editor . addAction ( {
309
+ const shareAction = {
298
310
id : "copy-clipboard" ,
299
311
label : "Save to clipboard" ,
300
312
keybindings : [ monaco . KeyMod . CtrlCmd | monaco . KeyCode . KEY_S ] ,
301
313
302
314
contextMenuGroupId : "run" ,
303
315
contextMenuOrder : 1.5 ,
304
316
305
- run : function ( ed ) {
317
+ run : function ( ) {
306
318
window . navigator . clipboard . writeText ( location . href . toString ( ) ) . then (
307
319
( ) => ui . flashInfo ( i ( "play_export_clipboard" ) ) ,
308
320
( e : any ) => alert ( e )
309
321
)
310
322
} ,
311
- } )
323
+ }
324
+
325
+ const shareButton = document . getElementById ( "share-button" ) !
326
+ shareButton . onclick = e => {
327
+ e . preventDefault ( )
328
+ shareAction . run ( )
329
+ return false
330
+ }
331
+
332
+ // Set up some key commands
333
+ sandbox . editor . addAction ( shareAction )
312
334
313
335
sandbox . editor . addAction ( {
314
336
id : "run-js" ,
@@ -335,6 +357,7 @@ export const setupPlayground = (
335
357
336
358
const isJS = sandbox . config . useJavaScript
337
359
ui . flashInfo ( i ( isJS ? "play_run_js" : "play_run_ts" ) )
360
+ return false
338
361
}
339
362
}
340
363
@@ -391,6 +414,15 @@ export const setupPlayground = (
391
414
}
392
415
settingsToggle . parentElement ! . classList . toggle ( "open" )
393
416
}
417
+
418
+ settingsToggle . addEventListener ( "keydown" , e => {
419
+ const isOpen = settingsToggle . parentElement ! . classList . contains ( "open" )
420
+ if ( e . keyCode === 9 && isOpen ) {
421
+ const result = document . querySelector ( ".playground-options li input" ) as any
422
+ result . focus ( )
423
+ e . preventDefault ( )
424
+ }
425
+ } )
394
426
}
395
427
396
428
// Support grabbing examples from the location hash
@@ -409,16 +441,6 @@ export const setupPlayground = (
409
441
localStorage . setItem ( "examples-seen" , JSON . stringify ( seen ) )
410
442
}
411
443
412
- // Set the menu to be the same section as this current example
413
- // this happens behind the scene and isn't visible till you hover
414
- // const sectionTitle = example.path[0]
415
- // const allSectionTitles = document.getElementsByClassName('section-name')
416
- // for (const title of allSectionTitles) {
417
- // if (title.textContent === sectionTitle) {
418
- // title.onclick({})
419
- // }
420
- // }
421
-
422
444
const allLinks = document . querySelectorAll ( "example-link" )
423
445
// @ts -ignore
424
446
for ( const link of allLinks ) {
@@ -428,8 +450,10 @@ export const setupPlayground = (
428
450
}
429
451
430
452
document . title = "TypeScript Playground - " + example . title
453
+ suppressNextTextChangeForHashChange = true
431
454
sandbox . setText ( code )
432
455
} else {
456
+ suppressNextTextChangeForHashChange = true
433
457
sandbox . setText ( "// There was an issue getting the example, bad URL? Check the console in the developer tools" )
434
458
}
435
459
} )
@@ -596,14 +620,12 @@ export const setupPlayground = (
596
620
export type Playground = ReturnType < typeof setupPlayground >
597
621
598
622
const redirectTabPressTo = ( element : HTMLElement , container : HTMLElement | undefined , query : string ) => {
599
- // element.style.backgroundColor = "red"
600
623
element . addEventListener ( "keydown" , e => {
601
624
if ( e . keyCode === 9 ) {
602
625
const host = container || document
603
626
const result = host . querySelector ( query ) as any
604
627
if ( ! result ) throw new Error ( `Expected to find a result for keydown` )
605
628
result . focus ( )
606
- console . log ( result )
607
629
e . preventDefault ( )
608
630
}
609
631
} )
0 commit comments