diff --git a/barcode_scanner/README.rst b/barcode_scanner/README.rst new file mode 100644 index 0000000..6db1637 --- /dev/null +++ b/barcode_scanner/README.rst @@ -0,0 +1,169 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +=============== +Barcode Scanner +=============== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ba6365d70b631c205696ddd617c9738e11ae52e29ec43c03f6394bdd7815d1a8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fbarcode--interface-lightgray.png?logo=github + :target: https://github.com/OCA/barcode-interface/tree/18.0/barcode_scanner + :alt: OCA/barcode-interface +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/barcode-interface-18-0/barcode-interface-18-0-barcode_scanner + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/barcode-interface&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module is the **base framework** of the Barcode suite: a fast, +full-screen scanning application shell for Odoo that other modules +extend. + +It ships no business logic of its own. Instead it exposes the building +blocks feature modules plug into, the same way Odoo widgets +self-register: + +- a full-screen client action and a lightweight in-app router; +- registries for screens, scan handlers and home-screen menu tiles + (``barcode_screens``, ``barcode_scan_handlers``, + ``barcode_menu_tiles``); +- the barcode input service (hardware/keyboard wedge), audio/vibration + feedback, an ORM/API wrapper and shared hooks. + +Install a feature module on top — such as ``barcode_stock`` — to get +actual warehouse operations. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +``barcode_scanner`` is a framework and is not useful on its own: install +a feature module such as ``barcode_stock`` to get actual operations. + +To build your own feature module, register your pieces into the +framework registries — no change to the base module is required. + +Register a screen: + +.. code:: js + + import {barcodeScreens} from "@barcode_scanner/js/registries"; + + barcodeScreens.add("my_screen", {component: MyScreen}); + +Register a scan handler (tried in ``sequence`` order; the first that +returns a truthy value consumes the scan): + +.. code:: js + + import {barcodeScanHandlers} from "@barcode_scanner/js/registries"; + + barcodeScanHandlers.add("my_handler", { + async handle(barcode, parsed, {api, navigate, notify}) { + // return true when handled + }, + }, {sequence: 50}); + +Register a home-screen tile: + +.. code:: js + + import {barcodeMenuTiles} from "@barcode_scanner/js/registries"; + + barcodeMenuTiles.add("my_tile", { + label: "My Screen", + icon: "fa-cube", + action: ({navigate}) => navigate("my_screen"), + }, {sequence: 50}); + +Register a barcode parser (tried in ``sequence`` order before the +built-in EAN13 one; return ``null`` to let the next parser try): + +.. code:: js + + import {barcodeParsers} from "@barcode_scanner/js/registries"; + + barcodeParsers.add("my_symbology", (barcode) => { + // return {type: "...", value: "", ...} or null + }, {sequence: 20}); + +Register a startup task, for data your module needs before the first +scan (``barcode_gs1`` reads its nomenclature this way). Tasks are +awaited when the app opens; one that fails is logged and skipped: + +.. code:: js + + import {barcodeStartupTasks} from "@barcode_scanner/js/registries"; + + barcodeStartupTasks.add("my_config", (env) => loadMyConfig(env.services.orm)); + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Binhex + +Contributors +------------ + +- `Antonio David Ruban Espinal `__ +- `Zuzanna Elzbieta Szalaty + Szalaty `__ + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-szalatyzuzanna| image:: https://github.com/szalatyzuzanna.png?size=40px + :target: https://github.com/szalatyzuzanna + :alt: szalatyzuzanna + +Current `maintainer `__: + +|maintainer-szalatyzuzanna| + +This module is part of the `OCA/barcode-interface `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/barcode_scanner/__init__.py b/barcode_scanner/__init__.py new file mode 100644 index 0000000..406f3fc --- /dev/null +++ b/barcode_scanner/__init__.py @@ -0,0 +1,3 @@ +# The base barcode_scanner module ships no Python models; it is a pure +# JavaScript scanning framework. Feature modules (e.g. barcode_stock) add +# their own models. diff --git a/barcode_scanner/__manifest__.py b/barcode_scanner/__manifest__.py new file mode 100644 index 0000000..1905ac7 --- /dev/null +++ b/barcode_scanner/__manifest__.py @@ -0,0 +1,44 @@ +{ + "name": "Barcode Scanner", + "version": "18.0.1.0.0", + "category": "Hidden", + "summary": "Base scanning framework: client action, registries, scanner " + "input and hooks", + "author": "Binhex, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/barcode-interface", + "maintainers": ["szalatyzuzanna"], + "license": "AGPL-3", + "depends": [ + "web", + ], + "data": [ + "views/action.xml", + "views/templates.xml", + ], + "assets": { + "web.assets_backend": [ + "barcode_scanner/static/src/scss/barcode_scanner.scss", + "barcode_scanner/static/src/xml/barcode_scanner_templates.xml", + "barcode_scanner/static/src/js/registries.esm.js", + "barcode_scanner/static/src/js/barcode_parser.esm.js", + "barcode_scanner/static/src/js/utils/scan_match.esm.js", + "barcode_scanner/static/src/js/barcode.esm.js", + "barcode_scanner/static/src/js/api.esm.js", + "barcode_scanner/static/src/js/router.esm.js", + "barcode_scanner/static/src/js/store.esm.js", + "barcode_scanner/static/src/js/app.esm.js", + "barcode_scanner/static/src/js/services/feedback_service.esm.js", + "barcode_scanner/static/src/js/hooks/use_barcode.esm.js", + "barcode_scanner/static/src/js/hooks/use_barcode_handler.esm.js", + "barcode_scanner/static/src/js/hooks/use_barcode_dispatcher.esm.js", + "barcode_scanner/static/src/js/hooks/use_inventory.esm.js", + "barcode_scanner/static/src/js/screens/main_screen.esm.js", + ], + "web.assets_unit_tests": [ + "barcode_scanner/static/tests/**/*", + ], + }, + "installable": True, + "application": True, + "auto_install": False, +} diff --git a/barcode_scanner/i18n/es.po b/barcode_scanner/i18n/es.po new file mode 100644 index 0000000..b96e053 --- /dev/null +++ b/barcode_scanner/i18n/es.po @@ -0,0 +1,59 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * barcode_scanner +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2026-07-31 11:45+0000\n" +"PO-Revision-Date: 2026-07-31 11:45+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/xml/barcode_scanner_templates.xml:0 +msgid "Avatar" +msgstr "Avatar" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/js/hooks/use_barcode_dispatcher.js:0 +msgid "Barcode not recognized" +msgstr "Código de barras no reconocido" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/js/screens/main_screen.js:0 +msgid "Good afternoon" +msgstr "Buenas tardes" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/js/screens/main_screen.js:0 +msgid "Good evening" +msgstr "Buenas noches" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/js/screens/main_screen.js:0 +msgid "Good morning" +msgstr "Buenos días" + +#. module: barcode_scanner +#. odoo-javascript +#: code:addons/barcode_scanner/static/src/xml/barcode_scanner_templates.xml:0 +msgid "Ready for today's distribution schedule." +msgstr "Listo para la planificación de distribución de hoy." + +#. module: barcode_scanner +#: model:ir.actions.client,name:barcode_scanner.action_barcode_scanner +#: model:ir.ui.menu,name:barcode_scanner.menu_barcode_scanner_root +msgid "Scanner" +msgstr "Escáner" diff --git a/barcode_scanner/pyproject.toml b/barcode_scanner/pyproject.toml new file mode 100644 index 0000000..4231d0c --- /dev/null +++ b/barcode_scanner/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/barcode_scanner/readme/CONTRIBUTORS.md b/barcode_scanner/readme/CONTRIBUTORS.md new file mode 100644 index 0000000..7a07748 --- /dev/null +++ b/barcode_scanner/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +* [Antonio David Ruban Espinal](https://github.com/antoniodavid) +* [Zuzanna Elzbieta Szalaty Szalaty](https://github.com/szalatyzuzanna) diff --git a/barcode_scanner/readme/DESCRIPTION.md b/barcode_scanner/readme/DESCRIPTION.md new file mode 100644 index 0000000..a7a63d6 --- /dev/null +++ b/barcode_scanner/readme/DESCRIPTION.md @@ -0,0 +1,14 @@ +This module is the **base framework** of the Barcode suite: a fast, +full-screen scanning application shell for Odoo that other modules extend. + +It ships no business logic of its own. Instead it exposes the building blocks +feature modules plug into, the same way Odoo widgets self-register: + +- a full-screen client action and a lightweight in-app router; +- registries for screens, scan handlers and home-screen menu tiles + (`barcode_screens`, `barcode_scan_handlers`, `barcode_menu_tiles`); +- the barcode input service (hardware/keyboard wedge), audio/vibration + feedback, an ORM/API wrapper and shared hooks. + +Install a feature module on top — such as `barcode_stock` — to get actual +warehouse operations. diff --git a/barcode_scanner/readme/USAGE.md b/barcode_scanner/readme/USAGE.md new file mode 100644 index 0000000..783a25d --- /dev/null +++ b/barcode_scanner/readme/USAGE.md @@ -0,0 +1,59 @@ +``barcode_scanner`` is a framework and is not useful on its own: install a +feature module such as ``barcode_stock`` to get actual operations. + +To build your own feature module, register your pieces into the framework +registries — no change to the base module is required. + +Register a screen: + +```js +import {barcodeScreens} from "@barcode_scanner/js/registries"; + +barcodeScreens.add("my_screen", {component: MyScreen}); +``` + +Register a scan handler (tried in ``sequence`` order; the first that returns a +truthy value consumes the scan): + +```js +import {barcodeScanHandlers} from "@barcode_scanner/js/registries"; + +barcodeScanHandlers.add("my_handler", { + async handle(barcode, parsed, {api, navigate, notify}) { + // return true when handled + }, +}, {sequence: 50}); +``` + +Register a home-screen tile: + +```js +import {barcodeMenuTiles} from "@barcode_scanner/js/registries"; + +barcodeMenuTiles.add("my_tile", { + label: "My Screen", + icon: "fa-cube", + action: ({navigate}) => navigate("my_screen"), +}, {sequence: 50}); +``` + +Register a barcode parser (tried in ``sequence`` order before the built-in +EAN13 one; return ``null`` to let the next parser try): + +```js +import {barcodeParsers} from "@barcode_scanner/js/registries"; + +barcodeParsers.add("my_symbology", (barcode) => { + // return {type: "...", value: "", ...} or null +}, {sequence: 20}); +``` + +Register a startup task, for data your module needs before the first scan +(``barcode_gs1`` reads its nomenclature this way). Tasks are awaited when the +app opens; one that fails is logged and skipped: + +```js +import {barcodeStartupTasks} from "@barcode_scanner/js/registries"; + +barcodeStartupTasks.add("my_config", (env) => loadMyConfig(env.services.orm)); +``` diff --git a/barcode_scanner/static/description/icon.png b/barcode_scanner/static/description/icon.png new file mode 100644 index 0000000..3a0328b Binary files /dev/null and b/barcode_scanner/static/description/icon.png differ diff --git a/barcode_scanner/static/description/index.html b/barcode_scanner/static/description/index.html new file mode 100644 index 0000000..e97e35d --- /dev/null +++ b/barcode_scanner/static/description/index.html @@ -0,0 +1,500 @@ + + + + + +Barcode Scanner + + + +
+ + + +Odoo Community Association + +
+

