fix(ui): document viewer back button returns to the page you came from (chat or sources)

This commit is contained in:
2026-08-22 15:26:43 -04:00
parent 8ca564cd83
commit 2485b50af0
6 changed files with 287 additions and 31 deletions
+13 -6
View File
@@ -65,14 +65,21 @@ const reducedMotion =
typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
const SCROLL = reducedMotion ? "auto" : "smooth";
/* ---------- document viewer link (phase 10) ----------
* Every cited document opens in the viewer, in a NEW tab. Both query
/* ---------- document viewer link (phase 10; phase 13 adds `back`) ----------
* Every cited document opens in the viewer, in a NEW tab. All query
* values are percent-encoded: real paths contain slashes and sometimes
* spaces, which would otherwise corrupt the query string. (The renderer
* spaces, which would otherwise corrupt the query string. `back` tells the
* viewer which page to return to when its back button is clicked — the
* chips live in the chat, so chat passes "/" (the viewer validates it:
* only same-origin relative URLs are honored; Sources links omit it and
* get the viewer's /sources.html default). (The renderer
* renderMarkdown/escapeHtml now lives in assets/markdown.js — a classic
* script loaded by index.html and document.html before these modules.) */
export function documentUrl(source, path) {
return "/document.html?source=" + encodeURIComponent(source) + "&path=" + encodeURIComponent(path);
export function documentUrl(source, path, back = "/") {
let url =
"/document.html?source=" + encodeURIComponent(source) + "&path=" + encodeURIComponent(path);
if (back) url += "&back=" + encodeURIComponent(back);
return url;
}
/* ---------- avatar glyphs (phase 08: emoji-free chrome) ----------
@@ -291,7 +298,7 @@ function appendSources(wrap, sources) {
const chip = document.createElement("a");
chip.className = "source-chip";
chip.setAttribute("role", "listitem");
chip.href = documentUrl(s.source, s.path);
chip.href = documentUrl(s.source, s.path, "/"); // back → the chat page
chip.target = "_blank"; // open the full document in a new tab
chip.rel = "noopener";
chip.textContent = label;
+25 -8
View File
@@ -28,14 +28,31 @@ const notFoundEl = document.querySelector("#doc-not-found");
const mainEl = document.querySelector("#main");
const backLink = document.querySelector("#doc-back");
/* Back: prefer the browser's own history when there is one (the viewer was
* opened from this tab's session); a fresh tab lands on the Sources page. */
backLink.addEventListener("click", (e) => {
if (window.history.length > 1) {
e.preventDefault();
window.history.back();
}
});
/* Back button (phase 13): the return target comes from the `back` query
* param, not the browser history — both entry points (chat source chips
* and the Sources table) open the viewer in a NEW tab, where there is no
* history to go back to. The param is honored only for same-origin
* relative URLs (starts with "/" but not "//"), so absolute (https://…),
* protocol-relative (//…), and pseudo-protocol (javascript:…) values are
* rejected; anything else falls back to the Sources page. The static
* href="/sources.html" in document.html remains the no-JS fallback, and
* with the href set the anchor's default click behavior IS the
* deterministic navigation (no browser-history heuristics). */
const backParam = params.get("back") || "";
const backTarget =
backParam.startsWith("/") && !backParam.startsWith("//")
? backParam
: "/sources.html";
backLink.href = backTarget;
const backLabel = backLink.querySelector("span");
if (backLabel) {
backLabel.textContent =
backTarget === "/"
? "Chat"
: backTarget === "/sources.html"
? "Sources"
: "Back";
}
function fmtDate(iso) {
try {