|
| 1 | +// Change the editor theme according to the furo light/dark/auto mode |
| 2 | +function changeTheme(editor, theme) { |
| 3 | + if (theme === 'dark') { |
| 4 | + editor.setOption('theme', 'monokai'); // the same with pygments dark style in conf.py |
| 5 | + } else if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
| 6 | + editor.setOption('theme', 'monokai'); |
| 7 | + } else { |
| 8 | + editor.setOption('theme', 'default'); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 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 |
| 21 | +// defined in https://github.com/pradyunsg/furo/blob/main/src/furo/assets/scripts/furo.js |
| 22 | +const body = document.body; |
| 23 | +const observer1 = new MutationObserver((mutationsList) => { |
| 24 | + for (let mutation of mutationsList) { |
| 25 | + if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') { |
| 26 | + const theme = body.dataset.theme; |
| 27 | + changeThemeAll(theme); |
| 28 | + } |
| 29 | + } |
| 30 | +}); |
| 31 | +observer1.observe(body, { attributes: true }); |
| 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 | + |
| 48 | +// Change the editor theme of a new CodeMirror cell. |
| 49 | +const callback = function(mutationsList, observer) { |
| 50 | + for(const mutation of mutationsList) { |
| 51 | + if (mutation.type === 'childList') { |
| 52 | + const theme = body.dataset.theme; |
| 53 | + for (const addedNode of mutation.addedNodes) { |
| 54 | + if (addedNode.classList && addedNode.classList.contains('CodeMirror')) { |
| 55 | + changeTheme(addedNode.CodeMirror, theme); |
| 56 | + }}}}}; |
| 57 | +const observer2 = new MutationObserver(callback); |
| 58 | +observer2.observe(document.getElementsByClassName("content")[0], { childList: true, subtree: true }); |
| 59 | + |
| 60 | + |
| 61 | +// Listen to the kernel status changes |
| 62 | +// https://thebe.readthedocs.io/en/stable/events.html |
| 63 | +thebelab.on("status", function (evt, data) { |
| 64 | + if (data.status === 'building') { |
| 65 | + const elements = document.querySelectorAll('.thebelab-cell'); |
| 66 | + elements.forEach(element => { |
| 67 | + element.style.filter = 'opacity(50%)'; |
| 68 | + }); |
| 69 | + const element = document.getElementById("thebelab-activate-button"); |
| 70 | + element.innerHTML = "Building"; |
| 71 | + element.style.right = '.4rem'; |
| 72 | + } |
| 73 | + else if (data.status === 'built') { |
| 74 | + const elements = document.querySelectorAll('.thebelab-cell'); |
| 75 | + elements.forEach(element => { |
| 76 | + element.style.filter = 'opacity(60%)'; |
| 77 | + }); |
| 78 | + const element = document.getElementById("thebelab-activate-button"); |
| 79 | + element.innerHTML = "Built"; |
| 80 | + element.style.right = '.4rem'; |
| 81 | + } |
| 82 | + else if (data.status === 'launching') { |
| 83 | + const elements = document.querySelectorAll('.thebelab-cell'); |
| 84 | + elements.forEach(element => { |
| 85 | + element.style.filter = 'opacity(70%)'; |
| 86 | + }); |
| 87 | + const element = document.getElementById("thebelab-activate-button"); |
| 88 | + element.innerHTML = "Launching"; |
| 89 | + element.style.right = '.4rem'; |
| 90 | + } |
| 91 | + else if (data.status === 'failed') { |
| 92 | + const elements = document.querySelectorAll('.thebelab-cell'); |
| 93 | + elements.forEach(element => { |
| 94 | + element.style.filter = 'opacity(50%)'; |
| 95 | + }); |
| 96 | + const element = document.getElementById("thebelab-activate-button"); |
| 97 | + element.innerHTML = 'Failed: ' + data.message; |
| 98 | + element.style.right = '.4rem'; |
| 99 | + element.style.width = 'auto'; |
| 100 | + element.style.color = 'red'; |
| 101 | + } |
| 102 | + else if (data.status === 'ready') { |
| 103 | + const elements = document.querySelectorAll('.thebelab-cell'); |
| 104 | + elements.forEach(element => { |
| 105 | + element.style.filter = 'opacity(100%)'; |
| 106 | + }); |
| 107 | + const element = document.getElementById("thebelab-activate-button"); |
| 108 | + element.innerHTML = "Ready"; |
| 109 | + element.style.right = null; |
| 110 | + // Run custom code when the kernel is ready |
| 111 | + const kernel = data.kernel; |
| 112 | + kernel.requestExecute({code: "%display latex"}); |
| 113 | + } |
| 114 | +}); |
0 commit comments