Files
vault171/app/javascript/controllers/scanner_controller.js
David Böhm a0e7272b6f
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled
Fixed qr-scanner (needs to be deployed (https))
2026-05-27 01:56:45 +02:00

54 lines
1.3 KiB
JavaScript

import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static targets = ["input", "preview", "modal"];
connect() {
this.qrScanner = null;
}
startCamera(event) {
event.preventDefault();
this.modalTarget.classList.remove("hidden");
const videoElement = this.previewTarget;
// Greift fehlerfrei auf die Klasse aus dem public-Ordner zu
this.qrScanner = new window.QrScanner(
videoElement,
(result) => { this.handleScanSuccess(result.data); },
{
onDecodeError: (error) => { /* Fehler ignorieren */ },
highlightScanRegion: true,
highlightCodeOutline: true,
maxScansPerSecond: 10
}
);
this.qrScanner.start().catch((err) => {
console.error("Kamera-Fehler:", err);
alert("Kamera konnte nicht gestartet werden.");
this.modalTarget.classList.add("hidden");
});
}
handleScanSuccess(decodedText) {
this.inputTarget.value = decodedText;
this.inputTarget.dispatchEvent(new Event("input", { bubbles: true }));
this.stopCamera();
}
stopCamera() {
if (this.qrScanner) {
this.qrScanner.stop();
this.qrScanner.destroy();
this.qrScanner = null;
}
this.modalTarget.classList.add("hidden");
}
disconnect() {
this.stopCamera();
}
}