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:
2026-10-19 21:23:10 +01:00
parent b2fd614ba4
commit 359912f822
11 changed files with 695 additions and 48 deletions
+141 -13
View File
@@ -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,