@@ -72,21 +72,21 @@ <h1 class="tagline">A custom element that uses the CSS Custom Highlight API for
72
72
</ clipboard-copy >
73
73
</ div >
74
74
</ div >
75
- < span aria-hidden ="true "> ~</ span >
76
75
</ section >
77
76
78
77
< section data-section ="playground ">
78
+ < h2 class ="playground-title "> Demo Playground</ h2 >
79
79
< div class ="menubar ">
80
- < div class ="menubar__item "> < svg xmlns ="http://www.w3.org/2000/svg " width ="16 " height ="16 " viewBox ="0 0 24 24 " fill ="none " stroke ="currentColor " stroke-width ="2 " stroke-linecap ="round " stroke-linejoin ="round " aria-hidden ="true "> < polyline points ="16 18 22 12 16 6 "/> < polyline points ="8 6 2 12 8 18 "/> </ svg >
80
+ < label class ="menubar__item "> < svg xmlns ="http://www.w3.org/2000/svg " width ="16 " height ="16 " viewBox ="0 0 24 24 " fill ="none " stroke ="currentColor " stroke-width ="2 " stroke-linecap ="round " stroke-linejoin ="round " aria-hidden ="true "> < polyline points ="16 18 22 12 16 6 "/> < polyline points ="8 6 2 12 8 18 "/> </ svg >
81
81
< select aria-label ="Code language " id ="example-language-select "> </ select >
82
- </ div >
83
- < div class ="menubar__item ">
82
+ </ label >
83
+ < label class ="menubar__item ">
84
84
< svg xmlns ="http://www.w3.org/2000/svg " width ="16 " height ="16 " viewBox ="0 0 24 24 " fill ="none " stroke ="currentColor " stroke-width ="2 " stroke-linecap ="round " stroke-linejoin ="round " aria-hidden ="true "> < path d ="M10 2v2 "/> < path d ="M14 2v4 "/> < path d ="M17 2a1 1 0 0 1 1 1v9H6V3a1 1 0 0 1 1-1z "/> < path d ="M6 12a1 1 0 0 0-1 1v1a2 2 0 0 0 2 2h2a1 1 0 0 1 1 1v2.9a2 2 0 1 0 4 0V17a1 1 0 0 1 1-1h2a2 2 0 0 0 2-2v-1a1 1 0 0 0-1-1 "/> </ svg >
85
85
< select aria-label ="Theme " id ="theme-preset-select "> </ select >
86
- </ div >
87
- < div class ="menubar__item " style ="margin-inline-start: auto "> < small > Playground</ small > </ div >
86
+ </ label >
88
87
</ div >
89
- < div class ="playground ">
88
+ < div class ="playground-code " data-loading >
89
+ < div class ="loader "> </ div >
90
90
< syntax-highlight language ="html "> </ syntax-highlight >
91
91
</ div >
92
92
</ section >
@@ -181,6 +181,8 @@ <h1>Credits</h1>
181
181
< p > Released under the MIT License.</ p >
182
182
</ footer >
183
183
184
+ < div class ="toast " popover > </ div >
185
+
184
186
< svg width ="0 " height ="0 " aria-hidden ="true " hidden >
185
187
< symbol xmlns ="http://www.w3.org/2000/svg " fill ="none " stroke ="currentColor " stroke-width ="2 " stroke-linecap ="round " stroke-linejoin ="round " viewBox ="0 0 24 24 " id ="sun ">
186
188
< circle cx ="12 " cy ="12 " r ="4 "> </ circle >
@@ -206,58 +208,85 @@ <h1>Credits</h1>
206
208
207
209
< script type ="module ">
208
210
import 'https://unpkg.com/syntax-highlight-element@next/dist/syntax-highlight-element.js' ;
209
- import { languageExamples } from './language-examples.js'
211
+ import { languageExamples , fetchFileContent } from './language-examples.js'
210
212
import { themePresets } from './theme-presets.js' ;
211
213
212
214
const DEFAULT_LANGUAGE_EXAMPLE = 'JS' ;
213
215
const SUPPORTS_CSS_HIGHLIGHTS = CSS . highlights ;
214
216
215
- if ( ! SUPPORTS_CSS_HIGHLIGHTS ) {
216
- document . querySelector ( '[data-alert="no-support"]' ) . removeAttribute ( 'hidden' )
217
- }
217
+ document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
218
+ if ( ! SUPPORTS_CSS_HIGHLIGHTS ) {
219
+ document . querySelector ( '[data-alert="no-support"]' ) . removeAttribute ( 'hidden' )
220
+ }
218
221
219
- /**
220
- * Language example switcher
221
- */
222
- const playgroundCode = document . querySelector ( '[data-section="playground"] syntax-highlight' ) ;
223
- const languageSelector = document . querySelector ( '#example-language-select' ) ;
224
- const languageOptions = Object . keys ( languageExamples ) . map ( ( name , index ) => {
225
- const option = document . createElement ( 'option' ) ;
226
- option . value = option . textContent = name ;
227
- option . selected = name === DEFAULT_LANGUAGE_EXAMPLE || ! index ;
228
- return option
229
- } ) ;
230
- languageOptions . forEach ( option => languageSelector . append ( option ) ) ;
231
- languageSelector . addEventListener ( 'change' , event => {
232
- const example = languageExamples [ event . target . value ] ;
233
- playgroundCode . scrollLeft = 0 ;
234
- playgroundCode . parentElement . scrollTop = 0 ;
235
- playgroundCode . innerHTML = example . code || 'Not found' ;
236
- if ( SUPPORTS_CSS_HIGHLIGHTS ) {
237
- playgroundCode . setAttribute ( 'language' , example . language ) ;
238
- playgroundCode . update ( ) ;
239
- } ;
240
- } ) ;
222
+ /**
223
+ * Set initial language example
224
+ */
225
+ const playgroundCode = document . querySelector ( '.playground-code syntax-highlight' ) ;
226
+ const defaultExample = languageExamples [ DEFAULT_LANGUAGE_EXAMPLE ] ;
227
+ const togglePlaygroundLoading = ( ) => playgroundCode . parentElement . toggleAttribute ( 'data-loading' ) ;
228
+ playgroundCode . innerHTML = await fetchFileContent ( defaultExample . file , { escapeHtml : defaultExample ?. escapeHtml } ) ;
229
+ playgroundCode . setAttribute ( 'language' , defaultExample . language ) ;
230
+ togglePlaygroundLoading ( ) ;
241
231
242
- // Set initial language example
243
- playgroundCode . innerHTML = languageExamples [ DEFAULT_LANGUAGE_EXAMPLE ] . code ;
244
- playgroundCode . setAttribute ( 'language' , languageExamples [ DEFAULT_LANGUAGE_EXAMPLE ] . language ) ;
232
+ /**
233
+ * Language example switcher
234
+ */
235
+ const languageSelector = document . querySelector ( '#example-language-select' ) ;
236
+ const languageOptions = Object . keys ( languageExamples ) . map ( ( name , index ) => {
237
+ const option = document . createElement ( 'option' ) ;
238
+ option . value = option . textContent = name ;
239
+ option . selected = name === DEFAULT_LANGUAGE_EXAMPLE || ! index ;
240
+ return option ;
241
+ } ) ;
242
+ languageOptions . forEach ( option => languageSelector . append ( option ) ) ;
243
+ languageSelector . addEventListener ( 'change' , async event => {
244
+ togglePlaygroundLoading ( ) ;
245
+ playgroundCode . textContent = '' ;
245
246
246
- /**
247
- * Theme preset switcher
248
- */
249
- const themeStyles = document . querySelector ( '[data-she-theme]' ) ;
250
- const themeSelector = document . querySelector ( '#theme-preset-select' ) ;
251
- const themeOptions = Object . keys ( themePresets ) . map ( ( name , index ) => {
252
- const option = document . createElement ( 'option' ) ;
253
- option . value = option . textContent = name ;
254
- option . selected = ! index ;
255
- return option
256
- } ) ;
257
- themeOptions . forEach ( option => themeSelector . append ( option ) ) ;
258
- themeSelector . addEventListener ( 'change' , event => {
259
- const theme = themePresets [ event . target . value ] || 'Not found' ;
260
- themeStyles . href = `https://unpkg.com/syntax-highlight-element@next/dist/themes/${ theme } .css`
247
+ const example = languageExamples [ event . target . value ] ;
248
+ const exampleContent = await fetchFileContent ( example . file , { escapeHtml : example ?. escapeHtml } ) ;
249
+ playgroundCode . innerHTML = exampleContent || 'Not found' ;
250
+ togglePlaygroundLoading ( ) ;
251
+
252
+ if ( SUPPORTS_CSS_HIGHLIGHTS ) {
253
+ playgroundCode . setAttribute ( 'language' , example . language ) ;
254
+ playgroundCode . update ( ) ;
255
+ } ;
256
+ } ) ;
257
+
258
+ /**
259
+ * Theme preset switcher
260
+ */
261
+ const themeSelector = document . querySelector ( '#theme-preset-select' ) ;
262
+ const themeOptions = Object . keys ( themePresets ) . map ( ( name , index ) => {
263
+ const option = document . createElement ( 'option' ) ;
264
+ option . value = option . textContent = name ;
265
+ option . selected = ! index ;
266
+ return option
267
+ } ) ;
268
+ themeOptions . forEach ( option => themeSelector . append ( option ) ) ;
269
+ themeSelector . addEventListener ( 'change' , event => {
270
+ console . log ( event )
271
+ togglePlaygroundLoading ( ) ;
272
+ const oldThemeStyles = document . querySelector ( '[data-she-theme]' ) ;
273
+ const link = document . createElement ( 'link' ) ;
274
+ link . rel = 'stylesheet' ;
275
+ link . dataset . sheTheme = '' ;
276
+ link . onload = ( ) => {
277
+ let timeoutID ;
278
+ togglePlaygroundLoading ( ) ;
279
+ document . head . removeChild ( oldThemeStyles ) ;
280
+ }
281
+ link . onerror = ( ) => {
282
+ togglePlaygroundLoading ( ) ;
283
+ const toast = document . querySelector ( '.toast' ) ;
284
+ toast . textContent = 'There was a problem loading the theme…'
285
+ toast . showPopover ( ) ;
286
+ } ;
287
+ link . href = `https://unpkg.com/syntax-highlight-element@next/dist/themes/${ themePresets [ event . target . value ] } .css` ;
288
+ document . head . appendChild ( link ) ;
289
+ } ) ;
261
290
} ) ;
262
291
</ script >
263
292
0 commit comments