Fix back-to-cancel behavior of editor
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
switch (e.key) {
|
||||
case 'E':
|
||||
e.preventDefault();
|
||||
// replace, not assign — same reasoning as history-nav.js.
|
||||
window.location.replace(window.location.pathname + '?edit');
|
||||
// assign, not replace — opening the editor pushes a history
|
||||
// entry so Back cancels the edit (see history-nav.js).
|
||||
window.location.href = window.location.pathname + '?edit';
|
||||
break;
|
||||
case 'N':
|
||||
e.preventDefault();
|
||||
|
||||
+16
-12
@@ -1,14 +1,14 @@
|
||||
// Keeps the page editor out of the browser history.
|
||||
// Keeps the editor from lingering in history once you leave it — while still
|
||||
// letting the browser/Android Back button CANCEL an edit session.
|
||||
//
|
||||
// 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.
|
||||
// Opening the editor pushes a normal history entry, so Back exits the editor
|
||||
// and returns to the page (the primary "back to cancel" gesture on mobile).
|
||||
//
|
||||
// Links to a *different* page's editor (new page, new child) still push — the
|
||||
// page you started from has to stay in history.
|
||||
// Leaving the editor by CANCEL is the one transition we rewrite: the CANCEL
|
||||
// link points back at the same page, so we replace the editor entry instead of
|
||||
// pushing a second page entry on top of it. Without this, page -> edit -> CANCEL
|
||||
// would leave [page, editor, page] and Back would walk straight back into the
|
||||
// editor. SAVE does the equivalent from editor/main.js (replaceState + reload).
|
||||
(function () {
|
||||
function isEdit(loc) {
|
||||
return new URLSearchParams(loc.search).has('edit');
|
||||
@@ -20,12 +20,16 @@
|
||||
var a = e.target.closest ? e.target.closest('a[href]') : null;
|
||||
if (!a || a.target || a.hasAttribute('download')) return;
|
||||
|
||||
// Only act while inside the editor. Entering the editor stays a normal
|
||||
// push so Back can cancel it.
|
||||
if (!isEdit(window.location)) 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;
|
||||
// Leaving the editor to another page (e.g. a wikilink) keeps its normal
|
||||
// push; only the same-page exit (CANCEL) is collapsed.
|
||||
if (isEdit(url)) return;
|
||||
|
||||
e.preventDefault();
|
||||
window.location.replace(url.href);
|
||||
|
||||
Reference in New Issue
Block a user