@codedoc/core
Version:
Create beautiful modern documentation websites.
111 lines • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.smoothLoading$ = exports.smoothLoading = void 0;
var transport_1 = require("@connectv/sdh/transport");
var custom_event_1 = require("./custom-event");
var lastpath = undefined;
function navigate(url, push) {
if (push === void 0) { push = true; }
var 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: url } }));
return;
}
container.style.opacity = '0';
if (push) {
history.pushState(url, '', url);
lastpath = location.pathname;
}
window.dispatchEvent(new CustomEvent('navigation-start', { detail: { url: url } }));
fetch(url)
.then(function (response) { return response.text(); })
.then(function (html) {
var _a;
var parser = new DOMParser();
var dom = parser.parseFromString(html, 'text/html');
var 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(function () {
var _a;
container.innerHTML = '';
var _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(function (s) {
var _a;
var _s = document.createElement('script');
s.getAttributeNames().forEach(function (attr) { return _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(function () {
if (push) {
window.scroll(0, 0);
setTimeout(function () {
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: url } }));
}, 150);
});
}
else {
window.location.href = url;
lastpath = location.pathname;
}
}
function smoothLoading() {
custom_event_1.polyfillCustomEvent();
transport_1.onReady(function () {
lastpath = location.pathname;
if (!window.__smooth_loading_plugged) {
window.__smooth_loading_plugged = true;
document.addEventListener('click', function (event) {
var _a;
var 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') {
var url = target.getAttribute('href') || '';
event.preventDefault();
navigate(url);
return;
}
});
var isSafari_1 = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
window.addEventListener('popstate', function (event) {
if (location.pathname === lastpath)
return;
lastpath = location.pathname;
if (isSafari_1)
window.location.href = event.state || window.location.href;
else
navigate(event.state || window.location.href, false);
});
}
});
}
exports.smoothLoading = smoothLoading;
exports.smoothLoading$ = transport_1.funcTransport(smoothLoading);
//# sourceMappingURL=smooth-loading.js.map