soma-kit 0.0.0-stage → 0.1.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 +539 -2
- package/bin/soma-kit.mjs +27 -0
- package/package.json +66 -4
- package/packs/ai/agent-run-card.ts +64 -0
- package/packs/ai/evaluation-card.ts +51 -0
- package/packs/ai/guardrail-policy-card.ts +48 -0
- package/packs/ai/index.ts +31 -0
- package/packs/ai/model-card.ts +55 -0
- package/packs/ai/model-routing-card.ts +49 -0
- package/packs/ai/prompt-template-card.ts +49 -0
- package/packs/ai/texts.ts +19 -0
- package/packs/commerce/address-card.ts +58 -0
- package/packs/commerce/coupon-card.ts +64 -0
- package/packs/commerce/index.ts +25 -0
- package/packs/commerce/order-card.ts +60 -0
- package/packs/commerce/purchase-order-card.ts +74 -0
- package/packs/commerce/texts.ts +14 -0
- package/packs/finance/index.ts +30 -0
- package/packs/finance/invoice-summary-card.ts +48 -0
- package/packs/finance/reconcile-board.ts +450 -0
- package/packs/finance/reconcile-case-card.ts +70 -0
- package/packs/finance/reconcile-desk/compile.ts +208 -0
- package/packs/finance/reconcile-desk/index.ts +20 -0
- package/packs/finance/reconcile-desk/shell.ts +27 -0
- package/packs/finance/texts.ts +21 -0
- package/packs/ops/change-window-card.ts +54 -0
- package/packs/ops/dependency-status-card.ts +50 -0
- package/packs/ops/error-budget-card.ts +60 -0
- package/packs/ops/execution-task-runner.ts +61 -0
- package/packs/ops/index.ts +36 -0
- package/packs/ops/integration-status-card.ts +45 -0
- package/packs/ops/release-readiness-card.ts +63 -0
- package/packs/ops/runbook-execution-card.ts +81 -0
- package/packs/ops/texts.ts +21 -0
- package/packs/security/control-evidence-card.ts +54 -0
- package/packs/security/finding-card.ts +96 -0
- package/packs/security/index.ts +29 -0
- package/packs/security/texts.ts +31 -0
- package/packs/shared/card-util.ts +60 -0
- package/packs/shared/wrap.ts +13 -0
- package/spec/generated/kit-app.schema.json +84 -0
- package/spec/generated/kit-page.schema.json +2766 -0
- package/spec/kit-grammar.json +1358 -0
- package/spec/kit-grammar.schema.json +716 -0
- package/src/app/app-config.ts +108 -0
- package/src/app/compose-apps.ts +139 -0
- package/src/app/define-kit-actions.ts +89 -0
- package/src/app/define-kit-app.ts +362 -0
- package/src/app/define-kit-pack.ts +114 -0
- package/src/app/envelope.ts +117 -0
- package/src/app/index.ts +50 -0
- package/src/app/notify-renderer.ts +114 -0
- package/src/app/paged-list-adapter.ts +246 -0
- package/src/app/server-pages.ts +303 -0
- package/src/app/server.ts +244 -0
- package/src/app/submit.types.ts +87 -0
- package/src/base/action-busy.ts +159 -0
- package/src/base/action-gate.ts +267 -0
- package/src/base/action-status.ts +31 -0
- package/src/base/alert.ts +136 -0
- package/src/base/avatar.ts +271 -0
- package/src/base/button.ts +139 -0
- package/src/base/cascader.ts +523 -0
- package/src/base/check-core.ts +214 -0
- package/src/base/checkbox.ts +35 -0
- package/src/base/choice.ts +183 -0
- package/src/base/color-picker.ts +316 -0
- package/src/base/combobox.ts +61 -0
- package/src/base/command-palette.ts +371 -0
- package/src/base/context-menu.ts +103 -0
- package/src/base/countdown.ts +168 -0
- package/src/base/date-range.ts +444 -0
- package/src/base/date.ts +899 -0
- package/src/base/divider.ts +156 -0
- package/src/base/fab.ts +192 -0
- package/src/base/icons.ts +106 -0
- package/src/base/image.ts +177 -0
- package/src/base/index.ts +214 -0
- package/src/base/input.ts +316 -0
- package/src/base/kbd.ts +99 -0
- package/src/base/link.ts +167 -0
- package/src/base/listbox-core.ts +965 -0
- package/src/base/mentions.ts +323 -0
- package/src/base/menu-button.ts +240 -0
- package/src/base/menu-core.ts +341 -0
- package/src/base/month-grid.ts +171 -0
- package/src/base/motion/aura.ts +94 -0
- package/src/base/motion/hover-3d.ts +154 -0
- package/src/base/motion/hover-gallery.ts +185 -0
- package/src/base/motion/index.ts +27 -0
- package/src/base/motion/limits.ts +75 -0
- package/src/base/motion/marquee.ts +157 -0
- package/src/base/motion/text-rotate.ts +195 -0
- package/src/base/number.ts +121 -0
- package/src/base/otp.ts +259 -0
- package/src/base/permission-gate.ts +99 -0
- package/src/base/popover.ts +352 -0
- package/src/base/popup.ts +542 -0
- package/src/base/portal-core.ts +297 -0
- package/src/base/price-text.ts +126 -0
- package/src/base/progress.ts +216 -0
- package/src/base/qr-code.ts +446 -0
- package/src/base/qty-stepper.ts +241 -0
- package/src/base/radio.ts +33 -0
- package/src/base/range.ts +444 -0
- package/src/base/rating.ts +249 -0
- package/src/base/segmented.ts +286 -0
- package/src/base/select.ts +57 -0
- package/src/base/shared.ts +708 -0
- package/src/base/swap.ts +86 -0
- package/src/base/switch.ts +111 -0
- package/src/base/tag-input.ts +464 -0
- package/src/base/tag.ts +250 -0
- package/src/base/text.ts +126 -0
- package/src/base/textarea.ts +129 -0
- package/src/base/time-picker.ts +606 -0
- package/src/base/tree-select.ts +629 -0
- package/src/base/upload.ts +548 -0
- package/src/base/virtual-list.ts +286 -0
- package/src/biz/approval-desk/compile.ts +179 -0
- package/src/biz/approval-desk/index.ts +21 -0
- package/src/biz/approval-desk/shell.ts +50 -0
- package/src/biz/auth-panel/compile.ts +196 -0
- package/src/biz/auth-panel/index.ts +24 -0
- package/src/biz/auth-panel/shell.ts +46 -0
- package/src/biz/category-crud/compile.ts +173 -0
- package/src/biz/category-crud/index.ts +19 -0
- package/src/biz/category-crud/shell.ts +29 -0
- package/src/biz/checkout-page/compile.ts +111 -0
- package/src/biz/checkout-page/index.ts +20 -0
- package/src/biz/checkout-page/shell.ts +50 -0
- package/src/biz/compose.ts +313 -0
- package/src/biz/data-components.ts +26 -0
- package/src/biz/define.ts +23 -0
- package/src/biz/import-wizard/compile.ts +204 -0
- package/src/biz/import-wizard/hub.ts +540 -0
- package/src/biz/import-wizard/index.ts +24 -0
- package/src/biz/import-wizard/shell.ts +161 -0
- package/src/biz/index.ts +80 -0
- package/src/biz/landing-page/compile.ts +67 -0
- package/src/biz/landing-page/index.ts +20 -0
- package/src/biz/landing-page/shell.ts +48 -0
- package/src/biz/link/hub.ts +255 -0
- package/src/biz/link/shared.ts +493 -0
- package/src/biz/master-sub/compile.ts +232 -0
- package/src/biz/master-sub/index.ts +20 -0
- package/src/biz/master-sub/shell.ts +56 -0
- package/src/biz/multi-board/compile.ts +234 -0
- package/src/biz/multi-board/index.ts +19 -0
- package/src/biz/multi-board/shell.ts +70 -0
- package/src/biz/names.ts +30 -0
- package/src/biz/notification-center/compile.ts +138 -0
- package/src/biz/notification-center/index.ts +29 -0
- package/src/biz/notification-center/shell.ts +42 -0
- package/src/biz/page-templates/result-page/compile.ts +46 -0
- package/src/biz/page-templates/result-page/index.ts +19 -0
- package/src/biz/page-templates/result-page/shell.ts +31 -0
- package/src/biz/page-templates/shared.ts +121 -0
- package/src/biz/page-templates/sign-in-page/compile.ts +176 -0
- package/src/biz/page-templates/sign-in-page/index.ts +18 -0
- package/src/biz/page-templates/sign-in-page/shell.ts +116 -0
- package/src/biz/record-detail/compile.ts +290 -0
- package/src/biz/record-detail/index.ts +22 -0
- package/src/biz/record-detail/shell.ts +64 -0
- package/src/biz/settings-center/compile.ts +100 -0
- package/src/biz/settings-center/index.ts +18 -0
- package/src/biz/settings-center/shell.ts +51 -0
- package/src/biz/shared/action-host.ts +469 -0
- package/src/biz/shared/coordinator.ts +516 -0
- package/src/biz/shared/index.ts +9 -0
- package/src/biz/shared/pipes.ts +58 -0
- package/src/biz/shared/runtime-hub.ts +112 -0
- package/src/biz/shared/step-values.ts +25 -0
- package/src/biz/single-form/compile.ts +146 -0
- package/src/biz/single-form/index.ts +18 -0
- package/src/biz/single-form/shell.ts +38 -0
- package/src/biz/standard-crud/bridge.ts +11 -0
- package/src/biz/standard-crud/compile.ts +546 -0
- package/src/biz/standard-crud/index.ts +18 -0
- package/src/biz/standard-crud/shell.ts +157 -0
- package/src/biz/step-form/compile.ts +400 -0
- package/src/biz/step-form/hub.ts +412 -0
- package/src/biz/step-form/index.ts +23 -0
- package/src/biz/step-form/shell.ts +96 -0
- package/src/biz/step-form/steps-ref.ts +112 -0
- package/src/biz/types.ts +96 -0
- package/src/cli/kit-cli.ts +102 -0
- package/src/compile/action-host-attach.ts +76 -0
- package/src/compile/compile-page.ts +472 -0
- package/src/compile/component-candidates.ts +147 -0
- package/src/compile/fragment.ts +104 -0
- package/src/compile/hints.ts +255 -0
- package/src/compile/index.ts +16 -0
- package/src/compile/locate.ts +166 -0
- package/src/compile/page-params.ts +80 -0
- package/src/compile/parse.ts +355 -0
- package/src/compile/redirect.ts +90 -0
- package/src/compile/registry.ts +30 -0
- package/src/compile/remove-guard.ts +99 -0
- package/src/compile/styles.ts +244 -0
- package/src/compile/types.ts +106 -0
- package/src/data/amount-breakdown.ts +132 -0
- package/src/data/anchor-nav.ts +225 -0
- package/src/data/approval-flow.ts +269 -0
- package/src/data/assignment-picker.ts +652 -0
- package/src/data/async-result-panel.ts +308 -0
- package/src/data/breadcrumbs.ts +197 -0
- package/src/data/calendar.ts +363 -0
- package/src/data/carousel.ts +408 -0
- package/src/data/chart.ts +726 -0
- package/src/data/checklist.ts +215 -0
- package/src/data/code-view.ts +415 -0
- package/src/data/comment-thread.ts +342 -0
- package/src/data/confirm-dialog.ts +144 -0
- package/src/data/context-menu.ts +216 -0
- package/src/data/d9-limits.ts +42 -0
- package/src/data/d9-util.ts +81 -0
- package/src/data/diff.ts +239 -0
- package/src/data/display-util.ts +135 -0
- package/src/data/dock.ts +152 -0
- package/src/data/embed.ts +200 -0
- package/src/data/field-mapper.ts +544 -0
- package/src/data/flow-editor-contract.ts +41 -0
- package/src/data/flow-editor.ts +2622 -0
- package/src/data/flow-remote-options.ts +257 -0
- package/src/data/footer.ts +157 -0
- package/src/data/form-draft-registry.ts +76 -0
- package/src/data/gantt.ts +430 -0
- package/src/data/hero.ts +149 -0
- package/src/data/image-viewer.ts +333 -0
- package/src/data/index.ts +268 -0
- package/src/data/item-list.ts +335 -0
- package/src/data/kanban.ts +667 -0
- package/src/data/media-grid.ts +246 -0
- package/src/data/mega-menu.ts +257 -0
- package/src/data/menu-dom.ts +169 -0
- package/src/data/menu.ts +464 -0
- package/src/data/mockup.ts +176 -0
- package/src/data/modal-dialog.ts +182 -0
- package/src/data/navbar.ts +292 -0
- package/src/data/object-form.ts +1981 -0
- package/src/data/options-bus.ts +24 -0
- package/src/data/outcome-watch.ts +40 -0
- package/src/data/overlay-portal.ts +6 -0
- package/src/data/pager.ts +473 -0
- package/src/data/permission-matrix.ts +432 -0
- package/src/data/permission-tree.ts +916 -0
- package/src/data/pricing-table.ts +269 -0
- package/src/data/prompt-dialog.ts +235 -0
- package/src/data/reaction-bar.ts +236 -0
- package/src/data/record-card.ts +413 -0
- package/src/data/record-picker.ts +489 -0
- package/src/data/redirect.ts +109 -0
- package/src/data/remote-options.ts +54 -0
- package/src/data/request-console.ts +399 -0
- package/src/data/resident.ts +58 -0
- package/src/data/result-panel.ts +160 -0
- package/src/data/rich-text.ts +626 -0
- package/src/data/row-editor.ts +1066 -0
- package/src/data/rule-builder.ts +1137 -0
- package/src/data/shape-util.ts +221 -0
- package/src/data/staged-rows.ts +69 -0
- package/src/data/stat-group.ts +231 -0
- package/src/data/static-lazy.ts +63 -0
- package/src/data/static-registry.ts +227 -0
- package/src/data/steps.ts +195 -0
- package/src/data/survey.ts +455 -0
- package/src/data/system-switcher.ts +243 -0
- package/src/data/timeline.ts +204 -0
- package/src/data/tour-guide.ts +392 -0
- package/src/data/transfer.ts +597 -0
- package/src/data/tree-check.ts +30 -0
- package/src/data/tree-table.ts +674 -0
- package/src/data/tree.ts +656 -0
- package/src/data/trend-line.ts +147 -0
- package/src/data/virtual-rows.ts +556 -0
- package/src/data/watermark.ts +140 -0
- package/src/data/wrap.ts +267 -0
- package/src/data/wrapped-candidates.ts +96 -0
- package/src/frames/blank-frame.ts +19 -0
- package/src/frames/index.ts +44 -0
- package/src/frames/page-bar.ts +185 -0
- package/src/frames/page-header.ts +185 -0
- package/src/frames/page-status.ts +54 -0
- package/src/frames/resize-handle.ts +147 -0
- package/src/frames/shared.ts +242 -0
- package/src/frames/single-frame.ts +16 -0
- package/src/frames/split-frame/index.ts +4 -0
- package/src/frames/split-frame/shell.ts +219 -0
- package/src/frames/split-frame/span.ts +64 -0
- package/src/frames/split-frame/wiring.ts +164 -0
- package/src/frames/tabs-frame/index.ts +3 -0
- package/src/frames/tabs-frame/shell.ts +112 -0
- package/src/frames/tabs-frame/wiring.ts +69 -0
- package/src/frames/top-main-frame.ts +20 -0
- package/src/frames/types.ts +72 -0
- package/src/frames/workspace-frame/extras.ts +197 -0
- package/src/frames/workspace-frame/index.ts +13 -0
- package/src/frames/workspace-frame/shell.ts +282 -0
- package/src/frames/workspace-frame/state.ts +152 -0
- package/src/frames/workspace-frame/view.ts +1013 -0
- package/src/generated/kit-grammar-runtime.ts +2822 -0
- package/src/generated/page-origin.ts +57 -0
- package/src/i18n/index.ts +22 -0
- package/src/i18n/kit-text.ts +692 -0
- package/src/i18n/resolve-text-refs.ts +128 -0
- package/src/index.ts +6 -0
- package/src/object-renderer/action-gate.ts +216 -0
- package/src/object-renderer/adopt-gate.ts +333 -0
- package/src/object-renderer/arrange.ts +411 -0
- package/src/object-renderer/candidates.ts +68 -0
- package/src/object-renderer/collapse-wiring.ts +99 -0
- package/src/object-renderer/collection-expand.ts +185 -0
- package/src/object-renderer/collection-parts.ts +653 -0
- package/src/object-renderer/collection-table.ts +503 -0
- package/src/object-renderer/collection.ts +628 -0
- package/src/object-renderer/compile-data.ts +549 -0
- package/src/object-renderer/defaults.ts +252 -0
- package/src/object-renderer/display-checks.ts +153 -0
- package/src/object-renderer/emit-events.ts +498 -0
- package/src/object-renderer/emit-object.ts +98 -0
- package/src/object-renderer/emit-static.ts +1036 -0
- package/src/object-renderer/field-policy.ts +89 -0
- package/src/object-renderer/field-value.ts +581 -0
- package/src/object-renderer/form-detail.ts +116 -0
- package/src/object-renderer/index.ts +13 -0
- package/src/object-renderer/linkage.ts +371 -0
- package/src/object-renderer/merge-class.ts +126 -0
- package/src/object-renderer/mode.ts +25 -0
- package/src/object-renderer/outcome-draft.ts +61 -0
- package/src/object-renderer/parts.ts +136 -0
- package/src/object-renderer/pick-component.ts +36 -0
- package/src/object-renderer/render-object.ts +724 -0
- package/src/object-renderer/request-actions.ts +581 -0
- package/src/object-renderer/skeleton.ts +101 -0
- package/src/object-renderer/table-actions.ts +438 -0
- package/src/object-renderer/table-summary.ts +105 -0
- package/src/object-renderer/table-tools.ts +1072 -0
- package/src/object-renderer/types.ts +936 -0
- package/src/page-target/ai-surface.ts +154 -0
- package/src/page-target/compile.ts +354 -0
- package/src/page-target/drift.ts +186 -0
- package/src/page-target/index.ts +25 -0
- package/src/page-target/intent-map.ts +137 -0
- package/src/page-target/intent-schema.ts +711 -0
- package/src/page-target/origin.ts +17 -0
- package/src/page-target/target.ts +450 -0
- package/src/page-target/writer-policy.ts +315 -0
- package/src/theme/block-theme.ts +202 -0
- package/src/theme/kit-theme-text.ts +2 -0
- package/src/theme/kit-theme.css +275 -0
- package/src/theme/kit-theme.tpl +275 -0
- package/src/theme/theme-tokens.ts +135 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
// QrCode:二维码(纯 TS 生成矩阵,SVG rect 输出)。D8 组。
|
|
2
|
+
//
|
|
3
|
+
// 归类(2.1a 四问):单值展示(文本 -> 图形),没有对象形状 -> 基础组件。不引入第三方库:最小的 QR Code Model 2 编码器在本文件
|
|
4
|
+
// (字节模式、UTF-8、版本 1–10、纠错 L / M / Q / H、8 种掩码按罚分自动择优),做法与公开的 QR 规范(ISO/IEC 18004)一致;
|
|
5
|
+
// 不支持数字 / 字母数字 / 汉字模式与版本 11 以上(容量:纠错 M 约 213 字节,L 约 271 字节),超出时不画并抛 `error { reason: 'too-long' }`。
|
|
6
|
+
// 输出是 SVG:背景一个 rect,暗模块按行合并成横向长条 rect,颜色走 `fill-fg` / `fill-surface` 类(跟随主题;深色主题下是浅码深底,
|
|
7
|
+
// 个别老扫码器不认反色,要打印请用浅色主题)。四周静区 `quiet` 默认 4 个模块(规范值)。读屏读 `label`(缺省「二维码」),不朗读矩阵。
|
|
8
|
+
// 值是纯字符串,不进样式;组件不联网、不做 URL 校验。
|
|
9
|
+
|
|
10
|
+
import type { SomaComponentDefinition } from 'soma-core';
|
|
11
|
+
import { kitText } from '../i18n/kit-text';
|
|
12
|
+
import {
|
|
13
|
+
CLASS_PROP, ID_PROPS, classCandidates,
|
|
14
|
+
createKitController, joinClass, num, setAttr, str,
|
|
15
|
+
} from './shared';
|
|
16
|
+
|
|
17
|
+
// ---- Tailwind 类常量与候选类清单 ----
|
|
18
|
+
|
|
19
|
+
const SIZE_CLASS = {
|
|
20
|
+
sm: 'block h-28 w-28 shrink-0',
|
|
21
|
+
md: 'block h-40 w-40 shrink-0',
|
|
22
|
+
lg: 'block h-56 w-56 shrink-0',
|
|
23
|
+
} as const;
|
|
24
|
+
const BG_CLASS = 'fill-surface';
|
|
25
|
+
const DARK_CLASS = 'fill-fg';
|
|
26
|
+
const SVG_CLASS = 'block h-full w-full';
|
|
27
|
+
const HIDDEN = 'hidden';
|
|
28
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
29
|
+
|
|
30
|
+
export const KIT_QR_CODE_CLASS_CANDIDATES: readonly string[] = classCandidates(
|
|
31
|
+
Object.values(SIZE_CLASS), BG_CLASS, DARK_CLASS, SVG_CLASS, HIDDEN,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// ---- 纯 TS 编码器(QR Code Model 2,字节模式、UTF-8、版本 1–10、纠错 L / M / Q / H、8 种掩码按罚分择优)与矩阵合并 ----
|
|
35
|
+
|
|
36
|
+
export type QrEcc = 'L' | 'M' | 'Q' | 'H';
|
|
37
|
+
const ECCS: readonly QrEcc[] = ['L', 'M', 'Q', 'H'];
|
|
38
|
+
const ECC_BITS: Record<QrEcc, number> = { L: 1, M: 0, Q: 3, H: 2 };
|
|
39
|
+
const ECC_PER_BLOCK: Record<QrEcc, readonly number[]> = {
|
|
40
|
+
L: [7, 10, 15, 20, 26, 18, 20, 24, 30, 18],
|
|
41
|
+
M: [10, 16, 26, 18, 24, 16, 18, 22, 22, 26],
|
|
42
|
+
Q: [13, 22, 18, 26, 18, 24, 18, 22, 20, 24],
|
|
43
|
+
H: [17, 28, 22, 16, 22, 28, 26, 26, 24, 28],
|
|
44
|
+
};
|
|
45
|
+
const NUM_BLOCKS: Record<QrEcc, readonly number[]> = {
|
|
46
|
+
L: [1, 1, 1, 1, 1, 2, 2, 2, 2, 4],
|
|
47
|
+
M: [1, 1, 1, 2, 2, 4, 4, 4, 5, 5],
|
|
48
|
+
Q: [1, 1, 2, 2, 4, 4, 6, 6, 8, 8],
|
|
49
|
+
H: [1, 1, 2, 4, 4, 4, 5, 6, 8, 8],
|
|
50
|
+
};
|
|
51
|
+
const MAX_VERSION = 10;
|
|
52
|
+
|
|
53
|
+
function rawModules(ver: number): number {
|
|
54
|
+
let n = (16 * ver + 128) * ver + 64;
|
|
55
|
+
if (ver >= 2) {
|
|
56
|
+
const na = Math.floor(ver / 7) + 2;
|
|
57
|
+
n -= (25 * na - 10) * na - 55;
|
|
58
|
+
if (ver >= 7) n -= 36;
|
|
59
|
+
}
|
|
60
|
+
return n;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function dataCodewords(ver: number, ecc: QrEcc): number {
|
|
64
|
+
return Math.floor(rawModules(ver) / 8) - (ECC_PER_BLOCK[ecc][ver - 1] ?? 0) * (NUM_BLOCKS[ecc][ver - 1] ?? 1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 该纠错级别下字节模式的最大字节数(版本 1–10 内) */
|
|
68
|
+
export function qrCapacity(ecc: QrEcc): number {
|
|
69
|
+
const ver = MAX_VERSION;
|
|
70
|
+
const countBits = ver <= 9 ? 8 : 16;
|
|
71
|
+
return Math.floor((dataCodewords(ver, ecc) * 8 - 4 - countBits) / 8);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function utf8(text: string): number[] {
|
|
75
|
+
const out: number[] = [];
|
|
76
|
+
for (const ch of text) {
|
|
77
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
78
|
+
if (cp < 0x80) out.push(cp);
|
|
79
|
+
else if (cp < 0x800) out.push(0xc0 | (cp >> 6), 0x80 | (cp & 0x3f));
|
|
80
|
+
else if (cp < 0x10000) out.push(0xe0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
|
81
|
+
else out.push(0xf0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function gfMul(x: number, y: number): number {
|
|
87
|
+
let z = 0;
|
|
88
|
+
for (let i = 7; i >= 0; i -= 1) {
|
|
89
|
+
z = (z << 1) ^ ((z >>> 7) * 0x11d);
|
|
90
|
+
z ^= ((y >>> i) & 1) * x;
|
|
91
|
+
}
|
|
92
|
+
return z;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function rsDivisor(degree: number): number[] {
|
|
96
|
+
const result: number[] = new Array<number>(degree).fill(0);
|
|
97
|
+
result[degree - 1] = 1;
|
|
98
|
+
let root = 1;
|
|
99
|
+
for (let i = 0; i < degree; i += 1) {
|
|
100
|
+
for (let j = 0; j < result.length; j += 1) {
|
|
101
|
+
result[j] = gfMul(result[j] ?? 0, root);
|
|
102
|
+
if (j + 1 < result.length) result[j] = (result[j] ?? 0) ^ (result[j + 1] ?? 0);
|
|
103
|
+
}
|
|
104
|
+
root = gfMul(root, 0x02);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function rsRemainder(data: readonly number[], divisor: readonly number[]): number[] {
|
|
110
|
+
const result: number[] = new Array<number>(divisor.length).fill(0);
|
|
111
|
+
for (const b of data) {
|
|
112
|
+
const factor = b ^ (result.shift() ?? 0);
|
|
113
|
+
result.push(0);
|
|
114
|
+
divisor.forEach((coef, i) => {
|
|
115
|
+
result[i] = (result[i] ?? 0) ^ gfMul(coef, factor);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function alignPositions(ver: number, size: number): number[] {
|
|
122
|
+
if (ver === 1) return [];
|
|
123
|
+
const num = Math.floor(ver / 7) + 2;
|
|
124
|
+
const step = Math.ceil((ver * 4 + 4) / (num * 2 - 2)) * 2;
|
|
125
|
+
const out = [6];
|
|
126
|
+
for (let pos = size - 7; out.length < num; pos -= step) out.splice(1, 0, pos);
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const bitOf = (value: number, i: number): boolean => ((value >>> i) & 1) !== 0;
|
|
131
|
+
|
|
132
|
+
function maskFn(mask: number, x: number, y: number): boolean {
|
|
133
|
+
switch (mask) {
|
|
134
|
+
case 0: return (x + y) % 2 === 0;
|
|
135
|
+
case 1: return y % 2 === 0;
|
|
136
|
+
case 2: return x % 3 === 0;
|
|
137
|
+
case 3: return (x + y) % 3 === 0;
|
|
138
|
+
case 4: return (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0;
|
|
139
|
+
case 5: return ((x * y) % 2) + ((x * y) % 3) === 0;
|
|
140
|
+
case 6: return ((((x * y) % 2) + ((x * y) % 3)) % 2) === 0;
|
|
141
|
+
default: return ((((x + y) % 2) + ((x * y) % 3)) % 2) === 0;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function penalty(m: boolean[][], size: number): number {
|
|
146
|
+
let score = 0;
|
|
147
|
+
const lines: string[] = [];
|
|
148
|
+
for (let y = 0; y < size; y += 1) lines.push((m[y] ?? []).map((v) => (v ? '1' : '0')).join(''));
|
|
149
|
+
for (let x = 0; x < size; x += 1) lines.push(m.map((row) => (row[x] ? '1' : '0')).join(''));
|
|
150
|
+
for (const line of lines) {
|
|
151
|
+
// 规则 1:同色连续 >= 5
|
|
152
|
+
let run = 1;
|
|
153
|
+
for (let i = 1; i <= line.length; i += 1) {
|
|
154
|
+
if (i < line.length && line[i] === line[i - 1]) run += 1;
|
|
155
|
+
else {
|
|
156
|
+
if (run >= 5) score += 3 + (run - 5);
|
|
157
|
+
run = 1;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// 规则 3:类定位图形
|
|
161
|
+
for (const pat of ['10111010000', '00001011101']) {
|
|
162
|
+
let at = line.indexOf(pat);
|
|
163
|
+
while (at !== -1) {
|
|
164
|
+
score += 40;
|
|
165
|
+
at = line.indexOf(pat, at + 1);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// 规则 2:2x2 同色块
|
|
170
|
+
for (let y = 0; y < size - 1; y += 1) {
|
|
171
|
+
for (let x = 0; x < size - 1; x += 1) {
|
|
172
|
+
const c = m[y]?.[x];
|
|
173
|
+
if (c === m[y]?.[x + 1] && c === m[y + 1]?.[x] && c === m[y + 1]?.[x + 1]) score += 3;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// 规则 4:深浅比例
|
|
177
|
+
let dark = 0;
|
|
178
|
+
for (const row of m) for (const v of row) if (v) dark += 1;
|
|
179
|
+
const total = size * size;
|
|
180
|
+
score += (Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1) * 10;
|
|
181
|
+
return score;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 生成 QR 矩阵(true = 深色模块)。文本按 UTF-8 字节模式编码;版本取能装下的最小版本(上限 10);
|
|
186
|
+
* 装不下回 null。空文本回 null。
|
|
187
|
+
*/
|
|
188
|
+
export function qrMatrix(text: string, ecc: QrEcc = 'M'): boolean[][] | null {
|
|
189
|
+
if (text === '') return null;
|
|
190
|
+
const bytes = utf8(text);
|
|
191
|
+
let ver = 1;
|
|
192
|
+
for (; ver <= MAX_VERSION; ver += 1) {
|
|
193
|
+
const need = 4 + (ver <= 9 ? 8 : 16) + bytes.length * 8;
|
|
194
|
+
if (need <= dataCodewords(ver, ecc) * 8) break;
|
|
195
|
+
}
|
|
196
|
+
if (ver > MAX_VERSION) return null;
|
|
197
|
+
|
|
198
|
+
// 数据位:模式 0100 + 长度 + 字节 + 终止符 + 补齐
|
|
199
|
+
const bits: number[] = [];
|
|
200
|
+
const push = (value: number, len: number): void => {
|
|
201
|
+
for (let i = len - 1; i >= 0; i -= 1) bits.push((value >>> i) & 1);
|
|
202
|
+
};
|
|
203
|
+
push(0b0100, 4);
|
|
204
|
+
push(bytes.length, ver <= 9 ? 8 : 16);
|
|
205
|
+
for (const b of bytes) push(b, 8);
|
|
206
|
+
const capBits = dataCodewords(ver, ecc) * 8;
|
|
207
|
+
push(0, Math.min(4, capBits - bits.length));
|
|
208
|
+
push(0, (8 - (bits.length % 8)) % 8);
|
|
209
|
+
const data: number[] = [];
|
|
210
|
+
for (let i = 0; i < bits.length; i += 8) {
|
|
211
|
+
let b = 0;
|
|
212
|
+
for (let j = 0; j < 8; j += 1) b = (b << 1) | (bits[i + j] ?? 0);
|
|
213
|
+
data.push(b);
|
|
214
|
+
}
|
|
215
|
+
for (let pad = 0xec; data.length < capBits / 8; pad ^= 0xec ^ 0x11) data.push(pad);
|
|
216
|
+
|
|
217
|
+
// 分块纠错并交织
|
|
218
|
+
const numBlocks = NUM_BLOCKS[ecc][ver - 1] ?? 1;
|
|
219
|
+
const blockEcc = ECC_PER_BLOCK[ecc][ver - 1] ?? 0;
|
|
220
|
+
const rawCodewords = Math.floor(rawModules(ver) / 8);
|
|
221
|
+
const numShort = numBlocks - (rawCodewords % numBlocks);
|
|
222
|
+
const shortLen = Math.floor(rawCodewords / numBlocks);
|
|
223
|
+
const divisor = rsDivisor(blockEcc);
|
|
224
|
+
const blocks: number[][] = [];
|
|
225
|
+
for (let i = 0, k = 0; i < numBlocks; i += 1) {
|
|
226
|
+
const dat = data.slice(k, k + shortLen - blockEcc + (i < numShort ? 0 : 1));
|
|
227
|
+
k += dat.length;
|
|
228
|
+
const rem = rsRemainder(dat, divisor);
|
|
229
|
+
if (i < numShort) dat.push(0);
|
|
230
|
+
blocks.push([...dat, ...rem]);
|
|
231
|
+
}
|
|
232
|
+
const codewords: number[] = [];
|
|
233
|
+
for (let i = 0; i < (blocks[0]?.length ?? 0); i += 1) {
|
|
234
|
+
blocks.forEach((blk, j) => {
|
|
235
|
+
if (i !== shortLen - blockEcc || j >= numShort) codewords.push(blk[i] ?? 0);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 矩阵
|
|
240
|
+
const size = ver * 4 + 17;
|
|
241
|
+
const m: boolean[][] = Array.from({ length: size }, () => new Array<boolean>(size).fill(false));
|
|
242
|
+
const fn: boolean[][] = Array.from({ length: size }, () => new Array<boolean>(size).fill(false));
|
|
243
|
+
const set = (x: number, y: number, dark: boolean): void => {
|
|
244
|
+
const row = m[y];
|
|
245
|
+
const frow = fn[y];
|
|
246
|
+
if (row && frow) {
|
|
247
|
+
row[x] = dark;
|
|
248
|
+
frow[x] = true;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
for (let i = 0; i < size; i += 1) {
|
|
252
|
+
set(6, i, i % 2 === 0);
|
|
253
|
+
set(i, 6, i % 2 === 0);
|
|
254
|
+
}
|
|
255
|
+
const finder = (cx: number, cy: number): void => {
|
|
256
|
+
for (let dy = -4; dy <= 4; dy += 1) {
|
|
257
|
+
for (let dx = -4; dx <= 4; dx += 1) {
|
|
258
|
+
const dist = Math.max(Math.abs(dx), Math.abs(dy));
|
|
259
|
+
const x = cx + dx;
|
|
260
|
+
const y = cy + dy;
|
|
261
|
+
if (x >= 0 && x < size && y >= 0 && y < size) set(x, y, dist !== 2 && dist !== 4);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
finder(3, 3);
|
|
266
|
+
finder(size - 4, 3);
|
|
267
|
+
finder(3, size - 4);
|
|
268
|
+
const pos = alignPositions(ver, size);
|
|
269
|
+
pos.forEach((px, i) => {
|
|
270
|
+
pos.forEach((py, j) => {
|
|
271
|
+
if ((i === 0 && j === 0) || (i === 0 && j === pos.length - 1) || (i === pos.length - 1 && j === 0)) return;
|
|
272
|
+
for (let dy = -2; dy <= 2; dy += 1) for (let dx = -2; dx <= 2; dx += 1) set(px + dx, py + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
const formatBits = (mask: number): void => {
|
|
276
|
+
const d = (ECC_BITS[ecc] << 3) | mask;
|
|
277
|
+
let rem = d;
|
|
278
|
+
for (let i = 0; i < 10; i += 1) rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
279
|
+
const bitsAll = ((d << 10) | rem) ^ 0x5412;
|
|
280
|
+
for (let i = 0; i <= 5; i += 1) set(8, i, bitOf(bitsAll, i));
|
|
281
|
+
set(8, 7, bitOf(bitsAll, 6));
|
|
282
|
+
set(8, 8, bitOf(bitsAll, 7));
|
|
283
|
+
set(7, 8, bitOf(bitsAll, 8));
|
|
284
|
+
for (let i = 9; i < 15; i += 1) set(14 - i, 8, bitOf(bitsAll, i));
|
|
285
|
+
for (let i = 0; i < 8; i += 1) set(size - 1 - i, 8, bitOf(bitsAll, i));
|
|
286
|
+
for (let i = 8; i < 15; i += 1) set(8, size - 15 + i, bitOf(bitsAll, i));
|
|
287
|
+
set(8, size - 8, true);
|
|
288
|
+
};
|
|
289
|
+
formatBits(0);
|
|
290
|
+
if (ver >= 7) {
|
|
291
|
+
let rem = ver;
|
|
292
|
+
for (let i = 0; i < 12; i += 1) rem = (rem << 1) ^ ((rem >>> 11) * 0x1f25);
|
|
293
|
+
const vbits = (ver << 12) | rem;
|
|
294
|
+
for (let i = 0; i < 18; i += 1) {
|
|
295
|
+
const a = size - 11 + (i % 3);
|
|
296
|
+
const b = Math.floor(i / 3);
|
|
297
|
+
set(a, b, bitOf(vbits, i));
|
|
298
|
+
set(b, a, bitOf(vbits, i));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// 数据位按蛇形顺序填入
|
|
303
|
+
let bi = 0;
|
|
304
|
+
for (let right = size - 1; right >= 1; right -= 2) {
|
|
305
|
+
if (right === 6) right = 5;
|
|
306
|
+
for (let vert = 0; vert < size; vert += 1) {
|
|
307
|
+
for (let j = 0; j < 2; j += 1) {
|
|
308
|
+
const x = right - j;
|
|
309
|
+
const upward = ((right + 1) & 2) === 0;
|
|
310
|
+
const y = upward ? size - 1 - vert : vert;
|
|
311
|
+
if (!fn[y]?.[x] && bi < codewords.length * 8) {
|
|
312
|
+
const row = m[y];
|
|
313
|
+
if (row) row[x] = bitOf(codewords[bi >>> 3] ?? 0, 7 - (bi & 7));
|
|
314
|
+
bi += 1;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const applyMask = (mask: number): void => {
|
|
321
|
+
for (let y = 0; y < size; y += 1) {
|
|
322
|
+
for (let x = 0; x < size; x += 1) {
|
|
323
|
+
const row = m[y];
|
|
324
|
+
if (row && !fn[y]?.[x] && maskFn(mask, x, y)) row[x] = !row[x];
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
let best = 0;
|
|
329
|
+
let bestScore = Infinity;
|
|
330
|
+
for (let mask = 0; mask < 8; mask += 1) {
|
|
331
|
+
applyMask(mask);
|
|
332
|
+
formatBits(mask);
|
|
333
|
+
const s = penalty(m, size);
|
|
334
|
+
if (s < bestScore) {
|
|
335
|
+
bestScore = s;
|
|
336
|
+
best = mask;
|
|
337
|
+
}
|
|
338
|
+
applyMask(mask);
|
|
339
|
+
}
|
|
340
|
+
applyMask(best);
|
|
341
|
+
formatBits(best);
|
|
342
|
+
return m;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function qrEccOf(raw: unknown): QrEcc {
|
|
346
|
+
return ECCS.includes(raw as QrEcc) ? (raw as QrEcc) : 'M';
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 把矩阵合并成横向长条:[x, y, width] */
|
|
350
|
+
export function qrRuns(matrix: readonly (readonly boolean[])[]): Array<[number, number, number]> {
|
|
351
|
+
const out: Array<[number, number, number]> = [];
|
|
352
|
+
matrix.forEach((row, y) => {
|
|
353
|
+
let start = -1;
|
|
354
|
+
for (let x = 0; x <= row.length; x += 1) {
|
|
355
|
+
if (x < row.length && row[x]) {
|
|
356
|
+
if (start === -1) start = x;
|
|
357
|
+
} else if (start !== -1) {
|
|
358
|
+
out.push([start, y, x - start]);
|
|
359
|
+
start = -1;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
return out;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// ---- 组件 ----
|
|
367
|
+
|
|
368
|
+
export function createQrCode(): SomaComponentDefinition {
|
|
369
|
+
return {
|
|
370
|
+
contract: {
|
|
371
|
+
name: 'QrCode',
|
|
372
|
+
layer: 'basic',
|
|
373
|
+
props: {
|
|
374
|
+
value: { type: 'string', default: '', desc: '要编码的文本(UTF-8,字节模式;空则不画)' },
|
|
375
|
+
ecc: { type: 'enum', values: [...ECCS], default: 'M', desc: '纠错级别 L / M / Q / H(越高容量越小)' },
|
|
376
|
+
quiet: { type: 'string', default: '', desc: '四周静区模块数 0..8,缺省 4(规范值)' },
|
|
377
|
+
label: { type: 'string', default: '', desc: '读屏名称(不朗读矩阵);缺省取内置文案(按页面语言)' },
|
|
378
|
+
size: { type: 'enum', values: ['sm', 'md', 'lg'], default: 'md', desc: '尺寸档(112 / 160 / 224 px)' },
|
|
379
|
+
...ID_PROPS,
|
|
380
|
+
class: CLASS_PROP,
|
|
381
|
+
},
|
|
382
|
+
emits: {
|
|
383
|
+
error: { payload: { reason: { type: 'string' } }, desc: '文本装不下(reason: too-long,版本上限 10);同一个值只抛一次' },
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
templateHtml: `<div data-soma-id="root" class="${HIDDEN}"></div>`,
|
|
387
|
+
controllerFactory: (ctx) => {
|
|
388
|
+
let lastKey = '';
|
|
389
|
+
let lastError = '';
|
|
390
|
+
return createKitController({
|
|
391
|
+
render(root, p, api) {
|
|
392
|
+
const text = str(p.value);
|
|
393
|
+
const ecc = qrEccOf(p.ecc);
|
|
394
|
+
const rawQuiet = num(p.quiet);
|
|
395
|
+
const quiet = rawQuiet !== null && rawQuiet >= 0 && rawQuiet <= 8 ? Math.floor(rawQuiet) : 4;
|
|
396
|
+
const size = p.size === 'sm' || p.size === 'lg' ? p.size : 'md';
|
|
397
|
+
const matrix = qrMatrix(text, ecc);
|
|
398
|
+
const key = `${text}\u0000${ecc}\u0000${quiet}`;
|
|
399
|
+
setAttr(root, 'id', str(p.id));
|
|
400
|
+
if (matrix === null) {
|
|
401
|
+
root.textContent = '';
|
|
402
|
+
lastKey = '';
|
|
403
|
+
root.className = HIDDEN;
|
|
404
|
+
root.setAttribute('data-kit-state', text === '' ? 'empty' : 'too-long');
|
|
405
|
+
if (text !== '' && lastError !== text) {
|
|
406
|
+
lastError = text;
|
|
407
|
+
api.emit('error', { reason: 'too-long' });
|
|
408
|
+
}
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
lastError = '';
|
|
412
|
+
root.removeAttribute('data-kit-state');
|
|
413
|
+
root.className = joinClass(SIZE_CLASS[size], p.class);
|
|
414
|
+
if (key === lastKey && root.firstElementChild) {
|
|
415
|
+
root.firstElementChild.setAttribute('aria-label', str(p.label, kitText('soma.kit.qr_label', undefined, root)));
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
lastKey = key;
|
|
419
|
+
const n = matrix.length + quiet * 2;
|
|
420
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
421
|
+
svg.setAttribute('viewBox', `0 0 ${n} ${n}`);
|
|
422
|
+
svg.setAttribute('class', SVG_CLASS);
|
|
423
|
+
svg.setAttribute('role', 'img');
|
|
424
|
+
svg.setAttribute('aria-label', str(p.label, kitText('soma.kit.qr_label', undefined, root)));
|
|
425
|
+
svg.setAttribute('shape-rendering', 'crispEdges');
|
|
426
|
+
const bg = document.createElementNS(SVG_NS, 'rect');
|
|
427
|
+
bg.setAttribute('width', String(n));
|
|
428
|
+
bg.setAttribute('height', String(n));
|
|
429
|
+
bg.setAttribute('class', BG_CLASS);
|
|
430
|
+
svg.appendChild(bg);
|
|
431
|
+
for (const [x, y, w] of qrRuns(matrix)) {
|
|
432
|
+
const r = document.createElementNS(SVG_NS, 'rect');
|
|
433
|
+
r.setAttribute('x', String(x + quiet));
|
|
434
|
+
r.setAttribute('y', String(y + quiet));
|
|
435
|
+
r.setAttribute('width', String(w));
|
|
436
|
+
r.setAttribute('height', '1');
|
|
437
|
+
r.setAttribute('class', DARK_CLASS);
|
|
438
|
+
svg.appendChild(r);
|
|
439
|
+
}
|
|
440
|
+
root.textContent = '';
|
|
441
|
+
root.appendChild(svg);
|
|
442
|
+
},
|
|
443
|
+
})(ctx);
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// 数字输入的自绘核心(Number / Pager 跳页共用)与 Number 的 `variant: stepper`(数量步进器,[-] 数字 [+])。
|
|
2
|
+
//
|
|
3
|
+
// 不用系统原生 type=number:输入框恒为 type=text + inputmode=decimal + role=spinbutton(保住输入法、自动填充与移动数字键盘),
|
|
4
|
+
// 外观与步进全部自绘:
|
|
5
|
+
// - 输入时过滤非数字字符(保留一个小数点、需要时保留负号),aria-valuenow / min / max 随值更新;
|
|
6
|
+
// - ↑ / ↓ 一步,PageUp / PageDown 十步,Home / End 到下限 / 上限(没有对应界限时留给光标移动);
|
|
7
|
+
// - 步进一律是「改 input 的值并派发 input 事件」,由 Number 自己的 input 处理抛 change,夹紧、禁用 / 只读口径一致。
|
|
8
|
+
// attachSpin 只做上面这些(Pager 跳页框直接用);attachStepper = attachSpin + 步进器形态的分组与 [-] [+] 按钮。
|
|
9
|
+
// 归并说明:数量步进器与 Number 是同一个数值输入,所以不另建组件:Number 带 `variant: stepper` 时,
|
|
10
|
+
// 在运行时把它的 <input>(仍是组件根)包进一个分组,两侧各放一个加减钮;到边的按钮自动禁用。
|
|
11
|
+
|
|
12
|
+
import { classCandidates, bool, num, sizeOf, str, type KitProps, type KitSize } from './shared';
|
|
13
|
+
import { kitText } from '../i18n/kit-text';
|
|
14
|
+
|
|
15
|
+
const GROUP: Record<KitSize, string> = {
|
|
16
|
+
sm: 'inline-flex h-8 items-stretch overflow-hidden rounded-md border border-input bg-surface text-sm transition-colors focus-within:border-primary focus-within:ring-2 focus-within:ring-ring',
|
|
17
|
+
md: 'inline-flex h-9 items-stretch overflow-hidden rounded-md border border-input bg-surface text-sm transition-colors focus-within:border-primary focus-within:ring-2 focus-within:ring-ring',
|
|
18
|
+
lg: 'inline-flex h-10 items-stretch overflow-hidden rounded-md border border-input bg-surface text-base transition-colors focus-within:border-primary focus-within:ring-2 focus-within:ring-ring',
|
|
19
|
+
};
|
|
20
|
+
const GROUP_OFF: Record<KitSize, string> = {
|
|
21
|
+
sm: 'inline-flex h-8 items-stretch overflow-hidden rounded-md border border-input bg-surface-sunken text-sm opacity-60',
|
|
22
|
+
md: 'inline-flex h-9 items-stretch overflow-hidden rounded-md border border-input bg-surface-sunken text-sm opacity-60',
|
|
23
|
+
lg: 'inline-flex h-10 items-stretch overflow-hidden rounded-md border border-input bg-surface-sunken text-base opacity-60',
|
|
24
|
+
};
|
|
25
|
+
const BTN_ON = 'inline-flex w-8 shrink-0 cursor-pointer items-center justify-center text-fg-muted transition-colors hover:bg-surface-sunken hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
26
|
+
const BTN_OFF = 'inline-flex w-8 shrink-0 cursor-not-allowed items-center justify-center text-fg-subtle opacity-60';
|
|
27
|
+
// 无 preflight:input 不继承分组的字号(浏览器默认 13px),所以按档位自己写 text-sm / text-base
|
|
28
|
+
export const STEPPER_INPUT: Record<KitSize, string> = {
|
|
29
|
+
sm: 'w-14 min-w-0 appearance-none autofill-clear border-s border-e border-border-subtle bg-transparent px-1 text-center text-sm text-fg focus-visible:outline-none read-only:bg-surface-sunken',
|
|
30
|
+
md: 'w-14 min-w-0 appearance-none autofill-clear border-s border-e border-border-subtle bg-transparent px-1 text-center text-sm text-fg focus-visible:outline-none read-only:bg-surface-sunken',
|
|
31
|
+
lg: 'w-14 min-w-0 appearance-none autofill-clear border-s border-e border-border-subtle bg-transparent px-1 text-center text-base text-fg focus-visible:outline-none read-only:bg-surface-sunken',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const KIT_QTY_STEPPER_CLASS_CANDIDATES: readonly string[] = classCandidates(
|
|
35
|
+
Object.values(GROUP), Object.values(GROUP_OFF), BTN_ON, BTN_OFF, Object.values(STEPPER_INPUT),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export interface QtySpec { min: number | null; max: number | null; step: number }
|
|
39
|
+
|
|
40
|
+
export function qtySpecOf(p: Record<string, unknown>): QtySpec {
|
|
41
|
+
const step = num(p.step);
|
|
42
|
+
return { min: num(p.min), max: num(p.max), step: step !== null && step > 0 ? step : 1 };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function qtyClamp(n: number, spec: QtySpec): number {
|
|
46
|
+
let out = n;
|
|
47
|
+
if (spec.min !== null && out < spec.min) out = spec.min;
|
|
48
|
+
if (spec.max !== null && out > spec.max) out = spec.max;
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 一步的结果:按步长吸附浮点误差(0.1 + 0.2),再夹紧 */
|
|
53
|
+
export function qtyStep(current: number | null, delta: number, spec: QtySpec): number {
|
|
54
|
+
const base = current ?? spec.min ?? 0;
|
|
55
|
+
const decimals = (String(spec.step).split('.')[1] ?? '').length;
|
|
56
|
+
return qtyClamp(Number((base + delta).toFixed(decimals)), spec);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 键盘 -> 目标值;不认识的键回 null */
|
|
60
|
+
export function qtyKeyTarget(key: string, current: number | null, spec: QtySpec): number | null {
|
|
61
|
+
switch (key) {
|
|
62
|
+
case 'ArrowUp': return qtyStep(current, spec.step, spec);
|
|
63
|
+
case 'ArrowDown': return qtyStep(current, -spec.step, spec);
|
|
64
|
+
case 'PageUp': return qtyStep(current, spec.step * 10, spec);
|
|
65
|
+
case 'PageDown': return qtyStep(current, -spec.step * 10, spec);
|
|
66
|
+
case 'Home': return spec.min;
|
|
67
|
+
case 'End': return spec.max;
|
|
68
|
+
default: return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 输入过滤:只留数字、一个小数点、需要时的负号(下限 >= 0 时不留负号);小数步长下逗号当小数点 */
|
|
73
|
+
export function qtySanitize(raw: string, spec: QtySpec): string {
|
|
74
|
+
const fractional = String(spec.step).includes('.');
|
|
75
|
+
let s = raw.replace(/,/g, fractional && !raw.includes('.') ? '.' : '').replace(/[^0-9.-]/g, '');
|
|
76
|
+
const negative = s.startsWith('-') && (spec.min === null || spec.min < 0);
|
|
77
|
+
s = s.replace(/-/g, '');
|
|
78
|
+
const dot = s.indexOf('.');
|
|
79
|
+
if (dot >= 0) s = s.slice(0, dot + 1) + s.slice(dot + 1).replace(/\./g, '');
|
|
80
|
+
return (negative ? '-' : '') + s;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface SpinHandle {
|
|
84
|
+
/** 按当前输入值与 props 刷新 aria-valuenow / min / max */
|
|
85
|
+
refresh(): void;
|
|
86
|
+
destroy(): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 给一个文本输入框装上 spinbutton 行为(见文件头)。
|
|
91
|
+
* 要在使用方自己的 input 监听之前调用,这样过滤先于使用方读值。getProps 给 min / max / step / disabled / readonly。
|
|
92
|
+
*/
|
|
93
|
+
export function attachSpin(input: HTMLInputElement, getProps: () => Record<string, unknown>): SpinHandle {
|
|
94
|
+
input.type = 'text';
|
|
95
|
+
input.setAttribute('inputmode', 'decimal');
|
|
96
|
+
input.setAttribute('role', 'spinbutton');
|
|
97
|
+
input.setAttribute('autocomplete', 'off');
|
|
98
|
+
input.removeAttribute('min');
|
|
99
|
+
input.removeAttribute('max');
|
|
100
|
+
input.removeAttribute('step');
|
|
101
|
+
|
|
102
|
+
const current = (): number | null => {
|
|
103
|
+
const raw = input.value.trim();
|
|
104
|
+
return raw === '' ? null : num(raw);
|
|
105
|
+
};
|
|
106
|
+
const locked = (): boolean => {
|
|
107
|
+
const p = getProps();
|
|
108
|
+
return bool(p.disabled) || bool(p.readonly);
|
|
109
|
+
};
|
|
110
|
+
const refresh = (): void => {
|
|
111
|
+
const spec = qtySpecOf(getProps());
|
|
112
|
+
const set = (name: string, value: number | null): void => {
|
|
113
|
+
if (value === null) input.removeAttribute(name);
|
|
114
|
+
else input.setAttribute(name, String(value));
|
|
115
|
+
};
|
|
116
|
+
set('aria-valuemin', spec.min);
|
|
117
|
+
set('aria-valuemax', spec.max);
|
|
118
|
+
set('aria-valuenow', current());
|
|
119
|
+
};
|
|
120
|
+
const onInput = (): void => {
|
|
121
|
+
const clean = qtySanitize(input.value, qtySpecOf(getProps()));
|
|
122
|
+
if (clean !== input.value) input.value = clean;
|
|
123
|
+
refresh();
|
|
124
|
+
};
|
|
125
|
+
const onKey = (event: KeyboardEvent): void => {
|
|
126
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
127
|
+
const spec = qtySpecOf(getProps());
|
|
128
|
+
const target = qtyKeyTarget(event.key, current(), spec);
|
|
129
|
+
// Home / End 没有对应界限时留给光标移动
|
|
130
|
+
if (target === null) {
|
|
131
|
+
if (['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown'].includes(event.key)) event.preventDefault();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
event.preventDefault();
|
|
135
|
+
if (locked()) return;
|
|
136
|
+
input.value = String(target);
|
|
137
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
138
|
+
};
|
|
139
|
+
input.addEventListener('input', onInput);
|
|
140
|
+
input.addEventListener('keydown', onKey);
|
|
141
|
+
refresh();
|
|
142
|
+
return {
|
|
143
|
+
refresh,
|
|
144
|
+
destroy() {
|
|
145
|
+
input.removeEventListener('input', onInput);
|
|
146
|
+
input.removeEventListener('keydown', onKey);
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface StepperHandle {
|
|
152
|
+
/** 按 props 画:stepper 形态包上分组与按钮,否则还原 */
|
|
153
|
+
sync(p: KitProps): void;
|
|
154
|
+
/** 当前是否处于步进器形态(Number 据此决定自己的类串) */
|
|
155
|
+
active(): boolean;
|
|
156
|
+
/** 输入值被程序改写(如夹紧)后刷新 aria-valuenow / min / max */
|
|
157
|
+
refresh(): void;
|
|
158
|
+
destroy(): void;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function isStepperProps(p: KitProps): boolean {
|
|
162
|
+
return p.variant === 'stepper';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 给 Number 的 input 挂数字输入能力(spinbutton)与步进器形态;变更值一律经 input 事件交给 Number 自己的处理 */
|
|
166
|
+
export function attachStepper(input: HTMLInputElement, getProps: () => KitProps): StepperHandle {
|
|
167
|
+
const spin = attachSpin(input, getProps);
|
|
168
|
+
let wrap: HTMLElement | null = null;
|
|
169
|
+
let dec: HTMLButtonElement | null = null;
|
|
170
|
+
let inc: HTMLButtonElement | null = null;
|
|
171
|
+
|
|
172
|
+
const current = (): number | null => {
|
|
173
|
+
const raw = input.value.trim();
|
|
174
|
+
return raw === '' ? null : num(raw);
|
|
175
|
+
};
|
|
176
|
+
const apply = (next: number | null): void => {
|
|
177
|
+
const p = getProps();
|
|
178
|
+
if (bool(p.disabled) || bool(p.readonly) || next === null) return;
|
|
179
|
+
input.value = String(next);
|
|
180
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
181
|
+
};
|
|
182
|
+
const onDec = (): void => apply(qtyStep(current(), -qtySpecOf(getProps()).step, qtySpecOf(getProps())));
|
|
183
|
+
const onInc = (): void => apply(qtyStep(current(), qtySpecOf(getProps()).step, qtySpecOf(getProps())));
|
|
184
|
+
|
|
185
|
+
const unwrap = (): void => {
|
|
186
|
+
if (!wrap) return;
|
|
187
|
+
wrap.parentNode?.insertBefore(input, wrap);
|
|
188
|
+
wrap.remove();
|
|
189
|
+
wrap = null;
|
|
190
|
+
dec = null;
|
|
191
|
+
inc = null;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const makeButton = (text: string, onClick: () => void): HTMLButtonElement => {
|
|
195
|
+
const b = document.createElement('button');
|
|
196
|
+
b.type = 'button';
|
|
197
|
+
b.textContent = text;
|
|
198
|
+
b.setAttribute('tabindex', '-1');
|
|
199
|
+
b.addEventListener('click', onClick);
|
|
200
|
+
return b;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
active: () => wrap !== null,
|
|
205
|
+
refresh: () => spin.refresh(),
|
|
206
|
+
sync(p) {
|
|
207
|
+
spin.refresh();
|
|
208
|
+
if (!isStepperProps(p)) {
|
|
209
|
+
unwrap();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!wrap) {
|
|
213
|
+
wrap = document.createElement('div');
|
|
214
|
+
wrap.setAttribute('role', 'group');
|
|
215
|
+
dec = makeButton('−', onDec);
|
|
216
|
+
inc = makeButton('+', onInc);
|
|
217
|
+
input.parentNode?.insertBefore(wrap, input);
|
|
218
|
+
wrap.append(dec, input, inc);
|
|
219
|
+
}
|
|
220
|
+
const spec = qtySpecOf(p);
|
|
221
|
+
const v = num(p.value);
|
|
222
|
+
const off = bool(p.disabled);
|
|
223
|
+
const stuck = off || bool(p.readonly);
|
|
224
|
+
if (dec && inc) {
|
|
225
|
+
dec.disabled = stuck || (v !== null && spec.min !== null && v <= spec.min);
|
|
226
|
+
inc.disabled = stuck || (v !== null && spec.max !== null && v >= spec.max);
|
|
227
|
+
dec.className = dec.disabled ? BTN_OFF : BTN_ON;
|
|
228
|
+
inc.className = inc.disabled ? BTN_OFF : BTN_ON;
|
|
229
|
+
dec.setAttribute('aria-label', str(p.decrease_label, kitText('soma.kit.qty_decrease', undefined, input)));
|
|
230
|
+
inc.setAttribute('aria-label', str(p.increase_label, kitText('soma.kit.qty_increase', undefined, input)));
|
|
231
|
+
}
|
|
232
|
+
wrap.className = off ? GROUP_OFF[sizeOf(p.size)] : GROUP[sizeOf(p.size)];
|
|
233
|
+
},
|
|
234
|
+
destroy() {
|
|
235
|
+
spin.destroy();
|
|
236
|
+
dec?.removeEventListener('click', onDec);
|
|
237
|
+
inc?.removeEventListener('click', onInc);
|
|
238
|
+
unwrap();
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|