1
- // Change the editor theme of all CodeMirror cells according to the furo ( dark) mode
1
+ // Change the editor theme according to the furo light/ dark/auto mode
2
2
function changeTheme ( editor , theme ) {
3
3
if ( theme === 'dark' ) {
4
4
editor . setOption ( 'theme' , 'monokai' ) ; // the same with pygments dark style in conf.py
@@ -9,19 +9,42 @@ function changeTheme(editor, theme) {
9
9
}
10
10
}
11
11
12
- // Use the theme data of the document.body element set by setTheme function
12
+ // Change the editor theme of all CodeMirror cells
13
+ function changeThemeAll ( theme ) {
14
+ const querySet = document . querySelectorAll ( '.CodeMirror' ) ;
15
+ for ( var i = 0 ; i < querySet . length ; i ++ ) {
16
+ changeTheme ( querySet [ i ] . CodeMirror , theme ) ;
17
+ }
18
+ }
19
+
20
+ // Use the theme data of the body element set by setTheme function
13
21
// defined in https://github.com/pradyunsg/furo/blob/main/src/furo/assets/scripts/furo.js
14
22
const body = document . body ;
15
23
const observer1 = new MutationObserver ( ( mutationsList ) => {
16
24
for ( let mutation of mutationsList ) {
17
25
if ( mutation . type === 'attributes' && mutation . attributeName === 'data-theme' ) {
18
26
const theme = body . dataset . theme ;
19
- const querySet = document . querySelectorAll ( '.CodeMirror' ) ;
20
- for ( var i = 0 ; i < querySet . length ; i ++ ) {
21
- changeTheme ( querySet [ i ] . CodeMirror , theme ) ;
22
- } } } } ) ;
27
+ changeThemeAll ( theme ) ;
28
+ }
29
+ }
30
+ } ) ;
23
31
observer1 . observe ( body , { attributes : true } ) ;
24
32
33
+
34
+ // In the furo auto mode, we watch prefers-color-scheme and use the theme data
35
+ // of the body element to change the CodeMirror editor theme
36
+ const prefersDarkMode = window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
37
+
38
+ function handlePrefersColorSchemeChange ( e ) {
39
+ const theme = body . dataset . theme ;
40
+ if ( theme === 'auto' ) {
41
+ changeThemeAll ( theme ) ;
42
+ }
43
+ }
44
+
45
+ prefersDarkMode . addEventListener ( 'change' , handlePrefersColorSchemeChange ) ;
46
+
47
+
25
48
// Change the editor theme of a new CodeMirror cell.
26
49
const callback = function ( mutationsList , observer ) {
27
50
for ( const mutation of mutationsList ) {
@@ -89,4 +112,3 @@ thebelab.on("status", function (evt, data) {
89
112
kernel . requestExecute ( { code : "%display latex" } ) ;
90
113
}
91
114
} ) ;
92
-
0 commit comments