Revert "fix: harden automatic mobile boards"
This reverts commit d0b9348b86.
This commit is contained in:
1 parent
967ed8dd73
commit
e93293f8a9
22 files changed
+69
-491
No files matched your search
@@ -1,51 +0,0 @@
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { classifyGuardedNavigation, removeGuardHistoryEntry, replaceGuardedSameDocumentUrl } from "./_guarded-history";
|
||||
|
||||
describe("guarded board edit history", () => {
|
||||
beforeEach(() => {
|
||||
window.history.replaceState({ page: "board" }, "", "/boards/demo");
|
||||
});
|
||||
|
||||
test("replaces the guard entry for same-document fragment navigation", () => {
|
||||
const section = document.createElement("section");
|
||||
section.id = "Media requests";
|
||||
document.body.appendChild(section);
|
||||
const scrollIntoView = vi.fn();
|
||||
Object.defineProperty(section, "scrollIntoView", { value: scrollIntoView });
|
||||
|
||||
replaceGuardedSameDocumentUrl(new URL("/boards/demo#Media%20requests", window.location.href), {
|
||||
key: "__guard",
|
||||
id: "guard-id",
|
||||
});
|
||||
|
||||
expect(window.location.hash).toBe("#Media%20requests");
|
||||
expect(window.history.state).toStrictEqual({
|
||||
page: "board",
|
||||
__guard: "guard-id",
|
||||
});
|
||||
expect(scrollIntoView).toHaveBeenCalledOnce();
|
||||
section.remove();
|
||||
});
|
||||
|
||||
test("treats an explicit empty fragment as same-document navigation", () => {
|
||||
const current = new URL(window.location.href);
|
||||
const destination = new URL("#", current);
|
||||
|
||||
expect(classifyGuardedNavigation(destination, current)).toBe("same-document");
|
||||
});
|
||||
|
||||
test("removes the guard while preserving the fragment when editing ends", () => {
|
||||
window.history.replaceState({ page: "board", __guard: "guard-id" }, "", "/boards/demo#section");
|
||||
const back = vi.spyOn(window.history, "back").mockImplementation(() => {
|
||||
window.history.replaceState({ page: "board" }, "", "/boards/demo");
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
});
|
||||
|
||||
removeGuardHistoryEntry("http://localhost:3000/boards/demo#section", "__guard");
|
||||
|
||||
expect(back).toHaveBeenCalledOnce();
|
||||
expect(window.location.href).toBe("http://localhost:3000/boards/demo#section");
|
||||
expect(window.history.state).toStrictEqual({ page: "board" });
|
||||
});
|
||||
});
|
||||
@@ -1,58 +0,0 @@
|
||||
const getHistoryState = () =>
|
||||
window.history.state && typeof window.history.state === "object"
|
||||
? (window.history.state as Record<string, unknown>)
|
||||
: {};
|
||||
|
||||
interface GuardState {
|
||||
key: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export const classifyGuardedNavigation = (destination: URL, current: URL) => {
|
||||
if (destination.href === current.href) return "same-url";
|
||||
if (
|
||||
destination.origin === current.origin &&
|
||||
destination.pathname === current.pathname &&
|
||||
destination.search === current.search
|
||||
) {
|
||||
return "same-document";
|
||||
}
|
||||
return "other-document";
|
||||
};
|
||||
|
||||
export const replaceGuardedSameDocumentUrl = (destination: URL, guard: GuardState) => {
|
||||
window.history.replaceState(
|
||||
{
|
||||
...getHistoryState(),
|
||||
[guard.key]: guard.id,
|
||||
},
|
||||
document.title,
|
||||
destination.href,
|
||||
);
|
||||
|
||||
if (!destination.hash) {
|
||||
window.scrollTo({ top: 0, left: 0 });
|
||||
return;
|
||||
}
|
||||
|
||||
let targetId = destination.hash.slice(1);
|
||||
try {
|
||||
targetId = decodeURIComponent(targetId);
|
||||
} catch {
|
||||
// Keep the encoded value when the fragment contains malformed escape sequences.
|
||||
}
|
||||
document.getElementById(targetId)?.scrollIntoView();
|
||||
};
|
||||
|
||||
export const removeGuardHistoryEntry = (guardedUrl: string, guardStateKey: string) => {
|
||||
window.addEventListener(
|
||||
"popstate",
|
||||
() => {
|
||||
const state = { ...getHistoryState() };
|
||||
delete state[guardStateKey];
|
||||
window.history.replaceState(state, document.title, guardedUrl);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
window.history.back();
|
||||
};
|
||||
Reference in new issue
Block a user