@littlepress/renderer-web 0.0.0-stage → 1.0.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/LICENSE +202 -0
- package/NOTICE +20 -0
- package/README.md +229 -2
- package/dist/answer/answerComponents.d.ts +107 -0
- package/dist/answer/answerComponents.js +637 -0
- package/dist/answer/download.d.ts +63 -0
- package/dist/answer/download.js +109 -0
- package/dist/answer/downloadExport.d.ts +44 -0
- package/dist/answer/downloadExport.js +266 -0
- package/dist/answer/hostValues.d.ts +28 -0
- package/dist/answer/hostValues.js +379 -0
- package/dist/answer/mapComponent.d.ts +6 -0
- package/dist/answer/mapComponent.js +213 -0
- package/dist/answer/math.d.ts +57 -0
- package/dist/answer/math.js +168 -0
- package/dist/answer/surfaceStream.d.ts +51 -0
- package/dist/answer/surfaceStream.js +260 -0
- package/dist/answer/tableLayout.d.ts +42 -0
- package/dist/answer/tableLayout.js +89 -0
- package/dist/controls/ActionFeedback.d.ts +5 -0
- package/dist/controls/ActionFeedback.js +43 -0
- package/dist/controls/actionDispatch.d.ts +51 -0
- package/dist/controls/actionDispatch.js +211 -0
- package/dist/controls/actionOutcome.generated.d.ts +64 -0
- package/dist/controls/actionOutcome.generated.js +64 -0
- package/dist/controls/calculationPublisher.d.ts +14 -0
- package/dist/controls/calculationPublisher.js +6 -0
- package/dist/controls/calculations.d.ts +20 -0
- package/dist/controls/calculations.generated.d.ts +73 -0
- package/dist/controls/calculations.generated.js +85 -0
- package/dist/controls/calculations.js +208 -0
- package/dist/controls/controlState.d.ts +84 -0
- package/dist/controls/controlState.js +256 -0
- package/dist/controls/fieldMessages.d.ts +41 -0
- package/dist/controls/fieldMessages.js +81 -0
- package/dist/controls/fieldValidation.d.ts +19 -0
- package/dist/controls/fieldValidation.js +98 -0
- package/dist/controls/formComponents.d.ts +23 -0
- package/dist/controls/formComponents.js +738 -0
- package/dist/controls/formProgress.d.ts +63 -0
- package/dist/controls/formProgress.js +122 -0
- package/dist/controls/inputDrafts.d.ts +55 -0
- package/dist/controls/inputDrafts.js +183 -0
- package/dist/controls/inputValidation.d.ts +33 -0
- package/dist/controls/inputValidation.js +119 -0
- package/dist/controls/localWrites.d.ts +9 -0
- package/dist/controls/localWrites.js +29 -0
- package/dist/core/A2uiSurface.d.ts +13 -0
- package/dist/core/A2uiSurface.js +15 -0
- package/dist/core/AtlasIcon.d.ts +18 -0
- package/dist/core/AtlasIcon.js +22 -0
- package/dist/core/AtlasMedia.d.ts +22 -0
- package/dist/core/AtlasMedia.js +45 -0
- package/dist/core/Carousel.d.ts +61 -0
- package/dist/core/Carousel.js +183 -0
- package/dist/core/CoreMedia.d.ts +19 -0
- package/dist/core/CoreMedia.js +25 -0
- package/dist/core/FoundationA2uiSurface.d.ts +86 -0
- package/dist/core/FoundationA2uiSurface.js +440 -0
- package/dist/core/ImagePreview.d.ts +42 -0
- package/dist/core/ImagePreview.js +73 -0
- package/dist/core/LucideIcon.d.ts +19 -0
- package/dist/core/LucideIcon.js +42 -0
- package/dist/core/SurfaceLink.d.ts +35 -0
- package/dist/core/SurfaceLink.js +60 -0
- package/dist/core/appearance/aegis.d.ts +49 -0
- package/dist/core/appearance/aegis.js +34 -0
- package/dist/core/appearance/colour.d.ts +24 -0
- package/dist/core/appearance/colour.js +103 -0
- package/dist/core/appearance/css.d.ts +17 -0
- package/dist/core/appearance/css.js +153 -0
- package/dist/core/appearance/presets.generated.d.ts +4 -0
- package/dist/core/appearance/presets.generated.js +1184 -0
- package/dist/core/appearance/react.d.ts +29 -0
- package/dist/core/appearance/react.js +110 -0
- package/dist/core/appearance/resolve.d.ts +76 -0
- package/dist/core/appearance/resolve.js +127 -0
- package/dist/core/appearance/textScale.d.ts +11 -0
- package/dist/core/appearance/textScale.js +51 -0
- package/dist/core/appearance/theme.d.ts +138 -0
- package/dist/core/appearance/theme.js +274 -0
- package/dist/core/baseComponents.d.ts +53 -0
- package/dist/core/baseComponents.js +480 -0
- package/dist/core/binding.d.ts +46 -0
- package/dist/core/binding.js +118 -0
- package/dist/core/bundle.d.ts +23 -0
- package/dist/core/bundle.js +75 -0
- package/dist/core/catalog/catalog-index.generated.d.ts +44 -0
- package/dist/core/catalog/catalog-index.generated.js +79 -0
- package/dist/core/catalog/icons.generated.d.ts +6 -0
- package/dist/core/catalog/icons.generated.js +1788 -0
- package/dist/core/catalog.d.ts +6 -0
- package/dist/core/catalog.generated.d.ts +50 -0
- package/dist/core/catalog.generated.js +2174 -0
- package/dist/core/catalog.js +8 -0
- package/dist/core/chartRows.d.ts +7 -0
- package/dist/core/chartRows.js +38 -0
- package/dist/core/component-support.d.ts +39 -0
- package/dist/core/component-support.js +127 -0
- package/dist/core/components/actions.d.ts +4 -0
- package/dist/core/components/actions.js +21 -0
- package/dist/core/components/cards.d.ts +9 -0
- package/dist/core/components/cards.js +227 -0
- package/dist/core/components/charts.d.ts +18 -0
- package/dist/core/components/charts.js +412 -0
- package/dist/core/components/comparisonTable.d.ts +50 -0
- package/dist/core/components/comparisonTable.js +89 -0
- package/dist/core/components/content.d.ts +13 -0
- package/dist/core/components/content.js +108 -0
- package/dist/core/components/seriesCharts.d.ts +531 -0
- package/dist/core/components/seriesCharts.js +1053 -0
- package/dist/core/components/stepFlow.d.ts +7 -0
- package/dist/core/components/stepFlow.js +13 -0
- package/dist/core/components.full.d.ts +10 -0
- package/dist/core/components.full.js +41 -0
- package/dist/core/context.d.ts +207 -0
- package/dist/core/context.js +172 -0
- package/dist/core/coverage.d.ts +30 -0
- package/dist/core/coverage.js +93 -0
- package/dist/core/foundation.d.ts +21 -0
- package/dist/core/foundation.js +22 -0
- package/dist/core/fullTheme.d.ts +5 -0
- package/dist/core/fullTheme.js +9 -0
- package/dist/core/iconNodes.generated.d.ts +7 -0
- package/dist/core/iconNodes.generated.js +5 -0
- package/dist/core/lucideIcons.d.ts +5 -0
- package/dist/core/lucideIcons.js +7 -0
- package/dist/core/messages.d.ts +111 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/pointer.d.ts +17 -0
- package/dist/core/pointer.js +104 -0
- package/dist/core/registry.d.ts +12 -0
- package/dist/core/registry.js +114 -0
- package/dist/core/runtime.d.ts +8 -0
- package/dist/core/runtime.js +6 -0
- package/dist/core/state.d.ts +73 -0
- package/dist/core/state.js +274 -0
- package/dist/core/surfaceMotion.d.ts +5 -0
- package/dist/core/surfaceMotion.js +46 -0
- package/dist/core/theme.d.ts +5 -0
- package/dist/core/theme.js +7 -0
- package/dist/core/useSurfaceImage.d.ts +20 -0
- package/dist/core/useSurfaceImage.js +65 -0
- package/dist/graph/RelationshipGraph.d.ts +14 -0
- package/dist/graph/RelationshipGraph.js +206 -0
- package/dist/graph/model.d.ts +20 -0
- package/dist/graph/model.js +36 -0
- package/dist/graph/program/assets.d.ts +3 -0
- package/dist/graph/program/assets.js +3 -0
- package/dist/graph/program/bridge.d.ts +6 -0
- package/dist/graph/program/bridge.generated.d.ts +136 -0
- package/dist/graph/program/bridge.generated.js +1 -0
- package/dist/graph/program/bridge.js +24 -0
- package/dist/graph/program/bridge.schema.json +717 -0
- package/dist/graph/program/bridgeValidator.cjs +2 -0
- package/dist/graph/program/bridgeValidator.d.cts +4 -0
- package/dist/graph/program/runner.d.ts +1 -0
- package/dist/graph/program/runner.html +2193 -0
- package/dist/graph/program/runner.js +157 -0
- package/dist/graph/program/runtime-manifest.json +1 -0
- package/dist/graph/program/view.d.ts +11 -0
- package/dist/graph/program/view.js +210 -0
- package/dist/graph/program/worker.d.ts +1 -0
- package/dist/graph/program/worker.js +2186 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +45 -0
- package/dist/internal/dsh.d.ts +4 -0
- package/dist/internal/dsh.js +3 -0
- package/dist/internal/relationship-graph.d.ts +4 -0
- package/dist/internal/relationship-graph.js +7202 -0
- package/dist/page/layoutComponents.d.ts +6 -0
- package/dist/page/layoutComponents.js +123 -0
- package/dist/page/layoutGeometry.d.ts +68 -0
- package/dist/page/layoutGeometry.js +174 -0
- package/dist/page/pageFormat.d.ts +34 -0
- package/dist/page/pageFormat.js +119 -0
- package/dist/page/printFit.d.ts +81 -0
- package/dist/page/printFit.js +338 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.js +2 -0
- package/dist/report/locate.d.ts +42 -0
- package/dist/report/locate.js +34 -0
- package/dist/styles/renderer.css +4580 -0
- package/licenses/LUCIDE-LICENSE.txt +43 -0
- package/licenses/QUICKJS-EMSCRIPTEN-LICENSE.txt +21 -0
- package/licenses/QUICKJS-VARIANT-LICENSE.txt +47 -0
- package/package.json +68 -3
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { calculationSpec } from "./calculations.generated.js";
|
|
2
|
+
import { foundationCatalogId } from "../core/catalog.generated.js";
|
|
3
|
+
import { rendererCatalogId } from "../core/catalog/catalog-index.generated.js";
|
|
4
|
+
import { isDataBinding, isFunctionCall, isRecord } from "../core/messages.js";
|
|
5
|
+
const definitions = new Map(calculationSpec.functions.map(definition => [definition.name, definition]));
|
|
6
|
+
const maximum = BigInt(calculationSpec.limits.max_safe_integer);
|
|
7
|
+
const limits = { maxDepth: calculationSpec.limits.max_depth, maxCalls: calculationSpec.limits.max_calls_per_evaluation };
|
|
8
|
+
export function isCalculationCall(value) {
|
|
9
|
+
return isFunctionCall(value) && definitions.has(value.call);
|
|
10
|
+
}
|
|
11
|
+
export function isCalculationFailure(value) {
|
|
12
|
+
return isRecord(value) && value.kind === "calculation-error" && typeof value.code === "string";
|
|
13
|
+
}
|
|
14
|
+
export function calculationMessage(error, locale) {
|
|
15
|
+
if (error.message)
|
|
16
|
+
return error.message;
|
|
17
|
+
if (typeof locale === "string" && locale.toLowerCase().startsWith("zh")) {
|
|
18
|
+
switch (error.code) {
|
|
19
|
+
case "invalid_integer": return "请输入有效整数,或选择整数选项后重新计算。";
|
|
20
|
+
case "invalid_input": return "请修改输入或选择可用的选项后重新计算。";
|
|
21
|
+
case "division_by_zero": return "除数不能为 0,请修改除数后重新计算。";
|
|
22
|
+
case "calculation_overflow": return "结果超出支持的计算范围,请减小输入后重试。";
|
|
23
|
+
case "unsupported_currency": return "当前币种不支持本地计算。";
|
|
24
|
+
case "calculation_budget": return "计算超过支持的大小,请重新生成内容。";
|
|
25
|
+
default: return "无法计算,请重新生成内容。";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
switch (error.code) {
|
|
29
|
+
case "invalid_integer": return "Enter a valid whole number or choose an integer option to calculate this amount.";
|
|
30
|
+
case "invalid_input": return "Correct the input or choose an available option to calculate this amount.";
|
|
31
|
+
case "division_by_zero": return "The divisor cannot be zero. Change the divisor to calculate this amount.";
|
|
32
|
+
case "calculation_overflow": return "This result exceeds the supported calculation range. Reduce the input and try again.";
|
|
33
|
+
case "unsupported_currency": return "This currency is not supported for local calculation.";
|
|
34
|
+
case "calculation_budget": return "This calculation exceeds the supported size.";
|
|
35
|
+
default: return "This amount cannot be calculated. Generate the content again.";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function failed(code, path) {
|
|
39
|
+
return { ok: false, error: { kind: "calculation-error", code, ...(path ? { path } : {}) } };
|
|
40
|
+
}
|
|
41
|
+
function abs(value) { return value < 0n ? -value : value; }
|
|
42
|
+
function gcd(left, right) {
|
|
43
|
+
while (right !== 0n)
|
|
44
|
+
[left, right] = [right, left % right];
|
|
45
|
+
return abs(left);
|
|
46
|
+
}
|
|
47
|
+
function rational(numerator, denominator = 1n) {
|
|
48
|
+
if (denominator === 0n)
|
|
49
|
+
return failed("division_by_zero");
|
|
50
|
+
if (denominator < 0n) {
|
|
51
|
+
numerator = -numerator;
|
|
52
|
+
denominator = -denominator;
|
|
53
|
+
}
|
|
54
|
+
const divisor = gcd(numerator, denominator);
|
|
55
|
+
numerator /= divisor;
|
|
56
|
+
denominator /= divisor;
|
|
57
|
+
return abs(numerator) > maximum || denominator > maximum
|
|
58
|
+
? failed("calculation_overflow")
|
|
59
|
+
: { ok: true, value: { numerator, denominator } };
|
|
60
|
+
}
|
|
61
|
+
function integer(value) {
|
|
62
|
+
if (typeof value === "number") {
|
|
63
|
+
if (!Number.isInteger(value))
|
|
64
|
+
return failed("invalid_integer");
|
|
65
|
+
return Number.isSafeInteger(value) ? rational(BigInt(value)) : failed("calculation_overflow");
|
|
66
|
+
}
|
|
67
|
+
if (typeof value !== "string" || value.length > 512 || !/^[+-]?[0-9]+$/.test(value))
|
|
68
|
+
return failed("invalid_integer");
|
|
69
|
+
const parsed = BigInt(value);
|
|
70
|
+
return abs(parsed) <= maximum ? rational(parsed) : failed("calculation_overflow");
|
|
71
|
+
}
|
|
72
|
+
function rounded(value) {
|
|
73
|
+
const magnitude = abs(value.numerator);
|
|
74
|
+
const quotient = magnitude / value.denominator;
|
|
75
|
+
const bump = (magnitude % value.denominator) * 2n >= value.denominator ? 1n : 0n;
|
|
76
|
+
return (value.numerator < 0n ? -1n : 1n) * (quotient + bump);
|
|
77
|
+
}
|
|
78
|
+
function money(value) {
|
|
79
|
+
const minor = rounded(value);
|
|
80
|
+
const absolute = abs(minor);
|
|
81
|
+
return `CNY ${minor < 0n ? "-" : ""}${absolute / 100n}.${String(absolute % 100n).padStart(2, "0")}`;
|
|
82
|
+
}
|
|
83
|
+
/** Keep terminal display exact; recurring decimals use the reduced fraction. */
|
|
84
|
+
function displayRational(value) {
|
|
85
|
+
if (value.denominator === 1n)
|
|
86
|
+
return Number(value.numerator);
|
|
87
|
+
let factors = value.denominator;
|
|
88
|
+
for (const prime of [2n, 5n])
|
|
89
|
+
while (factors % prime === 0n)
|
|
90
|
+
factors /= prime;
|
|
91
|
+
if (factors !== 1n)
|
|
92
|
+
return `${value.numerator}/${value.denominator}`;
|
|
93
|
+
const magnitude = abs(value.numerator);
|
|
94
|
+
let remainder = magnitude % value.denominator;
|
|
95
|
+
let decimal = "";
|
|
96
|
+
// With a safe-integer denominator made of 2s and 5s this terminates in at most 53 digits.
|
|
97
|
+
while (remainder !== 0n) {
|
|
98
|
+
remainder *= 10n;
|
|
99
|
+
decimal += String(remainder / value.denominator);
|
|
100
|
+
remainder %= value.denominator;
|
|
101
|
+
}
|
|
102
|
+
return `${value.numerator < 0n ? "-" : ""}${magnitude / value.denominator}.${decimal}`;
|
|
103
|
+
}
|
|
104
|
+
/** One input snapshot owns this budget and cache. A new edit or Host update gets a new session. */
|
|
105
|
+
export function createCalculationSession(read) {
|
|
106
|
+
const cache = new Map();
|
|
107
|
+
let calls = 0;
|
|
108
|
+
function evaluate(value, itemPointer, depth) {
|
|
109
|
+
if (isDataBinding(value)) {
|
|
110
|
+
if (Object.keys(value).some(key => key !== "path"))
|
|
111
|
+
return failed("invalid_calculation");
|
|
112
|
+
const resolved = read(value.path, itemPointer);
|
|
113
|
+
if ("error" in resolved)
|
|
114
|
+
return { ok: false, error: resolved.error };
|
|
115
|
+
// Strings are only consumed by parseInteger or the currency parameter.
|
|
116
|
+
if (typeof resolved.value === "string")
|
|
117
|
+
return { ok: true, value: resolved.value };
|
|
118
|
+
const result = integer(resolved.value);
|
|
119
|
+
return result.ok ? result : { ok: false, error: { ...result.error, path: value.path } };
|
|
120
|
+
}
|
|
121
|
+
if (typeof value === "number")
|
|
122
|
+
return integer(value);
|
|
123
|
+
if (typeof value === "string")
|
|
124
|
+
return { ok: true, value };
|
|
125
|
+
if (!isFunctionCall(value) || !definitions.has(value.call) || !isRecord(value.args)
|
|
126
|
+
|| (value.catalogId !== undefined && ![calculationSpec.catalog.id, foundationCatalogId, rendererCatalogId].includes(value.catalogId))
|
|
127
|
+
|| Object.keys(value).some(key => !["call", "args", "catalogId"].includes(key)))
|
|
128
|
+
return failed("invalid_calculation");
|
|
129
|
+
if (depth >= limits.maxDepth || ++calls > limits.maxCalls)
|
|
130
|
+
return failed("calculation_budget");
|
|
131
|
+
const fields = Object.keys(definitions.get(value.call).arguments);
|
|
132
|
+
if (Object.keys(value.args).length !== fields.length || fields.some(field => !(field in value.args)))
|
|
133
|
+
return failed("invalid_calculation");
|
|
134
|
+
const args = [];
|
|
135
|
+
for (const field of fields) {
|
|
136
|
+
const argument = evaluate(value.args[field], itemPointer, depth + 1);
|
|
137
|
+
if (!argument.ok)
|
|
138
|
+
return argument;
|
|
139
|
+
args.push(argument.value);
|
|
140
|
+
}
|
|
141
|
+
const [left, right] = args;
|
|
142
|
+
if (value.call === "parseInteger") {
|
|
143
|
+
return typeof left === "string" ? integer(left)
|
|
144
|
+
: left.denominator === 1n ? { ok: true, value: left } : failed("invalid_integer");
|
|
145
|
+
}
|
|
146
|
+
if (value.call === "formatMoney") {
|
|
147
|
+
if (right !== "CNY")
|
|
148
|
+
return failed("unsupported_currency");
|
|
149
|
+
return typeof left === "string" ? failed("invalid_calculation") : { ok: true, value: money(left) };
|
|
150
|
+
}
|
|
151
|
+
if (typeof left === "string")
|
|
152
|
+
return failed("invalid_calculation");
|
|
153
|
+
if (value.call === "roundMinor")
|
|
154
|
+
return rational(rounded(left));
|
|
155
|
+
if (typeof right === "string")
|
|
156
|
+
return failed("invalid_calculation");
|
|
157
|
+
switch (value.call) {
|
|
158
|
+
case "add": return rational(left.numerator * right.denominator + right.numerator * left.denominator, left.denominator * right.denominator);
|
|
159
|
+
case "subtract": return rational(left.numerator * right.denominator - right.numerator * left.denominator, left.denominator * right.denominator);
|
|
160
|
+
case "multiply": return rational(left.numerator * right.numerator, left.denominator * right.denominator);
|
|
161
|
+
case "divide": return rational(left.numerator * right.denominator, left.denominator * right.numerator);
|
|
162
|
+
default: return failed("invalid_calculation");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
resolve(value, itemPointer = null, numeric = false) {
|
|
167
|
+
// The static tree is limited before stringification as imported JSON need not have passed the server.
|
|
168
|
+
const seen = new Set();
|
|
169
|
+
let forbiddenLiteral = false;
|
|
170
|
+
const literals = calculationSpec.literal_arguments;
|
|
171
|
+
function bounded(node, depth, count) {
|
|
172
|
+
if (++count.value > limits.maxCalls * 8 || depth > limits.maxDepth * 2 + 4)
|
|
173
|
+
return false;
|
|
174
|
+
if (!isRecord(node) && !Array.isArray(node))
|
|
175
|
+
return true;
|
|
176
|
+
if (seen.has(node))
|
|
177
|
+
return false;
|
|
178
|
+
seen.add(node);
|
|
179
|
+
if (isFunctionCall(node) && definitions.has(node.call) && isRecord(node.args)) {
|
|
180
|
+
for (const [name, argument] of Object.entries(node.args)) {
|
|
181
|
+
if (!isDataBinding(argument) && !isFunctionCall(argument)
|
|
182
|
+
&& (typeof argument !== "number" || !literals[node.call]?.[name]?.includes(argument)))
|
|
183
|
+
forbiddenLiteral = true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const okay = Object.values(node).every(child => bounded(child, depth + 1, count));
|
|
187
|
+
seen.delete(node);
|
|
188
|
+
return okay;
|
|
189
|
+
}
|
|
190
|
+
if (!bounded(value, 0, { value: 0 }))
|
|
191
|
+
return { kind: "calculation-error", code: "calculation_budget" };
|
|
192
|
+
// Inspect every branch before an unfinished draft can short-circuit evaluation.
|
|
193
|
+
if (forbiddenLiteral)
|
|
194
|
+
return { kind: "calculation-error", code: "invalid_calculation" };
|
|
195
|
+
const key = JSON.stringify([itemPointer, value]);
|
|
196
|
+
let result = cache.get(key);
|
|
197
|
+
if (!result) {
|
|
198
|
+
result = evaluate(value, itemPointer, 0);
|
|
199
|
+
cache.set(key, result);
|
|
200
|
+
}
|
|
201
|
+
if (!result.ok)
|
|
202
|
+
return result.error;
|
|
203
|
+
if (typeof result.value === "string")
|
|
204
|
+
return result.value;
|
|
205
|
+
return numeric ? Number(result.value.numerator) / Number(result.value.denominator) : displayRational(result.value);
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type ControlOption = {
|
|
2
|
+
value: string;
|
|
3
|
+
label: string;
|
|
4
|
+
disabled: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type SliderMark = {
|
|
7
|
+
value: number;
|
|
8
|
+
label?: string;
|
|
9
|
+
ratio: number;
|
|
10
|
+
};
|
|
11
|
+
export type BoundedInputType = "date" | "time" | "datetime-local";
|
|
12
|
+
export type InputBoundError = "below_min" | "above_max";
|
|
13
|
+
/** Keep malformed remote options out of native controls; validation rejects them earlier. */
|
|
14
|
+
export declare function normalizeControlOptions(value: unknown): ControlOption[];
|
|
15
|
+
export declare function toggleChoiceValue(current: readonly string[], value: string, checked: boolean): string[];
|
|
16
|
+
export declare function sliderValueIsValid(value: number, min: number, max: number, step: number): boolean;
|
|
17
|
+
/** Normalize optional slider marks into sorted, deduplicated entries within [min, max]. */
|
|
18
|
+
export declare function normalizeSliderMarks(raw: unknown, min: number, max: number): SliderMark[];
|
|
19
|
+
/** Format the slider's live value readout: prefer an exact matching mark's label when present. */
|
|
20
|
+
export declare function formatSliderValue(value: number, marks: readonly SliderMark[]): string;
|
|
21
|
+
export type SliderMarkLabelMetric = {
|
|
22
|
+
ratio: number;
|
|
23
|
+
width: number;
|
|
24
|
+
active: boolean;
|
|
25
|
+
};
|
|
26
|
+
export type SliderMarkLabelPlacement = {
|
|
27
|
+
visible: ReadonlySet<number>;
|
|
28
|
+
lefts: ReadonlyMap<number, number>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Choose which slider mark labels fit below the track with zero horizontal overlap.
|
|
32
|
+
*
|
|
33
|
+
* When all labeled marks fit with at least `gap` spacing, every label is kept.
|
|
34
|
+
* When space is tight (e.g., 320px viewports with multi-word labels), every tick mark
|
|
35
|
+
* still renders along the track, while labels narrow to the first mark, the last mark,
|
|
36
|
+
* and the active mark (dropping the active mark under the track only if it would still
|
|
37
|
+
* collide with an endpoint — the live readout above the track already announces it).
|
|
38
|
+
*/
|
|
39
|
+
export declare function selectVisibleSliderMarkLabels(marks: readonly SliderMarkLabelMetric[], containerWidth: number, gap?: number, inset?: number): SliderMarkLabelPlacement;
|
|
40
|
+
export declare function inputBoundError(value: string, inputType: string, min?: string, max?: string): InputBoundError | null;
|
|
41
|
+
export declare function firstInvalidPanelIndex<T>(panels: readonly ({
|
|
42
|
+
contains(node: T): boolean;
|
|
43
|
+
} | null)[], invalidTarget: T, firstInvalid: T | null): number | null;
|
|
44
|
+
/**
|
|
45
|
+
* Whether a form's submit control is pressable.
|
|
46
|
+
*
|
|
47
|
+
* Validity is deliberately not part of this: a form with an empty required field keeps
|
|
48
|
+
* its submit control, and pressing it raises no Host action and moves focus to the first
|
|
49
|
+
* field that failed. Only the Host's own `disabled`, a missing submit action, or a
|
|
50
|
+
* submission already in flight take the control away. The Compose and SwiftUI renderers
|
|
51
|
+
* apply the same rule (`formSubmitIsEnabled` / `staticFormSubmitIsEnabled`), so one
|
|
52
|
+
* bundle offers the same affordance on every platform.
|
|
53
|
+
*
|
|
54
|
+
* `paused` is Web only: a field in the form holds a stale draft, an edit a refresh made
|
|
55
|
+
* impossible, such as a choice the Host has since removed. The native renderers keep no
|
|
56
|
+
* drafts across a refresh, so they never pause.
|
|
57
|
+
*/
|
|
58
|
+
export declare function formSubmitIsEnabled(state: {
|
|
59
|
+
hasSubmitAction: boolean;
|
|
60
|
+
disabled: boolean;
|
|
61
|
+
pending: boolean;
|
|
62
|
+
paused: boolean;
|
|
63
|
+
}): boolean;
|
|
64
|
+
export type InputEntry = "text" | "multiline" | "password" | "email" | "url" | "phone" | "number" | "date" | "time" | "datetime";
|
|
65
|
+
export type InputEntryHints = {
|
|
66
|
+
entry: InputEntry;
|
|
67
|
+
inputMode?: "email" | "url" | "tel" | "decimal" | "numeric";
|
|
68
|
+
autoComplete?: "email" | "url" | "tel" | "new-password";
|
|
69
|
+
autoCapitalize?: "none";
|
|
70
|
+
spellCheck?: false;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* What a user types into an `lp.input-field`, and the keyboard hints that follow from it.
|
|
74
|
+
*
|
|
75
|
+
* `validation` wins over `inputType` because it is what the field accepts: a `text` field
|
|
76
|
+
* validated as `phone-sg` still wants the phone keypad, so it gets `inputmode="tel"`.
|
|
77
|
+
* Where the native `type` already picks the keyboard (`number`, `date`, `time`), no
|
|
78
|
+
* `inputmode` overrides it; iOS's decimal pad, for one, has no minus sign. Structured
|
|
79
|
+
* values turn off spellcheck and capitalisation, and addresses and phone numbers name
|
|
80
|
+
* their autofill token. A password is a secret whatever its `validation`: nothing corrects
|
|
81
|
+
* or capitalises it, and `new-password` keeps the browser from filling a saved credential
|
|
82
|
+
* into an answer's form. Compose and SwiftUI classify the same way (`staticInputEntry`).
|
|
83
|
+
*/
|
|
84
|
+
export declare function inputEntryHints(inputType: string, validation: unknown): InputEntryHints;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { isRecord } from "../core/messages.js";
|
|
2
|
+
const dateInputBound = /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/u;
|
|
3
|
+
const timeInputBound = /^((?:[01][0-9]|2[0-3])):([0-5][0-9])(?::([0-5][0-9])(?:\.([0-9]{1,3}))?)?$/u;
|
|
4
|
+
function normalizeTimeInputBound(value) {
|
|
5
|
+
const match = timeInputBound.exec(value);
|
|
6
|
+
if (!match)
|
|
7
|
+
return undefined;
|
|
8
|
+
return `${match[1]}:${match[2]}:${match[3] ?? "00"}.${(match[4] ?? "").padEnd(3, "0")}`;
|
|
9
|
+
}
|
|
10
|
+
function normalizeInputBound(value, inputType) {
|
|
11
|
+
if (inputType === "date")
|
|
12
|
+
return dateInputBound.test(value) ? value : undefined;
|
|
13
|
+
if (inputType === "time")
|
|
14
|
+
return normalizeTimeInputBound(value);
|
|
15
|
+
if (inputType === "datetime-local") {
|
|
16
|
+
const separator = value.indexOf("T");
|
|
17
|
+
const date = value.slice(0, separator);
|
|
18
|
+
const time = normalizeTimeInputBound(value.slice(separator + 1));
|
|
19
|
+
return separator > 0 && dateInputBound.test(date) && time ? `${date}T${time}` : undefined;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Keep malformed remote options out of native controls; validation rejects them earlier. */
|
|
24
|
+
export function normalizeControlOptions(value) {
|
|
25
|
+
if (!Array.isArray(value))
|
|
26
|
+
return [];
|
|
27
|
+
return value.flatMap((entry) => {
|
|
28
|
+
if (!isRecord(entry) || typeof entry.value !== "string" || typeof entry.label !== "string")
|
|
29
|
+
return [];
|
|
30
|
+
if (entry.value.length === 0 || entry.label.length === 0)
|
|
31
|
+
return [];
|
|
32
|
+
return [{ value: entry.value, label: entry.label, disabled: entry.disabled === true }];
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export function toggleChoiceValue(current, value, checked) {
|
|
36
|
+
if (checked)
|
|
37
|
+
return current.includes(value) ? [...current] : [...current, value];
|
|
38
|
+
return current.filter((entry) => entry !== value);
|
|
39
|
+
}
|
|
40
|
+
export function sliderValueIsValid(value, min, max, step) {
|
|
41
|
+
if (![value, min, max, step].every(Number.isFinite) || min >= max || step <= 0)
|
|
42
|
+
return false;
|
|
43
|
+
if (value < min || value > max)
|
|
44
|
+
return false;
|
|
45
|
+
const steps = (value - min) / step;
|
|
46
|
+
return Math.abs(steps - Math.round(steps)) <= 1e-9;
|
|
47
|
+
}
|
|
48
|
+
/** Normalize optional slider marks into sorted, deduplicated entries within [min, max]. */
|
|
49
|
+
export function normalizeSliderMarks(raw, min, max) {
|
|
50
|
+
if (!Array.isArray(raw) || !Number.isFinite(min) || !Number.isFinite(max) || max <= min)
|
|
51
|
+
return [];
|
|
52
|
+
const span = max - min;
|
|
53
|
+
const parsed = [];
|
|
54
|
+
for (const entry of raw) {
|
|
55
|
+
if (!isRecord(entry) || typeof entry.value !== "number" || !Number.isFinite(entry.value))
|
|
56
|
+
continue;
|
|
57
|
+
if (entry.value < min || entry.value > max)
|
|
58
|
+
continue;
|
|
59
|
+
const label = typeof entry.label === "string" && entry.label.trim().length > 0 ? entry.label : undefined;
|
|
60
|
+
parsed.push({
|
|
61
|
+
value: entry.value,
|
|
62
|
+
...(label !== undefined ? { label } : {}),
|
|
63
|
+
ratio: Math.min(1, Math.max(0, (entry.value - min) / span)),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
parsed.sort((a, b) => a.value - b.value);
|
|
67
|
+
const deduped = [];
|
|
68
|
+
for (const mark of parsed) {
|
|
69
|
+
const prev = deduped[deduped.length - 1];
|
|
70
|
+
if (prev && Math.abs(prev.value - mark.value) <= 1e-9) {
|
|
71
|
+
if (!prev.label && mark.label)
|
|
72
|
+
deduped[deduped.length - 1] = mark;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
deduped.push(mark);
|
|
76
|
+
}
|
|
77
|
+
return deduped;
|
|
78
|
+
}
|
|
79
|
+
/** Format the slider's live value readout: prefer an exact matching mark's label when present. */
|
|
80
|
+
export function formatSliderValue(value, marks) {
|
|
81
|
+
const match = marks.find((mark) => Math.abs(mark.value - value) <= 1e-9 && typeof mark.label === "string");
|
|
82
|
+
return match?.label ?? String(value);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Choose which slider mark labels fit below the track with zero horizontal overlap.
|
|
86
|
+
*
|
|
87
|
+
* When all labeled marks fit with at least `gap` spacing, every label is kept.
|
|
88
|
+
* When space is tight (e.g., 320px viewports with multi-word labels), every tick mark
|
|
89
|
+
* still renders along the track, while labels narrow to the first mark, the last mark,
|
|
90
|
+
* and the active mark (dropping the active mark under the track only if it would still
|
|
91
|
+
* collide with an endpoint — the live readout above the track already announces it).
|
|
92
|
+
*/
|
|
93
|
+
export function selectVisibleSliderMarkLabels(marks, containerWidth, gap = 8, inset = 0) {
|
|
94
|
+
const labeled = marks
|
|
95
|
+
.map((mark, index) => ({ ...mark, index }))
|
|
96
|
+
.filter((mark) => mark.width > 0);
|
|
97
|
+
if (labeled.length === 0)
|
|
98
|
+
return { visible: new Set(), lefts: new Map() };
|
|
99
|
+
if (!(containerWidth > 0)) {
|
|
100
|
+
return { visible: new Set(labeled.map((mark) => mark.index)), lefts: new Map() };
|
|
101
|
+
}
|
|
102
|
+
const effectiveInset = Math.max(0, Math.min(inset, containerWidth / 2));
|
|
103
|
+
const trackWidth = Math.max(0, containerWidth - effectiveInset * 2);
|
|
104
|
+
const spanOf = (mark) => {
|
|
105
|
+
let rawLeft;
|
|
106
|
+
if (mark.ratio <= 0.01)
|
|
107
|
+
rawLeft = 0;
|
|
108
|
+
else if (mark.ratio >= 0.99)
|
|
109
|
+
rawLeft = containerWidth - mark.width;
|
|
110
|
+
else
|
|
111
|
+
rawLeft = effectiveInset + trackWidth * mark.ratio - mark.width / 2;
|
|
112
|
+
const left = Math.max(0, Math.min(rawLeft, Math.max(0, containerWidth - mark.width)));
|
|
113
|
+
return { left, right: left + mark.width };
|
|
114
|
+
};
|
|
115
|
+
const spans = new Map();
|
|
116
|
+
for (const mark of labeled)
|
|
117
|
+
spans.set(mark.index, spanOf(mark));
|
|
118
|
+
const fits = (indices) => {
|
|
119
|
+
for (const index of indices) {
|
|
120
|
+
if (marks[index].width > containerWidth)
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
for (let i = 1; i < indices.length; i += 1) {
|
|
124
|
+
const prev = spans.get(indices[i - 1]);
|
|
125
|
+
const curr = spans.get(indices[i]);
|
|
126
|
+
if (prev.right + gap > curr.left)
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
};
|
|
131
|
+
const build = (indices) => {
|
|
132
|
+
const visible = new Set(indices);
|
|
133
|
+
const lefts = new Map();
|
|
134
|
+
for (const index of indices)
|
|
135
|
+
lefts.set(index, spans.get(index).left);
|
|
136
|
+
return { visible, lefts };
|
|
137
|
+
};
|
|
138
|
+
const allIndices = labeled.map((mark) => mark.index);
|
|
139
|
+
if (fits(allIndices))
|
|
140
|
+
return build(allIndices);
|
|
141
|
+
const first = labeled[0].index;
|
|
142
|
+
const last = labeled[labeled.length - 1].index;
|
|
143
|
+
const active = labeled.find((mark) => mark.active)?.index;
|
|
144
|
+
const candidates = [first];
|
|
145
|
+
if (active !== undefined && active !== first && active !== last)
|
|
146
|
+
candidates.push(active);
|
|
147
|
+
if (last !== first)
|
|
148
|
+
candidates.push(last);
|
|
149
|
+
if (fits(candidates))
|
|
150
|
+
return build(candidates);
|
|
151
|
+
const endpoints = last !== first ? [first, last] : [first];
|
|
152
|
+
if (fits(endpoints))
|
|
153
|
+
return build(endpoints);
|
|
154
|
+
if (active !== undefined && fits([active]))
|
|
155
|
+
return build([active]);
|
|
156
|
+
if (fits([first]))
|
|
157
|
+
return build([first]);
|
|
158
|
+
return { visible: new Set(), lefts: new Map() };
|
|
159
|
+
}
|
|
160
|
+
export function inputBoundError(value, inputType, min, max) {
|
|
161
|
+
if (value.length === 0 || !["date", "time", "datetime-local"].includes(inputType))
|
|
162
|
+
return null;
|
|
163
|
+
const normalized = normalizeInputBound(value, inputType);
|
|
164
|
+
if (normalized === undefined)
|
|
165
|
+
return null;
|
|
166
|
+
if (min && normalized < normalizeInputBound(min, inputType))
|
|
167
|
+
return "below_min";
|
|
168
|
+
if (max && normalized > normalizeInputBound(max, inputType))
|
|
169
|
+
return "above_max";
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
export function firstInvalidPanelIndex(panels, invalidTarget, firstInvalid) {
|
|
173
|
+
if (firstInvalid !== invalidTarget)
|
|
174
|
+
return null;
|
|
175
|
+
const index = panels.findIndex((panel) => panel?.contains(invalidTarget) === true);
|
|
176
|
+
return index >= 0 ? index : null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Whether a form's submit control is pressable.
|
|
180
|
+
*
|
|
181
|
+
* Validity is deliberately not part of this: a form with an empty required field keeps
|
|
182
|
+
* its submit control, and pressing it raises no Host action and moves focus to the first
|
|
183
|
+
* field that failed. Only the Host's own `disabled`, a missing submit action, or a
|
|
184
|
+
* submission already in flight take the control away. The Compose and SwiftUI renderers
|
|
185
|
+
* apply the same rule (`formSubmitIsEnabled` / `staticFormSubmitIsEnabled`), so one
|
|
186
|
+
* bundle offers the same affordance on every platform.
|
|
187
|
+
*
|
|
188
|
+
* `paused` is Web only: a field in the form holds a stale draft, an edit a refresh made
|
|
189
|
+
* impossible, such as a choice the Host has since removed. The native renderers keep no
|
|
190
|
+
* drafts across a refresh, so they never pause.
|
|
191
|
+
*/
|
|
192
|
+
export function formSubmitIsEnabled(state) {
|
|
193
|
+
return state.hasSubmitAction && !state.disabled && !state.pending && !state.paused;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* What a user types into an `lp.input-field`, and the keyboard hints that follow from it.
|
|
197
|
+
*
|
|
198
|
+
* `validation` wins over `inputType` because it is what the field accepts: a `text` field
|
|
199
|
+
* validated as `phone-sg` still wants the phone keypad, so it gets `inputmode="tel"`.
|
|
200
|
+
* Where the native `type` already picks the keyboard (`number`, `date`, `time`), no
|
|
201
|
+
* `inputmode` overrides it; iOS's decimal pad, for one, has no minus sign. Structured
|
|
202
|
+
* values turn off spellcheck and capitalisation, and addresses and phone numbers name
|
|
203
|
+
* their autofill token. A password is a secret whatever its `validation`: nothing corrects
|
|
204
|
+
* or capitalises it, and `new-password` keeps the browser from filling a saved credential
|
|
205
|
+
* into an answer's form. Compose and SwiftUI classify the same way (`staticInputEntry`).
|
|
206
|
+
*/
|
|
207
|
+
export function inputEntryHints(inputType, validation) {
|
|
208
|
+
const entry = inputEntry(inputType, validation);
|
|
209
|
+
const structured = { autoCapitalize: "none", spellCheck: false };
|
|
210
|
+
if (validation === "integer" && entry !== "password")
|
|
211
|
+
return { entry: "number", inputMode: "numeric", ...structured };
|
|
212
|
+
switch (entry) {
|
|
213
|
+
case "password":
|
|
214
|
+
return { entry, autoComplete: "new-password", ...structured };
|
|
215
|
+
case "email":
|
|
216
|
+
return { entry, inputMode: "email", autoComplete: "email", ...structured };
|
|
217
|
+
case "url":
|
|
218
|
+
return { entry, inputMode: "url", autoComplete: "url", ...structured };
|
|
219
|
+
case "phone":
|
|
220
|
+
return { entry, inputMode: "tel", autoComplete: "tel", ...structured };
|
|
221
|
+
case "number":
|
|
222
|
+
return inputType === "number" ? { entry, ...structured } : { entry, inputMode: "decimal", ...structured };
|
|
223
|
+
case "date":
|
|
224
|
+
case "time":
|
|
225
|
+
case "datetime":
|
|
226
|
+
return { entry, ...structured };
|
|
227
|
+
default:
|
|
228
|
+
return { entry };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function inputEntry(inputType, validation) {
|
|
232
|
+
if (inputType === "textarea")
|
|
233
|
+
return "multiline";
|
|
234
|
+
if (inputType === "password")
|
|
235
|
+
return "password";
|
|
236
|
+
if (validation === "email")
|
|
237
|
+
return "email";
|
|
238
|
+
if (validation === "url")
|
|
239
|
+
return "url";
|
|
240
|
+
if (typeof validation === "string" && validation.startsWith("phone-"))
|
|
241
|
+
return "phone";
|
|
242
|
+
if (validation === "number")
|
|
243
|
+
return "number";
|
|
244
|
+
if (validation === "iso-date")
|
|
245
|
+
return "date";
|
|
246
|
+
switch (inputType) {
|
|
247
|
+
case "email": return "email";
|
|
248
|
+
case "url": return "url";
|
|
249
|
+
case "tel": return "phone";
|
|
250
|
+
case "number": return "number";
|
|
251
|
+
case "date": return "date";
|
|
252
|
+
case "time": return "time";
|
|
253
|
+
case "datetime-local": return "datetime";
|
|
254
|
+
default: return "text";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Display text for field and calculation failures.
|
|
3
|
+
*
|
|
4
|
+
* The rules themselves stay language-neutral: `validateFieldValue` and the calculation
|
|
5
|
+
* runtime return stable error codes with an English message, and the shared renderer case
|
|
6
|
+
* table asserts those codes. Only the text shown to a person is translated here, and only
|
|
7
|
+
* for the integer profile this feature introduced; other built-in prompts keep their
|
|
8
|
+
* English text until the client-wide prompt language is decided separately.
|
|
9
|
+
*/
|
|
10
|
+
/** The shape both the shared rules and the renderer's own checks return. */
|
|
11
|
+
export type FieldMessageInput = {
|
|
12
|
+
errorCode: string;
|
|
13
|
+
message: string;
|
|
14
|
+
};
|
|
15
|
+
type Copy = {
|
|
16
|
+
invalidInteger: string;
|
|
17
|
+
range: (minimum?: number, maximum?: number) => string;
|
|
18
|
+
step: (step: number, minimum: number) => string;
|
|
19
|
+
fieldPrefix: (label: string, message: string) => string;
|
|
20
|
+
unavailable: string;
|
|
21
|
+
unavailableLong: string;
|
|
22
|
+
showPassword: string;
|
|
23
|
+
hidePassword: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The surface language. Compiled artifacts carry `language`; imported or fixture artifacts
|
|
27
|
+
* carry `locale`, so both keys are read.
|
|
28
|
+
*/
|
|
29
|
+
export declare function surfaceLanguage(hints: Record<string, unknown> | undefined): unknown;
|
|
30
|
+
export declare function isChineseLanguage(locale: unknown): boolean;
|
|
31
|
+
/** The copy table for a surface locale. Only Chinese is translated; anything else is English. */
|
|
32
|
+
export declare function fieldCopy(locale: unknown): Copy;
|
|
33
|
+
/**
|
|
34
|
+
* Shown text for one failed field check. Only the integer profile has localised copy; other
|
|
35
|
+
* codes keep the English message from the rule.
|
|
36
|
+
*/
|
|
37
|
+
export declare function fieldErrorMessage(error: FieldMessageInput, props: Record<string, unknown>, locale: unknown): string;
|
|
38
|
+
/** One phrase for "these amounts cannot be calculated", shared so a live region says it once. */
|
|
39
|
+
export declare function amountsUnavailable(locale: unknown): string;
|
|
40
|
+
export declare function fieldMessageWithLabel(label: string | null, message: string, locale: unknown): string;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const copy = {
|
|
2
|
+
zh: {
|
|
3
|
+
invalidInteger: "请输入整数,不要有空格、小数点或指数写法。",
|
|
4
|
+
range: (minimum, maximum) => minimum !== undefined && maximum !== undefined
|
|
5
|
+
? `请输入 ${minimum} 到 ${maximum} 之间的整数。`
|
|
6
|
+
: minimum !== undefined ? `请输入不小于 ${minimum} 的整数。` : `请输入不大于 ${maximum} 的整数。`,
|
|
7
|
+
step: (step, minimum) => `请输入从 ${minimum} 开始、步长为 ${step} 的整数。`,
|
|
8
|
+
fieldPrefix: (label, message) => `${label}:${message}`,
|
|
9
|
+
unavailable: "不可用",
|
|
10
|
+
unavailableLong: "金额暂不可用。请修改标红的输入。",
|
|
11
|
+
showPassword: "显示密码",
|
|
12
|
+
hidePassword: "隐藏密码",
|
|
13
|
+
},
|
|
14
|
+
en: {
|
|
15
|
+
invalidInteger: "Enter a whole number without spaces, decimals or an exponent.",
|
|
16
|
+
range: (minimum, maximum) => minimum !== undefined && maximum !== undefined
|
|
17
|
+
? `Enter a whole number from ${minimum} to ${maximum}.`
|
|
18
|
+
: minimum !== undefined ? `Enter a whole number of at least ${minimum}.` : `Enter a whole number of at most ${maximum}.`,
|
|
19
|
+
step: (step, minimum) => `Enter a whole number in steps of ${step}, starting at ${minimum}.`,
|
|
20
|
+
fieldPrefix: (label, message) => `${label}: ${message}`,
|
|
21
|
+
unavailable: "unavailable",
|
|
22
|
+
unavailableLong: "Amounts are unavailable. Fix the highlighted input.",
|
|
23
|
+
showPassword: "Show password",
|
|
24
|
+
hidePassword: "Hide password",
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The surface language. Compiled artifacts carry `language`; imported or fixture artifacts
|
|
29
|
+
* carry `locale`, so both keys are read.
|
|
30
|
+
*/
|
|
31
|
+
export function surfaceLanguage(hints) {
|
|
32
|
+
const value = hints?.locale ?? hints?.language;
|
|
33
|
+
return typeof value === "string" ? value : undefined;
|
|
34
|
+
}
|
|
35
|
+
export function isChineseLanguage(locale) {
|
|
36
|
+
return typeof locale === "string" && locale.toLowerCase().startsWith("zh");
|
|
37
|
+
}
|
|
38
|
+
/** The copy table for a surface locale. Only Chinese is translated; anything else is English. */
|
|
39
|
+
export function fieldCopy(locale) {
|
|
40
|
+
return isChineseLanguage(locale) ? copy.zh : copy.en;
|
|
41
|
+
}
|
|
42
|
+
function integerBounds(props) {
|
|
43
|
+
// Wire values are decimal strings; the rule converts them the same way.
|
|
44
|
+
const number = (value) => {
|
|
45
|
+
if (value === undefined)
|
|
46
|
+
return undefined;
|
|
47
|
+
const parsed = Number(value);
|
|
48
|
+
return Number.isSafeInteger(parsed) ? parsed : undefined;
|
|
49
|
+
};
|
|
50
|
+
const step = number(props.step);
|
|
51
|
+
return {
|
|
52
|
+
minimum: number(props.min),
|
|
53
|
+
maximum: number(props.max),
|
|
54
|
+
step: step !== undefined && step > 0 ? step : 1,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Shown text for one failed field check. Only the integer profile has localised copy; other
|
|
59
|
+
* codes keep the English message from the rule.
|
|
60
|
+
*/
|
|
61
|
+
export function fieldErrorMessage(error, props, locale) {
|
|
62
|
+
const text = fieldCopy(locale);
|
|
63
|
+
const bounds = integerBounds(props);
|
|
64
|
+
switch (error.errorCode) {
|
|
65
|
+
case "invalid_integer":
|
|
66
|
+
return text.invalidInteger;
|
|
67
|
+
case "integer_range":
|
|
68
|
+
return text.range(bounds.minimum, bounds.maximum);
|
|
69
|
+
case "integer_step":
|
|
70
|
+
return text.step(bounds.step, bounds.minimum ?? 0);
|
|
71
|
+
default:
|
|
72
|
+
return error.message;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** One phrase for "these amounts cannot be calculated", shared so a live region says it once. */
|
|
76
|
+
export function amountsUnavailable(locale) {
|
|
77
|
+
return fieldCopy(locale).unavailableLong;
|
|
78
|
+
}
|
|
79
|
+
export function fieldMessageWithLabel(label, message, locale) {
|
|
80
|
+
return label ? fieldCopy(locale).fieldPrefix(label, message) : message;
|
|
81
|
+
}
|