Add credential references for steps: field ids, addUse/findFieldRef, usesView (Refs #24)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 12:35:22 -05:00
parent f089a00b66
commit edb3e25e67
5 changed files with 110 additions and 2 deletions

View File

@@ -31,7 +31,12 @@ import { isImageDataUrl, MAX_IMAGE_DATA_CHARS } from './images.js';
// frames: [Frame] } -- a numbered series of pictures with optional text.
// Frame: { image: PNG/JPEG data URL or '', text, layout: 'right'|'under'|'none', secret?: boolean,
// ifStuck?: string, what to do if the screen looks different or the step fails
// stop?: boolean } "stop here and call someone" marker; ifStuck says who/why
// stop?: boolean, "stop here and call someone" marker; ifStuck says who/why
// uses?: [{ record: <record id>, field: <field id> }] credentials this step needs,
// shown beside the step with Show/Hide and Copy. A reference
// that no longer resolves is shown as missing, never an error.
// A record Field may carry `id` (unique within its record): assigned the first time a step
// references it, so the reference survives reordering and renaming the field.
// A sequence may also carry `captured: 'YYYY-MM-DD'`, set when a step is captured from the screen.
// A sequence lives in the same ordered list as the records (records sections) or
// items (steps / checklist sections), so it sits wherever the user puts it, but it
@@ -168,12 +173,18 @@ export function validate(doc) {
if (!DATE_RE.test(r.updated ?? '')) errs.push(`${where}: updated must be YYYY-MM-DD`);
if (r.notes !== undefined && !isStr(r.notes)) errs.push(`${where}: notes must be a string`);
if (!Array.isArray(r.fields)) return errs.push(`${where}: fields must be an array`);
const fieldIds = new Set();
r.fields.forEach((f, k) => {
const fw = `${where}.fields[${k}]`;
if (!isObj(f)) return errs.push(`${fw}: must be an object`);
if (!isStr(f.label) || !f.label) errs.push(`${fw}: missing label`);
if (!isStr(f.value)) errs.push(`${fw}: value must be a string`);
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
if (f.id !== undefined) {
if (!isStr(f.id) || !f.id) errs.push(`${fw}: id must be a non-empty string`);
else if (fieldIds.has(f.id)) errs.push(`${fw}: duplicate field id "${f.id}"`);
else fieldIds.add(f.id);
}
if (f.kind !== undefined && !FIELD_KINDS.includes(f.kind)) errs.push(`${fw}: unknown kind "${f.kind}"`);
if (f.kind === 'image' && isStr(f.value) && f.value !== '' && !isImageDataUrl(f.value)) {
errs.push(`${fw}: image must be a PNG or JPEG under ${Math.round(MAX_IMAGE_DATA_CHARS / 1e6)} MB`);
@@ -199,6 +210,12 @@ export function validate(doc) {
if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`);
if (f.ifStuck !== undefined && !isStr(f.ifStuck)) errs.push(`${fw}: ifStuck must be a string`);
if (f.stop !== undefined && typeof f.stop !== 'boolean') errs.push(`${fw}: stop must be a boolean`);
if (f.uses !== undefined) {
if (!Array.isArray(f.uses)) errs.push(`${fw}: uses must be an array`);
else f.uses.forEach((u, m) => {
if (!isObj(u) || !isStr(u.record) || !u.record || !isStr(u.field) || !u.field) errs.push(`${fw}.uses[${m}]: needs record and field ids`);
});
}
});
}

View File

@@ -217,6 +217,43 @@ export function addFrame(seq, image = '', layout = 'under') {
return f;
}
// ---- credential references from steps (issue #24) ----
// Every record in the document, wherever it sits (sequences are not records).
export function allRecordsIn(doc) {
return doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r)));
}
// A field gets an id the first time something references it. Returns it.
export function ensureFieldId(record, field) {
if (field.id) return field.id;
const taken = new Set(record.fields.map((f) => f.id).filter(Boolean));
let n = record.fields.length + 1;
while (taken.has('f' + n)) n++;
field.id = 'f' + n;
return field.id;
}
// { record, field } for a reference, or null if either is gone or the field is an image.
export function findFieldRef(doc, ref) {
const record = allRecordsIn(doc).find((r) => r.id === ref.record);
const field = record && record.fields.find((f) => f.id === ref.field && f.kind !== 'image');
return field ? { record, field } : null;
}
// Add a reference to `frame` for `field` of `record`; no duplicates. Returns true if added.
export function addUse(frame, record, field) {
const ref = { record: record.id, field: ensureFieldId(record, field) };
frame.uses = frame.uses || [];
if (frame.uses.some((u) => u.record === ref.record && u.field === ref.field)) return false;
frame.uses.push(ref);
return true;
}
export function removeUse(frame, i) {
frame.uses.splice(i, 1);
if (!frame.uses.length) delete frame.uses;
}
// A step captured from the screen: a new frame, and the sequence remembers when,
// so the reader can be told the site may have changed since.
export function addCapture(seq, image) {

View File

@@ -58,6 +58,18 @@ export function frameView(frame, includeSecrets) {
};
}
// What a step's credential references show: [{ label, record, value, secret, missing }].
// A secret value is a placeholder unless includeSecrets; a reference that no longer
// resolves is marked missing. Uses displayValue so masking matches the rest of print.
export function usesView(doc, frame, includeSecrets) {
return (frame.uses || []).map((u) => {
const rec = doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r))).find((r) => r.id === u.record);
const f = rec && rec.fields.find((x) => x.id === u.field && x.kind !== 'image');
return f ? { label: f.label, record: rec.title, value: displayValue(f, includeSecrets), secret: !!f.secret, missing: false }
: { label: '', record: '', value: '', secret: false, missing: true };
});
}
export function countSecrets(doc) {
let n = 0;
const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim() || String(f.ifStuck || '').trim())) n++; };

View File

@@ -2,7 +2,7 @@
// YYMMDD is the date, X the iteration that day (A first, B second, ...).
// Bump with `npm run bump` (see bump-version.js). build.js strips `export`.
export const APP_VERSION = '261124B';
export const APP_VERSION = '261124C';
const VERSION_RE = /^(\d{6})([A-Z])$/;
export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v);