@@ -72,21 +72,21 @@ <h1 class="tagline">A custom element that uses the CSS Custom Highlight API for
7272 </ clipboard-copy >
7373 </ div >
7474 </ div >
75- < span aria-hidden ="true "> ~</ span >
7675 </ section >
7776
7877 < section data-section ="playground ">
78+ < h2 class ="playground-title "> Demo Playground</ h2 >
7979 < 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 >
8181 < select aria-label ="Code language " id ="example-language-select "> </ select >
82- </ div >
83- < div class ="menubar__item ">
82+ </ label >
83+ < label class ="menubar__item ">
8484 < 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 >
8585 < 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 >
8887 </ div >
89- < div class ="playground ">
88+ < div class ="playground-code " data-loading >
89+ < div class ="loader "> </ div >
9090 < syntax-highlight language ="html "> </ syntax-highlight >
9191 </ div >
9292 </ section >
@@ -181,6 +181,8 @@ <h1>Credits</h1>
181181 < p > Released under the MIT License.</ p >
182182 </ footer >
183183
184+ < div class ="toast " popover > </ div >
185+
184186 < svg width ="0 " height ="0 " aria-hidden ="true " hidden >
185187 < 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 ">
186188 < circle cx ="12 " cy ="12 " r ="4 "> </ circle >
@@ -206,58 +208,85 @@ <h1>Credits</h1>
206208
207209 < script type ="module ">
208210 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'
210212 import { themePresets } from './theme-presets.js' ;
211213
212214 const DEFAULT_LANGUAGE_EXAMPLE = 'JS' ;
213215 const SUPPORTS_CSS_HIGHLIGHTS = CSS . highlights ;
214216
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+ }
218221
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 ( ) ;
241231
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 = '' ;
245246
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+ } ) ;
261290 } ) ;
262291 </ script >
263292
0 commit comments