Barcode Scanner

+ +

Beta License: AGPL-3 OCA/barcode-interface Translate me on Weblate Try me on Runboat

+

This module is the base framework of the Barcode suite: a fast, +full-screen scanning application shell for Odoo that other modules +extend.

+

It ships no business logic of its own. Instead it exposes the building +blocks feature modules plug into, the same way Odoo widgets +self-register:

+
    +
  • a full-screen client action and a lightweight in-app router;
  • +
  • registries for screens, scan handlers and home-screen menu tiles +(barcode_screens, barcode_scan_handlers, +barcode_menu_tiles);
  • +
  • the barcode input service (hardware/keyboard wedge), audio/vibration +feedback, an ORM/API wrapper and shared hooks.
  • +
+

Install a feature module on top — such as barcode_stock — to get +actual warehouse operations.

+

Table of contents

+ +
+

Usage

+

barcode_scanner is a framework and is not useful on its own: install +a feature module such as barcode_stock to get actual operations.

+

To build your own feature module, register your pieces into the +framework registries — no change to the base module is required.

+

Register a screen:

+
+import {barcodeScreens} from "@barcode_scanner/js/registries";
+
+barcodeScreens.add("my_screen", {component: MyScreen});
+
+

Register a scan handler (tried in sequence order; the first that +returns a truthy value consumes the scan):

