]> BookStack Code Mirror - bookstack/blobdiff - resources/assets/js/pages/page-show.js
Removed invalid comments, and formatted the code.
[bookstack] / resources / assets / js / pages / page-show.js
index 3693031fbf4da14d29f5e6f87b4f083968cf44ef..32c1b6f5bf94cef42761b70ee85985b01231c9f5 100644 (file)
@@ -173,10 +173,8 @@ let setupPageShow = window.setupPageShow = function (pageId) {
 
       function addNavObserver(headings) {
           // Setup the intersection observer.
-          // margin top = -35px to trigger the threshold change before the heading
-          // has completely left the viewport on the top.
           let intersectOpts = {
-              rootMargin: '-35px 0px 0px 0px',
+              rootMargin: '0px 0px 0px 0px',
               threshold: 1.0
           }
           $pageNav = $('.sidebar-page-nav');
@@ -189,23 +187,25 @@ let setupPageShow = window.setupPageShow = function (pageId) {
       }
 
       function cbHeadingVisible(entries, observer) {
-          let element = null;
           for (let i = 0; i !== entries.length; ++i) {
               let currentEntry = entries[i];
-              // check if its currently visible and its distance from top of viewport is less than 100
-              if (currentEntry.intersectionRatio <= 1 && currentEntry.boundingClientRect.y < 100) {
-                  element = currentEntry.target;
+              let element = currentEntry.target;
+              // check if its currently visible
+              if (currentEntry.intersectionRatio === 1) {
+                  highlightElement(element.id);
               } else {
-                  break;
+                  removeHighlight(element.id);
               }
           }
-          if (!element) {
-              return;
-          }
-          let elementId = element.id;
-          $pageNav.find('a').removeClass('current-heading');
+      }
+
+      function highlightElement(elementId) {
           $pageNav.find('a[href="#' + elementId + '"]').addClass('current-heading');
       }
+
+      function removeHighlight(elementId) {
+          $pageNav.find('a[href="#' + elementId + '"]').removeClass('current-heading');
+      }
     }
 };