|
| 1 | +<script id="toggle-light-dark-elements" type="application/javascript"> |
| 2 | +// Extend Quarto theme toggling to allow elements to be displayed/hidden in light/dark themes |
| 3 | + |
| 4 | +// Replicated from https://github.com/quarto-dev/quarto-cli/blob/84d4659/src/resources/formats/html/templates/quarto-html.ejs |
| 5 | +const getColorSchemeSentinel = () => { |
| 6 | + const localAlternateSentinel = 'alternate'; |
| 7 | + if (window.location.protocol !== 'file:') { |
| 8 | + const storageValue = window.localStorage.getItem('quarto-color-scheme'); |
| 9 | + return storageValue != null ? storageValue : localAlternateSentinel; |
| 10 | + } else { |
| 11 | + return localAlternateSentinel; |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +// Function to toggle light and dark elements based on color scheme |
| 16 | +const toggleColorSchemeElements = () => { |
| 17 | + const scheme = getColorSchemeSentinel(); |
| 18 | + const lightElements = document.getElementsByClassName('only-light'); |
| 19 | + const darkElements = document.getElementsByClassName('only-dark'); |
| 20 | + |
| 21 | + for (var i = 0; i < lightElements.length; i++) { |
| 22 | + lightElements[i].style.display = scheme == 'default' ? 'block' : 'none'; |
| 23 | + } |
| 24 | + for (var i = 0; i < darkElements.length; i++) { |
| 25 | + darkElements[i].style.display = scheme == 'default' ? 'none' : 'block'; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +// Add event listener for when the readyState is complete (and default toggler is set) |
| 30 | +document.addEventListener('readystatechange', function() { |
| 31 | + if (document.readyState === 'complete') { |
| 32 | + // Toggle scheme once |
| 33 | + toggleColorSchemeElements(); |
| 34 | + // Append our toggle function to the old one |
| 35 | + const oldToggle = window.quartoToggleColorScheme; |
| 36 | + window.quartoToggleColorScheme = () => { |
| 37 | + oldToggle(); |
| 38 | + toggleColorSchemeElements(); |
| 39 | + } |
| 40 | + } |
| 41 | +}); |
| 42 | +</script> |
0 commit comments