+
+import {barcodeScanHandlers} from "@barcode_scanner/js/registries";
+
+barcodeScanHandlers.add("my_handler", {
+    async handle(barcode, parsed, {api, navigate, notify}) {
+        // return true when handled
+    },
+}, {sequence: 50});
+
+

Register a home-screen tile:

+
+import {barcodeMenuTiles} from "@barcode_scanner/js/registries";
+
+barcodeMenuTiles.add("my_tile", {
+    label: "My Screen",
+    icon: "fa-cube",
+    action: ({navigate}) => navigate("my_screen"),
+}, {sequence: 50});
+
+

Register a barcode parser (tried in sequence order before the +built-in EAN13 one; return null to let the next parser try):

+
+import {barcodeParsers} from "@barcode_scanner/js/registries";
+
+barcodeParsers.add("my_symbology", (barcode) => {
+    // return {type: "...", value: "<product code>", ...} or null
+}, {sequence: 20});
+
+

Register a startup task, for data your module needs before the first +scan (barcode_gs1 reads its nomenclature this way). Tasks are +awaited when the app opens; one that fails is logged and skipped:

+
+import {barcodeStartupTasks} from "@barcode_scanner/js/registries";
+
+barcodeStartupTasks.add("my_config", (env) => loadMyConfig(env.services.orm));
+
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Binhex
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

