4
4
window . onunload = function ( ) { } ;
5
5
6
6
// Global variable, shared between modules
7
- function playground_text ( playground ) {
7
+ function playground_text ( playground , hidden = true ) {
8
8
let code_block = playground . querySelector ( "code" ) ;
9
9
10
10
if ( window . ace && code_block . classList . contains ( "editable" ) ) {
11
11
let editor = window . ace . edit ( code_block ) ;
12
12
return editor . getValue ( ) ;
13
- } else {
13
+ } else if ( hidden ) {
14
14
return code_block . textContent ;
15
+ } else {
16
+ return code_block . innerText ;
15
17
}
16
18
}
17
19
@@ -66,7 +68,7 @@ function playground_text(playground) {
66
68
}
67
69
68
70
// updates the visibility of play button based on `no_run` class and
69
- // used crates vs ones available on http ://play.rust-lang.org
71
+ // used crates vs ones available on https ://play.rust-lang.org
70
72
function update_play_button ( pre_block , playground_crates ) {
71
73
var play_button = pre_block . querySelector ( ".play-button" ) ;
72
74
@@ -166,7 +168,6 @@ function playground_text(playground) {
166
168
. filter ( function ( node ) { return node . classList . contains ( "editable" ) ; } )
167
169
. forEach ( function ( block ) { block . classList . remove ( 'language-rust' ) ; } ) ;
168
170
169
- Array
170
171
code_nodes
171
172
. filter ( function ( node ) { return ! node . classList . contains ( "editable" ) ; } )
172
173
. forEach ( function ( block ) { hljs . highlightBlock ( block ) ; } ) ;
@@ -178,7 +179,7 @@ function playground_text(playground) {
178
179
// even if highlighting doesn't apply
179
180
code_nodes . forEach ( function ( block ) { block . classList . add ( 'hljs' ) ; } ) ;
180
181
181
- Array . from ( document . querySelectorAll ( "code.language-rust " ) ) . forEach ( function ( block ) {
182
+ Array . from ( document . querySelectorAll ( "code.hljs " ) ) . forEach ( function ( block ) {
182
183
183
184
var lines = Array . from ( block . querySelectorAll ( '.boring' ) ) ;
184
185
// If no lines were hidden, return
@@ -224,7 +225,7 @@ function playground_text(playground) {
224
225
}
225
226
226
227
var clipButton = document . createElement ( 'button' ) ;
227
- clipButton . className = 'fa fa-copy clip-button' ;
228
+ clipButton . className = 'clip-button' ;
228
229
clipButton . title = 'Copy to clipboard' ;
229
230
clipButton . setAttribute ( 'aria-label' , clipButton . title ) ;
230
231
clipButton . innerHTML = '<i class=\"tooltiptext\"></i>' ;
@@ -257,7 +258,7 @@ function playground_text(playground) {
257
258
258
259
if ( window . playground_copyable ) {
259
260
var copyCodeClipboardButton = document . createElement ( 'button' ) ;
260
- copyCodeClipboardButton . className = 'fa fa-copy clip-button' ;
261
+ copyCodeClipboardButton . className = 'clip-button' ;
261
262
copyCodeClipboardButton . innerHTML = '<i class="tooltiptext"></i>' ;
262
263
copyCodeClipboardButton . title = 'Copy to clipboard' ;
263
264
copyCodeClipboardButton . setAttribute ( 'aria-label' , copyCodeClipboardButton . title ) ;
@@ -288,6 +289,10 @@ function playground_text(playground) {
288
289
var themeToggleButton = document . getElementById ( 'theme-toggle' ) ;
289
290
var themePopup = document . getElementById ( 'theme-list' ) ;
290
291
var themeColorMetaTag = document . querySelector ( 'meta[name="theme-color"]' ) ;
292
+ var themeIds = [ ] ;
293
+ themePopup . querySelectorAll ( 'button.theme' ) . forEach ( function ( el ) {
294
+ themeIds . push ( el . id ) ;
295
+ } ) ;
291
296
var stylesheets = {
292
297
ayuHighlight : document . querySelector ( "[href$='ayu-highlight.css']" ) ,
293
298
tomorrowNight : document . querySelector ( "[href$='tomorrow-night.css']" ) ,
@@ -300,6 +305,13 @@ function playground_text(playground) {
300
305
themePopup . querySelector ( "button#" + get_theme ( ) ) . focus ( ) ;
301
306
}
302
307
308
+ function updateThemeSelected ( ) {
309
+ themePopup . querySelectorAll ( '.theme-selected' ) . forEach ( function ( el ) {
310
+ el . classList . remove ( 'theme-selected' ) ;
311
+ } ) ;
312
+ themePopup . querySelector ( "button#" + get_theme ( ) ) . classList . add ( 'theme-selected' ) ;
313
+ }
314
+
303
315
function hideThemes ( ) {
304
316
themePopup . style . display = 'none' ;
305
317
themeToggleButton . setAttribute ( 'aria-expanded' , false ) ;
@@ -309,7 +321,7 @@ function playground_text(playground) {
309
321
function get_theme ( ) {
310
322
var theme ;
311
323
try { theme = localStorage . getItem ( 'mdbook-theme' ) ; } catch ( e ) { }
312
- if ( theme === null || theme === undefined ) {
324
+ if ( theme === null || theme === undefined || ! themeIds . includes ( theme ) ) {
313
325
return default_theme ;
314
326
} else {
315
327
return theme ;
@@ -338,7 +350,7 @@ function playground_text(playground) {
338
350
}
339
351
340
352
setTimeout ( function ( ) {
341
- themeColorMetaTag . content = getComputedStyle ( document . body ) . backgroundColor ;
353
+ themeColorMetaTag . content = getComputedStyle ( document . documentElement ) . backgroundColor ;
342
354
} , 1 ) ;
343
355
344
356
if ( window . ace && window . editors ) {
@@ -355,6 +367,7 @@ function playground_text(playground) {
355
367
356
368
html . classList . remove ( previousTheme ) ;
357
369
html . classList . add ( theme ) ;
370
+ updateThemeSelected ( ) ;
358
371
}
359
372
360
373
// Set theme
@@ -432,16 +445,16 @@ function playground_text(playground) {
432
445
} ) ( ) ;
433
446
434
447
( function sidebar ( ) {
435
- var html = document . querySelector ( "html " ) ;
448
+ var body = document . querySelector ( "body " ) ;
436
449
var sidebar = document . getElementById ( "sidebar" ) ;
437
450
var sidebarLinks = document . querySelectorAll ( '#sidebar a' ) ;
438
451
var sidebarToggleButton = document . getElementById ( "sidebar-toggle" ) ;
439
452
var sidebarResizeHandle = document . getElementById ( "sidebar-resize-handle" ) ;
440
453
var firstContact = null ;
441
454
442
455
function showSidebar ( ) {
443
- html . classList . remove ( 'sidebar-hidden' )
444
- html . classList . add ( 'sidebar-visible' ) ;
456
+ body . classList . remove ( 'sidebar-hidden' )
457
+ body . classList . add ( 'sidebar-visible' ) ;
445
458
Array . from ( sidebarLinks ) . forEach ( function ( link ) {
446
459
link . setAttribute ( 'tabIndex' , 0 ) ;
447
460
} ) ;
@@ -450,20 +463,9 @@ function playground_text(playground) {
450
463
try { localStorage . setItem ( 'mdbook-sidebar' , 'visible' ) ; } catch ( e ) { }
451
464
}
452
465
453
-
454
- var sidebarAnchorToggles = document . querySelectorAll ( '#sidebar a.toggle' ) ;
455
-
456
- function toggleSection ( ev ) {
457
- ev . currentTarget . parentElement . classList . toggle ( 'expanded' ) ;
458
- }
459
-
460
- Array . from ( sidebarAnchorToggles ) . forEach ( function ( el ) {
461
- el . addEventListener ( 'click' , toggleSection ) ;
462
- } ) ;
463
-
464
466
function hideSidebar ( ) {
465
- html . classList . remove ( 'sidebar-visible' )
466
- html . classList . add ( 'sidebar-hidden' ) ;
467
+ body . classList . remove ( 'sidebar-visible' )
468
+ body . classList . add ( 'sidebar-hidden' ) ;
467
469
Array . from ( sidebarLinks ) . forEach ( function ( link ) {
468
470
link . setAttribute ( 'tabIndex' , - 1 ) ;
469
471
} ) ;
@@ -474,14 +476,14 @@ function playground_text(playground) {
474
476
475
477
// Toggle sidebar
476
478
sidebarToggleButton . addEventListener ( 'click' , function sidebarToggle ( ) {
477
- if ( html . classList . contains ( "sidebar-hidden" ) ) {
479
+ if ( body . classList . contains ( "sidebar-hidden" ) ) {
478
480
var current_width = parseInt (
479
481
document . documentElement . style . getPropertyValue ( '--sidebar-width' ) , 10 ) ;
480
482
if ( current_width < 150 ) {
481
483
document . documentElement . style . setProperty ( '--sidebar-width' , '150px' ) ;
482
484
}
483
485
showSidebar ( ) ;
484
- } else if ( html . classList . contains ( "sidebar-visible" ) ) {
486
+ } else if ( body . classList . contains ( "sidebar-visible" ) ) {
485
487
hideSidebar ( ) ;
486
488
} else {
487
489
if ( getComputedStyle ( sidebar ) [ 'transform' ] === 'none' ) {
@@ -497,14 +499,14 @@ function playground_text(playground) {
497
499
function initResize ( e ) {
498
500
window . addEventListener ( 'mousemove' , resize , false ) ;
499
501
window . addEventListener ( 'mouseup' , stopResize , false ) ;
500
- html . classList . add ( 'sidebar-resizing' ) ;
502
+ body . classList . add ( 'sidebar-resizing' ) ;
501
503
}
502
504
function resize ( e ) {
503
505
var pos = ( e . clientX - sidebar . offsetLeft ) ;
504
506
if ( pos < 20 ) {
505
507
hideSidebar ( ) ;
506
508
} else {
507
- if ( html . classList . contains ( "sidebar-hidden" ) ) {
509
+ if ( body . classList . contains ( "sidebar-hidden" ) ) {
508
510
showSidebar ( ) ;
509
511
}
510
512
pos = Math . min ( pos , window . innerWidth - 100 ) ;
@@ -513,7 +515,7 @@ function playground_text(playground) {
513
515
}
514
516
//on mouseup remove windows functions mousemove & mouseup
515
517
function stopResize ( e ) {
516
- html . classList . remove ( 'sidebar-resizing' ) ;
518
+ body . classList . remove ( 'sidebar-resizing' ) ;
517
519
window . removeEventListener ( 'mousemove' , resize , false ) ;
518
520
window . removeEventListener ( 'mouseup' , stopResize , false ) ;
519
521
}
@@ -542,33 +544,41 @@ function playground_text(playground) {
542
544
firstContact = null ;
543
545
}
544
546
} , { passive : true } ) ;
545
-
546
- // Scroll sidebar to current active section
547
- var activeSection = document . getElementById ( "sidebar" ) . querySelector ( ".active" ) ;
548
- if ( activeSection ) {
549
- // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
550
- activeSection . scrollIntoView ( { block : 'center' } ) ;
551
- }
552
547
} ) ( ) ;
553
548
554
549
( function chapterNavigation ( ) {
555
550
document . addEventListener ( 'keydown' , function ( e ) {
556
551
if ( e . altKey || e . ctrlKey || e . metaKey || e . shiftKey ) { return ; }
557
552
if ( window . search && window . search . hasFocus ( ) ) { return ; }
553
+ var html = document . querySelector ( 'html' ) ;
558
554
555
+ function next ( ) {
556
+ var nextButton = document . querySelector ( '.nav-chapters.next' ) ;
557
+ if ( nextButton ) {
558
+ window . location . href = nextButton . href ;
559
+ }
560
+ }
561
+ function prev ( ) {
562
+ var previousButton = document . querySelector ( '.nav-chapters.previous' ) ;
563
+ if ( previousButton ) {
564
+ window . location . href = previousButton . href ;
565
+ }
566
+ }
559
567
switch ( e . key ) {
560
568
case 'ArrowRight' :
561
569
e . preventDefault ( ) ;
562
- var nextButton = document . querySelector ( '.nav-chapters.next' ) ;
563
- if ( nextButton ) {
564
- window . location . href = nextButton . href ;
570
+ if ( html . dir == 'rtl' ) {
571
+ prev ( ) ;
572
+ } else {
573
+ next ( ) ;
565
574
}
566
575
break ;
567
576
case 'ArrowLeft' :
568
577
e . preventDefault ( ) ;
569
- var previousButton = document . querySelector ( '.nav-chapters.previous' ) ;
570
- if ( previousButton ) {
571
- window . location . href = previousButton . href ;
578
+ if ( html . dir == 'rtl' ) {
579
+ next ( ) ;
580
+ } else {
581
+ prev ( ) ;
572
582
}
573
583
break ;
574
584
}
@@ -580,19 +590,19 @@ function playground_text(playground) {
580
590
581
591
function hideTooltip ( elem ) {
582
592
elem . firstChild . innerText = "" ;
583
- elem . className = 'fa fa-copy clip-button' ;
593
+ elem . className = 'clip-button' ;
584
594
}
585
595
586
596
function showTooltip ( elem , msg ) {
587
597
elem . firstChild . innerText = msg ;
588
- elem . className = 'fa fa-copy tooltipped' ;
598
+ elem . className = 'clip-button tooltipped' ;
589
599
}
590
600
591
601
var clipboardSnippets = new ClipboardJS ( '.clip-button' , {
592
602
text : function ( trigger ) {
593
603
hideTooltip ( trigger ) ;
594
604
let playground = trigger . closest ( "pre" ) ;
595
- return playground_text ( playground ) ;
605
+ return playground_text ( playground , false ) ;
596
606
}
597
607
} ) ;
598
608
@@ -667,13 +677,14 @@ function playground_text(playground) {
667
677
} , { passive : true } ) ;
668
678
} ) ( ) ;
669
679
( function controllBorder ( ) {
670
- menu . classList . remove ( 'bordered' ) ;
671
- document . addEventListener ( 'scroll' , function ( ) {
680
+ function updateBorder ( ) {
672
681
if ( menu . offsetTop === 0 ) {
673
682
menu . classList . remove ( 'bordered' ) ;
674
683
} else {
675
684
menu . classList . add ( 'bordered' ) ;
676
685
}
677
- } , { passive : true } ) ;
686
+ }
687
+ updateBorder ( ) ;
688
+ document . addEventListener ( 'scroll' , updateBorder , { passive : true } ) ;
678
689
} ) ( ) ;
679
690
} ) ( ) ;
0 commit comments