feat(lex): open the focus pane from the evidence drawer

This commit is contained in:
kert
2026-11-14 11:24:57 -05:00
parent 15b70febf2
commit f0df76c350
4 changed files with 97 additions and 3 deletions

View File

@@ -349,6 +349,8 @@ button.dis { font-family: var(--font-body); font-size: 14px; padding: 3px 10px;
.ev-src .src-t { font-size: 13px; color: var(--muted-foreground); }
.ev-src blockquote { margin: 0; padding: 8px 12px; background: var(--card); border-left: 3px solid var(--primary); font-size: 14px; line-height: 1.55; }
.ev-src blockquote.cmt { border-left-color: var(--s-prop); }
.ev-src .focus-open { justify-self: start; }
.ev-src .focus-pane { width: 100%; height: 72vh; border: 1px solid var(--border); background: var(--card); }
.ev-dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 3px 10px; font-size: 14px; margin: 0; }
.ev-dl dt { font-family: var(--font-mono); font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted-foreground); padding-top: 3px; }
.ev-dl dd { margin: 0; min-width: 0; }

View File

@@ -235,6 +235,17 @@ document.addEventListener("click", (e) => {
closeDrawer();
return;
}
const focusBtn = e.target.closest("button[data-focus-src]");
if (focusBtn && el.drawer.contains(focusBtn)) {
e.preventDefault();
const src = focusBtn.getAttribute("data-focus-src") || "";
if (!/^\/ui\/pdf\/[A-Za-z0-9]{6,12}\?protocol=[a-z0-9-]+$/.test(src)) return;
const frame = focusBtn.parentElement && focusBtn.parentElement.querySelector("iframe.focus-pane");
if (!frame) return;
if (frame.getAttribute("src") !== src) frame.setAttribute("src", src);
frame.hidden = false;
return;
}
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
const a = e.target.closest("a[href]");
if (a) {

View File

@@ -41,6 +41,26 @@ export const CITE_KIND = {
const kindLabel = (k) => KIND_LABEL[k] || String(k || "fact").replace(/_/g, " ");
const linkable = (cite) => cite.kind !== "cpt_book" && /^https?:\/\//i.test(cite.url || "");
// A library key, same shape the llm viewer accepts. The pane path is a
// route on this host, never a citation URL (R10) and never cite.url.
const FOCUS_KEY = /^[A-Za-z0-9]{6,12}$/;
/** Same-origin path of the focus pane for this source, or "" when the
* source has no library key or is a licensed book. */
export function focusPath(cite) {
if (!cite || cite.kind === "cpt_book") return "";
const key = String(cite.item_key || "");
if (!FOCUS_KEY.test(key)) return "";
const protocol = cite.kind === "fr_para" || cite.kind === "fr_page" ? "cms-disposition" : "comment-stance";
return `/ui/pdf/${key}?protocol=${protocol}`;
}
function focusControls(cite) {
const src = focusPath(cite);
if (!src) return "";
return `<button type="button" class="fbtn focus-open" data-focus-src="${esc(src)}">Open in focus pane</button><iframe class="focus-pane" title="Focus pane" sandbox="allow-scripts allow-same-origin" hidden></iframe>`;
}
/** A cite as a link that opens apart, or as its locator when it has no url
* (a licensed book says why its text is not shown). */
export function citeHtml(cite, { ref = "", refs = null } = {}) {
@@ -131,7 +151,7 @@ export function groupHtml(facts, cite) {
const items = facts
.map((f) => `<li data-ref="${esc(f.id)}" tabindex="0"><span class="eyebrow">${esc(f.id)} · ${esc(kindLabel(f.kind))}${f.year ? " · CY" + esc(f.year) : ""}</span> ${esc(f.text)}</li>`)
.join("");
return `<div class="ev" data-evidence-group><div class="d-head"><span class="eyebrow">${facts.length} facts cite this source</span><button class="close" type="button" data-close>Close</button></div><div class="ev-src"><div>${citeHtml(cite)}</div></div><ul class="facts">${items}</ul></div>`;
return `<div class="ev" data-evidence-group><div class="d-head"><span class="eyebrow">${facts.length} facts cite this source</span><button class="close" type="button" data-close>Close</button></div>${sourceHtml(cite)}<ul class="facts">${items}</ul></div>`;
}
function valueText(v) {
@@ -144,7 +164,7 @@ const filled = (v) => v !== null && v !== undefined && v !== "" && !(Array.isArr
function sourceHtml(cite) {
const what = [cite.locator && cite.locator !== cite.label ? cite.locator : "", CITE_KIND[cite.kind] || ""].filter(Boolean).join(" · ");
return `<div class="ev-src"><div>${citeHtml(cite)}</div>${what ? `<div class="src-t">${esc(what)}</div>` : ""}</div>`;
return `<div class="ev-src"><div>${citeHtml(cite)}</div>${what ? `<div class="src-t">${esc(what)}</div>` : ""}${focusControls(cite)}</div>`;
}
/** The evidence drawer for one fact. */

View File

@@ -4,7 +4,7 @@ import { test } from "node:test";
import { equal as eq, ok } from "node:assert/strict";
import { readFileSync } from "node:fs";
import { citeHtml, evidenceHtml, factHtml, groupHtml, tagged } from "../../infra/gitea/custom/public/assets/dossier/cite.js";
import { citeHtml, evidenceHtml, factHtml, focusPath, groupHtml, tagged } from "../../infra/gitea/custom/public/assets/dossier/cite.js";
const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8"));
const H = fx("header");
@@ -202,6 +202,67 @@ test("an uncited tag in a run stays text and ends the run", () => {
eq(count(h, "A ×2"), 1);
});
test("a library key opens the focus pane; the path is not a citation url", () => {
const cite = {
kind: "comment",
label: "Comment 9",
url: "https://example.test/c?x=1",
locator: "cmt",
item_key: "ABCD1234",
};
eq(focusPath(cite), "/ui/pdf/ABCD1234?protocol=comment-stance");
const h = evidenceHtml({ id: "F9", kind: "event", text: "t", cite });
ok(h.includes('data-focus-src="/ui/pdf/ABCD1234?protocol=comment-stance"'));
ok(h.includes(">Open in focus pane</button>"));
ok(h.includes('sandbox="allow-scripts allow-same-origin"'));
ok(h.includes('class="focus-pane"') && h.includes('title="Focus pane"'));
ok(h.includes('href="https://example.test/c?x=1"'), "the citation link stays the url the API gave");
const pane = h.slice(h.indexOf("data-focus-src"));
ok(!pane.includes("https://") && !pane.includes("example.test"));
});
test("a federal register cite uses the disposition protocol", () => {
eq(
focusPath({ kind: "fr_para", label: "p", url: "https://example.test/fr", item_key: "RULEKEY1" }),
"/ui/pdf/RULEKEY1?protocol=cms-disposition",
);
eq(
focusPath({ kind: "fr_page", label: "p", url: "", item_key: "RULEKEY1" }),
"/ui/pdf/RULEKEY1?protocol=cms-disposition",
);
});
test("a licensed book never gets a focus pane, even with a library key", () => {
const cite = {
kind: "cpt_book",
label: "CPT 2019 p. 20",
url: "https://example.test/book",
locator: "CPT 2019 p. 20",
item_key: "BOOKKEY1",
};
eq(focusPath(cite), "");
const h = evidenceHtml({ id: "F2", kind: "formation", text: "t", cite });
ok(!h.includes("focus-open") && !h.includes("/ui/pdf") && !h.includes("href"));
});
test("a missing, short, or odd key does not open the pane", () => {
const base = { kind: "study", label: "S", url: "https://example.test/s", item_key: "" };
eq(focusPath(base), "");
eq(focusPath({ ...base, item_key: "ABC" }), "");
eq(focusPath({ ...base, item_key: "../etc/passwd" }), "");
eq(focusPath({ ...base, item_key: "has space" }), "");
eq(focusPath(null), "");
eq(focusPath(undefined), "");
});
test("groupHtml offers the pane for a keyed source and keeps the citation link", () => {
const cite = { kind: "study", label: "S", url: "https://example.test/s", locator: "", item_key: "STUDYKEY" };
const h = groupHtml([mk("F1", "S")], cite);
ok(h.includes('data-focus-src="/ui/pdf/STUDYKEY?protocol=comment-stance"'));
ok(h.includes('href="https://example.test/s"'));
ok(!h.slice(h.indexOf("data-focus-src")).includes("example.test"));
});
test("groupHtml lists the collapsed facts, each opening its own evidence", () => {
const f1 = mk("F1", "A <x>");
const h = groupHtml([f1, mk("F2", "A <x>")], f1.cite);