2024-10-12 04:55:51 +08:00
|
|
|
import * as DOM from '../services/dom.ts';
|
2024-10-10 19:03:24 +08:00
|
|
|
import {scrollAndHighlightElement} from '../services/util.ts';
|
2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2018-04-01 19:46:27 +08:00
|
|
|
|
2023-04-19 22:20:04 +08:00
|
|
|
function toggleAnchorHighlighting(elementId, shouldHighlight) {
|
2023-05-31 23:38:20 +08:00
|
|
|
DOM.forEach(`#page-navigation a[href="#${elementId}"]`, anchor => {
|
2023-04-19 22:20:04 +08:00
|
|
|
anchor.closest('li').classList.toggle('current-heading', shouldHighlight);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function headingVisibilityChange(entries) {
|
|
|
|
for (const entry of entries) {
|
|
|
|
const isVisible = (entry.intersectionRatio === 1);
|
|
|
|
toggleAnchorHighlighting(entry.target.id, isVisible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addNavObserver(headings) {
|
|
|
|
// Setup the intersection observer.
|
|
|
|
const intersectOpts = {
|
|
|
|
rootMargin: '0px 0px 0px 0px',
|
|
|
|
threshold: 1.0,
|
|
|
|
};
|
|
|
|
const pageNavObserver = new IntersectionObserver(headingVisibilityChange, intersectOpts);
|
|
|
|
|
|
|
|
// observe each heading
|
|
|
|
for (const heading of headings) {
|
|
|
|
pageNavObserver.observe(heading);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
export class PageDisplay extends Component {
|
2018-04-01 19:46:27 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.pageId = this.$opts.pageId;
|
2018-04-01 19:46:27 +08:00
|
|
|
|
2022-02-08 19:10:01 +08:00
|
|
|
window.importVersioned('code').then(Code => Code.highlight());
|
2018-04-01 19:46:27 +08:00
|
|
|
this.setupNavHighlighting();
|
|
|
|
|
|
|
|
// Check the hash on load
|
|
|
|
if (window.location.hash) {
|
2022-11-16 00:04:46 +08:00
|
|
|
const text = window.location.hash.replace(/%20/g, ' ').substring(1);
|
2018-04-01 19:46:27 +08:00
|
|
|
this.goToText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sidebar page nav click event
|
2019-06-08 00:46:19 +08:00
|
|
|
const sidebarPageNav = document.querySelector('.sidebar-page-nav');
|
2019-06-08 07:02:51 +08:00
|
|
|
if (sidebarPageNav) {
|
|
|
|
DOM.onChildEvent(sidebarPageNav, 'a', 'click', (event, child) => {
|
2019-06-16 19:46:23 +08:00
|
|
|
event.preventDefault();
|
2022-11-15 07:19:02 +08:00
|
|
|
window.$components.first('tri-layout').showContent();
|
2019-06-16 19:46:23 +08:00
|
|
|
const contentId = child.getAttribute('href').substr(1);
|
|
|
|
this.goToText(contentId);
|
2023-04-19 05:20:02 +08:00
|
|
|
window.history.pushState(null, null, `#${contentId}`);
|
2019-06-08 07:02:51 +08:00
|
|
|
});
|
|
|
|
}
|
2018-04-01 19:46:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
goToText(text) {
|
2019-06-08 00:46:19 +08:00
|
|
|
const idElem = document.getElementById(text);
|
|
|
|
|
|
|
|
DOM.forEach('.page-content [data-highlighted]', elem => {
|
|
|
|
elem.removeAttribute('data-highlighted');
|
|
|
|
elem.style.backgroundColor = null;
|
|
|
|
});
|
|
|
|
|
2018-04-01 19:46:27 +08:00
|
|
|
if (idElem !== null) {
|
2019-06-08 07:02:51 +08:00
|
|
|
scrollAndHighlightElement(idElem);
|
2018-04-01 19:46:27 +08:00
|
|
|
} else {
|
2019-06-08 00:46:19 +08:00
|
|
|
const textElem = DOM.findText('.page-content > div > *', text);
|
|
|
|
if (textElem) {
|
2019-06-08 07:02:51 +08:00
|
|
|
scrollAndHighlightElement(textElem);
|
2019-06-08 00:46:19 +08:00
|
|
|
}
|
2018-04-01 19:46:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupNavHighlighting() {
|
2022-11-16 00:04:46 +08:00
|
|
|
const pageNav = document.querySelector('.sidebar-page-nav');
|
2018-04-01 19:46:27 +08:00
|
|
|
|
|
|
|
// fetch all the headings.
|
2022-11-16 00:04:46 +08:00
|
|
|
const headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
|
2018-04-01 19:46:27 +08:00
|
|
|
// if headings are present, add observers.
|
|
|
|
if (headings.length > 0 && pageNav !== null) {
|
|
|
|
addNavObserver(headings);
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 01:04:30 +08:00
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|