Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #466. Adds support for header highlighting using intersection o… #580

Merged
merged 6 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions resources/assets/js/pages/page-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion resources/assets/sass/_grid.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ body.flexbox {
width: 30%;
left: 0;
height: 100%;
overflow-y: scroll;
overflow-y: auto;
Copy link
Member Author

@Abijeet Abijeet Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firefox 57 adds a scrollbar with overflow-y: scroll.

bc1bc2e9-c489-425d-b210-cd3606a7c06e

-ms-overflow-style: none;
//background-color: $primary-faded;
border-left: 1px solid #DDD;
Expand Down
3 changes: 3 additions & 0 deletions resources/assets/sass/_lists.scss
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
.h6 {
margin-left: $nav-indent*4;
}
.current-heading {
font-weight: bold;
}
}

// Sidebar list
Expand Down