veryfi-lens-wasm 3.1.4 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,17 @@
1
1
  import { createLayoutManager } from './croppedImageLayouts.js';
2
+ import { state } from '../core/state.js';
3
+
4
+ export const revealCropPreview = (previewImg) => {
5
+ state.cropPreviewRevealed = true;
6
+ const img = previewImg ?? document.getElementById('veryfi-crop-img-preview');
7
+ if (!img) {
8
+ return;
9
+ }
10
+ img.style.visibility = '';
11
+ if (img.style.display === 'none') {
12
+ img.style.display = '';
13
+ }
14
+ };
2
15
 
3
16
  export const createCroppedImage = (container, options = {}) => {
4
17
  const cropImgCanvas = document.createElement("canvas");
@@ -35,8 +48,15 @@ export const createCroppedImage = (container, options = {}) => {
35
48
  const updatePreviewImg = () => {
36
49
  if (cropImgCanvas.width > 0 && cropImgCanvas.height > 0) {
37
50
  previewImg.src = cropImgCanvas.toDataURL("image/jpeg", 0.97);
38
- if (previewImg.style.display === 'none') {
39
- previewImg.style.display = '';
51
+ const holdForFieldDetection = state.documentFieldsToDetect?.length > 0;
52
+ if (holdForFieldDetection) {
53
+ state.cropPreviewRevealed = false;
54
+ previewImg.style.visibility = 'hidden';
55
+ if (previewImg.style.display === 'none') {
56
+ previewImg.style.display = '';
57
+ }
58
+ } else {
59
+ revealCropPreview(previewImg);
40
60
  }
41
61
  }
42
62
  };
@@ -0,0 +1,282 @@
1
+ const WIDE_VIEWPORT_THRESHOLD = 900;
2
+ const MODAL_ID = 'veryfi-desktop-mode-modal';
3
+ const STYLE_ID = 'veryfi-desktop-mode-modal-styles';
4
+ const BODY_CLASS = 'veryfi-desktop-mode-on-mobile';
5
+ const BUTTON_SCALE = 2.5;
6
+
7
+ let installed = false;
8
+ let modalRoot = null;
9
+ let isActive = false;
10
+ let isDismissed = false;
11
+ let savedHtmlOverflow = '';
12
+ let savedBodyOverflow = '';
13
+
14
+ const isMobileHardware = () => {
15
+ if (typeof window === 'undefined' || typeof navigator === 'undefined') return false;
16
+ const mm = typeof window.matchMedia === 'function' ? window.matchMedia.bind(window) : null;
17
+ const coarsePointer = !!mm && mm('(pointer: coarse)').matches;
18
+ const noHover = !!mm && mm('(hover: none)').matches;
19
+ const anyCoarsePointer = !!mm && mm('(any-pointer: coarse)').matches;
20
+ const anyNoHover = !!mm && mm('(any-hover: none)').matches;
21
+ const hasTouch = (navigator.maxTouchPoints || 0) > 0 || 'ontouchstart' in window;
22
+ const uaDataMobile = !!(navigator.userAgentData && navigator.userAgentData.mobile);
23
+ const uaIsMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
24
+ navigator.userAgent || ''
25
+ );
26
+ return (
27
+ anyCoarsePointer || anyNoHover ||
28
+ coarsePointer || noHover ||
29
+ hasTouch ||
30
+ uaDataMobile || uaIsMobile
31
+ );
32
+ };
33
+
34
+ export const isDesktopModeOnMobile = () => {
35
+ if (typeof window === 'undefined') return false;
36
+ return isMobileHardware() && window.innerWidth >= WIDE_VIEWPORT_THRESHOLD;
37
+ };
38
+
39
+ const isInDesktopMode = isDesktopModeOnMobile;
40
+
41
+ const ensureStyles = () => {
42
+ if (document.getElementById(STYLE_ID)) return;
43
+ const style = document.createElement('style');
44
+ style.id = STYLE_ID;
45
+ style.textContent = `
46
+ #${MODAL_ID} {
47
+ position: fixed;
48
+ inset: 0;
49
+ z-index: 2147483646;
50
+ display: flex;
51
+ align-items: center;
52
+ justify-content: center;
53
+ padding: 16px;
54
+ background: rgba(0, 0, 0, 0.7);
55
+ backdrop-filter: blur(4px);
56
+ -webkit-backdrop-filter: blur(4px);
57
+ font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
58
+ box-sizing: border-box;
59
+ }
60
+ #${MODAL_ID} .veryfi-dm-card {
61
+ position: relative;
62
+ width: 100%;
63
+ max-width: 420px;
64
+ background: #1e293b;
65
+ color: #ffffff;
66
+ border-radius: 16px;
67
+ border: 1px solid #334155;
68
+ box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
69
+ padding: 24px;
70
+ box-sizing: border-box;
71
+ }
72
+ #${MODAL_ID} .veryfi-dm-close {
73
+ position: absolute;
74
+ top: 12px;
75
+ right: 12px;
76
+ width: 32px;
77
+ height: 32px;
78
+ display: flex;
79
+ align-items: center;
80
+ justify-content: center;
81
+ border-radius: 999px;
82
+ border: none;
83
+ background: transparent;
84
+ color: #94a3b8;
85
+ cursor: pointer;
86
+ padding: 0;
87
+ transition: background-color 0.15s, color 0.15s;
88
+ }
89
+ #${MODAL_ID} .veryfi-dm-close:hover {
90
+ background: #334155;
91
+ color: #ffffff;
92
+ }
93
+ #${MODAL_ID} .veryfi-dm-header {
94
+ display: flex;
95
+ align-items: center;
96
+ gap: 12px;
97
+ margin: 0 0 12px 0;
98
+ padding-right: 32px;
99
+ }
100
+ #${MODAL_ID} .veryfi-dm-icon {
101
+ width: 28px;
102
+ height: 28px;
103
+ color: #fbbf24;
104
+ flex-shrink: 0;
105
+ }
106
+ #${MODAL_ID} .veryfi-dm-title {
107
+ font-size: 18px;
108
+ font-weight: 600;
109
+ line-height: 1.3;
110
+ margin: 0;
111
+ }
112
+ #${MODAL_ID} .veryfi-dm-body {
113
+ font-size: 14px;
114
+ line-height: 1.5;
115
+ color: #cbd5e1;
116
+ margin: 0 0 20px 0;
117
+ }
118
+ #${MODAL_ID} .veryfi-dm-reload {
119
+ width: 100%;
120
+ padding: 12px 16px;
121
+ border-radius: 8px;
122
+ background: #10b981;
123
+ color: #0f172a;
124
+ font-weight: 600;
125
+ font-size: 14px;
126
+ border: none;
127
+ cursor: pointer;
128
+ transition: background-color 0.15s;
129
+ }
130
+ #${MODAL_ID} .veryfi-dm-reload:hover {
131
+ background: #34d399;
132
+ }
133
+ #${MODAL_ID} .veryfi-dm-reload:active {
134
+ background: #059669;
135
+ }
136
+ body.${BODY_CLASS} #veryfi-capture-button,
137
+ body.${BODY_CLASS} #veryfi-stitching-button {
138
+ transform: translateX(-50%) scale(${BUTTON_SCALE}) !important;
139
+ transform-origin: 50% 100% !important;
140
+ bottom: 5.75rem !important;
141
+ }
142
+ body.${BODY_CLASS} #veryfi-mirror-button {
143
+ transform: scale(${BUTTON_SCALE}) !important;
144
+ transform-origin: 0% 100% !important;
145
+ bottom: 125px !important;
146
+ left: 125px !important;
147
+ }
148
+ body.${BODY_CLASS} #veryfi-exit-button {
149
+ transform: scale(${BUTTON_SCALE}) !important;
150
+ transform-origin: 0% 100% !important;
151
+ bottom: 125px !important;
152
+ left: 287.5px !important;
153
+ }
154
+ body.${BODY_CLASS} #veryfi-switch-camera-button {
155
+ transform: scale(${BUTTON_SCALE}) !important;
156
+ transform-origin: 100% 100% !important;
157
+ bottom: 125px !important;
158
+ right: 125px !important;
159
+ }
160
+ body.${BODY_CLASS} #veryfi-torch-button {
161
+ transform: scale(${BUTTON_SCALE}) !important;
162
+ transform-origin: 100% 100% !important;
163
+ bottom: 125px !important;
164
+ right: 287.5px !important;
165
+ }
166
+ `;
167
+ document.head.appendChild(style);
168
+ };
169
+
170
+ const createModalNode = () => {
171
+ const root = document.createElement('div');
172
+ root.id = MODAL_ID;
173
+ root.setAttribute('role', 'dialog');
174
+ root.setAttribute('aria-modal', 'true');
175
+ root.innerHTML = `
176
+ <div class="veryfi-dm-card">
177
+ <button type="button" class="veryfi-dm-close" aria-label="Dismiss">
178
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
179
+ <path d="M18 6 6 18" />
180
+ <path d="m6 6 12 12" />
181
+ </svg>
182
+ </button>
183
+ <div class="veryfi-dm-header">
184
+ <svg class="veryfi-dm-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
185
+ <path d="M12 9v4" />
186
+ <path d="M12 17h.01" />
187
+ <path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0Z" />
188
+ </svg>
189
+ <h2 class="veryfi-dm-title">Desktop site mode detected</h2>
190
+ </div>
191
+ <p class="veryfi-dm-body">
192
+ Your browser is rendering this app in <strong>Desktop site</strong> mode.
193
+ The mobile capture experience may not work correctly. Please disable
194
+ &ldquo;Desktop site&rdquo; in your browser menu and reload the page.
195
+ </p>
196
+ <button type="button" class="veryfi-dm-reload">Reload page</button>
197
+ </div>
198
+ `;
199
+ root.querySelector('.veryfi-dm-close').addEventListener('click', () => {
200
+ isDismissed = true;
201
+ detachModal();
202
+ });
203
+ root.querySelector('.veryfi-dm-reload').addEventListener('click', () => {
204
+ window.location.reload();
205
+ });
206
+ return root;
207
+ };
208
+
209
+ const lockScroll = () => {
210
+ const html = document.documentElement;
211
+ const body = document.body;
212
+ savedHtmlOverflow = html.style.overflow;
213
+ savedBodyOverflow = body.style.overflow;
214
+ html.style.overflow = 'hidden';
215
+ body.style.overflow = 'hidden';
216
+ };
217
+
218
+ const unlockScroll = () => {
219
+ document.documentElement.style.overflow = savedHtmlOverflow;
220
+ document.body.style.overflow = savedBodyOverflow;
221
+ };
222
+
223
+ const attachModal = () => {
224
+ if (!modalRoot) {
225
+ ensureStyles();
226
+ modalRoot = createModalNode();
227
+ }
228
+ if (!modalRoot.isConnected) {
229
+ document.body.appendChild(modalRoot);
230
+ }
231
+ };
232
+
233
+ const detachModal = () => {
234
+ if (modalRoot && modalRoot.isConnected) {
235
+ modalRoot.remove();
236
+ }
237
+ };
238
+
239
+ const evaluate = () => {
240
+ const next = isInDesktopMode();
241
+ if (next === isActive) return;
242
+ isActive = next;
243
+ if (next) {
244
+ ensureStyles();
245
+ document.body.classList.add(BODY_CLASS);
246
+ lockScroll();
247
+ if (!isDismissed) attachModal();
248
+ } else {
249
+ document.body.classList.remove(BODY_CLASS);
250
+ unlockScroll();
251
+ detachModal();
252
+ isDismissed = false;
253
+ }
254
+ };
255
+
256
+ export const installDesktopModeWatchdog = () => {
257
+ if (typeof window === 'undefined' || typeof document === 'undefined') return;
258
+ isDismissed = false;
259
+ if (!installed) {
260
+ installed = true;
261
+ window.addEventListener('resize', evaluate);
262
+ window.addEventListener('orientationchange', evaluate);
263
+ }
264
+ evaluate();
265
+ if (isActive) attachModal();
266
+ };
267
+
268
+ export const uninstallDesktopModeWatchdog = () => {
269
+ if (typeof window === 'undefined' || typeof document === 'undefined') return;
270
+ if (installed) {
271
+ window.removeEventListener('resize', evaluate);
272
+ window.removeEventListener('orientationchange', evaluate);
273
+ installed = false;
274
+ }
275
+ if (isActive) {
276
+ unlockScroll();
277
+ isActive = false;
278
+ }
279
+ document.body.classList.remove(BODY_CLASS);
280
+ detachModal();
281
+ isDismissed = false;
282
+ };
@@ -0,0 +1,106 @@
1
+ import { state } from '../core/state.js';
2
+ import { createFieldLabelPill, createWarningPill } from './fieldDetectionPill.js';
3
+ import { createSafeResizeObserver } from '../core/safe-resize-observer.js';
4
+
5
+ const FIELD_LABELS = { vendor: 'Vendor', date: 'Date', total: 'Total' };
6
+ const ALL_FIELDS = ['vendor', 'date', 'total'];
7
+
8
+ let activeMessages = null;
9
+
10
+ const getRequestedFields = () => {
11
+ const requested = state.documentFieldsToDetect;
12
+ if (!requested?.length) {
13
+ return ALL_FIELDS;
14
+ }
15
+ return requested.filter((field) => ALL_FIELDS.includes(field));
16
+ };
17
+
18
+ const getMissingFields = (fieldDetection) => {
19
+ return getRequestedFields().filter(
20
+ (field) => !(fieldDetection[field]?.length && fieldDetection[field][0]?.text)
21
+ );
22
+ };
23
+
24
+ const positionMessages = (el, previewImg) => {
25
+ const parent = el.parentElement;
26
+ if (!parent || !previewImg) {
27
+ return;
28
+ }
29
+
30
+ const gap = 12;
31
+ const imageTop = previewImg.offsetTop;
32
+ const imageBottom = imageTop + previewImg.clientHeight;
33
+ const imageCenter = previewImg.offsetLeft + previewImg.clientWidth / 2;
34
+ const messageHeight = el.offsetHeight || 32;
35
+
36
+ el.style.left = `${imageCenter}px`;
37
+ el.style.transform = 'translateX(-50%)';
38
+ el.style.right = 'auto';
39
+ el.style.bottom = 'auto';
40
+ el.style.maxWidth = `min(92%, ${Math.max(previewImg.clientWidth, 280)}px)`;
41
+ el.style.top = `${Math.max(imageBottom - messageHeight - gap, imageTop + gap)}px`;
42
+ };
43
+
44
+ /**
45
+ * Missing-field and warning text outside the crop image (not over detections).
46
+ */
47
+ export const showFieldDetectionMessages = (previewImg, fieldDetection) => {
48
+ hideFieldDetectionMessages();
49
+ if (!previewImg?.parentElement || !fieldDetection) {
50
+ return;
51
+ }
52
+
53
+ const missingFields = getMissingFields(fieldDetection);
54
+ const showEstimated = Boolean(fieldDetection._estimated_boxes);
55
+ if (!missingFields.length && !showEstimated) {
56
+ return;
57
+ }
58
+
59
+ const submitBtn = document.getElementById('veryfi-submit-button');
60
+ const parent = previewImg.parentElement;
61
+
62
+ const el = document.createElement('div');
63
+ el.id = 'veryfi-field-detection-messages';
64
+ el.style.cssText =
65
+ 'position:absolute;z-index:141;pointer-events:none;display:flex;flex-direction:column;' +
66
+ 'align-items:center;gap:8px;width:max-content;max-width:92%;';
67
+
68
+ if (showEstimated) {
69
+ el.appendChild(
70
+ createWarningPill('Word positions estimated — box placement may be approximate.')
71
+ );
72
+ }
73
+
74
+ missingFields.forEach((field) => {
75
+ const { pill } = createFieldLabelPill(FIELD_LABELS[field] ?? field, { found: false });
76
+ el.appendChild(pill);
77
+ });
78
+
79
+ parent.appendChild(el);
80
+ activeMessages = el;
81
+
82
+ const reposition = () => positionMessages(el, previewImg);
83
+ const resizeObserver = createSafeResizeObserver(reposition);
84
+ resizeObserver?.observe(previewImg);
85
+ if (submitBtn) {
86
+ resizeObserver?.observe(submitBtn);
87
+ }
88
+ window.addEventListener('resize', reposition);
89
+
90
+ el._cleanup = () => {
91
+ resizeObserver?.disconnect();
92
+ window.removeEventListener('resize', reposition);
93
+ };
94
+
95
+ requestAnimationFrame(() => {
96
+ requestAnimationFrame(reposition);
97
+ });
98
+ };
99
+
100
+ export const hideFieldDetectionMessages = () => {
101
+ if (activeMessages?._cleanup) {
102
+ activeMessages._cleanup();
103
+ }
104
+ activeMessages?.remove();
105
+ activeMessages = null;
106
+ };
@@ -0,0 +1,146 @@
1
+ import {
2
+ hideFieldDetectionMessages,
3
+ showFieldDetectionMessages,
4
+ } from './fieldDetectionMessages.js';
5
+ import { createFieldLabelPill, measureFieldLabelPill } from './fieldDetectionPill.js';
6
+ import { createSafeResizeObserver } from '../core/safe-resize-observer.js';
7
+
8
+ const FIELD_LABELS = { vendor: 'Vendor', date: 'Date', total: 'Total' };
9
+ const FIELD_GREEN = '#04581a';
10
+ const FIELD_FILL = 'rgba(4, 88, 26, 0.31)';
11
+ const ALL_FIELDS = ['vendor', 'date', 'total'];
12
+
13
+ let activeOverlay = null;
14
+
15
+ const scaleBoxToDisplay = (box, sourceSize, displaySize) => {
16
+ const [left, top, right, bottom] = box;
17
+ const sourceWidth = sourceSize.width > 0 ? sourceSize.width : 1;
18
+ const sourceHeight = sourceSize.height > 0 ? sourceSize.height : 1;
19
+ const displayWidth = displaySize.width > 0 ? displaySize.width : sourceWidth;
20
+ const displayHeight = displaySize.height > 0 ? displaySize.height : sourceHeight;
21
+ const scaleX = displayWidth / sourceWidth;
22
+ const scaleY = displayHeight / sourceHeight;
23
+ return {
24
+ left: left * scaleX,
25
+ top: top * scaleY,
26
+ width: Math.max((right - left) * scaleX, 1),
27
+ height: Math.max((bottom - top) * scaleY, 1),
28
+ };
29
+ };
30
+
31
+ const flattenFieldDetection = (fieldDetection) => {
32
+ const items = [];
33
+ for (const field of ALL_FIELDS) {
34
+ const entries = fieldDetection[field];
35
+ if (!Array.isArray(entries)) continue;
36
+ for (const entry of entries) {
37
+ if (!entry?.box || entry.box.length < 4) continue;
38
+ const [l, t, r, b] = entry.box.map(Number);
39
+ if (!Number.isFinite(l) || !Number.isFinite(t) || r <= l || b <= t) continue;
40
+ items.push({ field, text: entry.text ?? '', box: [l, t, r, b] });
41
+ }
42
+ }
43
+ return items;
44
+ };
45
+
46
+ const inflateRect = (rect, scale) => {
47
+ const padX = Math.max(6 * scale, 5);
48
+ const padY = Math.max(5 * scale, 4);
49
+ return {
50
+ left: rect.left - padX,
51
+ top: rect.top - padY,
52
+ width: rect.width + padX * 2,
53
+ height: rect.height + padY * 2,
54
+ };
55
+ };
56
+
57
+ const boxCornerRadius = (rect) => Math.min(rect.width, rect.height) / 2;
58
+
59
+ const syncOverlayGeometry = (overlay, previewImg) => {
60
+ overlay.style.left = `${previewImg.offsetLeft}px`;
61
+ overlay.style.top = `${previewImg.offsetTop}px`;
62
+ overlay.style.width = `${previewImg.clientWidth}px`;
63
+ overlay.style.height = `${previewImg.clientHeight}px`;
64
+ };
65
+
66
+ const renderOverlay = (overlay, previewImg, fieldDetection) => {
67
+ const displayWidth = previewImg.clientWidth;
68
+ const displayHeight = previewImg.clientHeight;
69
+ if (displayWidth <= 0 || displayHeight <= 0) {
70
+ return;
71
+ }
72
+
73
+ syncOverlayGeometry(overlay, previewImg);
74
+ overlay.replaceChildren();
75
+
76
+ const sourceSize = {
77
+ width: fieldDetection._coord_width > 0 ? fieldDetection._coord_width : displayWidth,
78
+ height: fieldDetection._coord_height > 0 ? fieldDetection._coord_height : displayHeight,
79
+ };
80
+ const displaySize = { width: displayWidth, height: displayHeight };
81
+ const scale = sourceSize.width > 0 ? displayWidth / sourceSize.width : 1;
82
+
83
+ for (const { field, box } of flattenFieldDetection(fieldDetection)) {
84
+ const rawRect = scaleBoxToDisplay(box, sourceSize, displaySize);
85
+ const rect = inflateRect(rawRect, scale);
86
+ const labelText = FIELD_LABELS[field] ?? field;
87
+ const metrics = measureFieldLabelPill(labelText, scale);
88
+ const pillToBoxGap = 4 * scale;
89
+ const strokeHalf = 1;
90
+ const pillTop = rect.top - metrics.pillH - pillToBoxGap - strokeHalf;
91
+ const pillLeft = rect.left + rect.width / 2 - metrics.pillW / 2;
92
+ const cornerRadius = boxCornerRadius(rect);
93
+
94
+ const boxEl = document.createElement('div');
95
+ boxEl.style.cssText =
96
+ `position:absolute;left:${rect.left}px;top:${rect.top}px;width:${rect.width}px;height:${rect.height}px;` +
97
+ `border:2px solid ${FIELD_GREEN};background:${FIELD_FILL};border-radius:${cornerRadius}px;box-sizing:border-box;`;
98
+ overlay.appendChild(boxEl);
99
+
100
+ const { pill } = createFieldLabelPill(labelText, { found: true, scale });
101
+ pill.style.position = 'absolute';
102
+ pill.style.left = `${pillLeft}px`;
103
+ pill.style.top = `${Math.max(pillTop, 0)}px`;
104
+ overlay.appendChild(pill);
105
+ }
106
+ };
107
+
108
+ /**
109
+ * Draw vendor/date/total boxes on the crop preview image (pre-submit).
110
+ */
111
+ export const showFieldDetectionOverlay = (previewImg, fieldDetection) => {
112
+ hideFieldDetectionOverlay();
113
+ if (!previewImg || !fieldDetection || !previewImg.parentElement) {
114
+ return;
115
+ }
116
+
117
+ const overlay = document.createElement('div');
118
+ overlay.id = 'veryfi-field-detection-overlay';
119
+ overlay.style.cssText = 'position:absolute;pointer-events:none;z-index:140;overflow:visible;';
120
+ previewImg.parentElement.appendChild(overlay);
121
+ activeOverlay = overlay;
122
+
123
+ const render = () => renderOverlay(overlay, previewImg, fieldDetection);
124
+ const resizeObserver = createSafeResizeObserver(render);
125
+ resizeObserver?.observe(previewImg);
126
+ previewImg.addEventListener('load', render);
127
+ window.addEventListener('resize', render);
128
+
129
+ overlay._cleanup = () => {
130
+ resizeObserver?.disconnect();
131
+ previewImg.removeEventListener('load', render);
132
+ window.removeEventListener('resize', render);
133
+ };
134
+
135
+ render();
136
+ showFieldDetectionMessages(previewImg, fieldDetection);
137
+ };
138
+
139
+ export const hideFieldDetectionOverlay = () => {
140
+ hideFieldDetectionMessages();
141
+ if (activeOverlay?._cleanup) {
142
+ activeOverlay._cleanup();
143
+ }
144
+ activeOverlay?.remove();
145
+ activeOverlay = null;
146
+ };
@@ -0,0 +1,82 @@
1
+ const FIELD_GREEN = '#04581a';
2
+ const CHECK_GREEN = '#00AA00';
3
+ const MISSING_RED = '#ff0000';
4
+
5
+ const pillSizing = (labelText, scale = 1) => {
6
+ const iconSize = Math.min(Math.max(18 * scale, 16), 22);
7
+ const fontSize = Math.min(Math.max(14 * scale, 13), 18);
8
+ const gap = Math.max(6 * scale, 5);
9
+ const padV = Math.max(5 * scale, 4);
10
+ const padLeft = Math.max(6 * scale, 5);
11
+ const padRight = Math.max(10 * scale, 8);
12
+ const textWidth = labelText.length * fontSize * 0.55;
13
+ const pillH = 2 * padV + Math.max(iconSize, fontSize * 1.2);
14
+ const pillW = padLeft + iconSize + gap + textWidth + padRight;
15
+ return { iconSize, fontSize, gap, padV, padLeft, padRight, pillH, pillW };
16
+ };
17
+
18
+ const createIconBadge = (found, iconSize) => {
19
+ const badge = document.createElement('span');
20
+ const inner = iconSize * 0.58;
21
+ badge.style.cssText =
22
+ 'display:flex;align-items:center;justify-content:center;flex-shrink:0;' +
23
+ `width:${iconSize}px;height:${iconSize}px;border-radius:50%;background:#fff;`;
24
+
25
+ if (found) {
26
+ badge.innerHTML =
27
+ `<svg width="${inner}" height="${inner}" viewBox="0 0 12 12" fill="none" ` +
28
+ `xmlns="http://www.w3.org/2000/svg" aria-hidden="true">` +
29
+ `<path d="M2 6.5 4.5 9 10 3" stroke="${CHECK_GREEN}" stroke-width="2" ` +
30
+ `stroke-linecap="round" stroke-linejoin="round"/></svg>`;
31
+ } else {
32
+ badge.innerHTML =
33
+ `<svg width="${inner}" height="${inner}" viewBox="0 0 12 12" fill="none" ` +
34
+ `xmlns="http://www.w3.org/2000/svg" aria-hidden="true">` +
35
+ `<path d="M3 3 9 9M9 3 3 9" stroke="${MISSING_RED}" stroke-width="2" ` +
36
+ `stroke-linecap="round"/></svg>`;
37
+ }
38
+
39
+ return badge;
40
+ };
41
+
42
+ /**
43
+ * Field status pill: dark green capsule, white circle + check (found) or X (missing).
44
+ */
45
+ export const createFieldLabelPill = (labelText, { found = true, scale = 1 } = {}) => {
46
+ const { iconSize, fontSize, gap, padV, padLeft, padRight, pillH, pillW } = pillSizing(
47
+ labelText,
48
+ scale
49
+ );
50
+
51
+ const pill = document.createElement('div');
52
+ pill.style.cssText =
53
+ `display:inline-flex;align-items:center;gap:${gap}px;box-sizing:border-box;` +
54
+ `padding:${padV}px ${padRight}px ${padV}px ${padLeft}px;` +
55
+ `background:${FIELD_GREEN};border-radius:999px;white-space:nowrap;`;
56
+
57
+ const label = document.createElement('span');
58
+ label.textContent = labelText;
59
+ label.style.cssText =
60
+ `font-size:${fontSize}px;font-weight:600;line-height:1;color:#fff;` +
61
+ "font-family:system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;";
62
+
63
+ pill.appendChild(createIconBadge(found, iconSize));
64
+ pill.appendChild(label);
65
+
66
+ return { pill, pillH, pillW };
67
+ };
68
+
69
+ export const measureFieldLabelPill = (labelText, scale = 1) => {
70
+ const { pillH, pillW } = pillSizing(labelText, scale);
71
+ return { pillH, pillW };
72
+ };
73
+
74
+ export const createWarningPill = (text) => {
75
+ const pill = document.createElement('div');
76
+ pill.textContent = text;
77
+ pill.style.cssText =
78
+ 'padding:6px 12px;font-size:12px;font-weight:600;color:#fff;background:#92400e;' +
79
+ 'border-radius:10px;white-space:nowrap;text-align:center;' +
80
+ "font-family:system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;";
81
+ return pill;
82
+ };