szalatyzuzanna

+

This module is part of the OCA/barcode-interface project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/barcode_scanner/static/src/img/icon_barcode.svg b/barcode_scanner/static/src/img/icon_barcode.svg new file mode 100644 index 0000000..62b0945 --- /dev/null +++ b/barcode_scanner/static/src/img/icon_barcode.svg @@ -0,0 +1,65 @@ + + diff --git a/barcode_scanner/static/src/js/api.esm.js b/barcode_scanner/static/src/js/api.esm.js new file mode 100644 index 0000000..b4c18be --- /dev/null +++ b/barcode_scanner/static/src/js/api.esm.js @@ -0,0 +1,133 @@ +import {registry} from "@web/core/registry"; +import {_t} from "@web/core/l10n/translation"; + +// Server exceptions whose message is safe to show to operators. Anything +// else (real internal errors) falls back to a generic message so the PDA +// never displays raw Python tracebacks (which the RPC layer puts in +// error.data.debug, never read here). +// MissingError is included because the record is gone (operator can reload). +// AccessDenied is excluded because it signals authentication/session failure +// (generic message plus login flow, no raw detail). +const USER_ERROR_NAMES = new Set([ + "odoo.exceptions.UserError", + "odoo.exceptions.ValidationError", + "odoo.exceptions.AccessError", + "odoo.exceptions.MissingError", +]); + +export function errorMessage(error, fallback) { + const message = error?.data?.message; + return USER_ERROR_NAMES.has(error?.data?.name) && + typeof message === "string" && + message + ? message + : fallback; +} + +export const barcodeApiService = { + dependencies: ["orm", "notification", "dialog"], + start(env, {orm, notification, dialog}) { + return { + async call(model, method, args = [], kwargs = {}) { + try { + return await orm.call(model, method, args, kwargs); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while calling the server.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async searchRead(model, domain, fields, options = {}) { + try { + return await orm.searchRead(model, domain, fields, options); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while fetching data.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async readGroup(model, domain, fields, groupby, options = {}) { + try { + return await orm.readGroup(model, domain, fields, groupby, options); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while grouping data.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async read(model, ids, fields) { + try { + return await orm.read(model, ids, fields); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while reading records.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async write(model, ids, data) { + try { + return await orm.write(model, ids, data); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while saving records.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async create(model, data) { + try { + return await orm.create(model, data); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while creating records.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + async unlink(model, ids) { + try { + return await orm.unlink(model, ids); + } catch (error) { + const message = errorMessage( + error, + _t("An error occurred while deleting records.") + ); + notification.add(message, {type: "danger"}); + throw error; + } + }, + + notify(message, options = {}) { + notification.add(message, options); + }, + + openDialog(dialogClass, props = {}) { + dialog.add(dialogClass, props); + }, + }; + }, +}; + +registry.category("services").add("barcodeApi", barcodeApiService); diff --git a/barcode_scanner/static/src/js/app.esm.js b/barcode_scanner/static/src/js/app.esm.js new file mode 100644 index 0000000..2a4467b --- /dev/null +++ b/barcode_scanner/static/src/js/app.esm.js @@ -0,0 +1,66 @@ +import {Component, onMounted, onWillStart, onWillUnmount, useState} from "@odoo/owl"; +import {registry} from "@web/core/registry"; +import {useService} from "@web/core/utils/hooks"; + +import "@barcode_scanner/js/services/feedback_service.esm"; + +import { + barcodeAppWidgets, + barcodeScreens, + barcodeStartupTasks, +} from "@barcode_scanner/js/registries.esm"; + +export class BarcodeScannerApp extends Component { + setup() { + this.barcode = useService("barcodeScannerBarcode"); + this.feedback = useState(useService("barcodeScannerFeedback")); + this.dialog = useService("dialog"); + this.router = useState(useService("barcodeRouter")); + if (!this.router.currentRoute) { + this.router.navigate("main"); + } + onWillStart(() => this.runStartupTasks()); + // Capture scans (incl. Android IME/soft-input PDAs) only while the app + // is open, so it never steals focus on the rest of the back office. + onMounted(() => this.barcode.activate()); + onWillUnmount(() => this.barcode.deactivate()); + } + + /** + * Let the installed feature modules warm up before the first scan. One + * failing task (a missing record, no access) must not keep the app closed, + * so each is caught on its own. + */ + async runStartupTasks() { + await Promise.all( + barcodeStartupTasks.getAll().map(async (task) => { + try { + await task(this.env); + } catch (error) { + console.warn("Barcode: a startup task failed", error); + } + }) + ); + } + + get currentScreen() { + const name = this.router.currentRoute?.name; + return name ? barcodeScreens.get(name, null) : null; + } + + get currentScreenProps() { + const entry = this.currentScreen; + const params = this.router.routeParams || {}; + const navigate = this.router.navigate.bind(this.router); + const extra = entry && entry.props ? entry.props(params) : {params}; + return {navigate, ...extra}; + } + + get appWidgets() { + return barcodeAppWidgets.getAll(); + } +} + +BarcodeScannerApp.template = "barcode_scanner.App"; + +registry.category("actions").add("barcode_scanner_app", BarcodeScannerApp); diff --git a/barcode_scanner/static/src/js/barcode.esm.js b/barcode_scanner/static/src/js/barcode.esm.js new file mode 100644 index 0000000..6059a81 --- /dev/null +++ b/barcode_scanner/static/src/js/barcode.esm.js @@ -0,0 +1,408 @@ +import {EventBus} from "@odoo/owl"; +import {registry} from "@web/core/registry"; +import {session} from "@web/session"; +import {parseBarcode} from "@barcode_scanner/js/barcode_parser.esm"; + +/** + * Barcode capture for the scanning SPA, built for the Munbyn Android PDAs used + * by Donaldson and Spartan. + * + * These PDAs deliver every laser scan through the Android input method (IME), + * not as real key events: each keystroke arrives as `keydown key="Unidentified"` + * (keyCode 0/229) and the actual code is committed as TEXT into the focused + * field's `.value`. So we keep a hidden, focused and read the scan from + * its `.value` on the terminating Enter/Tab (or shortly after the bulk `input` + * event for scanners that send no Enter). + * + * The input must STAY a plain editable field: anything that hides the Android + * soft keyboard for good -- `inputmode="none"` OR the VirtualKeyboard API -- + * also closes the IME composition session on this hardware, after which the + * scanner reads only once per Chrome session (proven on-device, see the + * barcode-pda-scanner-ime-only note). To keep the laser reading on every screen + * without the soft keyboard popping up, we focus() the field while it is + * briefly `readonly` and restore it to editable right after: a readonly field + * never raises the soft keyboard on Android Chrome, yet it stays focused and + * connected to the IME the whole time. Flip HIDE_SOFT_KEYBOARD below to false + * if a PDA stops reading after the first scan (its IME only injects while the + * keyboard is actually up). + * + * A permanent on-screen "scan feedback" toast shows the full code the PDA just + * read, then fades, so a misread or a dead scan is visible instead of silent. + */ + +// GS1 FNC1 group separator (the real control char the parser splits on). +const GS1_FNC1 = String.fromCharCode(29); + +// Scanners / the Android IME sometimes deliver the FNC1 separator not as the +// real 0x1D control char but as its visible Unicode symbol (U+241D), a nearby +// separator symbol, or a replacement box (U+FFFD). Normalise all back to 0x1D so +// the GS1 parser detects the barcode as GS1 and splits the AIs. +const GS1_SEPARATOR_VARIANTS = /[␝␞␟�]/g; +function normalizeGs1Separators(str) { + return str.replace(GS1_SEPARATOR_VARIANTS, GS1_FNC1); +} + +// For the on-screen feedback: render control chars (incl. the GS1 separator), +// the "control pictures" block and the replacement box as a visible middot, so +// the whole scanned code stays readable. +const NON_PRINTABLE = /[\x00-\x1f\x7f␀-␿�]/g; + +function isEditable(element) { + if (!element || element.nodeType !== 1) { + return false; + } + return ( + element.matches('input, textarea, select, [contenteditable="true"]') || + element.isContentEditable + ); +} + +// Real interactive targets the user is deliberately operating: we must not steal +// focus from these, so their own keyboard shows and they can type -- and native +// pop-ups (a