34 lines
1.5 KiB
JavaScript
34 lines
1.5 KiB
JavaScript
// Keeps the page editor out of the browser history.
|
|
//
|
|
// Opening the editor for the page you are already on is a mode switch, not a
|
|
// new destination, so it replaces the current history entry instead of pushing
|
|
// one; CANCEL replaces it right back, and SAVE does the same via postSave in
|
|
// editor/main.js. Without this, page -> edit -> save leaves
|
|
// [prev, page, editor, page'] behind and Back walks through the editor and a
|
|
// stale pre-save snapshot of the page before reaching prev.
|
|
//
|
|
// Links to a *different* page's editor (new page, new child) still push — the
|
|
// page you started from has to stay in history.
|
|
(function () {
|
|
function isEdit(loc) {
|
|
return new URLSearchParams(loc.search).has('edit');
|
|
}
|
|
|
|
document.addEventListener('click', function (e) {
|
|
if (e.defaultPrevented || e.button !== 0) return;
|
|
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
var a = e.target.closest ? e.target.closest('a[href]') : null;
|
|
if (!a || a.target || a.hasAttribute('download')) return;
|
|
|
|
var url = new URL(a.href, window.location.href);
|
|
if (url.origin !== window.location.origin) return;
|
|
if (url.pathname !== window.location.pathname) return;
|
|
// Same page: only editor entry/exit is a mode switch. Plain anchor
|
|
// links share the pathname too and must keep their normal behaviour.
|
|
if (!isEdit(url) && !isEdit(window.location)) return;
|
|
|
|
e.preventDefault();
|
|
window.location.replace(url.href);
|
|
});
|
|
}());
|