@codedoc/core
Version:
Create beautiful modern documentation websites.
54 lines • 2.11 kB
JavaScript
import { funcTransport, onReady } from '@connectv/sdh/transport';
export function contentNavHighlight() {
onReady(() => {
let links = [];
const _exec = () => {
links = [];
document.querySelectorAll('a[data-content-highlight]').forEach(_a$ => {
let a$ = _a$;
const id = a$.getAttribute('data-content-highlight');
if (id) {
const ref$ = document.getElementById(id);
if (ref$) {
links.push({ a$, ref$ });
}
}
});
};
_exec();
window.addEventListener('navigation', _exec);
function update() {
var _a;
let noactive = true;
let latest;
links.forEach(({ a$, ref$ }) => {
const top = ref$.getBoundingClientRect().top;
if (top < window.innerHeight - 96 && top > 0) {
noactive = false;
a$.classList.add('active');
}
else {
if (top < 0)
latest = a$;
a$.classList.remove('active');
}
});
if (noactive)
latest === null || latest === void 0 ? void 0 : latest.classList.add('active');
const target = (_a = links.find(l => l.a$.classList.contains('active'))) === null || _a === void 0 ? void 0 : _a.a$;
if (target) {
const parent = target.parentElement;
if (parent.scrollHeight != parent.offsetHeight) {
parent.scrollTop = target.offsetTop - parent.offsetHeight / 3;
}
}
}
if (links.length > 0) {
update();
document.addEventListener('scroll', () => setTimeout(update, 1));
window.addEventListener('navigation', update);
}
});
}
export const contentNavHighlight$ = /*#__PURE__*/ funcTransport(contentNavHighlight);
//# sourceMappingURL=highlight.js.map