Capture steps from the screen and walk through a sequence one step at a time (Refs #23)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
72
src/app.js
72
src/app.js
@@ -13,6 +13,7 @@
|
||||
pwChanged: false, // passphrase changed since the last save
|
||||
tabs: {}, // section id -> id of the sub-tab being shown ('' = General); not saved
|
||||
collapsed: new Set(), // ids of records folded to just their title (view mode only)
|
||||
walk: null, // { id, k }: the visual sequence shown as a one-step-at-a-time walkthrough (not saved)
|
||||
revealed: new WeakSet(), // secret field objects currently shown
|
||||
blob: null, // encrypted payload taken from a file the user chose to open
|
||||
handle: null, // File System Access handle once the user picked a save location
|
||||
@@ -157,6 +158,35 @@
|
||||
const allRecords = () => state.doc.sections.flatMap((s) => (s.records || []).filter((r) => !isSequence(r)));
|
||||
const addFrameFrom = (seq) => (name, url) => addFrame(seq, url);
|
||||
|
||||
// Screen capture while editing a visual sequence. The stream lives here, not in
|
||||
// the DOM, so it survives render(). { stream, video, seqId } or null.
|
||||
let capture = null;
|
||||
function endCapture() { stopScreenCapture(capture); capture = null; }
|
||||
async function beginCapture(q) {
|
||||
endCapture();
|
||||
try {
|
||||
const cap = await startScreenCapture();
|
||||
cap.seqId = q.id;
|
||||
cap.stream.getVideoTracks()[0].onended = () => { if (capture === cap) { capture = null; setStatus('Screen sharing ended.'); render(); } };
|
||||
capture = cap;
|
||||
setStatus('Sharing. Get the site to the screen you want, come back here and click Capture step.');
|
||||
} catch (err) {
|
||||
setStatus(err.message);
|
||||
}
|
||||
render();
|
||||
}
|
||||
async function captureStep(q) {
|
||||
try {
|
||||
const url = await processImageFile(await grabScreenFrame(capture));
|
||||
addCapture(q, url);
|
||||
changed(q); // clears the status, so set it afterwards
|
||||
setStatus('Step ' + q.frames.length + ' captured.');
|
||||
} catch (err) {
|
||||
setStatus(err.message);
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
// Add each file as a new image field on `rec`, or put the first into
|
||||
// `opts.replace` (an existing image field). Per-file problems are reported
|
||||
// together; the files that worked are still kept.
|
||||
@@ -308,14 +338,37 @@
|
||||
onclick: () => openLightbox(label, f.image), onkeydown: (e) => { if (e.key === 'Enter') openLightbox(label, f.image); } })
|
||||
: h('span', { class: 'muted' }, '(no image)'),
|
||||
layout !== 'none' ? h('p', { class: 'ftext' }, f.text) : null,
|
||||
f.stop || String(f.ifStuck || '').trim() ? h('div', { class: 'fnotes' },
|
||||
f.stop ? h('p', { class: 'fstop' }, h('strong', {}, 'Stop here. Do not go further.')) : null,
|
||||
String(f.ifStuck || '').trim() ? h('p', { class: 'fstuck' }, h('strong', {}, 'If it looks different or fails: '), f.ifStuck) : null) : null,
|
||||
f.secret ? btn('Hide', toggle, 'link', { 'aria-label': 'Hide ' + label }) : null]);
|
||||
}
|
||||
|
||||
const capturedNote = (q) => (q.captured ? ' Captured ' + q.captured + '. The site may have changed since.' : '');
|
||||
|
||||
// One step at a time, with Back/Next. A secret step stays hidden until shown, as in the list.
|
||||
function walkSequence(q) {
|
||||
const n = q.frames.length;
|
||||
const k = Math.max(0, Math.min(state.walk.k, n - 1));
|
||||
const go = (to) => () => { state.walk = { id: q.id, k: to }; render(); };
|
||||
return h('article', { class: 'card seq walk', 'data-seq': q.id },
|
||||
h('h3', {}, q.title),
|
||||
viewFrame(q, q.frames[k], k),
|
||||
h('div', { class: 'row walknav' },
|
||||
btn('\u2190 Back', go(k - 1), '', { disabled: k === 0 }),
|
||||
h('span', { class: 'muted', role: 'status' }, 'Step ' + (k + 1) + ' of ' + n),
|
||||
btn('Next \u2192', go(k + 1), 'primary', { disabled: k === n - 1 }),
|
||||
btn('Show all steps', () => { state.walk = null; render(); }, 'link')),
|
||||
h('p', { class: 'muted small' }, 'Updated ' + q.updated + '.' + capturedNote(q)));
|
||||
}
|
||||
|
||||
function viewSequence(q) {
|
||||
if (state.walk && state.walk.id === q.id && q.frames.length) return walkSequence(q);
|
||||
return h('article', { class: 'card seq' },
|
||||
h('h3', {}, q.title),
|
||||
q.frames.length ? btn('Walk through, one step at a time', () => { state.walk = { id: q.id, k: 0 }; render(); }, 'small') : null,
|
||||
q.frames.length ? h('div', { class: 'frames' }, q.frames.map((f, k) => viewFrame(q, f, k))) : h('p', { class: 'muted' }, '(no steps yet)'),
|
||||
h('p', { class: 'muted small' }, 'Updated ' + q.updated));
|
||||
h('p', { class: 'muted small' }, 'Updated ' + q.updated + '.' + capturedNote(q)));
|
||||
}
|
||||
const seqItem = (q) => h('li', { class: 'seqitem' }, viewSequence(q));
|
||||
|
||||
@@ -467,13 +520,17 @@
|
||||
function editFrame(q, f, k) {
|
||||
const secret = h('input', { type: 'checkbox', checked: !!f.secret, 'aria-label': 'Secret step',
|
||||
onchange: () => { f.secret = secret.checked || undefined; if (!f.secret) delete f.secret; changed(q); } });
|
||||
const stop = h('input', { type: 'checkbox', checked: !!f.stop, 'aria-label': 'Stop here, step ' + (k + 1),
|
||||
onchange: () => { f.stop = stop.checked || undefined; if (!f.stop) delete f.stop; changed(q); } });
|
||||
const layout = h('select', { 'aria-label': 'Text position for step ' + (k + 1), onchange: () => { f.layout = layout.value; changed(q); render(); } },
|
||||
LAYOUT_LABELS.map(([v, t]) => h('option', { value: v, selected: f.layout === v }, t)));
|
||||
return h('div', { class: 'field imagefield frameedit' },
|
||||
h('div', { class: 'row' }, h('strong', {}, 'Step ' + (k + 1)), layout, h('label', { class: 'check' }, secret, ' secret'), rowTools(q.frames, k, 'step', q)),
|
||||
imageZone(f, 'image', q.title + ', step ' + (k + 1), { 'data-frame': String(k) },
|
||||
(files) => importFiles(q, files, { key: 'image', replace: f, add: addFrameFrom(q) }), () => changed(q)),
|
||||
f.layout === 'none' ? null : h('label', {}, 'Text', input(f, 'text', { multiline: true, label: 'Text for step ' + (k + 1) }, q)));
|
||||
f.layout === 'none' ? null : h('label', {}, 'Text', input(f, 'text', { multiline: true, label: 'Text for step ' + (k + 1) }, q)),
|
||||
h('label', {}, 'If it looks different or fails', input(f, 'ifStuck', { multiline: true, label: 'If step ' + (k + 1) + ' looks different or fails' }, q)),
|
||||
h('label', { class: 'check' }, stop, ' Stop here: the reader must not go further without help'));
|
||||
}
|
||||
|
||||
function editSequence(list, q, i, s) {
|
||||
@@ -487,7 +544,10 @@
|
||||
q.frames.map((f, k) => editFrame(q, f, k)),
|
||||
h('div', { class: 'row' },
|
||||
btn('+ Add step', () => { addFrame(q); changed(q); render(); }, 'small'),
|
||||
btn('+ Add images\u2026', () => picker.click(), 'small'), picker));
|
||||
btn('+ Add images\u2026', () => picker.click(), 'small'), picker,
|
||||
capture && capture.seqId === q.id
|
||||
? [btn('Capture step', () => captureStep(q), 'primary small'), btn('Stop sharing', () => { endCapture(); setStatus('Stopped sharing.'); render(); }, 'small')]
|
||||
: btn('Capture from screen\u2026', () => beginCapture(q), 'small', { title: 'Share the window with the site, then capture each screen you want as a step' })));
|
||||
}
|
||||
const addSeqBtn = (s) => btn('+ Add visual sequence', () => { addSequence(state.doc, s, s.kind === 'records' ? currentTab(s) : ''); markDirty(); render(); }, 'primary');
|
||||
|
||||
@@ -572,12 +632,15 @@
|
||||
v.withheld ? h('p', { class: 'psecret' }, '[withheld]')
|
||||
: v.image ? h('img', { class: 'pimg', src: v.image, alt: 'Step ' + (k + 1) })
|
||||
: h('p', {}, '(no image)'),
|
||||
v.layout !== 'none' ? h('p', { class: 'pftext' }, v.text) : null);
|
||||
v.layout !== 'none' ? h('p', { class: 'pftext' }, v.text) : null,
|
||||
v.stop ? h('p', { class: 'pstop' }, h('strong', {}, 'STOP HERE. Do not go further.')) : null,
|
||||
v.ifStuck ? h('p', { class: 'pftext' }, h('strong', {}, 'If it looks different or fails: '), v.ifStuck) : null);
|
||||
}
|
||||
|
||||
function printSequence(q, includeSecrets) {
|
||||
return h('div', { class: 'pseq' },
|
||||
h('h3', {}, q.title),
|
||||
q.captured ? h('p', { class: 'pftext' }, 'Captured ' + q.captured + '. The site may have changed since.') : null,
|
||||
q.frames.map((f, k) => printFrame(f, k, includeSecrets)));
|
||||
}
|
||||
|
||||
@@ -782,6 +845,7 @@
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (capture && !state.editing) endCapture(); // sharing only lasts while editing
|
||||
root.replaceChildren();
|
||||
if (!state.doc) return root.append(renderStart());
|
||||
const onHeader = state.current === HEADER_ID;
|
||||
|
||||
@@ -85,3 +85,37 @@ export async function processImageFile(blob) {
|
||||
if (bmp.close) bmp.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Browser only. Screen capture for building a walkthrough: the user shares a
|
||||
// window or tab, then each grab is one frame. Nothing is recorded or sent anywhere.
|
||||
export async function startScreenCapture() {
|
||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getDisplayMedia) {
|
||||
throw new Error('This browser cannot capture the screen. Take a screenshot and paste it instead.');
|
||||
}
|
||||
let stream;
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: false });
|
||||
} catch (err) {
|
||||
throw new Error(err && err.name === 'NotAllowedError' ? 'Screen sharing was cancelled.' : 'Screen sharing could not start.');
|
||||
}
|
||||
const video = document.createElement('video');
|
||||
video.srcObject = stream;
|
||||
video.muted = true;
|
||||
await video.play();
|
||||
return { stream, video };
|
||||
}
|
||||
|
||||
export function stopScreenCapture(cap) {
|
||||
if (cap && cap.stream) cap.stream.getTracks().forEach((t) => t.stop());
|
||||
}
|
||||
|
||||
// The current picture of a started capture, as a PNG blob.
|
||||
export function grabScreenFrame(cap) {
|
||||
const v = cap.video;
|
||||
if (!v.videoWidth || !v.videoHeight) return Promise.reject(new Error('The shared window has no picture yet. Try again in a moment.'));
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = v.videoWidth;
|
||||
canvas.height = v.videoHeight;
|
||||
canvas.getContext('2d').drawImage(v, 0, 0);
|
||||
return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('Could not grab the picture.'))), 'image/png'));
|
||||
}
|
||||
|
||||
@@ -58,6 +58,10 @@ textarea { width: 100%; display: block; } label { display: block; margin-top: .5
|
||||
.frame.right { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); } .frame .fimg { margin: 0; }
|
||||
.fnum { font-weight: 700; color: var(--accent); } .ftext { white-space: pre-wrap; margin: 0; }
|
||||
li.seqitem { display: block; list-style: none; margin-left: -1.4rem; } .frameedit { border-top: 1px dashed var(--line); padding-top: .5rem; }
|
||||
.fnotes { grid-column: 1 / -1; display: grid; gap: .35rem; } .fnotes p { margin: 0; }
|
||||
.fstop { border: 2px solid var(--danger); color: var(--danger); border-radius: .35rem; padding: .35rem .6rem; }
|
||||
.fstuck { background: var(--warn-bg); color: var(--warn-fg); border-radius: .35rem; padding: .35rem .6rem; white-space: pre-wrap; }
|
||||
.walk .fimg { max-height: 28rem; } .walknav { align-items: center; margin-top: .75rem; }
|
||||
@media (max-width: 700px) { .frame.right { grid-template-columns: minmax(0, 1fr); } }
|
||||
dialog.lightbox { max-width: 92vw; max-height: 92vh; overflow: auto; } .bigimg { display: block; max-width: 100%; max-height: 75vh; margin: 0 auto .75rem; background: #fff; }
|
||||
|
||||
@@ -101,6 +105,7 @@ dialog.lightbox { max-width: 92vw; max-height: 92vh; overflow: auto; } .bigimg {
|
||||
.pframe { break-inside: avoid; display: grid; grid-template-columns: minmax(0, 1fr); gap: 3pt 12pt; align-items: start; margin: 0 0 10pt; }
|
||||
.pframe.right { grid-template-columns: 3.9in minmax(0, 1fr); } .pframe.right .pimg { max-height: 3.4in; }
|
||||
.pfnum { grid-column: 1 / -1; font: 700 9.5pt system-ui, sans-serif; break-after: avoid; } .pframe p { margin: 0; } .pftext { white-space: pre-wrap; }
|
||||
.pstop { grid-column: 1 / -1; border: 1.5pt solid #000; padding: 2pt 5pt; }
|
||||
.psecret { font-family: ui-monospace, Consolas, monospace; }
|
||||
.ptag { font: 700 8pt system-ui, sans-serif; border: 1pt solid #000; padding: 0 4pt; margin-left: 8pt; border-radius: 2pt; vertical-align: middle; }
|
||||
.pnotes, .pbody, .pintro { white-space: pre-wrap; margin: 4pt 0 0; }
|
||||
|
||||
@@ -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 = '261124A';
|
||||
export const APP_VERSION = '261124B';
|
||||
|
||||
const VERSION_RE = /^(\d{6})([A-Z])$/;
|
||||
export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v);
|
||||
|
||||
Reference in New Issue
Block a user