@eifi1/ui-kit 0.11.0 → 0.12.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.
- package/README.md +57 -32
- package/dist/chart.d.ts +3 -1
- package/dist/chart.js +2 -0
- package/dist/chart.js.map +1 -1
- package/dist/components/account-settings-labels.d.ts +105 -0
- package/dist/components/account-settings-labels.js +81 -0
- package/dist/components/account-settings-labels.js.map +1 -0
- package/dist/components/account-settings.d.ts +36 -52
- package/dist/components/account-settings.js +44 -8
- package/dist/components/account-settings.js.map +1 -1
- package/dist/components/amount-input.d.ts +24 -4
- package/dist/components/amount-input.js +5 -2
- package/dist/components/amount-input.js.map +1 -1
- package/dist/components/authed-image.d.ts +68 -0
- package/dist/components/authed-image.js +117 -0
- package/dist/components/authed-image.js.map +1 -0
- package/dist/components/breadcrumbs.d.ts +2 -1
- package/dist/components/breadcrumbs.js +5 -2
- package/dist/components/breadcrumbs.js.map +1 -1
- package/dist/components/button-group.d.ts +51 -2
- package/dist/components/button-group.js +5 -1
- package/dist/components/button-group.js.map +1 -1
- package/dist/components/calculator.d.ts +15 -4
- package/dist/components/calendar-heatmap.d.ts +41 -3
- package/dist/components/calendar-heatmap.js +48 -7
- package/dist/components/calendar-heatmap.js.map +1 -1
- package/dist/components/chip.d.ts +2 -1
- package/dist/components/chip.js +6 -3
- package/dist/components/chip.js.map +1 -1
- package/dist/components/choice-card.d.ts +35 -3
- package/dist/components/choice-card.js +83 -25
- package/dist/components/choice-card.js.map +1 -1
- package/dist/components/combobox-core.js +2 -0
- package/dist/components/combobox-core.js.map +1 -1
- package/dist/components/combobox.d.ts +2 -2
- package/dist/components/combobox.js +31 -9
- package/dist/components/combobox.js.map +1 -1
- package/dist/components/copy-button.d.ts +51 -70
- package/dist/components/copy-button.js +1 -1
- package/dist/components/copy-button.js.map +1 -1
- package/dist/components/data-table-cells.d.ts +63 -0
- package/dist/components/data-table-cells.js +64 -0
- package/dist/components/data-table-cells.js.map +1 -0
- package/dist/components/data-table-filter-popover.d.ts +1 -1
- package/dist/components/data-table-filters.d.ts +1 -1
- package/dist/components/data-table-filters.js +31 -1
- package/dist/components/data-table-filters.js.map +1 -1
- package/dist/components/data-table-labels.d.ts +13 -0
- package/dist/components/data-table-labels.js +8 -1
- package/dist/components/data-table-labels.js.map +1 -1
- package/dist/components/data-table.d.ts +1 -1
- package/dist/components/data-table.js +102 -12
- package/dist/components/data-table.js.map +1 -1
- package/dist/components/date-picker.d.ts +3 -1
- package/dist/components/date-picker.js +15 -2
- package/dist/components/date-picker.js.map +1 -1
- package/dist/components/description-list.d.ts +23 -3
- package/dist/components/description-list.js +23 -3
- package/dist/components/description-list.js.map +1 -1
- package/dist/components/dialog-frame.d.ts +1 -0
- package/dist/components/entity-combobox.d.ts +1 -1
- package/dist/components/entity-combobox.js +18 -3
- package/dist/components/entity-combobox.js.map +1 -1
- package/dist/components/error-boundary.d.ts +92 -0
- package/dist/components/error-boundary.js +111 -0
- package/dist/components/error-boundary.js.map +1 -0
- package/dist/components/field.d.ts +30 -4
- package/dist/components/field.js +40 -17
- package/dist/components/field.js.map +1 -1
- package/dist/components/file-button.d.ts +50 -208
- package/dist/components/file-dropzone.d.ts +49 -2
- package/dist/components/floating-panel.d.ts +13 -4
- package/dist/components/floating-panel.js +7 -3
- package/dist/components/floating-panel.js.map +1 -1
- package/dist/components/form-actions.d.ts +53 -0
- package/dist/components/form-actions.js +98 -0
- package/dist/components/form-actions.js.map +1 -0
- package/dist/components/image-grid.d.ts +67 -0
- package/dist/components/image-grid.js +112 -0
- package/dist/components/image-grid.js.map +1 -0
- package/dist/components/lightbox.d.ts +104 -0
- package/dist/components/lightbox.js +229 -0
- package/dist/components/lightbox.js.map +1 -0
- package/dist/components/line-items.d.ts +114 -0
- package/dist/components/line-items.js +238 -0
- package/dist/components/line-items.js.map +1 -0
- package/dist/components/list.d.ts +23 -2
- package/dist/components/list.js +32 -13
- package/dist/components/list.js.map +1 -1
- package/dist/components/loading-state.d.ts +24 -0
- package/dist/components/loading-state.js +32 -0
- package/dist/components/loading-state.js.map +1 -0
- package/dist/components/menu-item.d.ts +2 -1
- package/dist/components/menu-item.js +5 -1
- package/dist/components/menu-item.js.map +1 -1
- package/dist/components/mini-calendar.d.ts +13 -1
- package/dist/components/mini-calendar.js +46 -2
- package/dist/components/mini-calendar.js.map +1 -1
- package/dist/components/modal.d.ts +20 -1
- package/dist/components/modal.js +26 -2
- package/dist/components/modal.js.map +1 -1
- package/dist/components/month-picker.d.ts +21 -1
- package/dist/components/month-picker.js +139 -26
- package/dist/components/month-picker.js.map +1 -1
- package/dist/components/multi-entity-combobox.d.ts +1 -1
- package/dist/components/multi-entity-combobox.js +19 -3
- package/dist/components/multi-entity-combobox.js.map +1 -1
- package/dist/components/nav-pills.d.ts +2 -1
- package/dist/components/nav-pills.js +5 -1
- package/dist/components/nav-pills.js.map +1 -1
- package/dist/components/number-field.d.ts +24 -5
- package/dist/components/number-field.js +5 -7
- package/dist/components/number-field.js.map +1 -1
- package/dist/components/number-input.d.ts +25 -5
- package/dist/components/number-input.js +7 -1
- package/dist/components/number-input.js.map +1 -1
- package/dist/components/numpad-sheet.d.ts +15 -4
- package/dist/components/passkeys-setting.d.ts +67 -0
- package/dist/components/passkeys-setting.js +214 -0
- package/dist/components/passkeys-setting.js.map +1 -0
- package/dist/components/picker-sheet.js +2 -1
- package/dist/components/picker-sheet.js.map +1 -1
- package/dist/components/pie-chart-labels.d.ts +20 -0
- package/dist/components/pie-chart-labels.js +12 -0
- package/dist/components/pie-chart-labels.js.map +1 -0
- package/dist/components/pie-chart.d.ts +101 -0
- package/dist/components/pie-chart.js +283 -0
- package/dist/components/pie-chart.js.map +1 -0
- package/dist/components/progress-bar.d.ts +44 -3
- package/dist/components/progress-bar.js +58 -22
- package/dist/components/progress-bar.js.map +1 -1
- package/dist/components/qr-code.d.ts +34 -0
- package/dist/components/qr-code.js +57 -0
- package/dist/components/qr-code.js.map +1 -0
- package/dist/components/search-field.d.ts +15 -3
- package/dist/components/search-field.js +5 -1
- package/dist/components/search-field.js.map +1 -1
- package/dist/components/settings-fields.d.ts +49 -1
- package/dist/components/signed-amount.d.ts +121 -0
- package/dist/components/signed-amount.js +152 -0
- package/dist/components/signed-amount.js.map +1 -0
- package/dist/components/skeleton.d.ts +10 -3
- package/dist/components/skeleton.js +12 -2
- package/dist/components/skeleton.js.map +1 -1
- package/dist/components/stat-tile.d.ts +2 -1
- package/dist/components/stat-tile.js +5 -2
- package/dist/components/stat-tile.js.map +1 -1
- package/dist/components/table.d.ts +39 -5
- package/dist/components/table.js +45 -11
- package/dist/components/table.js.map +1 -1
- package/dist/components/text-link.d.ts +176 -0
- package/dist/components/text-link.js +109 -0
- package/dist/components/text-link.js.map +1 -0
- package/dist/components/time-input.d.ts +49 -1
- package/dist/components/toggle-group.d.ts +13 -0
- package/dist/components/toggle-group.js +35 -6
- package/dist/components/toggle-group.js.map +1 -1
- package/dist/components/toggle-legend.d.ts +36 -3
- package/dist/components/toggle-legend.js +49 -0
- package/dist/components/toggle-legend.js.map +1 -1
- package/dist/components/ui.d.ts +51 -779
- package/dist/components/ui.js +215 -23
- package/dist/components/ui.js.map +1 -1
- package/dist/components/use-table-state.d.ts +3 -52
- package/dist/components/use-table-state.js +128 -30
- package/dist/components/use-table-state.js.map +1 -1
- package/dist/{data-table-filters-Dh9uF_S-.d.ts → data-table-Drun8Fk9.d.ts} +305 -61
- package/dist/data-table.d.ts +2 -1
- package/dist/data-table.js +5 -1
- package/dist/data-table.js.map +1 -1
- package/dist/feedback/feedback-dialog.d.ts +26 -3
- package/dist/feedback/feedback-dialog.js +16 -2
- package/dist/feedback/feedback-dialog.js.map +1 -1
- package/dist/feedback/feedback-thread.d.ts +133 -0
- package/dist/feedback/feedback-thread.js +237 -0
- package/dist/feedback/feedback-thread.js.map +1 -0
- package/dist/feedback.d.ts +2 -1
- package/dist/feedback.js +1 -0
- package/dist/feedback.js.map +1 -1
- package/dist/hooks/use-authed-src.d.ts +71 -0
- package/dist/hooks/use-authed-src.js +63 -0
- package/dist/hooks/use-authed-src.js.map +1 -0
- package/dist/hooks/use-file-drop.d.ts +49 -2
- package/dist/hooks/use-hotkey.d.ts +59 -0
- package/dist/hooks/use-hotkey.js +105 -0
- package/dist/hooks/use-hotkey.js.map +1 -0
- package/dist/hooks/use-search-param-state.d.ts +83 -0
- package/dist/hooks/use-search-param-state.js +94 -0
- package/dist/hooks/use-search-param-state.js.map +1 -0
- package/dist/i18n/defaults.d.ts +15 -4
- package/dist/i18n/defaults.js +31 -1
- package/dist/i18n/defaults.js.map +1 -1
- package/dist/i18n/kit-labels.d.ts +46 -312
- package/dist/i18n/kit-labels.js +20 -5
- package/dist/i18n/kit-labels.js.map +1 -1
- package/dist/i18n/locales/de-CH-informal.d.ts +15 -4
- package/dist/i18n/locales/de-CH.d.ts +15 -4
- package/dist/i18n/locales/de-informal.d.ts +15 -4
- package/dist/i18n/locales/de.d.ts +15 -4
- package/dist/i18n/locales/de.js +156 -3
- package/dist/i18n/locales/de.js.map +1 -1
- package/dist/i18n/locales/es.d.ts +15 -4
- package/dist/i18n/locales/es.js +153 -3
- package/dist/i18n/locales/es.js.map +1 -1
- package/dist/i18n/locales/fr.d.ts +15 -4
- package/dist/i18n/locales/fr.js +153 -3
- package/dist/i18n/locales/fr.js.map +1 -1
- package/dist/i18n/locales/hu.d.ts +15 -4
- package/dist/i18n/locales/hu.js +153 -3
- package/dist/i18n/locales/hu.js.map +1 -1
- package/dist/i18n/locales/it.d.ts +15 -4
- package/dist/i18n/locales/it.js +153 -3
- package/dist/i18n/locales/it.js.map +1 -1
- package/dist/i18n/locales/zh.d.ts +15 -4
- package/dist/i18n/locales/zh.js +153 -3
- package/dist/i18n/locales/zh.js.map +1 -1
- package/dist/index.d.ts +37 -17
- package/dist/index.js +40 -1
- package/dist/index.js.map +1 -1
- package/dist/kit-labels-v3biUF1L.d.ts +1645 -0
- package/dist/lib/format.d.ts +144 -0
- package/dist/lib/format.js +149 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/qr-encode.d.ts +44 -0
- package/dist/lib/qr-encode.js +338 -0
- package/dist/lib/qr-encode.js.map +1 -0
- package/dist/rhf/fields.d.ts +241 -0
- package/dist/rhf/fields.js +601 -0
- package/dist/rhf/fields.js.map +1 -0
- package/dist/rhf/form.d.ts +49 -1
- package/dist/rhf/line-items.d.ts +31 -0
- package/dist/rhf/line-items.js +59 -0
- package/dist/rhf/line-items.js.map +1 -0
- package/dist/rhf.d.ts +61 -1
- package/dist/rhf.js +2 -0
- package/dist/rhf.js.map +1 -1
- package/dist/shell/app-shell.d.ts +99 -2
- package/dist/shell/app-shell.js +58 -3
- package/dist/shell/app-shell.js.map +1 -1
- package/dist/shell/auth-layout.d.ts +74 -0
- package/dist/shell/auth-layout.js +97 -0
- package/dist/shell/auth-layout.js.map +1 -0
- package/dist/shell/top-bar-brand.d.ts +81 -0
- package/dist/shell/top-bar-brand.js +47 -0
- package/dist/shell/top-bar-brand.js.map +1 -0
- package/dist/shell/topbar-action-menu.d.ts +12 -1
- package/dist/shell/topbar-action-menu.js +16 -1
- package/dist/shell/topbar-action-menu.js.map +1 -1
- package/dist/shell.d.ts +56 -1
- package/dist/shell.js +2 -0
- package/dist/shell.js.map +1 -1
- package/dist/wizard/stepper-nav.d.ts +48 -1
- package/dist/wizard/wizard-context.d.ts +24 -1
- package/dist/wizard/wizard-context.js +19 -2
- package/dist/wizard/wizard-context.js.map +1 -1
- package/dist/wizard/wizard-step.d.ts +20 -5
- package/dist/wizard/wizard-step.js +16 -3
- package/dist/wizard/wizard-step.js.map +1 -1
- package/dist/wizard.d.ts +50 -3
- package/package.json +1 -1
- package/src/chart.ts +4 -0
- package/src/components/account-settings-labels.ts +190 -0
- package/src/components/account-settings.tsx +110 -63
- package/src/components/amount-input.tsx +13 -1
- package/src/components/authed-image.tsx +190 -0
- package/src/components/breadcrumbs.tsx +8 -2
- package/src/components/button-group.tsx +7 -2
- package/src/components/calendar-heatmap.tsx +105 -10
- package/src/components/chip.tsx +8 -4
- package/src/components/choice-card.tsx +136 -25
- package/src/components/combobox-core.tsx +5 -0
- package/src/components/combobox.tsx +31 -9
- package/src/components/copy-button.tsx +3 -2
- package/src/components/data-table-cells.tsx +119 -0
- package/src/components/data-table-filters.ts +77 -0
- package/src/components/data-table-labels.ts +23 -0
- package/src/components/data-table.tsx +224 -13
- package/src/components/date-picker.tsx +19 -3
- package/src/components/description-list.tsx +52 -3
- package/src/components/entity-combobox.tsx +18 -3
- package/src/components/error-boundary.tsx +216 -0
- package/src/components/field.tsx +56 -5
- package/src/components/floating-panel.tsx +20 -7
- package/src/components/form-actions.tsx +191 -0
- package/src/components/image-grid.tsx +181 -0
- package/src/components/lightbox.tsx +374 -0
- package/src/components/line-items.tsx +385 -0
- package/src/components/list.tsx +65 -16
- package/src/components/loading-state.tsx +47 -0
- package/src/components/menu-item.tsx +7 -2
- package/src/components/mini-calendar.tsx +72 -3
- package/src/components/modal.tsx +61 -3
- package/src/components/month-picker.tsx +181 -26
- package/src/components/multi-entity-combobox.tsx +19 -3
- package/src/components/nav-pills.tsx +8 -1
- package/src/components/number-field.tsx +15 -10
- package/src/components/number-input.tsx +16 -1
- package/src/components/passkeys-setting.tsx +331 -0
- package/src/components/picker-sheet.tsx +7 -1
- package/src/components/pie-chart-labels.ts +32 -0
- package/src/components/pie-chart.tsx +479 -0
- package/src/components/progress-bar.tsx +129 -19
- package/src/components/qr-code.tsx +83 -0
- package/src/components/search-field.tsx +20 -4
- package/src/components/signed-amount.tsx +286 -0
- package/src/components/skeleton.tsx +21 -3
- package/src/components/stat-tile.tsx +8 -2
- package/src/components/table.tsx +102 -13
- package/src/components/text-link.tsx +248 -0
- package/src/components/toggle-group.tsx +58 -10
- package/src/components/toggle-legend.tsx +87 -2
- package/src/components/ui.tsx +426 -36
- package/src/components/use-table-state.ts +265 -33
- package/src/data-table.ts +10 -0
- package/src/feedback/feedback-dialog.tsx +42 -3
- package/src/feedback/feedback-thread.tsx +434 -0
- package/src/feedback.ts +1 -0
- package/src/hooks/use-authed-src.ts +161 -0
- package/src/hooks/use-hotkey.ts +172 -0
- package/src/hooks/use-search-param-state.ts +187 -0
- package/src/i18n/defaults.ts +30 -0
- package/src/i18n/kit-labels.tsx +102 -8
- package/src/i18n/locales/de.ts +153 -0
- package/src/i18n/locales/es.ts +150 -0
- package/src/i18n/locales/fr.ts +153 -0
- package/src/i18n/locales/hu.ts +151 -0
- package/src/i18n/locales/it.ts +151 -0
- package/src/i18n/locales/zh.ts +150 -0
- package/src/index.ts +59 -0
- package/src/lib/format.ts +325 -0
- package/src/lib/qr-encode.ts +420 -0
- package/src/rhf/fields.tsx +1054 -0
- package/src/rhf/line-items.tsx +130 -0
- package/src/rhf.ts +6 -0
- package/src/shell/app-shell.tsx +147 -2
- package/src/shell/auth-layout.tsx +190 -0
- package/src/shell/top-bar-brand.tsx +76 -0
- package/src/shell/topbar-action-menu.tsx +21 -0
- package/src/shell.ts +2 -0
- package/src/wizard/wizard-context.tsx +41 -1
- package/src/wizard/wizard-step.tsx +43 -6
- package/tokens.css +12 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A QR code encoder: text in, a square of dark / light modules out (ISO/IEC 18004).
|
|
3
|
+
*
|
|
4
|
+
* WHY IT IS IN THE KIT rather than a dependency. `TwoFactorSetting` needs one QR code —
|
|
5
|
+
* an `otpauth://` URI, around 100–200 bytes — and every app that shows it was pulling
|
|
6
|
+
* in `qrcode.react` plus `react-dom/server` to render that one code to a static SVG
|
|
7
|
+
* string, then base64-encoding it so the kit could draw it back as an `<img>`.
|
|
8
|
+
* A dependency here would land in all three apps whether they show 2FA or not; an
|
|
9
|
+
* optional peer would mean a lazy import and a loading state for something that is
|
|
10
|
+
* a few hundred lines of arithmetic. So it is those lines: pure, no imports, no module
|
|
11
|
+
* state beyond two constant tables, safe under `sideEffects: false`, and
|
|
12
|
+
* tree-shaken out of every app that never renders a {@link QrCode}.
|
|
13
|
+
*
|
|
14
|
+
* Scope, chosen for what the kit renders: BYTE mode only (UTF-8), all 40 versions,
|
|
15
|
+
* all four error-correction levels, automatic version and mask selection. Numeric,
|
|
16
|
+
* alphanumeric and Kanji modes are denser for their alphabets but would not shrink an
|
|
17
|
+
* otpauth URI (lower-case letters are not in the alphanumeric set). The algorithm and
|
|
18
|
+
* its penalty rules follow the reference implementation by Project Nayuki (MIT),
|
|
19
|
+
* which is also what `qrcode.react` wraps — so for the same input, level and boost
|
|
20
|
+
* the modules match that library's output exactly (the test suite pins a matrix).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type QrErrorCorrection = "L" | "M" | "Q" | "H";
|
|
24
|
+
|
|
25
|
+
export interface QrEncodeOptions {
|
|
26
|
+
/** How much of the code may be damaged and still read. Default `"M"` (~15 %). */
|
|
27
|
+
errorCorrection?: QrErrorCorrection;
|
|
28
|
+
/** Raise the level as far as the chosen version still fits, for free. Default true. */
|
|
29
|
+
boostErrorCorrection?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface QrMatrix {
|
|
33
|
+
/** 1–40. */
|
|
34
|
+
version: number;
|
|
35
|
+
/** Modules per side: `version * 4 + 17`. */
|
|
36
|
+
size: number;
|
|
37
|
+
/** The level actually used — higher than asked when boosted. */
|
|
38
|
+
errorCorrection: QrErrorCorrection;
|
|
39
|
+
/** 0–7. */
|
|
40
|
+
mask: number;
|
|
41
|
+
/** `modules[y][x]`, true = dark. No quiet zone: the renderer adds its own. */
|
|
42
|
+
modules: boolean[][];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const LEVELS: QrErrorCorrection[] = ["L", "M", "Q", "H"];
|
|
46
|
+
/** The two format-information bits per level (not in ordinal order: M is 00). */
|
|
47
|
+
const FORMAT_BITS: Record<QrErrorCorrection, number> = { L: 1, M: 0, Q: 3, H: 2 };
|
|
48
|
+
|
|
49
|
+
// Index 0 is padding; rows are L, M, Q, H.
|
|
50
|
+
const ECC_CODEWORDS_PER_BLOCK: readonly (readonly number[])[] = [
|
|
51
|
+
[-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
|
52
|
+
[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
|
|
53
|
+
[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
|
54
|
+
[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
|
55
|
+
];
|
|
56
|
+
const NUM_ERROR_CORRECTION_BLOCKS: readonly (readonly number[])[] = [
|
|
57
|
+
[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
|
|
58
|
+
[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
|
|
59
|
+
[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
|
|
60
|
+
[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81],
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/** Encode `text` (as UTF-8 bytes). Throws a `RangeError` past version 40's capacity. */
|
|
64
|
+
export function encodeQr(text: string, options: QrEncodeOptions = {}): QrMatrix {
|
|
65
|
+
const bytes = Array.from(new TextEncoder().encode(text));
|
|
66
|
+
let level = LEVELS.indexOf(options.errorCorrection ?? "M");
|
|
67
|
+
|
|
68
|
+
// The smallest version that holds the data at the requested level.
|
|
69
|
+
let version = 1;
|
|
70
|
+
let usedBits: number;
|
|
71
|
+
for (; ; version++) {
|
|
72
|
+
const countBits = version <= 9 ? 8 : 16;
|
|
73
|
+
usedBits = bytes.length < 1 << countBits ? 4 + countBits + bytes.length * 8 : Infinity;
|
|
74
|
+
if (usedBits <= dataCodewords(version, level) * 8) break;
|
|
75
|
+
if (version >= 40) throw new RangeError("QR data too long");
|
|
76
|
+
}
|
|
77
|
+
if (options.boostErrorCorrection ?? true) {
|
|
78
|
+
for (let higher = level + 1; higher < LEVELS.length; higher++) {
|
|
79
|
+
if (usedBits <= dataCodewords(version, higher) * 8) level = higher;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Mode, count, data; then terminator, byte padding and the 0xEC/0x11 fill.
|
|
84
|
+
const bits: number[] = [];
|
|
85
|
+
const append = (value: number, length: number) => {
|
|
86
|
+
for (let i = length - 1; i >= 0; i--) bits.push((value >>> i) & 1);
|
|
87
|
+
};
|
|
88
|
+
append(0b0100, 4);
|
|
89
|
+
append(bytes.length, version <= 9 ? 8 : 16);
|
|
90
|
+
for (const b of bytes) append(b, 8);
|
|
91
|
+
const capacity = dataCodewords(version, level) * 8;
|
|
92
|
+
append(0, Math.min(4, capacity - bits.length));
|
|
93
|
+
append(0, (8 - (bits.length % 8)) % 8);
|
|
94
|
+
for (let pad = 0xec; bits.length < capacity; pad ^= 0xec ^ 0x11) append(pad, 8);
|
|
95
|
+
|
|
96
|
+
const data: number[] = [];
|
|
97
|
+
for (let i = 0; i < bits.length; i += 8) {
|
|
98
|
+
let byte = 0;
|
|
99
|
+
for (let j = 0; j < 8; j++) byte = (byte << 1) | bits[i + j];
|
|
100
|
+
data.push(byte);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const grid = new Grid(version, LEVELS[level]);
|
|
104
|
+
grid.drawFunctionPatterns();
|
|
105
|
+
grid.drawCodewords(interleaveWithEcc(data, version, level));
|
|
106
|
+
|
|
107
|
+
let bestMask = 0;
|
|
108
|
+
let bestPenalty = Infinity;
|
|
109
|
+
for (let mask = 0; mask < 8; mask++) {
|
|
110
|
+
grid.applyMask(mask);
|
|
111
|
+
grid.drawFormatBits(mask);
|
|
112
|
+
const penalty = grid.penalty();
|
|
113
|
+
if (penalty < bestPenalty) {
|
|
114
|
+
bestMask = mask;
|
|
115
|
+
bestPenalty = penalty;
|
|
116
|
+
}
|
|
117
|
+
grid.applyMask(mask); // XOR again undoes it
|
|
118
|
+
}
|
|
119
|
+
grid.applyMask(bestMask);
|
|
120
|
+
grid.drawFormatBits(bestMask);
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
version,
|
|
124
|
+
size: grid.size,
|
|
125
|
+
errorCorrection: LEVELS[level],
|
|
126
|
+
mask: bestMask,
|
|
127
|
+
modules: grid.modules,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function rawDataModules(version: number): number {
|
|
132
|
+
let result = (16 * version + 128) * version + 64;
|
|
133
|
+
if (version >= 2) {
|
|
134
|
+
const numAlign = Math.floor(version / 7) + 2;
|
|
135
|
+
result -= (25 * numAlign - 10) * numAlign - 55;
|
|
136
|
+
if (version >= 7) result -= 36;
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function dataCodewords(version: number, level: number): number {
|
|
142
|
+
return (
|
|
143
|
+
Math.floor(rawDataModules(version) / 8) -
|
|
144
|
+
ECC_CODEWORDS_PER_BLOCK[level][version] * NUM_ERROR_CORRECTION_BLOCKS[level][version]
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Split into blocks, append each block's Reed–Solomon codewords, interleave. */
|
|
149
|
+
function interleaveWithEcc(data: number[], version: number, level: number): number[] {
|
|
150
|
+
const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[level][version];
|
|
151
|
+
const eccLen = ECC_CODEWORDS_PER_BLOCK[level][version];
|
|
152
|
+
const rawCodewords = Math.floor(rawDataModules(version) / 8);
|
|
153
|
+
const numShortBlocks = numBlocks - (rawCodewords % numBlocks);
|
|
154
|
+
const shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
|
155
|
+
const divisor = reedSolomonDivisor(eccLen);
|
|
156
|
+
|
|
157
|
+
const blocks: number[][] = [];
|
|
158
|
+
for (let i = 0, k = 0; i < numBlocks; i++) {
|
|
159
|
+
const block = data.slice(k, k + shortBlockLen - eccLen + (i < numShortBlocks ? 0 : 1));
|
|
160
|
+
k += block.length;
|
|
161
|
+
const ecc = reedSolomonRemainder(block, divisor);
|
|
162
|
+
if (i < numShortBlocks) block.push(0); // placeholder, skipped below
|
|
163
|
+
blocks.push(block.concat(ecc));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const result: number[] = [];
|
|
167
|
+
for (let i = 0; i < blocks[0].length; i++) {
|
|
168
|
+
blocks.forEach((block, j) => {
|
|
169
|
+
if (i !== shortBlockLen - eccLen || j >= numShortBlocks) result.push(block[i]);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function gfMultiply(x: number, y: number): number {
|
|
176
|
+
let z = 0;
|
|
177
|
+
for (let i = 7; i >= 0; i--) {
|
|
178
|
+
z = (z << 1) ^ ((z >>> 7) * 0x11d);
|
|
179
|
+
z ^= ((y >>> i) & 1) * x;
|
|
180
|
+
}
|
|
181
|
+
return z;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function reedSolomonDivisor(degree: number): number[] {
|
|
185
|
+
const result = new Array<number>(degree).fill(0);
|
|
186
|
+
result[degree - 1] = 1;
|
|
187
|
+
let root = 1;
|
|
188
|
+
for (let i = 0; i < degree; i++) {
|
|
189
|
+
for (let j = 0; j < degree; j++) {
|
|
190
|
+
result[j] = gfMultiply(result[j], root);
|
|
191
|
+
if (j + 1 < degree) result[j] ^= result[j + 1];
|
|
192
|
+
}
|
|
193
|
+
root = gfMultiply(root, 0x02);
|
|
194
|
+
}
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function reedSolomonRemainder(data: number[], divisor: number[]): number[] {
|
|
199
|
+
const result = new Array<number>(divisor.length).fill(0);
|
|
200
|
+
for (const b of data) {
|
|
201
|
+
const factor = b ^ (result.shift() as number);
|
|
202
|
+
result.push(0);
|
|
203
|
+
divisor.forEach((coef, i) => {
|
|
204
|
+
result[i] ^= gfMultiply(coef, factor);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const bit = (value: number, i: number) => ((value >>> i) & 1) !== 0;
|
|
211
|
+
|
|
212
|
+
class Grid {
|
|
213
|
+
readonly size: number;
|
|
214
|
+
readonly modules: boolean[][];
|
|
215
|
+
private readonly reserved: boolean[][];
|
|
216
|
+
private readonly version: number;
|
|
217
|
+
private readonly level: QrErrorCorrection;
|
|
218
|
+
|
|
219
|
+
constructor(version: number, level: QrErrorCorrection) {
|
|
220
|
+
this.version = version;
|
|
221
|
+
this.level = level;
|
|
222
|
+
this.size = version * 4 + 17;
|
|
223
|
+
this.modules = Array.from({ length: this.size }, () => new Array<boolean>(this.size).fill(false));
|
|
224
|
+
this.reserved = Array.from({ length: this.size }, () => new Array<boolean>(this.size).fill(false));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private set(x: number, y: number, dark: boolean) {
|
|
228
|
+
this.modules[y][x] = dark;
|
|
229
|
+
this.reserved[y][x] = true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
drawFunctionPatterns() {
|
|
233
|
+
const { size } = this;
|
|
234
|
+
for (let i = 0; i < size; i++) {
|
|
235
|
+
this.set(6, i, i % 2 === 0);
|
|
236
|
+
this.set(i, 6, i % 2 === 0);
|
|
237
|
+
}
|
|
238
|
+
this.drawFinder(3, 3);
|
|
239
|
+
this.drawFinder(size - 4, 3);
|
|
240
|
+
this.drawFinder(3, size - 4);
|
|
241
|
+
const align = this.alignmentPositions();
|
|
242
|
+
const n = align.length;
|
|
243
|
+
for (let i = 0; i < n; i++) {
|
|
244
|
+
for (let j = 0; j < n; j++) {
|
|
245
|
+
const corner = (i === 0 && j === 0) || (i === 0 && j === n - 1) || (i === n - 1 && j === 0);
|
|
246
|
+
if (!corner) this.drawAlignment(align[i], align[j]);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
this.drawFormatBits(0); // reserves the area; redrawn once the mask is chosen
|
|
250
|
+
this.drawVersion();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private drawFinder(x: number, y: number) {
|
|
254
|
+
for (let dy = -4; dy <= 4; dy++) {
|
|
255
|
+
for (let dx = -4; dx <= 4; dx++) {
|
|
256
|
+
const dist = Math.max(Math.abs(dx), Math.abs(dy));
|
|
257
|
+
const xx = x + dx;
|
|
258
|
+
const yy = y + dy;
|
|
259
|
+
if (xx >= 0 && xx < this.size && yy >= 0 && yy < this.size) {
|
|
260
|
+
this.set(xx, yy, dist !== 2 && dist !== 4);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private drawAlignment(x: number, y: number) {
|
|
267
|
+
for (let dy = -2; dy <= 2; dy++) {
|
|
268
|
+
for (let dx = -2; dx <= 2; dx++) {
|
|
269
|
+
this.set(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private alignmentPositions(): number[] {
|
|
275
|
+
if (this.version === 1) return [];
|
|
276
|
+
const numAlign = Math.floor(this.version / 7) + 2;
|
|
277
|
+
const step = Math.floor((this.version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4)) * 2;
|
|
278
|
+
const result = [6];
|
|
279
|
+
for (let pos = this.size - 7; result.length < numAlign; pos -= step) result.splice(1, 0, pos);
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
drawFormatBits(mask: number) {
|
|
284
|
+
const data = (FORMAT_BITS[this.level] << 3) | mask;
|
|
285
|
+
let rem = data;
|
|
286
|
+
for (let i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
287
|
+
const bits = ((data << 10) | rem) ^ 0x5412;
|
|
288
|
+
const { size } = this;
|
|
289
|
+
for (let i = 0; i <= 5; i++) this.set(8, i, bit(bits, i));
|
|
290
|
+
this.set(8, 7, bit(bits, 6));
|
|
291
|
+
this.set(8, 8, bit(bits, 7));
|
|
292
|
+
this.set(7, 8, bit(bits, 8));
|
|
293
|
+
for (let i = 9; i < 15; i++) this.set(14 - i, 8, bit(bits, i));
|
|
294
|
+
for (let i = 0; i < 8; i++) this.set(size - 1 - i, 8, bit(bits, i));
|
|
295
|
+
for (let i = 8; i < 15; i++) this.set(8, size - 15 + i, bit(bits, i));
|
|
296
|
+
this.set(8, size - 8, true); // the always-dark module
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private drawVersion() {
|
|
300
|
+
if (this.version < 7) return;
|
|
301
|
+
let rem = this.version;
|
|
302
|
+
for (let i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25);
|
|
303
|
+
const bits = (this.version << 12) | rem;
|
|
304
|
+
for (let i = 0; i < 18; i++) {
|
|
305
|
+
const dark = bit(bits, i);
|
|
306
|
+
const a = this.size - 11 + (i % 3);
|
|
307
|
+
const b = Math.floor(i / 3);
|
|
308
|
+
this.set(a, b, dark);
|
|
309
|
+
this.set(b, a, dark);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** The zig-zag placement: two-module columns, right to left, alternating up and down. */
|
|
314
|
+
drawCodewords(data: number[]) {
|
|
315
|
+
const { size } = this;
|
|
316
|
+
let i = 0;
|
|
317
|
+
for (let right = size - 1; right >= 1; right -= 2) {
|
|
318
|
+
if (right === 6) right = 5; // skip the vertical timing column
|
|
319
|
+
for (let vert = 0; vert < size; vert++) {
|
|
320
|
+
for (let j = 0; j < 2; j++) {
|
|
321
|
+
const x = right - j;
|
|
322
|
+
const upward = ((right + 1) & 2) === 0;
|
|
323
|
+
const y = upward ? size - 1 - vert : vert;
|
|
324
|
+
if (!this.reserved[y][x] && i < data.length * 8) {
|
|
325
|
+
this.modules[y][x] = bit(data[i >>> 3], 7 - (i & 7));
|
|
326
|
+
i++;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
applyMask(mask: number) {
|
|
334
|
+
for (let y = 0; y < this.size; y++) {
|
|
335
|
+
for (let x = 0; x < this.size; x++) {
|
|
336
|
+
if (!this.reserved[y][x] && maskBit(mask, x, y)) this.modules[y][x] = !this.modules[y][x];
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
penalty(): number {
|
|
342
|
+
const { size, modules } = this;
|
|
343
|
+
let result = 0;
|
|
344
|
+
const line = (get: (a: number, b: number) => boolean) => {
|
|
345
|
+
for (let a = 0; a < size; a++) {
|
|
346
|
+
let runColor = false;
|
|
347
|
+
let run = 0;
|
|
348
|
+
const history = [0, 0, 0, 0, 0, 0, 0];
|
|
349
|
+
for (let b = 0; b < size; b++) {
|
|
350
|
+
if (get(a, b) === runColor) {
|
|
351
|
+
run++;
|
|
352
|
+
if (run === 5) result += 3;
|
|
353
|
+
else if (run > 5) result++;
|
|
354
|
+
} else {
|
|
355
|
+
this.addHistory(run, history);
|
|
356
|
+
if (!runColor) result += this.countFinderLike(history) * 40;
|
|
357
|
+
runColor = get(a, b);
|
|
358
|
+
run = 1;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (runColor) {
|
|
362
|
+
this.addHistory(run, history);
|
|
363
|
+
run = 0;
|
|
364
|
+
}
|
|
365
|
+
this.addHistory(run + size, history);
|
|
366
|
+
result += this.countFinderLike(history) * 40;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
line((y, x) => modules[y][x]);
|
|
370
|
+
line((x, y) => modules[y][x]);
|
|
371
|
+
|
|
372
|
+
for (let y = 0; y < size - 1; y++) {
|
|
373
|
+
for (let x = 0; x < size - 1; x++) {
|
|
374
|
+
const c = modules[y][x];
|
|
375
|
+
if (c === modules[y][x + 1] && c === modules[y + 1][x] && c === modules[y + 1][x + 1]) {
|
|
376
|
+
result += 3;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
let dark = 0;
|
|
382
|
+
for (const row of modules) for (const m of row) if (m) dark++;
|
|
383
|
+
const total = size * size;
|
|
384
|
+
result += (Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1) * 10;
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private addHistory(run: number, history: number[]) {
|
|
389
|
+
if (history[0] === 0) run += this.size; // the light border before the first run
|
|
390
|
+
history.pop();
|
|
391
|
+
history.unshift(run);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private countFinderLike(h: number[]): number {
|
|
395
|
+
const n = h[1];
|
|
396
|
+
const core = n > 0 && h[2] === n && h[3] === n * 3 && h[4] === n && h[5] === n;
|
|
397
|
+
return (core && h[0] >= n * 4 && h[6] >= n ? 1 : 0) + (core && h[6] >= n * 4 && h[0] >= n ? 1 : 0);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function maskBit(mask: number, x: number, y: number): boolean {
|
|
402
|
+
switch (mask) {
|
|
403
|
+
case 0:
|
|
404
|
+
return (x + y) % 2 === 0;
|
|
405
|
+
case 1:
|
|
406
|
+
return y % 2 === 0;
|
|
407
|
+
case 2:
|
|
408
|
+
return x % 3 === 0;
|
|
409
|
+
case 3:
|
|
410
|
+
return (x + y) % 3 === 0;
|
|
411
|
+
case 4:
|
|
412
|
+
return (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0;
|
|
413
|
+
case 5:
|
|
414
|
+
return ((x * y) % 2) + ((x * y) % 3) === 0;
|
|
415
|
+
case 6:
|
|
416
|
+
return (((x * y) % 2) + ((x * y) % 3)) % 2 === 0;
|
|
417
|
+
default:
|
|
418
|
+
return (((x + y) % 2) + ((x * y) % 3)) % 2 === 0;
|
|
419
|
+
}
|
|
420
|
+
}
|