Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
fixup: [spec] npm run build
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 20, 2023
1 parent 90c5724 commit 805c733
Showing 1 changed file with 98 additions and 30 deletions.
128 changes: 98 additions & 30 deletions index.html
Expand Up @@ -455,25 +455,68 @@
};
function findActiveClause(root, path) {
let clauses = getChildClauses(root);
path = path || [];
for (let $clause of clauses) {
let rect = $clause.getBoundingClientRect();
let visibleClauses = getVisibleClauses(root, path);
let midpoint = Math.floor(window.innerHeight / 2);
for (let [$clause, path] of visibleClauses) {
let { top: clauseTop, bottom: clauseBottom } = $clause.getBoundingClientRect();
let isFullyVisibleAboveTheFold =
clauseTop > 0 && clauseTop < midpoint && clauseBottom < window.innerHeight;
if (isFullyVisibleAboveTheFold) {
return path;
}
}
visibleClauses.sort(([, pathA], [, pathB]) => pathB.length - pathA.length);
for (let [$clause, path] of visibleClauses) {
let { top: clauseTop, bottom: clauseBottom } = $clause.getBoundingClientRect();
let $header = $clause.querySelector('h1');
let clauseStyles = getComputedStyle($clause);
let marginTop = Math.max(
parseInt(getComputedStyle($clause)['margin-top']),
0,
parseInt(clauseStyles['margin-top']),
parseInt(getComputedStyle($header)['margin-top'])
);
if (rect.top - marginTop <= 1 && rect.bottom > 0) {
return findActiveClause($clause, path.concat($clause)) || path;
let marginBottom = Math.max(0, parseInt(clauseStyles['margin-bottom']));
let crossesMidpoint =
clauseTop - marginTop <= midpoint && clauseBottom + marginBottom >= midpoint;
if (crossesMidpoint) {
return path;
}
}
return path;
}
function getVisibleClauses(root, path) {
let childClauses = getChildClauses(root);
path = path || [];
let result = [];
let seenVisibleClause = false;
for (let $clause of childClauses) {
let { top: clauseTop, bottom: clauseBottom } = $clause.getBoundingClientRect();
let isPartiallyVisible =
(clauseTop > 0 && clauseTop < window.innerHeight) ||
(clauseBottom > 0 && clauseBottom < window.innerHeight) ||
(clauseTop < 0 && clauseBottom > window.innerHeight);
if (isPartiallyVisible) {
seenVisibleClause = true;
let innerPath = path.concat($clause);
result.push([$clause, innerPath]);
result.push(...getVisibleClauses($clause, innerPath));
} else if (seenVisibleClause) {
break;
}
}
return result;
}
function* getChildClauses(root) {
for (let el of root.children) {
switch (el.nodeName) {
Expand Down Expand Up @@ -1165,12 +1208,40 @@
return [...menu.$menu.querySelectorAll('.active')].map(getTocPath).filter(p => p != null);
}
function loadStateFromSessionStorage() {
if (!window.sessionStorage || typeof menu === 'undefined' || window.navigating) {
function initTOCExpansion(visibleItemLimit) {
// Initialize to a reasonable amount of TOC expansion:
// * Expand any full-breadth nesting level up to visibleItemLimit.
// * Expand any *single-item* level while under visibleItemLimit (even if that pushes over it).
// Limit to initialization by bailing out if any parent item is already expanded.
const tocItems = Array.from(document.querySelectorAll('#menu-toc li'));
if (tocItems.some(li => li.classList.contains('active') && li.querySelector('li'))) {
return;
}
const selfAndSiblings = maybe => Array.from(maybe?.parentNode.children ?? []);
let currentLevelItems = selfAndSiblings(tocItems[0]);
let availableCount = visibleItemLimit - currentLevelItems.length;
while (availableCount > 0 && currentLevelItems.length) {
const nextLevelItems = currentLevelItems.flatMap(li => selfAndSiblings(li.querySelector('li')));
availableCount -= nextLevelItems.length;
if (availableCount > 0 || currentLevelItems.length === 1) {
// Expand parent items of the next level down (i.e., current-level items with children).
for (const ol of new Set(nextLevelItems.map(li => li.parentNode))) {
ol.closest('li').classList.add('active');
}
}
currentLevelItems = nextLevelItems;
}
}
function initState() {
if (typeof menu === 'undefined' || window.navigating) {
return;
}
if (sessionStorage.referencePaneState != null) {
let state = JSON.parse(sessionStorage.referencePaneState);
const storage = typeof sessionStorage !== 'undefined' ? sessionStorage : Object.create(null);
if (storage.referencePaneState != null) {
let state = JSON.parse(storage.referencePaneState);
if (state != null) {
if (state.type === 'ref') {
let entry = menu.search.biblio.byId[state.id];
Expand All @@ -1184,39 +1255,36 @@
referencePane.showSDOsBody(sdos, state.id);
}
}
delete sessionStorage.referencePaneState;
delete storage.referencePaneState;
}
}
if (sessionStorage.activeTocPaths != null) {
document
.getElementById('menu-toc')
.querySelectorAll('.active')
.forEach(e => {
e.classList.remove('active');
});
let active = JSON.parse(sessionStorage.activeTocPaths);
if (storage.activeTocPaths != null) {
document.querySelectorAll('#menu-toc li.active').forEach(li => li.classList.remove('active'));
let active = JSON.parse(storage.activeTocPaths);
active.forEach(activateTocPath);
delete sessionStorage.activeTocPaths;
delete storage.activeTocPaths;
} else {
initTOCExpansion(20);
}
if (sessionStorage.searchValue != null) {
let value = JSON.parse(sessionStorage.searchValue);
if (storage.searchValue != null) {
let value = JSON.parse(storage.searchValue);
menu.search.$searchBox.value = value;
menu.search.search(value);
delete sessionStorage.searchValue;
delete storage.searchValue;
}
if (sessionStorage.tocScroll != null) {
let tocScroll = JSON.parse(sessionStorage.tocScroll);
if (storage.tocScroll != null) {
let tocScroll = JSON.parse(storage.tocScroll);
menu.$toc.scrollTop = tocScroll;
delete sessionStorage.tocScroll;
delete storage.tocScroll;
}
}
document.addEventListener('DOMContentLoaded', loadStateFromSessionStorage);
document.addEventListener('DOMContentLoaded', initState);
window.addEventListener('pageshow', loadStateFromSessionStorage);
window.addEventListener('pageshow', initState);
window.addEventListener('beforeunload', () => {
if (!window.sessionStorage || typeof menu === 'undefined') {
Expand Down Expand Up @@ -2696,7 +2764,7 @@ <h2>Copyright Notice</h2>
<p>© 2023 Justin Ridgewell, Jordan Harband</p>
<h2>Software License</h2>
<p>All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.</p>
<p>All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT <a href="https://ecma-international.org/memento/codeofconduct.htm">https://ecma-international.org/memento/codeofconduct.htm</a> FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.</p>
<p>Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:</p>
Expand Down

0 comments on commit 805c733

Please sign in to comment.