diff --git a/resources/assets/js/pages/page-show.js b/resources/assets/js/pages/page-show.js index 14437cd68..32c1b6f5b 100644 --- a/resources/assets/js/pages/page-show.js +++ b/resources/assets/js/pages/page-show.js @@ -150,6 +150,63 @@ let setupPageShow = window.setupPageShow = function (pageId) { unstickTree(); } }); + + + // Check if support is present for IntersectionObserver + if ('IntersectionObserver' in window && + 'IntersectionObserverEntry' in window && + 'intersectionRatio' in window.IntersectionObserverEntry.prototype) { + addPageHighlighting(); + } + + function addPageHighlighting() { + let $pageNav = null; + + $(document).ready(function () { + // fetch all the headings. + let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6'); + // if headings are present, add observers. + if (headings.length > 0) { + addNavObserver(headings); + } + }); + + function addNavObserver(headings) { + // Setup the intersection observer. + let intersectOpts = { + rootMargin: '0px 0px 0px 0px', + threshold: 1.0 + } + $pageNav = $('.sidebar-page-nav'); + let pageNavObserver = new IntersectionObserver(cbHeadingVisible, intersectOpts); + + // observe each heading + for (let i = 0; i !== headings.length; ++i) { + pageNavObserver.observe(headings[i]); + } + } + + function cbHeadingVisible(entries, observer) { + for (let i = 0; i !== entries.length; ++i) { + let currentEntry = entries[i]; + let element = currentEntry.target; + // check if its currently visible + if (currentEntry.intersectionRatio === 1) { + highlightElement(element.id); + } else { + removeHighlight(element.id); + } + } + } + + function highlightElement(elementId) { + $pageNav.find('a[href="#' + elementId + '"]').addClass('current-heading'); + } + + function removeHighlight(elementId) { + $pageNav.find('a[href="#' + elementId + '"]').removeClass('current-heading'); + } + } }; module.exports = setupPageShow; \ No newline at end of file diff --git a/resources/assets/sass/_grid.scss b/resources/assets/sass/_grid.scss index b42ad37bb..c145f4280 100644 --- a/resources/assets/sass/_grid.scss +++ b/resources/assets/sass/_grid.scss @@ -135,7 +135,7 @@ body.flexbox { width: 30%; left: 0; height: 100%; - overflow-y: scroll; + overflow-y: auto; -ms-overflow-style: none; //background-color: $primary-faded; border-left: 1px solid #DDD; diff --git a/resources/assets/sass/_lists.scss b/resources/assets/sass/_lists.scss index 4bc31bbb0..0496d794e 100644 --- a/resources/assets/sass/_lists.scss +++ b/resources/assets/sass/_lists.scss @@ -82,6 +82,9 @@ .h6 { margin-left: $nav-indent*4; } + .current-heading { + font-weight: bold; + } } // Sidebar list