Fix touch input highlights and barcode camera focus
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 46s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 46s
Remove field focus outlines. Preserve camera constraints across light and focus changes, fall back to one-shot autofocus, and offer supported manual focus, lens selection, and camera restart. Add scanner regression coverage.
This commit is contained in:
parent
40d9bc062b
commit
0ff7279cf6
5 changed files with 394 additions and 21 deletions
|
|
@ -35,6 +35,8 @@ The food sheet's **Quick entry** tab starts with **Scan barcode** and **Scan lab
|
||||||
3. The server's copy of **Open Food Facts** (~3.4 million products worldwide, community-entered).
|
3. The server's copy of **Open Food Facts** (~3.4 million products worldwide, community-entered).
|
||||||
4. The live Open Food Facts API, for products newer than the copies. Answers, including "not found", are cached on the server.
|
4. The live Open Food Facts API, for products newer than the copies. Answers, including "not found", are cached on the server.
|
||||||
|
|
||||||
|
The scanner requests continuous autofocus when supported, with one-shot autofocus as a fallback. **Focus distance** appears only on cameras that expose manual lens control; adjusting it holds a fixed focus until you choose **Use autofocus** or restart. If the image stays blurry, move the package farther away, try **Restart camera**, or choose a different lens under **Camera** when multiple cameras are available. Browser and camera support determine which focus controls are available; the native **Take a photo instead** option remains available.
|
||||||
|
|
||||||
A find fills the form with the product's name, serving and every nutrient the source has: the seven tracked ones plus extras such as saturated fat, cholesterol, added sugars, vitamins and minerals, under **More nutrients**. **Save this food to my library** is pre-checked so the next scan is instant. When nothing is found, scan the label instead; the barcode stays attached to the food.
|
A find fills the form with the product's name, serving and every nutrient the source has: the seven tracked ones plus extras such as saturated fat, cholesterol, added sugars, vitamins and minerals, under **More nutrients**. **Save this food to my library** is pre-checked so the next scan is instant. When nothing is found, scan the label instead; the barcode stays attached to the food.
|
||||||
|
|
||||||
**Scan label** opens the iPhone camera app (full resolution, focus and flash, no permission prompt), or a file picker on desktop. The photo is downscaled on the device and read on the server with PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX Runtime, CPU only, typically in 1–3 seconds. A parser then pulls out the serving size and each nutrient, undoes common OCR confusions (O/0, l/1, a unit read as a digit), checks amounts against their printed %DV, and handles US/Canadian panels, linear labels, tilted photos, Spanish labels and EU "per 100 g" tables (salt → sodium). Values it found replace the form's values; check them against the package before saving. Fill the frame with the panel and keep it flat and sharp: curved containers and glare are the usual causes of misses.
|
**Scan label** opens the iPhone camera app (full resolution, focus and flash, no permission prompt), or a file picker on desktop. The photo is downscaled on the device and read on the server with PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX Runtime, CPU only, typically in 1–3 seconds. A parser then pulls out the serving size and each nutrient, undoes common OCR confusions (O/0, l/1, a unit read as a digit), checks amounts against their printed %DV, and handles US/Canadian panels, linear labels, tilted photos, Spanish labels and EU "per 100 g" tables (salt → sodium). Values it found replace the form's values; check them against the package before saving. Fill the frame with the panel and keep it flat and sharp: curved containers and glare are the usual causes of misses.
|
||||||
|
|
|
||||||
159
src/Scanner.tsx
159
src/Scanner.tsx
|
|
@ -1,8 +1,14 @@
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { Camera, Flashlight, Search } from "lucide-react";
|
import { Camera, Flashlight, RotateCcw, Search } from "lucide-react";
|
||||||
import { normalizeBarcode } from "../shared/barcode.js";
|
import { normalizeBarcode } from "../shared/barcode.js";
|
||||||
import { Modal } from "./components";
|
import { Modal } from "./components";
|
||||||
import { barcodeDetector, detectBarcode } from "./scan";
|
import { barcodeDetector, detectBarcode } from "./scan";
|
||||||
|
import {
|
||||||
|
autofocus,
|
||||||
|
cameraCapabilities,
|
||||||
|
cameraConstraint,
|
||||||
|
manualFocusRange,
|
||||||
|
} from "./cameraFocus";
|
||||||
|
|
||||||
type Status = "starting" | "scanning" | "denied" | "unavailable";
|
type Status = "starting" | "scanning" | "denied" | "unavailable";
|
||||||
|
|
||||||
|
|
@ -20,6 +26,14 @@ export function BarcodeScanner({
|
||||||
const [torch, setTorch] = useState<boolean | null>(null);
|
const [torch, setTorch] = useState<boolean | null>(null);
|
||||||
const [digits, setDigits] = useState("");
|
const [digits, setDigits] = useState("");
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
|
const [restart, setRestart] = useState(0);
|
||||||
|
const [focus, setFocus] = useState<ReturnType<typeof manualFocusRange>>(null);
|
||||||
|
const [adjusting, setAdjusting] = useState(false);
|
||||||
|
const [manual, setManual] = useState(false);
|
||||||
|
const [canAutofocus, setCanAutofocus] = useState(false);
|
||||||
|
const [cameras, setCameras] = useState<MediaDeviceInfo[]>([]);
|
||||||
|
const [cameraId, setCameraId] = useState("");
|
||||||
|
const [activeCameraId, setActiveCameraId] = useState("");
|
||||||
const finish = (gtin: string) => {
|
const finish = (gtin: string) => {
|
||||||
if (done.current) return;
|
if (done.current) return;
|
||||||
done.current = true;
|
done.current = true;
|
||||||
|
|
@ -33,12 +47,19 @@ export function BarcodeScanner({
|
||||||
let stream: MediaStream | undefined;
|
let stream: MediaStream | undefined;
|
||||||
let stopped = false;
|
let stopped = false;
|
||||||
let timer = 0;
|
let timer = 0;
|
||||||
|
setStatus("starting");
|
||||||
|
setTorch(null);
|
||||||
|
setFocus(null);
|
||||||
|
setManual(false);
|
||||||
|
setCanAutofocus(false);
|
||||||
|
setAdjusting(false);
|
||||||
const reader = barcodeDetector();
|
const reader = barcodeDetector();
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
stream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
|
...(cameraId ? { deviceId: { exact: cameraId } } : {}),
|
||||||
facingMode: { ideal: "environment" },
|
facingMode: { ideal: "environment" },
|
||||||
width: { ideal: 1920 },
|
width: { ideal: 1920 },
|
||||||
height: { ideal: 1080 },
|
height: { ideal: 1080 },
|
||||||
|
|
@ -57,20 +78,27 @@ export function BarcodeScanner({
|
||||||
const element = video.current!;
|
const element = video.current!;
|
||||||
element.srcObject = stream;
|
element.srcObject = stream;
|
||||||
await element.play().catch(() => {});
|
await element.play().catch(() => {});
|
||||||
|
if (stopped) return;
|
||||||
const camera = stream.getVideoTracks()[0];
|
const camera = stream.getVideoTracks()[0];
|
||||||
track.current = camera;
|
track.current = camera;
|
||||||
const capabilities = camera.getCapabilities?.() as
|
const capabilities = cameraCapabilities(camera);
|
||||||
| (MediaTrackCapabilities & { torch?: boolean; focusMode?: string[] })
|
|
||||||
| undefined;
|
|
||||||
if (capabilities?.torch) setTorch(false);
|
if (capabilities?.torch) setTorch(false);
|
||||||
if (capabilities?.focusMode?.includes("continuous"))
|
setActiveCameraId(camera.getSettings().deviceId ?? "");
|
||||||
camera
|
setCanAutofocus(Boolean(capabilities.focusMode?.some(
|
||||||
.applyConstraints({
|
mode => mode === "continuous" || mode === "single-shot",
|
||||||
advanced: [{ focusMode: "continuous" } as MediaTrackConstraintSet],
|
)));
|
||||||
})
|
// Permission makes lens names available. Let the user switch away from
|
||||||
.catch(() => {});
|
// a fixed-focus rear lens when a phone chooses it by default.
|
||||||
|
void navigator.mediaDevices.enumerateDevices?.().then(devices => {
|
||||||
|
if (!stopped)
|
||||||
|
setCameras(devices.filter(device => device.kind === "videoinput" && device.deviceId));
|
||||||
|
}).catch(() => {});
|
||||||
|
await autofocus(camera);
|
||||||
|
if (stopped) return;
|
||||||
|
setFocus(manualFocusRange(camera));
|
||||||
setStatus("scanning");
|
setStatus("scanning");
|
||||||
const detector = await reader.catch(() => null);
|
const detector = await reader.catch(() => null);
|
||||||
|
if (stopped) return;
|
||||||
if (!detector) return setStatus("unavailable");
|
if (!detector) return setStatus("unavailable");
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
const context = canvas.getContext("2d", { willReadFrequently: true })!;
|
const context = canvas.getContext("2d", { willReadFrequently: true })!;
|
||||||
|
|
@ -109,16 +137,40 @@ export function BarcodeScanner({
|
||||||
// A dropped frame; try the next one.
|
// A dropped frame; try the next one.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer = window.setTimeout(tick, 100);
|
if (!stopped && !done.current) timer = window.setTimeout(tick, 100);
|
||||||
};
|
};
|
||||||
void tick();
|
void tick();
|
||||||
})();
|
})();
|
||||||
return () => {
|
return () => {
|
||||||
stopped = true;
|
stopped = true;
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
|
track.current = null;
|
||||||
stream?.getTracks().forEach((t) => t.stop());
|
stream?.getTracks().forEach((t) => t.stop());
|
||||||
};
|
};
|
||||||
}, []);
|
}, [restart, cameraId]);
|
||||||
|
|
||||||
|
async function adjustFocus(value?: number) {
|
||||||
|
const camera = track.current;
|
||||||
|
if (!camera) return;
|
||||||
|
setAdjusting(true);
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
if (value === undefined) {
|
||||||
|
const applied = await autofocus(camera);
|
||||||
|
if (track.current === camera) {
|
||||||
|
if (applied) setManual(false);
|
||||||
|
else setError("Autofocus isn’t available. Adjust focus or restart the camera.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await cameraConstraint(camera, { focusMode: "manual", focusDistance: value });
|
||||||
|
if (track.current === camera) setManual(true);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
if (track.current === camera) setError("This camera couldn’t adjust focus. Try restarting it or taking a photo.");
|
||||||
|
} finally {
|
||||||
|
if (track.current === camera) setAdjusting(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function photo(file: File | undefined) {
|
async function photo(file: File | undefined) {
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
|
|
@ -161,20 +213,89 @@ export function BarcodeScanner({
|
||||||
className="scanner-torch"
|
className="scanner-torch"
|
||||||
aria-label={torch ? "Turn light off" : "Turn light on"}
|
aria-label={torch ? "Turn light off" : "Turn light on"}
|
||||||
aria-pressed={torch}
|
aria-pressed={torch}
|
||||||
onClick={() => {
|
disabled={adjusting}
|
||||||
|
onClick={async () => {
|
||||||
|
const camera = track.current;
|
||||||
|
if (!camera) return;
|
||||||
const next = !torch;
|
const next = !torch;
|
||||||
track.current
|
setAdjusting(true);
|
||||||
?.applyConstraints({
|
try {
|
||||||
advanced: [{ torch: next } as MediaTrackConstraintSet],
|
await cameraConstraint(camera, { torch: next });
|
||||||
})
|
if (track.current === camera) setTorch(next);
|
||||||
.then(() => setTorch(next))
|
} catch {
|
||||||
.catch(() => setTorch(null));
|
if (track.current === camera) setTorch(null);
|
||||||
|
} finally {
|
||||||
|
if (track.current === camera) setAdjusting(false);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Flashlight size={18} />
|
<Flashlight size={18} />
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
{status === "scanning" && (
|
||||||
|
<div className="scanner-focus">
|
||||||
|
<p className="muted small">
|
||||||
|
If it’s blurry, move the barcode farther away and hold still.
|
||||||
|
</p>
|
||||||
|
{focus && (
|
||||||
|
<label>
|
||||||
|
Focus distance
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min={focus.min}
|
||||||
|
max={focus.max}
|
||||||
|
step={focus.step}
|
||||||
|
value={focus.value}
|
||||||
|
onChange={event => {
|
||||||
|
const value = Number(event.target.value);
|
||||||
|
setFocus(current => current && { ...current, value });
|
||||||
|
void adjustFocus(value);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
{canAutofocus && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
disabled={adjusting}
|
||||||
|
onClick={() => void adjustFocus()}
|
||||||
|
>
|
||||||
|
{manual ? "Use autofocus" : "Refocus"}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={() => {
|
||||||
|
setError("");
|
||||||
|
setRestart(current => current + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<RotateCcw size={17} />Restart camera
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{cameras.length > 1 && (
|
||||||
|
<label className="scanner-camera">
|
||||||
|
Camera
|
||||||
|
<select
|
||||||
|
value={cameraId || activeCameraId}
|
||||||
|
onChange={event => {
|
||||||
|
setError("");
|
||||||
|
setCameraId(event.target.value);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{!activeCameraId && !cameraId && <option value="">Default camera</option>}
|
||||||
|
{cameras.map((camera, index) => (
|
||||||
|
<option key={camera.deviceId} value={camera.deviceId}>
|
||||||
|
{camera.label || `Camera ${index + 1}`}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
<div className="scanner-actions">
|
<div className="scanner-actions">
|
||||||
<label className="secondary">
|
<label className="secondary">
|
||||||
<Camera size={17} />
|
<Camera size={17} />
|
||||||
|
|
|
||||||
85
src/cameraFocus.ts
Normal file
85
src/cameraFocus.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
// Image Capture controls aren't included in every browser's DOM typings.
|
||||||
|
export type CameraCapabilities = MediaTrackCapabilities & {
|
||||||
|
torch?: boolean;
|
||||||
|
focusMode?: string[];
|
||||||
|
focusDistance?: { min: number; max: number; step?: number };
|
||||||
|
};
|
||||||
|
|
||||||
|
export function cameraCapabilities(camera: MediaStreamTrack): CameraCapabilities {
|
||||||
|
try {
|
||||||
|
return camera.getCapabilities?.() ?? {};
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyConstraints replaces the previous constraints. Keep resolution, focus
|
||||||
|
// and torch together, including when switching between auto and manual focus.
|
||||||
|
const pending = new WeakMap<MediaStreamTrack, Promise<void>>();
|
||||||
|
|
||||||
|
export function cameraConstraint(
|
||||||
|
camera: MediaStreamTrack,
|
||||||
|
changes: Record<string, string | number | boolean>,
|
||||||
|
): Promise<void> {
|
||||||
|
// A dragged slider and a light toggle can overlap. Read settings only after
|
||||||
|
// the previous update completes so neither request discards the other.
|
||||||
|
const update = (pending.get(camera) ?? Promise.resolve())
|
||||||
|
.catch(() => {})
|
||||||
|
.then(async () => {
|
||||||
|
const current = camera.getConstraints();
|
||||||
|
const advanced = (current.advanced ?? [])
|
||||||
|
.map((entry) => Object.fromEntries(
|
||||||
|
Object.entries(entry).filter(([key]) => !(key in changes)),
|
||||||
|
))
|
||||||
|
.filter((entry) => Object.keys(entry).length);
|
||||||
|
const base = Object.fromEntries(
|
||||||
|
Object.entries(current).filter(([key]) => !(key in changes)),
|
||||||
|
);
|
||||||
|
await camera.applyConstraints({
|
||||||
|
...base,
|
||||||
|
advanced: [...advanced, changes] as MediaTrackConstraintSet[],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
pending.set(camera, update);
|
||||||
|
return update;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function autofocus(camera: MediaStreamTrack): Promise<boolean> {
|
||||||
|
const modes = cameraCapabilities(camera).focusMode ?? [];
|
||||||
|
for (const mode of ["continuous", "single-shot"]) {
|
||||||
|
if (!modes.includes(mode)) continue;
|
||||||
|
try {
|
||||||
|
await cameraConstraint(camera, { focusMode: mode });
|
||||||
|
const settings = camera.getSettings() as MediaTrackSettings & {
|
||||||
|
focusMode?: string;
|
||||||
|
};
|
||||||
|
// Advanced constraints can be silently ignored. Try the fallback if the
|
||||||
|
// browser reports that it kept a different mode.
|
||||||
|
if (settings.focusMode && settings.focusMode !== mode) continue;
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
// Some cameras advertise a mode but reject it at this resolution.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function manualFocusRange(camera: MediaStreamTrack) {
|
||||||
|
const capabilities = cameraCapabilities(camera);
|
||||||
|
const range = capabilities.focusDistance;
|
||||||
|
if (
|
||||||
|
!capabilities.focusMode?.includes("manual") || !range ||
|
||||||
|
!Number.isFinite(range.min) || !Number.isFinite(range.max) ||
|
||||||
|
range.max <= range.min
|
||||||
|
)
|
||||||
|
return null;
|
||||||
|
const settings = camera.getSettings() as MediaTrackSettings & {
|
||||||
|
focusDistance?: number;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
min: range.min,
|
||||||
|
max: range.max,
|
||||||
|
step: range.step && range.step > 0 ? range.step : (range.max - range.min) / 100,
|
||||||
|
value: Math.max(range.min, Math.min(range.max, settings.focusDistance ?? (range.min + range.max) / 2)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -99,8 +99,7 @@ a:focus-visible {
|
||||||
input:focus,
|
input:focus,
|
||||||
select:focus,
|
select:focus,
|
||||||
textarea:focus {
|
textarea:focus {
|
||||||
outline: 2px solid var(--accent);
|
outline: none;
|
||||||
outline-offset: 1px;
|
|
||||||
}
|
}
|
||||||
button {
|
button {
|
||||||
touch-action: manipulation;
|
touch-action: manipulation;
|
||||||
|
|
@ -1327,6 +1326,19 @@ textarea {
|
||||||
.scanner-actions {
|
.scanner-actions {
|
||||||
margin-top: 14px;
|
margin-top: 14px;
|
||||||
}
|
}
|
||||||
|
.scanner-focus {
|
||||||
|
display: grid;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 12px;
|
||||||
|
}
|
||||||
|
.scanner-camera {
|
||||||
|
margin-top: 12px;
|
||||||
|
}
|
||||||
|
.scanner-focus input[type="range"] {
|
||||||
|
padding: 0;
|
||||||
|
border: 0;
|
||||||
|
accent-color: var(--accent);
|
||||||
|
}
|
||||||
.scanner-actions .secondary {
|
.scanner-actions .secondary {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
|
||||||
153
tests/scanner.test.js
Normal file
153
tests/scanner.test.js
Normal file
|
|
@ -0,0 +1,153 @@
|
||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import vm from "node:vm";
|
||||||
|
import ts from "typescript";
|
||||||
|
|
||||||
|
// Mount the real scanner with a browser-shaped camera. This exercises startup
|
||||||
|
// and the actual UI handlers without needing permission or physical hardware.
|
||||||
|
async function mount({ modes = ["continuous", "single-shot"], rejectContinuous = false, capabilities = {} } = {}) {
|
||||||
|
const calls = [];
|
||||||
|
let constraints = { width: { ideal: 1920 }, height: { ideal: 1080 } };
|
||||||
|
let settings = {};
|
||||||
|
let stops = 0;
|
||||||
|
const camera = {
|
||||||
|
getCapabilities: () => ({ focusMode: modes, torch: true, ...capabilities }),
|
||||||
|
getConstraints: () => constraints,
|
||||||
|
getSettings: () => settings,
|
||||||
|
stop: () => stops++,
|
||||||
|
async applyConstraints(next) {
|
||||||
|
calls.push(next);
|
||||||
|
const applied = Object.assign({}, next, ...(next.advanced ?? []));
|
||||||
|
const mode = applied.focusMode?.exact ?? applied.focusMode;
|
||||||
|
if (mode === "continuous" && rejectContinuous) throw new Error("Focus rejected");
|
||||||
|
constraints = next;
|
||||||
|
settings = { focusMode: mode, focusDistance: applied.focusDistance, torch: applied.torch };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const stream = { getTracks: () => [camera], getVideoTracks: () => [camera] };
|
||||||
|
const video = { play: async () => {}, videoWidth: 0, videoHeight: 0 };
|
||||||
|
const refs = [], states = [], effects = [];
|
||||||
|
let refIndex = 0, stateIndex = 0, mounted = false;
|
||||||
|
const jsx = (type, props) => ({ type, props });
|
||||||
|
const react = {
|
||||||
|
useRef(initial) {
|
||||||
|
const index = refIndex++;
|
||||||
|
return refs[index] ??= { current: index === 0 ? video : initial };
|
||||||
|
},
|
||||||
|
useState(initial) {
|
||||||
|
const index = stateIndex++;
|
||||||
|
if (!(index in states)) states[index] = initial;
|
||||||
|
return [states[index], value => { states[index] = typeof value === "function" ? value(states[index]) : value; }];
|
||||||
|
},
|
||||||
|
useEffect(effect) { if (!mounted) effects.push(effect); },
|
||||||
|
};
|
||||||
|
const cache = new Map();
|
||||||
|
function load(file) {
|
||||||
|
if (cache.has(file.href)) return cache.get(file.href);
|
||||||
|
const exports = {};
|
||||||
|
cache.set(file.href, exports);
|
||||||
|
const context = {
|
||||||
|
exports,
|
||||||
|
require(name) {
|
||||||
|
if (name === "react") return react;
|
||||||
|
if (name === "react/jsx-runtime") return { jsx, jsxs: jsx };
|
||||||
|
if (name === "lucide-react") return {};
|
||||||
|
if (name === "./components") return { Modal: "Modal" };
|
||||||
|
if (name === "./scan") return { barcodeDetector: async () => ({}), detectBarcode: async () => null };
|
||||||
|
if (name === "../shared/barcode.js") return { normalizeBarcode: () => null };
|
||||||
|
return load(new URL(name + ".ts", file));
|
||||||
|
},
|
||||||
|
navigator: { mediaDevices: { getUserMedia: async () => stream } },
|
||||||
|
document: { createElement: () => ({ getContext: () => ({}) }) },
|
||||||
|
window: { setTimeout: () => 1 }, clearTimeout() {},
|
||||||
|
};
|
||||||
|
vm.runInNewContext(ts.transpileModule(readFileSync(file, "utf8"), {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX },
|
||||||
|
}).outputText, context);
|
||||||
|
return exports;
|
||||||
|
}
|
||||||
|
const { BarcodeScanner } = load(new URL("../src/Scanner.tsx", import.meta.url));
|
||||||
|
const render = () => {
|
||||||
|
refIndex = stateIndex = 0;
|
||||||
|
return BarcodeScanner({ close() {}, detected() {} });
|
||||||
|
};
|
||||||
|
render();
|
||||||
|
mounted = true;
|
||||||
|
const cleanups = effects.map(effect => effect());
|
||||||
|
const settle = () => new Promise(resolve => setImmediate(resolve));
|
||||||
|
await settle();
|
||||||
|
const walk = (predicate, tree) => {
|
||||||
|
if (!tree || typeof tree !== "object") return;
|
||||||
|
if (predicate(tree)) return tree;
|
||||||
|
for (const child of [tree.props?.children].flat(Infinity)) {
|
||||||
|
const result = walk(predicate, child);
|
||||||
|
if (result) return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return { camera, calls, find: predicate => walk(predicate, render()), settle, cleanup: () => cleanups.forEach(fn => fn?.()), stops: () => stops };
|
||||||
|
}
|
||||||
|
|
||||||
|
test("scanner falls back to one-shot autofocus when continuous focus fails", async () => {
|
||||||
|
const app = await mount({ rejectContinuous: true });
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "single-shot");
|
||||||
|
app.find(node => node.props?.children === "Refocus").props.onClick();
|
||||||
|
await app.settle();
|
||||||
|
assert.equal(app.calls.filter(call => call.advanced?.some(entry => entry.focusMode === "single-shot")).length, 2);
|
||||||
|
app.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("switching the scanner light preserves autofocus and video constraints", async () => {
|
||||||
|
const app = await mount();
|
||||||
|
await app.find(node => node.props?.className === "scanner-torch").props.onClick();
|
||||||
|
await app.settle();
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "continuous");
|
||||||
|
assert.equal(app.camera.getSettings().torch, true);
|
||||||
|
assert.equal(app.camera.getConstraints().width.ideal, 1920);
|
||||||
|
app.cleanup();
|
||||||
|
assert.equal(app.stops(), 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("manual focus locks the chosen distance and survives light changes", async () => {
|
||||||
|
const app = await mount({
|
||||||
|
modes: ["continuous", "manual"],
|
||||||
|
capabilities: { focusDistance: { min: 0, max: 10, step: 0.1 } },
|
||||||
|
});
|
||||||
|
const slider = () => app.find(node => node.type === "input" && node.props.type === "range");
|
||||||
|
assert.equal(slider().props.min, 0);
|
||||||
|
assert.equal(slider().props.max, 10);
|
||||||
|
// Simulate a fast drag: queued requests must retain the final position.
|
||||||
|
slider().props.onChange({ target: { value: "2" } });
|
||||||
|
slider().props.onChange({ target: { value: "3" } });
|
||||||
|
await app.settle();
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "manual");
|
||||||
|
assert.equal(app.camera.getSettings().focusDistance, 3);
|
||||||
|
assert.equal(slider().props.value, 3);
|
||||||
|
await app.find(node => node.props?.className === "scanner-torch").props.onClick();
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "manual");
|
||||||
|
assert.equal(app.camera.getSettings().focusDistance, 3);
|
||||||
|
assert.equal(app.camera.getSettings().torch, true);
|
||||||
|
app.find(node => node.props?.children === "Use autofocus").props.onClick();
|
||||||
|
await app.settle();
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "continuous");
|
||||||
|
assert.equal(app.camera.getSettings().torch, true);
|
||||||
|
app.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("scanner stays usable when the browser exposes no focus controls", async () => {
|
||||||
|
const app = await mount({ modes: [] });
|
||||||
|
assert.equal(app.calls.length, 0);
|
||||||
|
assert.equal(app.find(node => node.type === "input" && node.props.type === "range"), undefined);
|
||||||
|
assert.ok(app.find(node => node.props?.className === "scanner-view live"));
|
||||||
|
assert.ok(app.find(node => node.props?.onClick && Array.isArray(node.props.children) && node.props.children.includes("Restart camera")));
|
||||||
|
app.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("manual-only cameras offer fixed focus without an unsupported autofocus button", async () => {
|
||||||
|
const app = await mount({ modes: ["manual"], capabilities: { focusDistance: { min: 0, max: 1 } } });
|
||||||
|
app.find(node => node.type === "input" && node.props.type === "range").props.onChange({ target: { value: "0.4" } });
|
||||||
|
await app.settle();
|
||||||
|
assert.equal(app.camera.getSettings().focusMode, "manual");
|
||||||
|
assert.equal(app.find(node => node.props?.children === "Use autofocus"), undefined);
|
||||||
|
app.cleanup();
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue