diff --git a/src/front-end/js/book.js b/src/front-end/js/book.js index 160ee93700..266a3a2b64 100644 --- a/src/front-end/js/book.js +++ b/src/front-end/js/book.js @@ -1,6 +1,6 @@ 'use strict'; -/* global default_theme, default_dark_theme, default_light_theme, hljs, ClipboardJS */ +/* global default_theme, default_dark_theme, default_light_theme, hljs, ClipboardJS, mdBook */ // Fix back button cache problem window.onunload = function() { }; @@ -523,34 +523,28 @@ aria-label="Show hidden lines">'; const sidebarResizeHandle = document.getElementById('sidebar-resize-handle'); let firstContact = null; - function showSidebar() { - body.classList.remove('sidebar-hidden'); - body.classList.add('sidebar-visible'); + function setSidebarVisibility(isVisible) { + body.classList.toggle('sidebar-hidden', !isVisible); + body.classList.toggle('sidebar-visible', isVisible); Array.from(sidebarLinks).forEach(function(link) { - link.setAttribute('tabIndex', 0); + link.setAttribute('tabIndex', isVisible ? 0 : -1); }); - sidebarToggleButton.setAttribute('aria-expanded', true); - sidebar.setAttribute('aria-hidden', false); - try { - localStorage.setItem('mdbook-sidebar', 'visible'); - } catch (e) { - // Ignore error. - } + sidebarToggleButton.setAttribute('aria-expanded', isVisible); + sidebar.setAttribute('aria-hidden', !isVisible); + + // We use sessionStorage for the sidebar visibility setting, as we want it to be + // specific to the current browser window and tab (since different windows can have + // different sizes), unlike the theme which is a global preference (ie. if you want + // a dark theme, you probably want it on all your windows) + mdBook.setConfig('sidebar', isVisible ? 'visible' : 'hidden', sessionStorage); + } + + function showSidebar() { + setSidebarVisibility(true); } function hideSidebar() { - body.classList.remove('sidebar-visible'); - body.classList.add('sidebar-hidden'); - Array.from(sidebarLinks).forEach(function(link) { - link.setAttribute('tabIndex', -1); - }); - sidebarToggleButton.setAttribute('aria-expanded', false); - sidebar.setAttribute('aria-hidden', true); - try { - localStorage.setItem('mdbook-sidebar', 'hidden'); - } catch (e) { - // Ignore error. - } + setSidebarVisibility(false); } // Toggle sidebar @@ -569,21 +563,36 @@ aria-label="Show hidden lines">'; sidebarResizeHandle.addEventListener('mousedown', initResize, false); - function initResize() { + let startClientX; + let startSidebarWidth; + function initResize(e) { window.addEventListener('mousemove', resize, false); window.addEventListener('mouseup', stopResize, false); + body.classList.add('sidebar-resizing'); + + startClientX = e.clientX; + startSidebarWidth = parseInt( + getComputedStyle(document.body) + .getPropertyValue('--sidebar-width') + ?.replace(/px$/, ''), + 10, + ); } + function resize(e) { - let pos = e.clientX - sidebar.offsetLeft; + const pos = e.clientX - sidebar.offsetLeft; if (pos < 20) { hideSidebar(); } else { if (body.classList.contains('sidebar-hidden')) { showSidebar(); } - pos = Math.min(pos, window.innerWidth - 100); - document.documentElement.style.setProperty('--sidebar-width', pos + 'px'); + const sidebarWidth = startSidebarWidth + + Math.min(e.clientX, window.innerWidth - 100) - + startClientX; + mdBook.setConfig('sidebarWidth', sidebarWidth, sessionStorage); + document.documentElement.style.setProperty('--sidebar-width', sidebarWidth + 'px'); } } //on mouseup remove windows functions mousemove & mouseup diff --git a/src/front-end/templates/index.hbs b/src/front-end/templates/index.hbs index 98f3b886c9..88499e1821 100644 --- a/src/front-end/templates/index.hbs +++ b/src/front-end/templates/index.hbs @@ -64,50 +64,83 @@