Files
pipelines/web/src/dom.js
T

112 lines
3.9 KiB
JavaScript

// Shared DOM construction and small browser helpers. The page builds every
// node with these functions; server-derived text is always assigned through
// textContent, never innerHTML.
import { artifactBrowserLinkProps } from "./artifactNavigation.js";
export { artifactBrowserLinkProps } from "./artifactNavigation.js";
export function h(tag, props = {}, ...children) {
const node = document.createElement(tag);
for (const [key, value] of Object.entries(props)) {
if (value == null || value === false) continue;
if (key === "class") node.className = value;
else if (key === "text") node.textContent = value;
else if (key.startsWith("on") && typeof value === "function") {
node.addEventListener(key.slice(2).toLowerCase(), value);
} else node.setAttribute(key, value === true ? "" : String(value));
}
for (const child of children.flat()) {
if (child == null || child === false) continue;
node.append(child instanceof Node ? child : document.createTextNode(String(child)));
}
return node;
}
/** Run synchronously inside a click/keyboard gesture: Android rejects intents
* launched from timers. Never fall back to replacing the app's own location. */
export function openArtifactInBrowser(resolved) {
const props = artifactBrowserLinkProps(resolved);
if (!props) return false;
const link = h("a", { ...props, hidden: true });
document.body.append(link);
link.click();
link.remove();
return true; // A hand-off was requested; the OS decides which browser handles it.
}
/** One small live-stream badge (LiveBadge.kt): a dot plus the honest word. */
export function renderLiveBadge(info) {
return h(
"span",
{ class: `live-badge live-${info.tone}`, title: info.title ?? "Live run stream" },
h("span", { class: "live-dot", "aria-hidden": "true" }),
h("span", { class: "live-word", text: info.label }),
);
}
/** Whether the Clipboard API can be used for a copy control. */
export function canCopyLink() {
return typeof navigator !== "undefined" && Boolean(navigator.clipboard?.writeText);
}
/** Whether the Web Share API can be used for a share control. */
export function canShare() {
return typeof navigator !== "undefined" && typeof navigator.share === "function";
}
export async function copyText(text) {
if (!text || !canCopyLink()) return;
try {
await navigator.clipboard.writeText(text);
} catch {
// Clipboard permission may be denied; the patch is still readable.
}
}
export async function copyArtifactLink(resolved) {
if (!resolved || !canCopyLink()) return;
try {
await navigator.clipboard.writeText(resolved);
} catch {
// Clipboard permission may be denied; the link is still openable.
}
}
export async function shareArtifact(resolved) {
if (!resolved || !canShare()) return;
try {
await navigator.share({ url: resolved });
} catch {
// The share chooser may be dismissed.
}
}
/** A key/value row whose value is monospace (a key, an id) rather than prose. */
export function kvMono(key, value) {
return h(
"div",
{ class: "kv-row" },
h("span", { class: "kv-key", text: key }),
h("span", { class: "kv-val" }, h("code", { text: value })),
);
}
/** A 24px chevron-left for the one Back control every detail screen uses. */
export function backChevronIcon(className = "back-chevron") {
const NS = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(NS, "svg");
svg.setAttribute("class", className);
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("aria-hidden", "true");
svg.setAttribute("focusable", "false");
const path = document.createElementNS(NS, "path");
path.setAttribute("d", "M15 5 8 12l7 7");
path.setAttribute("fill", "none");
path.setAttribute("stroke", "currentColor");
path.setAttribute("stroke-width", "2");
path.setAttribute("stroke-linecap", "round");
path.setAttribute("stroke-linejoin", "round");
svg.append(path);
return svg;
}