Path: blob/develop/src/doc/common/static/jupyter-sphinx-furo.js
4111 views
// Change the editor theme according to the furo light/dark/auto mode1function changeTheme(editor, theme) {2if (theme === 'dark') {3editor.setOption('theme', 'monokai'); // the same with pygments dark style in conf.py4} else if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {5editor.setOption('theme', 'monokai');6} else {7editor.setOption('theme', 'default');8}9}1011// Change the editor theme of all CodeMirror cells12function changeThemeAll(theme) {13const querySet = document.querySelectorAll('.CodeMirror');14for (var i = 0; i < querySet.length; i++) {15changeTheme(querySet[i].CodeMirror, theme);16}17}1819// Use the theme data of the body element set by setTheme function20// defined in https://github.com/pradyunsg/furo/blob/main/src/furo/assets/scripts/furo.js21const body = document.body;22const observer1 = new MutationObserver((mutationsList) => {23for (let mutation of mutationsList) {24if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {25const theme = body.dataset.theme;26changeThemeAll(theme);27}28}29});30observer1.observe(body, { attributes: true });313233// In the furo auto mode, we watch prefers-color-scheme and use the theme data34// of the body element to change the CodeMirror editor theme35const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');3637function handlePrefersColorSchemeChange(e) {38const theme = body.dataset.theme;39if (theme === 'auto') {40changeThemeAll(theme);41}42}4344prefersDarkMode.addEventListener('change', handlePrefersColorSchemeChange);454647// Change the editor theme of a new CodeMirror cell48const callback = function(mutationsList, observer) {49for(const mutation of mutationsList) {50if (mutation.type === 'childList') {51const theme = body.dataset.theme;52for (const addedNode of mutation.addedNodes) {53if (addedNode.classList && addedNode.classList.contains('CodeMirror')) {54changeTheme(addedNode.CodeMirror, theme);55}}}}};56const observer2 = new MutationObserver(callback);57observer2.observe(document.getElementsByClassName("content")[0], { childList: true, subtree: true });585960//61// Version selector62//6364function fetchVersions() {65try {66let menu = document.getElementById('versions-menu');6768// For the origin of the this site, see .github/workflows/doc-publish.yml69fetch('https://doc-release--sagemath.netlify.app/html/en/versions.txt')70.then(response => {71if (!response.ok) {72throw new Error('Network response was not ok ' + response.statusText);73}74return response.text();75})76.then(text => {77const lines = text.split('\n');78lines.forEach(line => {79if (!line.startsWith('#')) { // Ignore the comment line80let [ver, url] = line.split(' ');81if (ver && url) {82if (!url.startsWith('https://')) {83url = 'https://' + url;84}85let option = document.createElement('option');86option.value = url;87option.text = ver;88menu.add(option);89}90}91});92})93.catch(error => {94console.log('Failed to fetch versions.txt file.');95});96} catch (error) {97console.log('Failed to fetch versions.txt file.');98}99}100101fetchVersions()102103// Function to change the version based on versions menu selection104function changeVersion() {105let select_element = document.getElementById("versions-menu");106let selected_ver = select_element.options[select_element.selectedIndex].text;107let selected_url = select_element.value;108if (selected_url) {109if (window.location.protocol == 'file:') {110let pathname = window.location.pathname;111let cutoff_point = pathname.indexOf('/html');112if (cutoff_point !== -1) {113pathname = pathname.substring(cutoff_point);114window.location.href = selected_url + pathname;115} else {116window.location.href = selected_url + '/index.html';117}118} else {119window.location.href = selected_url + window.location.pathname;120}121}122}123124125// Listen to the kernel status changes126// https://thebe.readthedocs.io/en/stable/events.html127thebelab.on("status", function (evt, data) {128if (data.status === 'building') {129const elements = document.querySelectorAll('.thebelab-cell');130elements.forEach(element => {131element.style.filter = 'opacity(50%)';132});133const element = document.getElementById("thebelab-activate-button");134element.innerHTML = "Building";135element.style.right = '.4rem';136}137else if (data.status === 'built') {138const elements = document.querySelectorAll('.thebelab-cell');139elements.forEach(element => {140element.style.filter = 'opacity(60%)';141});142const element = document.getElementById("thebelab-activate-button");143element.innerHTML = "Built";144element.style.right = '.4rem';145}146else if (data.status === 'launching') {147const elements = document.querySelectorAll('.thebelab-cell');148elements.forEach(element => {149element.style.filter = 'opacity(70%)';150});151const element = document.getElementById("thebelab-activate-button");152element.innerHTML = "Launching";153element.style.right = '.4rem';154}155else if (data.status === 'failed') {156const elements = document.querySelectorAll('.thebelab-cell');157elements.forEach(element => {158element.style.filter = 'opacity(50%)';159});160const element = document.getElementById("thebelab-activate-button");161element.innerHTML = 'Failed: ' + data.message;162element.style.right = '.4rem';163element.style.width = 'auto';164element.style.color = 'red';165}166else if (data.status === 'ready') {167const elements = document.querySelectorAll('.thebelab-cell');168elements.forEach(element => {169element.style.filter = 'opacity(100%)';170});171const element = document.getElementById("thebelab-activate-button");172element.innerHTML = "Ready";173element.style.right = null;174// Run custom code when the kernel is ready175const kernel = data.kernel;176kernel.requestExecute({code: "%display latex"});177}178});179180181// Activate Thebe when "Sage Live" tab is clicked182document.querySelectorAll('input[class="tab-input"]').forEach((elem) => {183elem.addEventListener("click", function(event) {184if (elem.nextElementSibling) {185if (elem.nextElementSibling.nextElementSibling) {186if (elem.nextElementSibling.nextElementSibling.querySelector('div[class="thebelab-code"]')) {187initThebelab();188}189}190}191});192});193194195