@codedoc/core
Version:
Create beautiful modern documentation websites.
106 lines • 4.77 kB
JavaScript
import { funcTransport, onReady } from '@connectv/sdh/transport';
import { polyfillCustomEvent } from './custom-event';
let lastpath = undefined;
function navigate(url, push = true) {
let container = document.getElementById('-codedoc-container');
if (container) {
if (window.innerWidth <= 1200 && window.codedocToggleToC)
window.codedocToggleToC(false);
if (push && url === location.pathname) {
window.dispatchEvent(new CustomEvent('same-page-navigation', { detail: { url } }));
return;
}
container.style.opacity = '0';
if (push) {
history.pushState(url, '', url);
lastpath = location.pathname;
}
window.dispatchEvent(new CustomEvent('navigation-start', { detail: { url } }));
fetch(url)
.then(response => response.text())
.then(html => {
var _a;
const parser = new DOMParser();
const dom = parser.parseFromString(html, 'text/html');
const title = document.head.querySelector('title');
if (title)
title.innerHTML = ((_a = dom.head.querySelector('title')) === null || _a === void 0 ? void 0 : _a.innerHTML) || title.innerHTML;
setTimeout(() => {
var _a;
container.innerHTML = '';
const _newc = dom.getElementById('-codedoc-container');
if (_newc) {
(_a = container.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(_newc, container);
container.remove();
container = _newc;
container.style.transition = 'none';
container.style.opacity = '0';
container.querySelectorAll('script').forEach(s => {
var _a;
const _s = document.createElement('script');
s.getAttributeNames().forEach(attr => _s.setAttribute(attr, s.getAttribute(attr)));
_s.text = s.text;
(_a = s.parentElement) === null || _a === void 0 ? void 0 : _a.insertBefore(_s, s);
s.remove();
});
}
if (push) {
document.documentElement.style.scrollBehavior = 'auto';
}
setTimeout(() => {
if (push) {
window.scroll(0, 0);
setTimeout(() => {
var _a;
document.documentElement.style.scrollBehavior = '';
if (window.location.hash) {
(_a = document.querySelector(window.location.hash)) === null || _a === void 0 ? void 0 : _a.scrollIntoView();
}
}, 10);
}
container.style.transition = '';
container.style.opacity = '1';
}, 50);
window.dispatchEvent(new CustomEvent('navigation', { detail: { url } }));
}, 150);
});
}
else {
window.location.href = url;
lastpath = location.pathname;
}
}
export function smoothLoading() {
polyfillCustomEvent();
onReady(() => {
lastpath = location.pathname;
if (!window.__smooth_loading_plugged) {
window.__smooth_loading_plugged = true;
document.addEventListener('click', event => {
var _a;
let target = event.target;
while (target && !target.href) {
target = target.parentNode;
}
if (target && ((_a = target.getAttribute('href')) === null || _a === void 0 ? void 0 : _a.startsWith('/')) && target.getAttribute('target') !== '_blank') {
const url = target.getAttribute('href') || '';
event.preventDefault();
navigate(url);
return;
}
});
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
window.addEventListener('popstate', event => {
if (location.pathname === lastpath)
return;
lastpath = location.pathname;
if (isSafari)
window.location.href = event.state || window.location.href;
else
navigate(event.state || window.location.href, false);
});
}
});
}
export const smoothLoading$ = /*#__PURE__*/ funcTransport(smoothLoading);
//# sourceMappingURL=smooth-loading.js.map