Skip to content

Commit

Permalink
website: TOC plugin new behavior & bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Oct 6, 2020
1 parent b92b960 commit 37a1149
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions build/plugins/table-of-contents.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- table of contents plugin -->

<script>
(function() {
// /////////////////////////
Expand All @@ -22,10 +20,13 @@
// which types of elements to add links for, in
// "document.querySelector" format
typesQuery: 'h1, h2, h3',
// whether default behavior is to be closed ('false'), open
// ('true'), or only open when screen wide enough to fit panel
// ('auto'). note: still always starts closed when page loads.
open: 'auto',
// whether toc starts open. use 'true' or 'false', or 'auto' to
// use 'true' behavior when screen wide enough and 'false' when not
startOpen: 'false',
// whether toc closes when clicking on toc link. use 'true' or
// 'false', or 'auto' to use 'false' behavior when screen wide
// enough and 'true' when not
clickClose: 'auto',
// if list item is more than this many characters, text will be
// truncated
charLimit: '50',
Expand All @@ -50,13 +51,21 @@
if (!panel)
return;
makeEntries(panel);
// attach panel to document after making entries, so 'toc' heading
// in panel isn't included in toc
document.body.insertBefore(panel, document.body.firstChild);

closePanel();
// initial panel state
if (
options.startOpen === 'true' ||
(options.startOpen === 'auto' && !isSmallScreen())
)
openPanel();
else
closePanel();

// attach click, scroll, and hash change listeners to window
window.addEventListener('click', onClick);
window.addEventListener('touchstart', onClick);
window.addEventListener('scroll', onScroll);
window.addEventListener('hashchange', onScroll);
window.addEventListener('keyup', onKeyUp);
Expand All @@ -74,25 +83,10 @@
return window.innerWidth < 816 + 260 * 2;
}

// open/close panel based on option and screen size
function openOrClosePanel() {
if (
options.open === 'true' ||
(options.open === 'auto' && !isSmallScreen())
)
openPanel();
else
closePanel();
}

// when mouse is clicked anywhere in window
function onClick() {
const panel = document.getElementById('toc_panel');
if (!panel)
return;

if (panel.dataset.open === 'true')
openOrClosePanel();
if (isSmallScreen())
closePanel();
}

// when window is scrolled or hash changed
Expand Down Expand Up @@ -174,8 +168,8 @@
button.classList.add('icon_button');

// create header text
const text = document.createElement('h3');
text.innerHTML = 'View Table of Contents';
const text = document.createElement('h4');
text.innerHTML = 'Table of Contents';

// create container for toc list
const list = document.createElement('div');
Expand Down Expand Up @@ -252,8 +246,14 @@
}

// when link is clicked
function onLinkClick() {
openOrClosePanel();
function onLinkClick(event) {
if (
options.clickClose === 'true' ||
(options.clickClose === 'auto' && isSmallScreen())
)
closePanel();
else
openPanel();
}

// open panel if closed, close if opened
Expand Down

0 comments on commit 37a1149

Please sign in to comment.