web: render stage-turn markdown in the artifact viewer
The full-screen viewer now opens markdown/text artifacts (text/markdown, text/plain, .md/.markdown/.txt) through the resolved artifact URL and renders them with the page's own DOM markdown renderer, with an honest failure message and an external fallback. ArrowLeft/ArrowRight page the shared media+document list, and a backdrop click closes the viewer while panel clicks do not. The run gallery and per-visit artifacts present input.md, memory.md and output.md as ordinary Document tiles with a drawn glyph and open the viewer. Adds isMarkdownArtifact/isViewerArtifact with unit coverage, deterministic viewer fixtures, and ui-smoke assertions/captures for rendered markdown, keyboard paging, the honest 404 and backdrop dismissal.
This commit is contained in:
+40
-11
@@ -94,6 +94,9 @@ const DOCUMENT_EXTENSIONS = new Set([
|
||||
"txt", "md", "markdown", "rst", "doc", "docx", "odt", "rtf", "html", "htm",
|
||||
]);
|
||||
|
||||
/** The text file kinds the browser can render in the artifact viewer. */
|
||||
const MARKDOWN_EXTENSIONS = new Set(["md", "markdown", "txt"]);
|
||||
|
||||
const DOCUMENT_MIMES = new Set([
|
||||
"application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
@@ -195,6 +198,31 @@ export function artifactKindLabel(kind) {
|
||||
return KIND_LABELS[kind] ?? "File";
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an artifact is text the reader can render as markdown in the viewer:
|
||||
* `text/markdown` or `text/plain` (parameters ignored), or a `.md`, `.markdown`
|
||||
* or `.txt` name. The viewer uses the page's existing markdown renderer, so these
|
||||
* are the text files it is allowed to fetch and draw. Pure and total, mirroring
|
||||
* the content-type tests above.
|
||||
*/
|
||||
export function isMarkdownArtifact(contentType, name) {
|
||||
const mime = String(contentType ?? "").split(";")[0].trim().toLowerCase();
|
||||
if (mime === "text/markdown" || mime === "text/plain") return true;
|
||||
return MARKDOWN_EXTENSIONS.has(extensionOf(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an artifact opens inside the full-screen viewer: the two media kinds
|
||||
* the gallery already previewed, plus the markdown/text documents it now renders
|
||||
* itself. The kind gate keeps a `text/plain` `.log`, `.py` or `.csv` an external
|
||||
* file: those are evidence, not prose. This is the one predicate the grid tiles,
|
||||
* paging and the viewer share, so they cannot disagree about the list.
|
||||
*/
|
||||
export function isViewerArtifact(contentType, name) {
|
||||
if (isImageContentType(contentType) || isVideoContentType(contentType)) return true;
|
||||
return artifactKind(contentType, name) === ArtifactKind.Document && isMarkdownArtifact(contentType, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Human-readable byte size, matching Android Format.kt humanBytes (and the TUI):
|
||||
* whole bytes under 1 KiB, otherwise one decimal with a binary unit
|
||||
@@ -320,28 +348,29 @@ export function artifactCaptureNotes(step) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The media artifacts `groups` hold, in the exact order the gallery draws them
|
||||
* (group by group, each group's images and videos in recorded order): the
|
||||
* neighbours the full-screen viewer pages through, so a swipe visits the same
|
||||
* list the grid showed. Non-media artifacts open externally and are not part of
|
||||
* it. Mirrors ArtifactViewer.kt pagedArtifacts.
|
||||
* The artifacts `groups` hold that the full-screen viewer can page through, in
|
||||
* the exact order the gallery draws them (group by group, each group's images,
|
||||
* videos and markdown/text documents in recorded order): the neighbours a
|
||||
* swipe/keyboard step visits, so the viewer walks the same list the grid showed.
|
||||
* Other artifacts open externally and are not part of it. Mirrors
|
||||
* ArtifactViewer.kt pagedArtifacts, extended for markdown-first stage turns.
|
||||
*/
|
||||
export function pagedArtifacts(groups) {
|
||||
const viewable = [];
|
||||
for (const group of groups ?? []) {
|
||||
for (const artifact of group?.artifacts ?? []) {
|
||||
const kind = artifactKind(artifact?.content_type, artifact?.name);
|
||||
if (kind === ArtifactKind.Image || kind === ArtifactKind.Video) viewable.push(artifact);
|
||||
if (isViewerArtifact(artifact?.content_type, artifact?.name)) viewable.push(artifact);
|
||||
}
|
||||
}
|
||||
return viewable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each gallery media artifact's url mapped to its position in `pagedArtifacts`,
|
||||
* the same ordered list the viewer pages through. The grid's tiles resolve their
|
||||
* viewer index here, so tile and viewer derive from one source and cannot drift.
|
||||
* The first occurrence of a url wins. Mirrors ArtifactViewer.kt viewerIndexByUrl.
|
||||
* Each viewer-selectable artifact's url mapped to its position in
|
||||
* `pagedArtifacts`, the same ordered list the viewer pages through. The grid's
|
||||
* tiles resolve their viewer index here, so tile and viewer derive from one
|
||||
* source and cannot drift. The first occurrence of a url wins. Mirrors
|
||||
* ArtifactViewer.kt viewerIndexByUrl.
|
||||
*/
|
||||
export function viewerIndexByUrl(groups) {
|
||||
const indices = new Map();
|
||||
|
||||
+141
-13
@@ -23,6 +23,7 @@ import {
|
||||
ArtifactKind,
|
||||
artifactKind,
|
||||
artifactViewerPositionLabel,
|
||||
isMarkdownArtifact,
|
||||
pagedIndex,
|
||||
resolvedArtifactUrl,
|
||||
} from "../artifacts.js";
|
||||
@@ -538,42 +539,167 @@ function dismissActionError() {
|
||||
}
|
||||
|
||||
let viewerOverlay = null;
|
||||
let viewerSession = null;
|
||||
|
||||
/** Close the full-screen viewer; the controller returns focus to its trigger. */
|
||||
function closeArtifactViewer() {
|
||||
if (viewerSession) {
|
||||
viewerSession.closed = true;
|
||||
viewerSession.abort?.abort();
|
||||
viewerSession = null;
|
||||
}
|
||||
modal.close("artifact-viewer");
|
||||
viewerOverlay = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the full-screen viewer at `startIndex` into `viewable`, the same ordered
|
||||
* media list the grid drew. Previous/Next are disabled at the ends and absent for
|
||||
* a single artifact; Escape and a backdrop click close it. Nothing autoplays: a
|
||||
* non-image artifact shows a still poster placeholder instead.
|
||||
* list the grid drew (images, videos and markdown/text documents). Previous/Next
|
||||
* are disabled at the ends; ArrowLeft/ArrowRight page the same list; Escape and a
|
||||
* click on the dimmed backdrop close it, while clicks inside the panel never do.
|
||||
* Nothing autoplays: a video shows a still poster placeholder instead.
|
||||
*
|
||||
* Markdown and text artifacts are fetched once per viewer session from the
|
||||
* resolved artifact URL and rendered with the page's own markdown renderer,
|
||||
* building DOM nodes (never innerHTML). A failed fetch or render keeps the
|
||||
* viewer honest: it shows describeError's message, the resolved-external action
|
||||
* and the normal Open/Copy/Share actions.
|
||||
*/
|
||||
function openArtifactViewer(viewable, startIndex, trigger = null) {
|
||||
closeArtifactViewer();
|
||||
if (!viewable?.length) return;
|
||||
let index = pagedIndex(startIndex, 0, viewable.length);
|
||||
const session = {
|
||||
closed: false,
|
||||
generation: 0,
|
||||
abort: null,
|
||||
cache: new Map(),
|
||||
errors: new Map(),
|
||||
};
|
||||
const overlay = h("div", {
|
||||
class: "artifact-viewer",
|
||||
role: "dialog",
|
||||
"aria-modal": "true",
|
||||
"aria-label": "Artifact viewer",
|
||||
});
|
||||
const panel = h("div", { class: "viewer-panel", "data-viewer-focus": "panel", tabindex: "-1" });
|
||||
overlay.append(panel);
|
||||
overlay.addEventListener("click", (event) => {
|
||||
if (event.target === overlay) closeArtifactViewer();
|
||||
});
|
||||
|
||||
const renderViewer = () => {
|
||||
/** The focus key of the control inside the panel that currently owns focus. */
|
||||
const focusedKey = () => {
|
||||
const active = document.activeElement;
|
||||
if (!panel.contains(active)) return null;
|
||||
return active?.getAttribute?.("data-viewer-focus") ?? null;
|
||||
};
|
||||
|
||||
/** Keep focus inside the viewer after the panel is rebuilt. */
|
||||
const restoreFocus = (key) => {
|
||||
if (!key) return;
|
||||
const target =
|
||||
key === "panel" ? panel : panel.querySelector(`[data-viewer-focus="${key}"]`);
|
||||
if (target && !target.disabled) target.focus({ preventScroll: true });
|
||||
else panel.focus({ preventScroll: true });
|
||||
};
|
||||
|
||||
const step = (delta) => {
|
||||
const next = pagedIndex(index, delta, viewable.length);
|
||||
if (next === index) return;
|
||||
index = next;
|
||||
renderViewer({ restore: true });
|
||||
};
|
||||
|
||||
overlay.addEventListener("keydown", (event) => {
|
||||
if (event.key === "ArrowLeft") {
|
||||
event.preventDefault();
|
||||
step(-1);
|
||||
} else if (event.key === "ArrowRight") {
|
||||
event.preventDefault();
|
||||
step(1);
|
||||
}
|
||||
});
|
||||
|
||||
const renderViewer = ({ restore = false } = {}) => {
|
||||
const keepFocus = restore ? focusedKey() : null;
|
||||
const generation = ++session.generation;
|
||||
const artifact = viewable[index] ?? {};
|
||||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||||
const kind = artifactKind(artifact.content_type, artifact.name);
|
||||
const markdown = isMarkdownArtifact(artifact.content_type, artifact.name);
|
||||
const name = artifact.name || "(unnamed artifact)";
|
||||
|
||||
const board = h("div", { class: "viewer-board" });
|
||||
if (kind === ArtifactKind.Image && resolved) {
|
||||
board.append(h("img", { class: "viewer-image", src: resolved, alt: name }));
|
||||
} else if (kind === ArtifactKind.Video) {
|
||||
board.append(
|
||||
h(
|
||||
"div",
|
||||
{ class: "viewer-placeholder" },
|
||||
h("span", { class: "artifact-play", text: "▶" }),
|
||||
h("span", { class: "viewer-placeholder-name", text: name }),
|
||||
),
|
||||
);
|
||||
} else if (markdown) {
|
||||
board.classList.add("viewer-board-document");
|
||||
const renderMarkdown = rt.views?.chat?.renderMarkdown;
|
||||
const showFailure = (message) => {
|
||||
const error = h("div", { class: "viewer-error" });
|
||||
error.append(h("div", { class: "viewer-error-message", text: message }));
|
||||
if (resolved) {
|
||||
error.append(
|
||||
h("a", {
|
||||
class: "viewer-external",
|
||||
href: resolved,
|
||||
target: "_blank",
|
||||
rel: "noreferrer",
|
||||
text: `Open ${name} externally`,
|
||||
}),
|
||||
);
|
||||
}
|
||||
board.append(error);
|
||||
};
|
||||
const draw = (text) => {
|
||||
try {
|
||||
const article = h("div", { class: "viewer-markdown" });
|
||||
article.append(renderMarkdown(text));
|
||||
board.append(article);
|
||||
return true;
|
||||
} catch (error) {
|
||||
showFailure(describeError(error));
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if (!resolved) {
|
||||
showFailure("Set the server URL in Settings to open this artifact.");
|
||||
} else if (session.cache.has(resolved)) {
|
||||
draw(session.cache.get(resolved));
|
||||
} else if (session.errors.has(resolved)) {
|
||||
showFailure(session.errors.get(resolved));
|
||||
} else if (typeof renderMarkdown !== "function") {
|
||||
showFailure("Markdown rendering is unavailable.");
|
||||
} else {
|
||||
board.append(h("div", { class: "viewer-loading muted", text: "Loading markdown…" }));
|
||||
session.abort?.abort();
|
||||
const controller = new AbortController();
|
||||
session.abort = controller;
|
||||
(async () => {
|
||||
try {
|
||||
const response = await fetch(resolved, { signal: controller.signal });
|
||||
if (!response.ok) throw new Error(`The server answered ${response.status}.`);
|
||||
const text = await response.text();
|
||||
if (session.closed || generation !== session.generation) return;
|
||||
session.cache.set(resolved, text);
|
||||
renderViewer({ restore: true });
|
||||
} catch (error) {
|
||||
if (error?.name === "AbortError" || session.closed || generation !== session.generation) return;
|
||||
session.errors.set(resolved, describeError(error));
|
||||
renderViewer({ restore: true });
|
||||
}
|
||||
})();
|
||||
}
|
||||
} else {
|
||||
board.append(
|
||||
h(
|
||||
@@ -601,12 +727,14 @@ function openArtifactViewer(viewable, startIndex, trigger = null) {
|
||||
h("button", {
|
||||
class: "viewer-action",
|
||||
type: "button",
|
||||
"data-viewer-focus": "open",
|
||||
text: "Open",
|
||||
onclick: () => window.open(resolved, "_blank", "noopener,noreferrer"),
|
||||
}),
|
||||
h("button", {
|
||||
class: "viewer-action",
|
||||
type: "button",
|
||||
"data-viewer-focus": "copy",
|
||||
text: "Copy link",
|
||||
disabled: !canCopyLink(),
|
||||
onclick: () => copyArtifactLink(resolved),
|
||||
@@ -614,6 +742,7 @@ function openArtifactViewer(viewable, startIndex, trigger = null) {
|
||||
h("button", {
|
||||
class: "viewer-action",
|
||||
type: "button",
|
||||
"data-viewer-focus": "share",
|
||||
text: "Share",
|
||||
disabled: !canShare(),
|
||||
onclick: () => shareArtifact(resolved),
|
||||
@@ -624,6 +753,7 @@ function openArtifactViewer(viewable, startIndex, trigger = null) {
|
||||
h("button", {
|
||||
class: "viewer-action viewer-close",
|
||||
type: "button",
|
||||
"data-viewer-focus": "close",
|
||||
text: "Close viewer",
|
||||
onclick: closeArtifactViewer,
|
||||
}),
|
||||
@@ -638,32 +768,30 @@ function openArtifactViewer(viewable, startIndex, trigger = null) {
|
||||
h("button", {
|
||||
class: "viewer-page",
|
||||
type: "button",
|
||||
"data-viewer-focus": "prev",
|
||||
text: "Previous artifact",
|
||||
disabled: pagedIndex(index, -1, viewable.length) === index,
|
||||
onclick: () => {
|
||||
index = pagedIndex(index, -1, viewable.length);
|
||||
renderViewer();
|
||||
},
|
||||
onclick: () => step(-1),
|
||||
}),
|
||||
h("button", {
|
||||
class: "viewer-page",
|
||||
type: "button",
|
||||
"data-viewer-focus": "next",
|
||||
text: "Next artifact",
|
||||
disabled: pagedIndex(index, 1, viewable.length) === index,
|
||||
onclick: () => {
|
||||
index = pagedIndex(index, 1, viewable.length);
|
||||
renderViewer();
|
||||
},
|
||||
onclick: () => step(1),
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
nodes.push(actions);
|
||||
overlay.replaceChildren(...nodes);
|
||||
panel.replaceChildren(...nodes);
|
||||
if (restore) restoreFocus(keepFocus ?? "panel");
|
||||
};
|
||||
|
||||
renderViewer();
|
||||
viewerOverlay = overlay;
|
||||
viewerSession = session;
|
||||
modal.open("artifact-viewer", overlay, {
|
||||
onClose: () => closeArtifactViewer(),
|
||||
trigger,
|
||||
|
||||
+22
-10
@@ -43,6 +43,7 @@ import {
|
||||
artifactViewerPositionLabel,
|
||||
displayVisit,
|
||||
humanBytes,
|
||||
isViewerArtifact,
|
||||
pagedArtifacts,
|
||||
presentedArtifacts,
|
||||
resolvedArtifactUrl,
|
||||
@@ -580,7 +581,7 @@ function renderVerdictBlock(step) {
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/** One inline artifact reference in a verdict reason (media opens the viewer). */
|
||||
/** One inline artifact reference in a verdict reason (viewer files open the viewer). */
|
||||
function renderVerdictLink(text, artifact, step) {
|
||||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||||
if (!resolved) return h("span", { class: "verdict-link disabled", text });
|
||||
@@ -604,17 +605,18 @@ function renderVerdictRef(artifact, step) {
|
||||
return chip;
|
||||
}
|
||||
|
||||
/** Open a referenced artifact: media in the viewer, everything else externally. */
|
||||
/** Open a referenced artifact: viewer-selectable files in the viewer, others externally. */
|
||||
function openArtifactFromVerdict(artifact, step, resolved, trigger = null) {
|
||||
if (!resolved) return;
|
||||
const group = { stageKey: step.stage, visit: displayVisit(step, 0), artifacts: presentedArtifacts(step) };
|
||||
const viewable = pagedArtifacts([group]);
|
||||
const viewerIndexOf = viewerIndexByUrl([group]);
|
||||
const at = viewerIndexOf.get(artifact.url);
|
||||
const kind = artifactKind(artifact.content_type, artifact.name);
|
||||
const media = kind === ArtifactKind.Image || kind === ArtifactKind.Video;
|
||||
if (media && at != null) openArtifactViewer(viewable, at, trigger);
|
||||
else window.open(resolved, "_blank", "noopener,noreferrer");
|
||||
if (isViewerArtifact(artifact.content_type, artifact.name) && at != null) {
|
||||
openArtifactViewer(viewable, at, trigger);
|
||||
} else {
|
||||
window.open(resolved, "_blank", "noopener,noreferrer");
|
||||
}
|
||||
}
|
||||
|
||||
/** Open the stage pane for one visit of a stage; the controller moves focus in. */
|
||||
@@ -808,7 +810,8 @@ function renderArtifactGallery(steps) {
|
||||
* The per-visit artifacts section shown inside one step: the summary line, the
|
||||
* capture notes, the no-server-URL hint and a headerless grid of that visit's
|
||||
* tiles (matching the Android ArtifactsSection). Its viewer pages only through
|
||||
* that visit's media, exactly as the Android section does.
|
||||
* that visit's viewable artifacts (media and markdown), exactly as the Android
|
||||
* section does.
|
||||
*/
|
||||
function renderVisitArtifacts(step) {
|
||||
const wrap = h("div", { class: "artifacts" });
|
||||
@@ -842,13 +845,15 @@ function renderArtifactGrid(artifacts, viewable, viewerIndexOf) {
|
||||
|
||||
/**
|
||||
* One gallery tile: an image thumbnail, a video poster with a play marker (never
|
||||
* a <video> element or autoplay), or a kind/name/size label. Every server string
|
||||
* is set as text, never as markup.
|
||||
* a <video> element or autoplay), or a kind/name/size label. Markdown and text
|
||||
* documents get a paper glyph and open in the viewer like media. Every server
|
||||
* string is set as text, never as markup.
|
||||
*/
|
||||
function renderArtifactTile(artifact, viewable, viewerIndexOf) {
|
||||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||||
const kind = artifactKind(artifact.content_type, artifact.name);
|
||||
const media = kind === ArtifactKind.Image || kind === ArtifactKind.Video;
|
||||
const opensViewer = isViewerArtifact(artifact.content_type, artifact.name);
|
||||
const name = artifact.name || "(unnamed artifact)";
|
||||
// The tile is the native control (M5): Enter/Space activate it for free, and
|
||||
// the Copy link / Share buttons are siblings in the wrapper, never children.
|
||||
@@ -857,6 +862,9 @@ function renderArtifactTile(artifact, viewable, viewerIndexOf) {
|
||||
class: "artifact-tile clickable",
|
||||
type: "button",
|
||||
"aria-label": `Open ${name}`,
|
||||
"data-artifact-name": artifact.name ?? "",
|
||||
"data-artifact-kind": kind,
|
||||
"data-artifact-url": artifact.url ?? "",
|
||||
});
|
||||
const openViewer = () => {
|
||||
const at = viewerIndexOf.get(artifact.url);
|
||||
@@ -881,12 +889,16 @@ function renderArtifactTile(artifact, viewable, viewerIndexOf) {
|
||||
tile.append(h("div", { class: "artifact-name", text: name }));
|
||||
tile.addEventListener("click", openViewer);
|
||||
} else {
|
||||
if (opensViewer) {
|
||||
tile.classList.add("artifact-document-tile");
|
||||
tile.append(h("span", { class: "artifact-doc-glyph", "aria-hidden": "true" }));
|
||||
}
|
||||
tile.append(
|
||||
h("div", { class: "artifact-kind", text: artifactKindLabel(kind) }),
|
||||
h("div", { class: "artifact-name", text: name }),
|
||||
h("div", { class: "artifact-size", text: humanBytes(artifact.size_bytes) }),
|
||||
);
|
||||
tile.addEventListener("click", openTab);
|
||||
tile.addEventListener("click", opensViewer ? openViewer : openTab);
|
||||
}
|
||||
wrap.append(tile, renderTileActions(resolved, media));
|
||||
return wrap;
|
||||
|
||||
Reference in New Issue
Block a user