web: templates gallery and template detail (P4)

This commit is contained in:
2026-10-19 02:30:39 +01:00
parent 4721b8af10
commit da3c488506
9 changed files with 586 additions and 2 deletions
+93
View File
@@ -0,0 +1,93 @@
// Pure Templates derivations, mirroring the Android app's ui/Templates.kt,
// ui/Format.kt and ui/graph/StageDetailPane.kt. No DOM and no protocol here, so
// the gallery/detail wording and the stage-count mapping are unit-tested without
// a browser. Every value returned is plain text (or plain data); the caller sets
// it with textContent and never with innerHTML.
/** A gallery card's name: a blank name is the explicit unnamed-template placeholder. */
export function templateGalleryName(name) {
return (name ?? "").trim() ? name : "(unnamed template)";
}
/** A detail's heading: a blank name falls back to the template's key. */
export function templateDetailName(entry) {
const name = (entry?.name ?? "").trim();
return name ? entry.name : (entry?.key ?? "");
}
/** "1 stage" / "N stages", mirroring ui/Format.kt stageCountText. */
export function stageCountText(count) {
return `${count} ${count === 1 ? "stage" : "stages"}`;
}
/**
* The card's stage-count line: the count parsed from the source, or an explicit
* "unavailable" when the source could not be parsed (never "0 stages", which
* would claim a count the client does not have).
*/
export function templateStageCountLabel(stageCount) {
return stageCount == null ? "stage count unavailable" : stageCountText(stageCount);
}
/** The effective stage type, mirroring internal/stage.EffectiveType: blank is the model stage. */
export function effectiveStageType(type) {
return (type ?? "").trim() ? type : "model";
}
/**
* One gallery card's content: the template's name and description (from the
* catalogue) and its stage count (from the graph the server parsed out of the
* source; null when the source did not parse, so the card says so rather than
* claiming zero stages).
*/
export function templateCard(entry, graph = null) {
return {
key: entry?.key ?? "",
name: entry?.name ?? "",
description: entry?.description ?? "",
stageCount: graph?.stages?.length ?? null,
};
}
/** The unparsed-source note the Summary tab shows instead of a stage list. */
export const TEMPLATE_UNPARSED_SUMMARY_NOTE =
"This template's source could not be parsed, so its stages are not listed. See the Source tab for the raw definition.";
/** The note the Summary tab shows for a parsed template that declares no stages. */
export const TEMPLATE_NO_STAGES_NOTE = "This template has no stages.";
/** The Graph tab's note when the source did not parse. */
export const TEMPLATE_UNPARSED_GRAPH_NOTE =
"This template's source could not be parsed, so there is no graph. The Source tab shows the raw definition.";
/** The Graph tab's note when the source parsed; the graph is the parsed definition. */
export const TEMPLATE_GRAPH_NOTE =
"Pinch to zoom and drag to pan. The graph is the parsed definition; nothing runs.";
/** The Source tab's note when the source did not parse on the server. */
export const TEMPLATE_UNPARSED_SOURCE_NOTE =
"This source could not be parsed on the server; showing the raw definition.";
/**
* The Summary tab's stage section: an explicit [note] and no stages when the
* source did not parse or parsed to nothing, else the stage rows (key + effective
* type). One derivation, so the note and the list can never disagree.
*/
export function templateStageSummary(graph) {
if (graph == null) {
return { note: TEMPLATE_UNPARSED_SUMMARY_NOTE, stages: [] };
}
const stages = graph.stages ?? [];
if (!stages.length) {
return { note: TEMPLATE_NO_STAGES_NOTE, stages: [] };
}
return {
note: null,
stages: stages.map((stage) => ({ key: stage.key, type: effectiveStageType(stage.type) })),
};
}
/** The Graph tab's note: the unparsed note when there is no graph, else the parsed-definition note. */
export function templateGraphNote(graph) {
return graph == null ? TEMPLATE_UNPARSED_GRAPH_NOTE : TEMPLATE_GRAPH_NOTE;
}