commit b97f77a412ec7e56ff478c796eb5098b40c24338
parent c4e4bf5180153ca2b891f17655bd5892cfcc7c3a
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Thu, 12 Nov 2026 05:52:02 +0530
parent c4e4bf5180153ca2b891f17655bd5892cfcc7c3a
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Thu, 12 Nov 2026 05:52:02 +0530
Carry verified signing placement and enrolment UI into roadmap branch
2 files changed, 111 insertions(+), 45 deletions(-)
diff --git a/www/app.js b/www/app.js @@ -46,14 +46,14 @@ function h(tag, attrs, ...kids) { function toast(msg, isError) { const t = document.querySelector('.toast'); t.textContent = msg; - t.className = isError ? 'error' : ''; + t.className = isError ? 'toast error' : 'toast'; t.hidden = false; clearTimeout(toast.timer); toast.timer = setTimeout(() => { t.hidden = true; }, isError ? 7000 : 3000); } /* Local development without the auth proxy: identity headers come from - * localStorage. Behind Authelia/lighttpd these are overwritten upstream. */ + * localStorage. The deployment's trusted proxy overwrites these upstream. */ function devHeaders() { try { const dev = JSON.parse(localStorage.getItem('signetDevIdentity') || 'null'); @@ -223,6 +223,24 @@ function syncOverlay(page) { }); area.title = describe(area); overlay.append(box); + if (area.dataset.placement) { + box.classList.add('placement'); + if (area === selected) box.classList.add('selected'); + box.tabIndex = 0; + box.setAttribute('aria-label', 'Signature placement: arrow keys move, Shift and arrows resize'); + box.append(h('span', { class: 'grip' })); + box.addEventListener('pointerdown', (e) => startDrag(e, area, box, page)); + box.addEventListener('keydown', (e) => { + if (!['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(e.key)) return; + e.preventDefault(); + const b = bboxOf(area), dx = e.key === 'ArrowLeft' ? -5 : e.key === 'ArrowRight' ? 5 : 0; + const dy = e.key === 'ArrowUp' ? -5 : e.key === 'ArrowDown' ? 5 : 0; + constrainPlacement(area, e.shiftKey ? [b[0], b[1], b[2] + dx + dy, b[3]] : b.map((v, i) => v + (i % 2 ? dy : dx)), e.shiftKey); + syncOverlay(page); + page.querySelector('.placement').focus(); + }); + return; + } if (!editing) return; /* edit mode: the box is the layer itself */ @@ -240,8 +258,8 @@ function syncOverlay(page) { } else { box.append(h('span', { class: 'ph' }, '[PIM image not found]')); } - } else if (type === 'signature') { - box.append(h('span', { class: 'ph' }, 'Signature' + (area.dataset.ref ? ': ' + area.dataset.ref : ''))); + } else if (type === 'signature_zone') { + box.append(h('span', { class: 'ph' }, 'Signature zone: ' + (area.dataset.party || '(choose party)'))); } else { box.append(h('span', { class: 'ph' }, 'choose an image')); } @@ -251,6 +269,35 @@ function syncOverlay(page) { }); } +function constrainPlacement(area, bounds, resizing) { + const zone = [...area.closest('.page').querySelectorAll('area:not([data-placement])')].find((a) => a.dataset.type === 'signature_zone' && a.dataset.party === area.dataset.party); + const z = bboxOf(zone), ratio = app.me.facsimile.width / app.me.facsimile.height; + let [x, y, x2, y2] = bounds, w = x2 - x, height = y2 - y; + if (resizing) { + w = Math.min(z[2] - x, (z[3] - y) * ratio, Math.max(+zone.dataset.min_w, +zone.dataset.min_h * ratio, w)); + height = w / ratio; + } + x = Math.max(z[0], Math.min(z[2] - w, x)); + y = Math.max(z[1], Math.min(z[3] - height, y)); + setBbox(area, [x, y, x + w, y + height]); +} + +function setupPlacement() { + const party = doc.route.parties[doc.current_party - 1]; + const zone = [...document.querySelectorAll('area[data-type="signature_zone"]')].find((a) => a.dataset.party === party.name); + if (!zone) return; + const page = zone.closest('.page'), z = bboxOf(zone); + const ratio = app.me.facsimile.width / app.me.facsimile.height; + let w = z[2] - z[0], height = w / ratio; + if (height > z[3] - z[1]) { height = z[3] - z[1]; w = height * ratio; } + const x = z[0] + (z[2] - z[0] - w) / 2, y = z[1] + (z[3] - z[1] - height) / 2; + addArea(page, { type: 'signature_zone', party: party.name, placement: true, coords: [x, y, x + w, y + height] }); + if (w < +zone.dataset.min_w || height < +zone.dataset.min_h) { + document.querySelector('.side').append(h('p', { class: 'notice error' }, 'Your facsimile cannot fit this zone at its minimum size. Request a correction to enlarge the zone.')); + } + syncOverlay(page); +} + function startDrag(e, area, box, page) { e.preventDefault(); select(area); @@ -273,7 +320,14 @@ function startDrag(e, area, box, page) { x1 = Math.min(W - w, Math.max(0, x1 + dx)); y1 = Math.min(H - hh, Math.max(0, y1 + dy)); x2 = x1 + w; y2 = y1 + hh; } - setBbox(area, [x1, y1, x2, y2]); + if (area.dataset.placement) { + if (resizing) { + const ratio = app.me.facsimile.width / app.me.facsimile.height; + x2 = x1 + start[2] - start[0] + (Math.abs(dx) >= Math.abs(dy * ratio) ? dx : dy * ratio); + } + constrainPlacement(area, [x1, y1, x2, y2], resizing); + [x1, y1, x2, y2] = bboxOf(area); + } else setBbox(area, [x1, y1, x2, y2]); Object.assign(live.style, { left: (x1 / W) * 100 + '%', top: (y1 / H) * 100 + '%', width: ((x2 - x1) / W) * 100 + '%', height: ((y2 - y1) / H) * 100 + '%', @@ -305,7 +359,7 @@ function addArea(page, imp) { const d = area.dataset; d.type = imp.type; d.coords = imp.coords.join(','); - for (const k of ['ref', 'font', 'prefix', 'format', 'align']) if (imp[k]) d[k] = imp[k]; + for (const k of ['ref', 'font', 'prefix', 'format', 'align', 'party', 'min_w', 'min_h', 'placement']) if (imp[k]) d[k] = imp[k]; map.append(area); scaleArea(area); return area; @@ -319,20 +373,23 @@ function loadLayout(layout) { if (!page || !TYPES[imp.type]) continue; const img = page.querySelector('img'); const k = img.naturalWidth / (imp.ref_w || layout.reference_width || img.naturalWidth); - addArea(page, { ...imp, coords: imp.coords.map((v) => v * k) }); + const ky = img.naturalHeight / (imp.ref_h || layout.reference_height || img.naturalHeight); + addArea(page, { ...imp, min_w: imp.min_w * k, min_h: imp.min_h * ky, + coords: imp.coords.map((v, i) => v * (imp.shape === 'circle' || i % 2 === 0 ? k : ky)) }); } refitAll(); } function collectLayout() { const first = document.querySelector('.page img'); - const imprints = [...document.querySelectorAll('.page area')].map((area) => { + const imprints = [...document.querySelectorAll('.page area:not([data-placement])')].map((area) => { const img = area.closest('.page').querySelector('img'), d = area.dataset; const imp = { page: +area.closest('.page').dataset.page, shape: area.shape || 'rect', type: d.type, ref: d.ref || '', coords: getCoords(area), ref_w: img.naturalWidth, ref_h: img.naturalHeight, }; - for (const k of ['font', 'prefix', 'format', 'align']) if (d[k]) imp[k] = d[k]; + for (const k of ['font', 'prefix', 'format', 'align', 'party']) if (d[k]) imp[k] = d[k]; + for (const k of ['min_w', 'min_h']) if (d[k]) imp[k] = +d[k]; return imp; }); return { reference_width: first.naturalWidth, reference_height: first.naturalHeight, imprints }; @@ -388,7 +445,7 @@ function placeNew(page, type, cx, cy) { const x1 = Math.max(0, Math.min(img.naturalWidth - t.w, cx - t.w / 2)); const y1 = Math.max(0, Math.min(img.naturalHeight - t.h, cy - t.h / 2)); const imp = { type, coords: [x1, y1, x1 + t.w, y1 + t.h] }; - if (type === 'signature_zone') imp.party = collectRoute().parties[0]?.name || ''; + if (type === 'signature_zone') Object.assign(imp, { party: collectRoute().parties[0]?.name || '', min_w: 50, min_h: 30 }); select(addArea(page, imp)); } @@ -490,11 +547,10 @@ function collectRoute() { } function historyList() { - if (!doc.decisions.length) return null; - const words = { SIGNOFF: 'signed off', CORRECTION: 'asked for correction', DISAPPROVE: 'disapproved' }; - return [h('h2', {}, 'History'), h('ul', { class: 'history' }, doc.decisions.map((x) => - h('li', {}, h('strong', {}, x.signer), ` ${words[x.decision]} · party ${x.party} · rev ${x.revision}`, - h('span', { class: 'c' }, fmtTime(x.decided_at) + (x.comment ? ' · “' + x.comment + '”' : '')))))]; + if (!doc.events?.length) return []; + return [h('h2', {}, 'Event log'), h('ul', { class: 'history' }, doc.events.map((x) => + h('li', {}, h('strong', {}, x.actor), ' · ' + x.action.replace(/_/g, ' '), + h('span', { class: 'c' }, fmtTime(x.at) + (x.detail ? ' · ' + x.detail : '')))))]; } /* ---------- document view ---------- */ @@ -529,9 +585,10 @@ async function showDoc(id) { view.replaceChildren(h('p', { class: 'notice error' }, e.message)); return; } + if (doc.can_decide) app.me = await api('/me'); doc.mode = doc.can_edit ? 'edit' : 'preview'; const pending = ['PENDING'].includes(doc.status); - const stamping = pending && doc.render_seq < doc.revision && !doc.render_error; + const rendering = pending && doc.rendering && !doc.render_error; /* which flattened copy is the base image */ let kind = 'source'; @@ -553,11 +610,13 @@ async function showDoc(id) { notices.append(h('p', { class: 'notice' }, `Correction requested by ${last ? last.signer : 'a checker'}: “${last ? last.comment : ''}”`)); } if (doc.render_error) { - notices.append(h('p', { class: 'notice error' }, 'Stamping failed: ' + doc.render_error + ' ', - h('button', { onclick: () => act(() => api(`/docs/${id}/rerender`, { method: 'POST' }), 'Queued again') }, 'Retry stamping'))); + notices.append(h('p', { class: 'notice error' }, 'Rendering failed: ' + doc.render_error)); + } + if (doc.can_retry) { + notices.append(h('button', { onclick: () => act(() => api(`/docs/${id}/rerender`, { method: 'POST' }), 'Queued again') }, 'Retry rendering')); } - if (stamping) { - notices.append(h('p', { class: 'notice' }, 'Stamping in progress…')); + if (rendering) { + notices.append(h('p', { class: 'notice' }, 'Rendering in progress…')); pollTimer = setTimeout(() => showDoc(id), 1200); kind = 'source'; } @@ -567,9 +626,9 @@ async function showDoc(id) { if (doc.mode === 'edit') { const orgs = Object.keys(app.pim || {}).sort(); side.append( - ...field('Title (blank = use the serial number)', h('input', { id: 'f-title', value: doc.title, maxlength: 200 })), + ...field('Title (required)', h('input', { id: 'f-title', value: doc.title, maxlength: 200, required: true, oninput: (e) => { document.getElementById('submit-doc').disabled = !e.target.value.trim(); } })), h('p', { style: 'margin: 0.5rem 0; font-size: 12px' }, h('span', { class: 'status ' + doc.status }, doc.status.replace(/_/g, ' '))), - ...field('Organisation (required)', selectEl([['', '(choose)'], ...orgs.map((org) => [org, org])], doc.org_code, refitAll, { id: 'f-org' })), + ...field('Organisation (required)', selectEl([['', '(choose)'], ...orgs.map((org) => [org, org])], doc.org_code, (value) => { doc.org_code = value; refitAll(); renderProps(); }, { id: 'f-org' })), ...field('Series', selectEl([['', '(none)'], ...app.series.map((s) => [s.code, `${s.code} · ${s.pattern}`])], doc.series_code, refitAll, { id: 'f-series', disabled: !!doc.serial })), doc.serial ? h('p', { class: 'mono' }, doc.serial) : null, h('h2', {}, 'Imprints'), @@ -579,10 +638,10 @@ async function showDoc(id) { ondblclick: () => { const p = document.querySelector('.page'); const i = p.querySelector('img'); placeNew(p, type, i.naturalWidth / 2, i.naturalHeight / 2); }, }, t.label))), h('div', { id: 'props' }), - h('h2', {}, 'Approval route'), routeEditor(doc.route, true), + h('h2', {}, 'Signing route'), routeEditor(doc.route, true), h('div', { class: 'actions' }, h('button', { onclick: () => act(saveDoc, 'Saved', false) }, 'Save draft'), - h('button', { class: 'primary', onclick: () => act(async () => { await saveDoc(); await api(`/docs/${id}/submit`, { method: 'POST' }); }, 'Submitted for approval') }, doc.status === 'CORRECTION_REQUIRED' ? 'Resubmit' : 'Submit for approval'), + h('button', { id: 'submit-doc', class: 'primary', disabled: !doc.title.trim(), onclick: () => act(async () => { await saveDoc(); await api(`/docs/${id}/submit`, { method: 'POST' }); }, 'Submitted') }, doc.status === 'CORRECTION_REQUIRED' ? 'Resubmit' : 'Submit'), h('button', { onclick: () => { const name = prompt('Template name'); if (name) act(async () => { await saveDoc(); await api('/templates', { method: 'POST', fields: { doc_id: id, name } }); app.templates = await api('/templates'); }, 'Template saved', false); } }, 'Save as template'), doc.status === 'DRAFT' && !doc.serial ? h('button', { class: 'danger', onclick: () => { if (confirm('Delete this draft?')) act(async () => { await api(`/docs/${id}/delete`, { method: 'POST' }); location.hash = '#/'; }, 'Deleted', false); } }, 'Delete draft') : null)); } else { @@ -593,7 +652,7 @@ async function showDoc(id) { h('p', {}, h('span', { class: 'mono' }, doc.serial || '(no serial)'), h('br'), `Prepared by ${doc.drafter_user}`, h('br'), `Submitted ${fmtTime(doc.submitted_at)} · revision ${doc.revision}`), doc.status === 'RELEASED' ? h('p', {}, `Released ${fmtTime(doc.released_at)}`, h('br'), h('span', { class: 'mono' }, 'sha256 ' + doc.final_sha256), h('br'), h('button', { class: 'primary', onclick: () => act(async () => { const buf = await api(`/docs/${id}/file/final`, { raw: true }); window.open(URL.createObjectURL(new Blob([buf], { type: 'application/pdf' }))); }, null, false) }, 'Open print copy (PDF)')) : null, - h('h2', {}, 'Approval route'), routeEditor(doc.route, false)); + h('h2', {}, 'Signing route'), routeEditor(doc.route, false)); if (doc.can_decide) { const comment = h('textarea', { rows: 3, placeholder: 'Comment (required unless signing off)' }); const currentParty = doc.current_party > 0 && doc.route.parties ? doc.route.parties[doc.current_party - 1] : null; @@ -602,7 +661,7 @@ async function showDoc(id) { if (needsEnrolment) { /* Show enrolment dialog */ - const canvas = h('canvas', { width: 400, height: 200, style: 'border: 1px solid #ccc; display: block; margin: 1rem 0' }); + const canvas = h('canvas', { width: 400, height: 200, class: 'enrol-canvas' }); const fileInput = h('input', { type: 'file', accept: 'image/png', style: 'margin: 1rem 0' }); let isDrawing = false; let ctx = canvas.getContext('2d'); @@ -611,13 +670,14 @@ async function showDoc(id) { ctx.strokeStyle = '#000'; ctx.lineWidth = 2; - const drawStart = (e) => { isDrawing = true; const rect = canvas.getBoundingClientRect(); ctx.beginPath(); ctx.moveTo(e.clientX - rect.left, e.clientY - rect.top); }; - const drawMove = (e) => { if (!isDrawing) return; const rect = canvas.getBoundingClientRect(); ctx.lineTo(e.clientX - rect.left, e.clientY - rect.top); ctx.stroke(); }; + const point = (e) => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) * canvas.width / r.width, (e.clientY - r.top) * canvas.height / r.height]; }; + const drawStart = (e) => { isDrawing = true; canvas.setPointerCapture(e.pointerId); ctx.beginPath(); ctx.moveTo(...point(e)); }; + const drawMove = (e) => { if (isDrawing) { ctx.lineTo(...point(e)); ctx.stroke(); } }; const drawEnd = () => { isDrawing = false; }; - canvas.addEventListener('mousedown', drawStart); - canvas.addEventListener('mousemove', drawMove); - canvas.addEventListener('mouseup', drawEnd); - canvas.addEventListener('mouseout', drawEnd); + canvas.addEventListener('pointerdown', drawStart); + canvas.addEventListener('pointermove', drawMove); + canvas.addEventListener('pointerup', drawEnd); + canvas.addEventListener('pointercancel', drawEnd); const enrol = async () => { let blob; @@ -634,7 +694,8 @@ async function showDoc(id) { const me = await api('/me'); if (me.enrolled) { toast('Ready to sign!'); - location.hash = '#/doc/' + id; /* Reload the page */ + app.me = me; + await showDoc(id); return; } } @@ -650,18 +711,19 @@ async function showDoc(id) { const placementInput = hasZone ? h('input', { type: 'hidden', id: 'f-placement', value: '' }) : null; const decide = (decision) => act(async () => { const fields = { decision, comment: comment.value }; - if (hasZone && decision === 'approve') { - const pl = document.getElementById('f-placement').value; - if (!pl) throw new Error('Placement required for signature zones'); - fields.placement = pl; + if (hasZone && decision === 'signoff') { + const area = document.querySelector('area[data-placement]'); + if (!area) throw new Error('Placement required for signature zones'); + const img = area.closest('.page').querySelector('img'); + fields.placement = JSON.stringify({ page: +area.closest('.page').dataset.page, coords: getCoords(area), ref_w: img.naturalWidth, ref_h: img.naturalHeight }); } return api(`/docs/${id}/decide`, { method: 'POST', fields }); }, 'Recorded'); side.append(h('h2', {}, 'Your decision'), comment); - if (placementInput) side.append(h('p', { class: 'notice' }, 'Draw your signature placement in the zone on the page')); + if (placementInput) side.append(h('p', { class: 'notice' }, 'Position your facsimile inside the signature zone. Drag the placement or use arrow keys; Shift and arrows resize.')); if (placementInput) side.append(placementInput); side.append(h('div', { class: 'actions' }, - h('button', { class: 'primary', onclick: () => decide('approve') }, 'Sign off'), + h('button', { class: 'primary', onclick: () => decide('signoff') }, 'Sign off'), h('button', { onclick: () => decide('correction') }, 'Correction required'), h('button', { class: 'danger', onclick: () => { if (confirm('Disapprove this document? This is final.')) decide('disapprove'); } }, 'Disapprove'))); } @@ -673,6 +735,7 @@ async function showDoc(id) { try { await renderPdf(pages, await api(`/docs/${id}/file/${kind}`, { raw: true })); loadLayout(doc.layout); + if (doc.can_decide && app.me.enrolled) setupPlacement(); renderProps(); } catch (e) { pages.replaceChildren(h('p', { class: 'notice error' }, 'Could not load the PDF: ' + e.message));
diff --git a/www/style.css b/www/style.css @@ -49,7 +49,7 @@ tr.click:hover td { background: #f6f9fc; } .side { flex: 0 0 25%; padding: 1.2rem; background: var(--bg); border-left: 1px solid var(--line); overflow-y: auto; } @media (max-width: 900px) { .workspace { flex-direction: column; } .pages { flex: 0 0 auto; overflow-y: visible; } .side { flex: 0 0 auto; background: transparent; border-left: none; overflow-y: visible; } } .pages { display: flex; flex-direction: column; gap: 1rem; align-items: center; } -.page { position: relative; width: 100%; max-width: 1000px; background: #fff; box-shadow: 0 1px 4px rgba(20, 30, 45, .25); line-height: 0; } +.page { position: relative; flex-shrink: 0; width: 100%; max-width: 1000px; background: #fff; box-shadow: 0 1px 4px rgba(20, 30, 45, .25); line-height: 0; } .page > img { width: 100%; height: auto; display: block; user-select: none; -webkit-user-drag: none; } .page .overlay { position: absolute; inset: 0; line-height: normal; } .page .pageno { position: absolute; top: 4px; right: -2.2rem; font-size: 11px; color: var(--muted); line-height: 1; } @@ -57,8 +57,8 @@ tr.click:hover td { background: #f6f9fc; } .notice.error { background: #f8d9d8; color: #7a201c; } .box { position: absolute; border: 1.5px solid rgb(var(--c)); background: rgba(var(--c), .10); overflow: hidden; white-space: nowrap; color: #000; } -.box.t-signature { --c: var(--sig); } -.box.t-dam_image { --c: var(--img); } +.box.t-signature_zone { --c: var(--sig); } +.box.t-pim_image { --c: var(--img); } .box.t-pim_text, .box.t-date, .box.t-serial { --c: var(--txt); } .box.circle { border-radius: 50%; } .box img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; display: block; } @@ -71,15 +71,17 @@ tr.click:hover td { background: #f6f9fc; } .hide-overlays .overlay { display: none; } /* edit mode */ -.edit .box { cursor: move; touch-action: none; } +.edit .box, .preview .placement { cursor: move; touch-action: none; } +.preview .placement { pointer-events: auto; border-style: solid; } +.preview .placement:focus { outline: 2px solid rgb(var(--sig)); outline-offset: 2px; } .edit .box.selected { outline: 2px solid rgb(var(--c)); outline-offset: 1px; background: rgba(var(--c), .18); } -.edit .box .grip { position: absolute; right: 0; bottom: 0; width: 9px; height: 9px; background: rgb(var(--c)); opacity: .55; cursor: nwse-resize; clip-path: polygon(100% 0, 100% 100%, 0 100%); } +.edit .box .grip, .preview .placement .grip { position: absolute; right: 0; bottom: 0; width: 9px; height: 9px; background: rgb(var(--c)); opacity: .55; cursor: nwse-resize; clip-path: polygon(100% 0, 100% 100%, 0 100%); } .edit .box.circle .grip { right: 12%; bottom: 12%; border-radius: 50%; } .page.dragover { outline: 3px dashed var(--accent); } .palette { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; } .palette .chip { border: 1.5px solid rgb(var(--c)); background: rgba(var(--c), .1); border-radius: 4px; padding: .35rem .5rem; cursor: grab; font-size: 13px; text-align: center; user-select: none; } -.chip.t-signature { --c: var(--sig); } .chip.t-dam_image { --c: var(--img); } +.chip.t-signature_zone { --c: var(--sig); } .chip.t-pim_image { --c: var(--img); } .chip.t-pim_text, .chip.t-date, .chip.t-serial { --c: var(--txt); } .level { border: 1px solid var(--line); border-radius: 4px; padding: .5rem; margin-bottom: .4rem; } @@ -88,6 +90,7 @@ tr.click:hover td { background: #f6f9fc; } .history { font-size: 13px; margin: 0; padding: 0; list-style: none; } .history li { padding: .35rem 0; border-bottom: 1px solid var(--line); } .history .c { color: var(--muted); display: block; } +.enrol-canvas { display: block; max-width: 100%; touch-action: none; border: 1px solid var(--line); margin: 1rem 0; } .actions { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .8rem; } .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; } .legend { display: flex; gap: .8rem; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
: asapien/signet