reactive-md 0.6.0 → 0.6.1
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/CHANGELOG-ch.md +203 -194
- package/CHANGELOG.md +203 -194
- package/CONTRIBUTING-ch.md +67 -67
- package/CONTRIBUTING.md +69 -69
- package/README-ch.md +157 -157
- package/README-en.md +184 -184
- package/README.md +184 -184
- package/SECURITY-ch.md +81 -81
- package/SECURITY.md +81 -81
- package/THIRD_PARTY_NOTICES.md +313 -209
- package/bin/reactive-md.mjs +39 -39
- package/cli/args.mjs +181 -181
- package/cli/assets.mjs +390 -390
- package/cli/build.mjs +268 -268
- package/cli/document.mjs +130 -130
- package/cli/errors.mjs +61 -61
- package/cli/fs-utils.mjs +133 -133
- package/cli/glob.mjs +203 -203
- package/cli/help.mjs +138 -138
- package/cli/lint.mjs +177 -177
- package/cli/main.mjs +55 -55
- package/cli/migrate.mjs +368 -368
- package/cli/package.mjs +210 -210
- package/dist-library/{authoring-DSC0VsNK.js → authoring-BEVyth5X.js} +1 -1
- package/dist-library/compiler-Cm3ppMxG.js +46044 -0
- package/dist-library/{react-Dk94rcOp.js → react-Bh7_oxZp.js} +20110 -20113
- package/dist-library/reactive-md-authoring.js +2 -2
- package/dist-library/reactive-md-print.css +284 -284
- package/dist-library/reactive-md-react-unstyled.js +1 -1
- package/dist-library/reactive-md-react.js +1 -1
- package/dist-library/reactive-md.css +1 -1
- package/dist-library/reactive-md.js +3 -3
- package/dist-library/{runtime-D8KbnCd0.js → runtime-DlFYQ4s_.js} +1 -1
- package/dist-library/types/local/math-display.d.ts +22 -23
- package/dist-library/types/local/math-view.d.ts +6 -7
- package/dist-standalone/reactive-md-standalone.css +1 -1
- package/dist-standalone/reactive-md-standalone.js +318 -62
- package/docs/compatibility/compatibility-ch.md +121 -119
- package/docs/compatibility/compatibility-en.md +121 -119
- package/docs/compatibility/source-versioning-ch.md +159 -159
- package/docs/compatibility/source-versioning-en.md +159 -159
- package/docs/development/component-development-ch.md +210 -210
- package/docs/development/component-development-en.md +210 -210
- package/docs/development/component-protocol-ch.md +747 -747
- package/docs/development/component-protocol-en.md +747 -747
- package/docs/development/performance-ch.md +76 -76
- package/docs/development/performance-en.md +76 -76
- package/docs/development/releasing-ch.md +108 -108
- package/docs/development/releasing-en.md +108 -108
- package/docs/integration/author-tools-ch.md +62 -62
- package/docs/integration/author-tools-en.md +62 -62
- package/docs/integration/shareable-export-ch.md +209 -209
- package/docs/integration/shareable-export-en.md +211 -211
- package/docs/reference/computations-formatting-ch.md +87 -87
- package/docs/reference/computations-formatting-en.md +87 -87
- package/docs/reference/constraints-collections-ch.md +84 -84
- package/docs/reference/constraints-collections-en.md +84 -84
- package/docs/reference/diagnostics-counting-ch.md +104 -104
- package/docs/reference/diagnostics-counting-en.md +104 -104
- package/docs/reference/local-components-v2-ch.md +138 -138
- package/docs/reference/local-components-v2-en.md +104 -104
- package/docs/reference/local-interaction-syntax-ch.md +515 -511
- package/docs/reference/local-interaction-syntax-en.md +511 -507
- package/docs/reference/theme-protocol-ch.md +107 -107
- package/docs/reference/theme-protocol-en.md +107 -107
- package/docs/releases/release-0.4.0-ch.md +89 -89
- package/docs/releases/release-0.4.0-en.md +89 -89
- package/docs/releases/release-0.4.1-ch.md +23 -23
- package/docs/releases/release-0.4.1-en.md +23 -23
- package/docs/releases/release-0.6.1-ch.md +27 -0
- package/docs/releases/release-0.6.1-en.md +27 -0
- package/examples/conformance/README.md +53 -53
- package/examples/conformance/component-protocol-1.json +482 -482
- package/examples/conformance/component-protocol-1.schema.json +76 -76
- package/examples/conformance/local-draft-1.json +50 -50
- package/examples/conformance/local-draft-2.json +212 -212
- package/examples/integration/README.md +47 -47
- package/examples/integration/core/index.mjs +32 -32
- package/examples/integration/worker/index.html +19 -19
- package/examples/integration/worker/main.js +61 -61
- package/examples/integration/worker/package.json +13 -13
- package/examples/integration/worker/worker.js +3 -3
- package/examples/local/README.md +74 -74
- package/examples/local/charts.reactive.md +73 -73
- package/examples/local/recipes/invoice-zh.reactive.md +36 -36
- package/examples/local/recipes/invoice.reactive.md +36 -36
- package/examples/local/recipes/workshop-zh.reactive.md +33 -33
- package/examples/local/recipes/workshop.reactive.md +33 -33
- package/examples/local/showcase/README.md +25 -25
- package/examples/local/showcase/explore.reactive.md +102 -102
- package/examples/local/showcase/flow.reactive.md +86 -86
- package/examples/local/showcase/forms.reactive.md +126 -126
- package/examples/local/showcase/functions.reactive.md +119 -119
- package/examples/local/showcase/layout.reactive.md +100 -100
- package/examples/local/showcase/statistics.reactive.md +108 -108
- package/examples/local/showcase/tasks.reactive.md +89 -89
- package/examples/local/showcase/zh/explore.reactive.md +102 -102
- package/examples/local/showcase/zh/flow.reactive.md +86 -86
- package/examples/local/showcase/zh/forms.reactive.md +126 -126
- package/examples/local/showcase/zh/functions.reactive.md +119 -119
- package/examples/local/showcase/zh/layout.reactive.md +100 -100
- package/examples/local/showcase/zh/statistics.reactive.md +108 -108
- package/examples/local/showcase/zh/tasks.reactive.md +89 -89
- package/package.json +152 -152
- package/dist-library/compiler-CRD88BMt.js +0 -24964
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
# Reusing calculations and formatting values
|
|
2
|
-
|
|
3
|
-
These additions belong to `0.4.0`, which uses `local-draft-2`. See the [syntax specification](local-interaction-syntax-en.md), [integration API](../integration/local-integration-en.md) and [working example](../../examples/local/recipes/invoice.reactive.md).
|
|
4
|
-
|
|
5
|
-
## Named values
|
|
6
|
-
|
|
7
|
-
```markdown
|
|
8
|
-
<const name="unitPrice" value="{{ 12.5 }}">
|
|
9
|
-
<computed name="total" value="{{ quantity * unitPrice }}">
|
|
10
|
-
<computed name="invoice" value="{{ formatCurrency(total) }}">
|
|
11
|
-
|
|
12
|
-
<number bind="quantity" label="Quantity" initial="2" min="0" max="100">
|
|
13
|
-
|
|
14
|
-
Invoice: **{{ invoice }}**.
|
|
15
|
-
|
|
16
|
-
<if when="{{ total >= 100 }}">
|
|
17
|
-
Eligible for bulk delivery.
|
|
18
|
-
</if>
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
`computed` and `const` are invisible, void declarations. Write each on its own line at document top level; `/>` is accepted too. Both require `name` and a complete `value="{{ expression }}"`. Their values use the expression types: number, string, boolean, null, string array or numeric array. The compiler infers the type; there is no type annotation or implicit conversion. See [collections](constraints-collections-en.md) for array literals and aggregates.
|
|
22
|
-
|
|
23
|
-
Names follow the same identifier and reserved-name rules as `bind`. Controls, multi-slot bindings and declarations share one namespace. A declaration cannot replace another declaration or a control. Declarations inside containers are compile errors. Code fences, inline code and comments remain literal or inert.
|
|
24
|
-
|
|
25
|
-
Declarations are visible throughout the document, including before their source position. A `computed` value may read controls, constants and other computations. A `const` may read other constants and built-in constants such as `pi`; it must remain independent of controls and computations. This supports fixed prices and thresholds without displaying a disabled input. Constants have no editing or reset control.
|
|
26
|
-
|
|
27
|
-
The compiler extracts dependencies from the expression AST, including all conditional branches. It orders declarations by dependency before inferring types. Unknown names, duplicate names, self references and indirect cycles produce source-positioned errors. A cycle in an unused branch is still a compile error. Type checking uses the same operator and function rules as ordinary expressions; runtime domain errors remain possible, for example division by a control that becomes zero.
|
|
28
|
-
|
|
29
|
-
Use a named value anywhere an expression is accepted: prose, dynamic attributes, conditions and numeric table cells used by charts. Computable LaTeX formulas read multi-letter numeric names as `\mathrm{total}`, for example `y = \mathrm{total} x`. Formatted strings belong in labels or prose; numeric chart cells and plot parameters need the underlying number.
|
|
30
|
-
|
|
31
|
-
## Updates and errors
|
|
32
|
-
|
|
33
|
-
Each runtime owns its calculation cache. Computations are reused within a revision and refreshed after a successful control edit, atomic multi-slot edit or panel reset. Constants are evaluated at compilation and copied into each runtime. Hiding a section keeps its inputs and calculations; reset follows the existing control subtree rules.
|
|
34
|
-
|
|
35
|
-
A failed calculation propagates its expression error when a consumer reads it. The runtime never substitutes zero, null or a previous successful value. Independent consumers continue to work, and a valid input edit lets failed consumers recover. Runtime logical and conditional operators keep their usual short-circuit behavior: an unvisited branch does not read its failed value. A constant with an invalid static value is a compile error.
|
|
36
|
-
|
|
37
|
-
`document.bindings` and `document.initialState` describe writable controls. `document.computations` contains serializable declaration metadata. `runtime.snapshot().state` also contains successful named results for the current revision; failed results are absent. Use `runtime.evaluate(name)` to read a result with its error semantics. Declarations cannot be changed through `set` or `setMany`. Returned arrays remain protected from external mutation.
|
|
38
|
-
|
|
39
|
-
## Number formatting
|
|
40
|
-
|
|
41
|
-
All four functions return strings. Optional arguments are positional.
|
|
42
|
-
|
|
43
|
-
| Function | Defaults and example |
|
|
44
|
-
| --- | --- |
|
|
45
|
-
| `formatNumber(value, digits, locale, grouping)` | `digits=2`, `locale="en-US"`, `grouping=true`; `formatNumber(1234.5)` gives `"1,234.50"` |
|
|
46
|
-
| `formatPercent(value, digits, locale)` | `digits=0`, `locale="en-US"`; `formatPercent(0.125, 1)` gives `"12.5%"` |
|
|
47
|
-
| `formatScientific(value, digits, locale)` | `digits=2`, `locale="en-US"`; `formatScientific(1234)` gives `"1.23E3"` |
|
|
48
|
-
| `formatCurrency(value, currency, digits, locale)` | `currency="USD"`, `digits=2`, `locale="en-US"`; `formatCurrency(12.5)` gives `"$12.50"` |
|
|
49
|
-
|
|
50
|
-
`digits` must be an integer from 0 to 20. It fixes both minimum and maximum fraction digits, including for currencies such as JPY. `grouping` must be a boolean. Supported locales are exactly `en-US`, `zh-CN`, `de-DE`, `fr-FR` and `ja-JP`; omission always uses `en-US`, independently of the renderer's UI language or device settings. Unsupported locales are errors. Currency codes must be three uppercase ASCII letters accepted by `Intl.NumberFormat`; this is a syntax check, not a registry of circulating currencies.
|
|
51
|
-
|
|
52
|
-
Formatting uses decimal `halfExpand` rounding: ties round away from zero, so `formatNumber(1.005)` gives `"1.01"` and `formatNumber(-1.005)` gives `"-1.01"`. Zero displays without a negative sign. Arithmetic still uses finite JavaScript numbers; `round(x)` retains its existing `Math.round` rule, with ties toward positive infinity. Formatting changes presentation, not the source number.
|
|
53
|
-
|
|
54
|
-
A null value formats as an empty string. Other argument types remain strict: strings are not converted into numbers. Explicitly guard optional dates or text before applying their functions. Locale-specific punctuation, currency symbols and spacing follow the host's `Intl` data; hosts that need byte-identical text across platforms should pin their runtime and locale data.
|
|
55
|
-
|
|
56
|
-
## Strings
|
|
57
|
-
|
|
58
|
-
| Function | Behavior |
|
|
59
|
-
| --- | --- |
|
|
60
|
-
| `trim(text)` | Remove leading and trailing JavaScript Unicode whitespace |
|
|
61
|
-
| `lower(text)`, `upper(text)` | Default Unicode case conversion, independent of locale |
|
|
62
|
-
| `startsWith(text, search)`, `endsWith(text, search)` | Case-sensitive literal checks; empty search matches |
|
|
63
|
-
| `replace(text, search, replacement)` | Replace every non-overlapping literal match; empty search is an error; replacement text such as `$&` stays literal |
|
|
64
|
-
| `slice(text, start, end)` | Unicode code point indices; end is exclusive and defaults to the length; negative indices count from the end; indices are integers and clamp to bounds |
|
|
65
|
-
|
|
66
|
-
These functions require strings, including when the value is null. No regular expressions or property access are available. Code points are distinct from grapheme clusters: a combined emoji or an accented letter can contain several code points. All results keep the existing string budget of 100,000 UTF-16 code units. Replacement expansion is checked before constructing an oversized result.
|
|
67
|
-
|
|
68
|
-
## Dates
|
|
69
|
-
|
|
70
|
-
`dateCompare(a, b)` returns `-1`, `0` or `1`. `dateDiff(a, b, unit)` returns the signed difference **a minus b**; the default unit is `"days"`. Other units are `"hours"`, `"minutes"`, `"seconds"` and `"milliseconds"`.
|
|
71
|
-
|
|
72
|
-
Both arguments must be valid `YYYY-MM-DD` strings in years `0001` through `9999`, matching date controls. The calendar is Gregorian; leap days are validated. Each date means midnight UTC. Device timezone and daylight saving changes therefore do not change the result: `dateDiff("2026-03-09", "2026-03-08", "hours")` is `24`. Timestamps, timezone suffixes and ambiguous regional date strings are rejected.
|
|
73
|
-
|
|
74
|
-
An empty date control contains `""`; this is an absent date, not epoch zero. Empty or invalid dates produce a domain error; null produces a type error. Guard optional dates explicitly:
|
|
75
|
-
|
|
76
|
-
```markdown
|
|
77
|
-
<computed name="days" value="{{ startDate == '' || endDate == '' ? null : dateDiff(endDate, startDate) }}">
|
|
78
|
-
Days: {{ days == null ? 'Choose both dates' : formatNumber(days, 0) }}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## Diagnostics and limits
|
|
82
|
-
|
|
83
|
-
Declaration diagnostics use `local.computation.*`: `placement`, `name`, `reserved`, `duplicate`, `value`, `unknown`, `cycle`, `const`, `expression` and `budget`. Expression type errors keep the existing `local.expression.*` codes. Diagnostics include source ranges, lines and columns. The graph uses an iterative dependency order, so long chains do not require recursive evaluation. Existing source, syntax-node, expression and value budgets still apply; cycle reports are capped by the diagnostic budget.
|
|
84
|
-
|
|
85
|
-
## Compatibility
|
|
86
|
-
|
|
87
|
-
Existing expressions and `round(x)` keep their behavior. The new function names are reserved identifiers in this release. Rename any pre-existing bindings with these names and update their references before upgrading. The [changelog](../../CHANGELOG.md) lists this addition.
|
|
1
|
+
# Reusing calculations and formatting values
|
|
2
|
+
|
|
3
|
+
These additions belong to `0.4.0`, which uses `local-draft-2`. See the [syntax specification](local-interaction-syntax-en.md), [integration API](../integration/local-integration-en.md) and [working example](../../examples/local/recipes/invoice.reactive.md).
|
|
4
|
+
|
|
5
|
+
## Named values
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
<const name="unitPrice" value="{{ 12.5 }}">
|
|
9
|
+
<computed name="total" value="{{ quantity * unitPrice }}">
|
|
10
|
+
<computed name="invoice" value="{{ formatCurrency(total) }}">
|
|
11
|
+
|
|
12
|
+
<number bind="quantity" label="Quantity" initial="2" min="0" max="100">
|
|
13
|
+
|
|
14
|
+
Invoice: **{{ invoice }}**.
|
|
15
|
+
|
|
16
|
+
<if when="{{ total >= 100 }}">
|
|
17
|
+
Eligible for bulk delivery.
|
|
18
|
+
</if>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`computed` and `const` are invisible, void declarations. Write each on its own line at document top level; `/>` is accepted too. Both require `name` and a complete `value="{{ expression }}"`. Their values use the expression types: number, string, boolean, null, string array or numeric array. The compiler infers the type; there is no type annotation or implicit conversion. See [collections](constraints-collections-en.md) for array literals and aggregates.
|
|
22
|
+
|
|
23
|
+
Names follow the same identifier and reserved-name rules as `bind`. Controls, multi-slot bindings and declarations share one namespace. A declaration cannot replace another declaration or a control. Declarations inside containers are compile errors. Code fences, inline code and comments remain literal or inert.
|
|
24
|
+
|
|
25
|
+
Declarations are visible throughout the document, including before their source position. A `computed` value may read controls, constants and other computations. A `const` may read other constants and built-in constants such as `pi`; it must remain independent of controls and computations. This supports fixed prices and thresholds without displaying a disabled input. Constants have no editing or reset control.
|
|
26
|
+
|
|
27
|
+
The compiler extracts dependencies from the expression AST, including all conditional branches. It orders declarations by dependency before inferring types. Unknown names, duplicate names, self references and indirect cycles produce source-positioned errors. A cycle in an unused branch is still a compile error. Type checking uses the same operator and function rules as ordinary expressions; runtime domain errors remain possible, for example division by a control that becomes zero.
|
|
28
|
+
|
|
29
|
+
Use a named value anywhere an expression is accepted: prose, dynamic attributes, conditions and numeric table cells used by charts. Computable LaTeX formulas read multi-letter numeric names as `\mathrm{total}`, for example `y = \mathrm{total} x`. Formatted strings belong in labels or prose; numeric chart cells and plot parameters need the underlying number.
|
|
30
|
+
|
|
31
|
+
## Updates and errors
|
|
32
|
+
|
|
33
|
+
Each runtime owns its calculation cache. Computations are reused within a revision and refreshed after a successful control edit, atomic multi-slot edit or panel reset. Constants are evaluated at compilation and copied into each runtime. Hiding a section keeps its inputs and calculations; reset follows the existing control subtree rules.
|
|
34
|
+
|
|
35
|
+
A failed calculation propagates its expression error when a consumer reads it. The runtime never substitutes zero, null or a previous successful value. Independent consumers continue to work, and a valid input edit lets failed consumers recover. Runtime logical and conditional operators keep their usual short-circuit behavior: an unvisited branch does not read its failed value. A constant with an invalid static value is a compile error.
|
|
36
|
+
|
|
37
|
+
`document.bindings` and `document.initialState` describe writable controls. `document.computations` contains serializable declaration metadata. `runtime.snapshot().state` also contains successful named results for the current revision; failed results are absent. Use `runtime.evaluate(name)` to read a result with its error semantics. Declarations cannot be changed through `set` or `setMany`. Returned arrays remain protected from external mutation.
|
|
38
|
+
|
|
39
|
+
## Number formatting
|
|
40
|
+
|
|
41
|
+
All four functions return strings. Optional arguments are positional.
|
|
42
|
+
|
|
43
|
+
| Function | Defaults and example |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `formatNumber(value, digits, locale, grouping)` | `digits=2`, `locale="en-US"`, `grouping=true`; `formatNumber(1234.5)` gives `"1,234.50"` |
|
|
46
|
+
| `formatPercent(value, digits, locale)` | `digits=0`, `locale="en-US"`; `formatPercent(0.125, 1)` gives `"12.5%"` |
|
|
47
|
+
| `formatScientific(value, digits, locale)` | `digits=2`, `locale="en-US"`; `formatScientific(1234)` gives `"1.23E3"` |
|
|
48
|
+
| `formatCurrency(value, currency, digits, locale)` | `currency="USD"`, `digits=2`, `locale="en-US"`; `formatCurrency(12.5)` gives `"$12.50"` |
|
|
49
|
+
|
|
50
|
+
`digits` must be an integer from 0 to 20. It fixes both minimum and maximum fraction digits, including for currencies such as JPY. `grouping` must be a boolean. Supported locales are exactly `en-US`, `zh-CN`, `de-DE`, `fr-FR` and `ja-JP`; omission always uses `en-US`, independently of the renderer's UI language or device settings. Unsupported locales are errors. Currency codes must be three uppercase ASCII letters accepted by `Intl.NumberFormat`; this is a syntax check, not a registry of circulating currencies.
|
|
51
|
+
|
|
52
|
+
Formatting uses decimal `halfExpand` rounding: ties round away from zero, so `formatNumber(1.005)` gives `"1.01"` and `formatNumber(-1.005)` gives `"-1.01"`. Zero displays without a negative sign. Arithmetic still uses finite JavaScript numbers; `round(x)` retains its existing `Math.round` rule, with ties toward positive infinity. Formatting changes presentation, not the source number.
|
|
53
|
+
|
|
54
|
+
A null value formats as an empty string. Other argument types remain strict: strings are not converted into numbers. Explicitly guard optional dates or text before applying their functions. Locale-specific punctuation, currency symbols and spacing follow the host's `Intl` data; hosts that need byte-identical text across platforms should pin their runtime and locale data.
|
|
55
|
+
|
|
56
|
+
## Strings
|
|
57
|
+
|
|
58
|
+
| Function | Behavior |
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| `trim(text)` | Remove leading and trailing JavaScript Unicode whitespace |
|
|
61
|
+
| `lower(text)`, `upper(text)` | Default Unicode case conversion, independent of locale |
|
|
62
|
+
| `startsWith(text, search)`, `endsWith(text, search)` | Case-sensitive literal checks; empty search matches |
|
|
63
|
+
| `replace(text, search, replacement)` | Replace every non-overlapping literal match; empty search is an error; replacement text such as `$&` stays literal |
|
|
64
|
+
| `slice(text, start, end)` | Unicode code point indices; end is exclusive and defaults to the length; negative indices count from the end; indices are integers and clamp to bounds |
|
|
65
|
+
|
|
66
|
+
These functions require strings, including when the value is null. No regular expressions or property access are available. Code points are distinct from grapheme clusters: a combined emoji or an accented letter can contain several code points. All results keep the existing string budget of 100,000 UTF-16 code units. Replacement expansion is checked before constructing an oversized result.
|
|
67
|
+
|
|
68
|
+
## Dates
|
|
69
|
+
|
|
70
|
+
`dateCompare(a, b)` returns `-1`, `0` or `1`. `dateDiff(a, b, unit)` returns the signed difference **a minus b**; the default unit is `"days"`. Other units are `"hours"`, `"minutes"`, `"seconds"` and `"milliseconds"`.
|
|
71
|
+
|
|
72
|
+
Both arguments must be valid `YYYY-MM-DD` strings in years `0001` through `9999`, matching date controls. The calendar is Gregorian; leap days are validated. Each date means midnight UTC. Device timezone and daylight saving changes therefore do not change the result: `dateDiff("2026-03-09", "2026-03-08", "hours")` is `24`. Timestamps, timezone suffixes and ambiguous regional date strings are rejected.
|
|
73
|
+
|
|
74
|
+
An empty date control contains `""`; this is an absent date, not epoch zero. Empty or invalid dates produce a domain error; null produces a type error. Guard optional dates explicitly:
|
|
75
|
+
|
|
76
|
+
```markdown
|
|
77
|
+
<computed name="days" value="{{ startDate == '' || endDate == '' ? null : dateDiff(endDate, startDate) }}">
|
|
78
|
+
Days: {{ days == null ? 'Choose both dates' : formatNumber(days, 0) }}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Diagnostics and limits
|
|
82
|
+
|
|
83
|
+
Declaration diagnostics use `local.computation.*`: `placement`, `name`, `reserved`, `duplicate`, `value`, `unknown`, `cycle`, `const`, `expression` and `budget`. Expression type errors keep the existing `local.expression.*` codes. Diagnostics include source ranges, lines and columns. The graph uses an iterative dependency order, so long chains do not require recursive evaluation. Existing source, syntax-node, expression and value budgets still apply; cycle reports are capped by the diagnostic budget.
|
|
84
|
+
|
|
85
|
+
## Compatibility
|
|
86
|
+
|
|
87
|
+
Existing expressions and `round(x)` keep their behavior. The new function names are reserved identifiers in this release. Rename any pre-existing bindings with these names and update their references before upgrading. The [changelog](../../CHANGELOG.md) lists this addition.
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
# 条件、校验与本地集合
|
|
2
|
-
|
|
3
|
-
这些能力扩展使用 `local-draft-2` 的 `0.4.0`。[工作坊示例](../../examples/local/recipes/workshop-zh.reactive.md)把人数预算、日期区间和已选主题表格放在同一份文档里,[命名计算](computations-formatting-ch.md)保存其中间结果。
|
|
4
|
-
|
|
5
|
-
## 跨字段校验
|
|
6
|
-
|
|
7
|
-
```markdown
|
|
8
|
-
<number bind="people" label="人数" initial="4" min="1" max="20">
|
|
9
|
-
<number bind="capacity" label="房间容量" initial="3" min="1" max="20">
|
|
10
|
-
<validation when="{{ people <= capacity }}" message="请选择能容纳 {{ people }} 人的房间。" fields="people capacity">
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
`validation` 是无结束标签的块组件,必须提供 `when`、`message`、`fields`。`when` 包含一个完整的布尔表达式;`message` 是提示文字,可以包含普通插值;`fields` 以空白分隔关联的公开可写绑定名,名称不能重复,也可使用多槽位控件的绑定。规则能读取控件和计算值,允许前向引用。只读声明不能作为关联字段。
|
|
14
|
-
|
|
15
|
-
约束提示文档当前是否一致。读者可以继续编辑不一致的计划,初始值不满足跨字段规则也能正常显示和修改。例如,人数从四人改为六人后可能暂时超过容量,再换房间即可消除提示。单个控件原有的数字范围、日期合法性等规则继续拒绝非法写入。
|
|
16
|
-
|
|
17
|
-
`runtime.validate()` 按源码顺序返回当前失败规则的诊断。条件为 false 时使用 `local.validation.failed`,表达式无法求值时使用 `local.validation.expression`,错误也视为失败。一条失败规则不会阻断其他规则。结果按运行时修订缓存,对外返回副本;编辑、原子槽位更新及重置会同步刷新计算与校验。
|
|
18
|
-
|
|
19
|
-
渲染器显示失败提示,并通过 `aria-describedby` 和 `aria-invalid` 把提示关联到字段或其可访问分组。提示 ID 属于各自文档实例;通过校验后清除错误文字。隐藏内容中的规则继续生效,但隐藏提示不会主动播报。每份文档最多有 100 条规则,隐藏区域也计入;诊断文字沿用 2,000 字符上限。
|
|
20
|
-
|
|
21
|
-
## 受控的条件样式
|
|
22
|
-
|
|
23
|
-
```markdown
|
|
24
|
-
<badge tone="{{ total <= budget ? 'success' : 'warning' }}">{{ formatCurrency(total) }}</badge>
|
|
25
|
-
<alert tone="{{ total <= budget ? 'info' : 'warning' }}">预订前请核对预算。</alert>
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
本版本允许两个现有组件动态设置 `tone`:`badge` 接受 `neutral`、`success`、`warning`,`alert` 接受 `info`、`success`、`warning`。静态值保持原有行为。动态表达式必须是字符串类型,渲染器还会检查结果是否属于组件允许的枚举。表达式错误或非法值会使用默认样式并显示计算提示。这个条件接口不接受作者 CSS 类、样式字符串或动态颜色。
|
|
29
|
-
|
|
30
|
-
有限枚举可表达警告和成功状态,同时让主题继续控制颜色与对比度。更广泛的条件样式需要另行确定可访问性与主题规则。
|
|
31
|
-
|
|
32
|
-
## 数组与索引
|
|
33
|
-
|
|
34
|
-
```markdown
|
|
35
|
-
<const name="fixedFees" value="{{ [100, 20, 5] }}">
|
|
36
|
-
<computed name="fees" value="{{ [people * 80, includeKit ? people * 20 : 0] }}">
|
|
37
|
-
|
|
38
|
-
第一项:{{ fees[0] }}。合计:{{ sum(fees) }}。
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
数组字面量只能包含同一种类型的字符串或有限数字,元素表达式可以读取文档命名值。嵌套数组、混合类型、布尔值和 null 元素会被拒绝。`[]` 默认是空数字数组;条件分支直接写空字面量时,可采用另一分支的字符串数组类型,因此字符串多选值也可使用 `show ? subjects : []`。已有清单与多选控件继续产生字符串数组。公开类型 `LocalValue` 增加 `number[]`,`LocalValueKind` 增加 `number-array`,原来的 `array` 继续表示字符串数组。
|
|
42
|
-
|
|
43
|
-
索引从零开始,`values[0]` 读取第一项。索引必须是有限的安全整数;超出范围的整数(包括负数)返回 null,非整数报错。字符串和其他标量不能作为数组索引。数组不能直接插值显示,应使用索引、`len`、聚合或数据表格。
|
|
44
|
-
|
|
45
|
-
`len` 支持两种数组;`contains` 在字符串数组中接受字符串,在数字数组中接受数字,原有字符串子串检查不变。每个数组最多 10,000 项;数组字面量还计入既有表达式长度、AST 节点和求值预算。字符串数组内容继续受字符预算限制。运行时返回数组副本,快照数组被冻结。
|
|
46
|
-
|
|
47
|
-
## 简单数字聚合
|
|
48
|
-
|
|
49
|
-
| 函数 | 结果 |
|
|
50
|
-
| --- | --- |
|
|
51
|
-
| `sum(values)` | 数字数组之和,空数组返回 `0` |
|
|
52
|
-
| `average(values)` | 算术平均值,空数组返回 null |
|
|
53
|
-
| `minOf(values)` | 最小项,空数组返回 null |
|
|
54
|
-
| `maxOf(values)` | 最大项,空数组返回 null |
|
|
55
|
-
|
|
56
|
-
聚合只接受数字数组,不自动转换看起来像数字的字符串。结果使用有限的 JavaScript 数字,求和或结果溢出会产生表达式错误;`average` 先求和再除以项数,中间求和溢出也会报错。空聚合产生的 null 沿用现有可空表达式规则,需要时先判断再参与数值运算。标量 `min`、`max`、`round` 保持原有签名与行为。
|
|
57
|
-
|
|
58
|
-
## 从本地值生成表格行
|
|
59
|
-
|
|
60
|
-
```markdown
|
|
61
|
-
<const name="subjects" value="{{ ['mathematics', 'physics', 'chemistry'] }}">
|
|
62
|
-
|
|
63
|
-
<data-table label="主题准备工作" rows="{{ subjects }}" key="{{ item }}" max-rows="3">
|
|
64
|
-
<column label="主题" value="{{ item }}">
|
|
65
|
-
<column label="序号" value="{{ index + 1 }}">
|
|
66
|
-
<column label="准备工作" value="{{ item == 'chemistry' ? '准备材料' : '准备例题' }}">
|
|
67
|
-
</data-table>
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
`data-table` 是成对的块组件,必须提供可访问名称 `label`、完整数组表达式 `rows`,以及完整字符串或数字表达式 `key`。子内容只能是 1 至 20 个无结束标签的 `column` 声明。列必须提供静态标题 `label` 和完整的标量 `value` 表达式;单元格只输出文本,null 为空单元格。
|
|
71
|
-
|
|
72
|
-
每行有两个局部名称:`item` 是当前字符串或数字,`index` 是当前从零开始的位置。列表达式中这两个名称覆盖同名全局值,其他引用仍读取文档环境。`rows` 自身使用普通文档作用域。
|
|
73
|
-
|
|
74
|
-
键表达式只能依赖 `item`,且至少引用它一次,不能依赖 `index` 或其他文档值。表格检查键是否为非空字符串或有限数字,并确保唯一。数字键和字符串键属于不同标识,数字正负零视为同一标识;条目重排时保留 React 行身份。需要重复显示值时,应以不同的本地 ID 组成 `rows`,再根据 ID 生成显示列。重复键或非法键会使整张表显示错误。
|
|
75
|
-
|
|
76
|
-
`max-rows` 是 1 至 1,000 的静态整数,默认 100。整份文档各表声明的行预算总和最多 2,000,行预算乘列数的总和最多 10,000 个单元格,隐藏表格也计入。每张生成表还有 100,000 个 UTF-16 代码单元的预算,覆盖表头、单元格、错误或空状态占位文字及编码后的行键。渲染器在显示行之前检查预算,超出时显示错误,不截断数据。
|
|
77
|
-
|
|
78
|
-
空数组显示表头与空状态。数据源或键求值失败时清除表格行并显示错误;单元格表达式失败只影响该单元格,其余内容继续显示。组件只生成原生表格行,不重复任意文档区块或控件。原有 Markdown 表格及其图表输入保持原有行为。
|
|
79
|
-
|
|
80
|
-
## 集成与兼容性
|
|
81
|
-
|
|
82
|
-
`LocalDocument.validations` 保存可序列化的 `LocalValidation` 元数据,宿主可随时调用 `runtime.validate()`,调用不会修改状态。编译后的文档仍可用 `structuredClone` 传递给 Worker。对象生命周期和持久化规则见[集成说明](../integration/local-integration-ch.md)。
|
|
83
|
-
|
|
84
|
-
新增聚合函数名 `sum`、`average`、`minOf`、`maxOf` 成为保留字,升级前需重命名冲突绑定及其引用。对 `LocalValueKind` 做穷举检查的宿主应加入 `number-array`;可写多选与清单控件仍只接受字符串。预览版发布在 `next` 渠道。
|
|
1
|
+
# 条件、校验与本地集合
|
|
2
|
+
|
|
3
|
+
这些能力扩展使用 `local-draft-2` 的 `0.4.0`。[工作坊示例](../../examples/local/recipes/workshop-zh.reactive.md)把人数预算、日期区间和已选主题表格放在同一份文档里,[命名计算](computations-formatting-ch.md)保存其中间结果。
|
|
4
|
+
|
|
5
|
+
## 跨字段校验
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
<number bind="people" label="人数" initial="4" min="1" max="20">
|
|
9
|
+
<number bind="capacity" label="房间容量" initial="3" min="1" max="20">
|
|
10
|
+
<validation when="{{ people <= capacity }}" message="请选择能容纳 {{ people }} 人的房间。" fields="people capacity">
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`validation` 是无结束标签的块组件,必须提供 `when`、`message`、`fields`。`when` 包含一个完整的布尔表达式;`message` 是提示文字,可以包含普通插值;`fields` 以空白分隔关联的公开可写绑定名,名称不能重复,也可使用多槽位控件的绑定。规则能读取控件和计算值,允许前向引用。只读声明不能作为关联字段。
|
|
14
|
+
|
|
15
|
+
约束提示文档当前是否一致。读者可以继续编辑不一致的计划,初始值不满足跨字段规则也能正常显示和修改。例如,人数从四人改为六人后可能暂时超过容量,再换房间即可消除提示。单个控件原有的数字范围、日期合法性等规则继续拒绝非法写入。
|
|
16
|
+
|
|
17
|
+
`runtime.validate()` 按源码顺序返回当前失败规则的诊断。条件为 false 时使用 `local.validation.failed`,表达式无法求值时使用 `local.validation.expression`,错误也视为失败。一条失败规则不会阻断其他规则。结果按运行时修订缓存,对外返回副本;编辑、原子槽位更新及重置会同步刷新计算与校验。
|
|
18
|
+
|
|
19
|
+
渲染器显示失败提示,并通过 `aria-describedby` 和 `aria-invalid` 把提示关联到字段或其可访问分组。提示 ID 属于各自文档实例;通过校验后清除错误文字。隐藏内容中的规则继续生效,但隐藏提示不会主动播报。每份文档最多有 100 条规则,隐藏区域也计入;诊断文字沿用 2,000 字符上限。
|
|
20
|
+
|
|
21
|
+
## 受控的条件样式
|
|
22
|
+
|
|
23
|
+
```markdown
|
|
24
|
+
<badge tone="{{ total <= budget ? 'success' : 'warning' }}">{{ formatCurrency(total) }}</badge>
|
|
25
|
+
<alert tone="{{ total <= budget ? 'info' : 'warning' }}">预订前请核对预算。</alert>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
本版本允许两个现有组件动态设置 `tone`:`badge` 接受 `neutral`、`success`、`warning`,`alert` 接受 `info`、`success`、`warning`。静态值保持原有行为。动态表达式必须是字符串类型,渲染器还会检查结果是否属于组件允许的枚举。表达式错误或非法值会使用默认样式并显示计算提示。这个条件接口不接受作者 CSS 类、样式字符串或动态颜色。
|
|
29
|
+
|
|
30
|
+
有限枚举可表达警告和成功状态,同时让主题继续控制颜色与对比度。更广泛的条件样式需要另行确定可访问性与主题规则。
|
|
31
|
+
|
|
32
|
+
## 数组与索引
|
|
33
|
+
|
|
34
|
+
```markdown
|
|
35
|
+
<const name="fixedFees" value="{{ [100, 20, 5] }}">
|
|
36
|
+
<computed name="fees" value="{{ [people * 80, includeKit ? people * 20 : 0] }}">
|
|
37
|
+
|
|
38
|
+
第一项:{{ fees[0] }}。合计:{{ sum(fees) }}。
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
数组字面量只能包含同一种类型的字符串或有限数字,元素表达式可以读取文档命名值。嵌套数组、混合类型、布尔值和 null 元素会被拒绝。`[]` 默认是空数字数组;条件分支直接写空字面量时,可采用另一分支的字符串数组类型,因此字符串多选值也可使用 `show ? subjects : []`。已有清单与多选控件继续产生字符串数组。公开类型 `LocalValue` 增加 `number[]`,`LocalValueKind` 增加 `number-array`,原来的 `array` 继续表示字符串数组。
|
|
42
|
+
|
|
43
|
+
索引从零开始,`values[0]` 读取第一项。索引必须是有限的安全整数;超出范围的整数(包括负数)返回 null,非整数报错。字符串和其他标量不能作为数组索引。数组不能直接插值显示,应使用索引、`len`、聚合或数据表格。
|
|
44
|
+
|
|
45
|
+
`len` 支持两种数组;`contains` 在字符串数组中接受字符串,在数字数组中接受数字,原有字符串子串检查不变。每个数组最多 10,000 项;数组字面量还计入既有表达式长度、AST 节点和求值预算。字符串数组内容继续受字符预算限制。运行时返回数组副本,快照数组被冻结。
|
|
46
|
+
|
|
47
|
+
## 简单数字聚合
|
|
48
|
+
|
|
49
|
+
| 函数 | 结果 |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| `sum(values)` | 数字数组之和,空数组返回 `0` |
|
|
52
|
+
| `average(values)` | 算术平均值,空数组返回 null |
|
|
53
|
+
| `minOf(values)` | 最小项,空数组返回 null |
|
|
54
|
+
| `maxOf(values)` | 最大项,空数组返回 null |
|
|
55
|
+
|
|
56
|
+
聚合只接受数字数组,不自动转换看起来像数字的字符串。结果使用有限的 JavaScript 数字,求和或结果溢出会产生表达式错误;`average` 先求和再除以项数,中间求和溢出也会报错。空聚合产生的 null 沿用现有可空表达式规则,需要时先判断再参与数值运算。标量 `min`、`max`、`round` 保持原有签名与行为。
|
|
57
|
+
|
|
58
|
+
## 从本地值生成表格行
|
|
59
|
+
|
|
60
|
+
```markdown
|
|
61
|
+
<const name="subjects" value="{{ ['mathematics', 'physics', 'chemistry'] }}">
|
|
62
|
+
|
|
63
|
+
<data-table label="主题准备工作" rows="{{ subjects }}" key="{{ item }}" max-rows="3">
|
|
64
|
+
<column label="主题" value="{{ item }}">
|
|
65
|
+
<column label="序号" value="{{ index + 1 }}">
|
|
66
|
+
<column label="准备工作" value="{{ item == 'chemistry' ? '准备材料' : '准备例题' }}">
|
|
67
|
+
</data-table>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`data-table` 是成对的块组件,必须提供可访问名称 `label`、完整数组表达式 `rows`,以及完整字符串或数字表达式 `key`。子内容只能是 1 至 20 个无结束标签的 `column` 声明。列必须提供静态标题 `label` 和完整的标量 `value` 表达式;单元格只输出文本,null 为空单元格。
|
|
71
|
+
|
|
72
|
+
每行有两个局部名称:`item` 是当前字符串或数字,`index` 是当前从零开始的位置。列表达式中这两个名称覆盖同名全局值,其他引用仍读取文档环境。`rows` 自身使用普通文档作用域。
|
|
73
|
+
|
|
74
|
+
键表达式只能依赖 `item`,且至少引用它一次,不能依赖 `index` 或其他文档值。表格检查键是否为非空字符串或有限数字,并确保唯一。数字键和字符串键属于不同标识,数字正负零视为同一标识;条目重排时保留 React 行身份。需要重复显示值时,应以不同的本地 ID 组成 `rows`,再根据 ID 生成显示列。重复键或非法键会使整张表显示错误。
|
|
75
|
+
|
|
76
|
+
`max-rows` 是 1 至 1,000 的静态整数,默认 100。整份文档各表声明的行预算总和最多 2,000,行预算乘列数的总和最多 10,000 个单元格,隐藏表格也计入。每张生成表还有 100,000 个 UTF-16 代码单元的预算,覆盖表头、单元格、错误或空状态占位文字及编码后的行键。渲染器在显示行之前检查预算,超出时显示错误,不截断数据。
|
|
77
|
+
|
|
78
|
+
空数组显示表头与空状态。数据源或键求值失败时清除表格行并显示错误;单元格表达式失败只影响该单元格,其余内容继续显示。组件只生成原生表格行,不重复任意文档区块或控件。原有 Markdown 表格及其图表输入保持原有行为。
|
|
79
|
+
|
|
80
|
+
## 集成与兼容性
|
|
81
|
+
|
|
82
|
+
`LocalDocument.validations` 保存可序列化的 `LocalValidation` 元数据,宿主可随时调用 `runtime.validate()`,调用不会修改状态。编译后的文档仍可用 `structuredClone` 传递给 Worker。对象生命周期和持久化规则见[集成说明](../integration/local-integration-ch.md)。
|
|
83
|
+
|
|
84
|
+
新增聚合函数名 `sum`、`average`、`minOf`、`maxOf` 成为保留字,升级前需重命名冲突绑定及其引用。对 `LocalValueKind` 做穷举检查的宿主应加入 `number-array`;可写多选与清单控件仍只接受字符串。预览版发布在 `next` 渠道。
|
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
# Constraints, conditional tones and local collections
|
|
2
|
-
|
|
3
|
-
These additions extend `0.4.0`, which uses `local-draft-2`. The [workshop example](../../examples/local/recipes/workshop.reactive.md) combines a participant budget, a date interval and a selected-subject table. [Named calculations](computations-formatting-en.md) hold its intermediate results.
|
|
4
|
-
|
|
5
|
-
## Cross-field validation
|
|
6
|
-
|
|
7
|
-
```markdown
|
|
8
|
-
<number bind="people" label="Participants" initial="4" min="1" max="20">
|
|
9
|
-
<number bind="capacity" label="Room capacity" initial="3" min="1" max="20">
|
|
10
|
-
<validation when="{{ people <= capacity }}" message="Choose a room for at least {{ people }} people." fields="people capacity">
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
`validation` is a void block component with required `when`, `message` and `fields` attributes. `when` must contain one complete boolean expression. `message` is text with optional ordinary interpolations. `fields` lists distinct public writable binding names, separated by whitespace; names from multi-slot controls are accepted too. A rule can read controls and computed values, including values declared later. Fields identify the controls associated with its hint; read-only declarations cannot be fields.
|
|
14
|
-
|
|
15
|
-
Constraints report the current consistency of a document. Readers can still edit an inconsistent plan, including one whose initial values fail a cross-field rule. For example, changing the participant count from four to six can temporarily exceed room capacity; changing the room then clears the hint. Existing individual control rules, such as numeric bounds and invalid calendar dates, keep their write-rejection behavior.
|
|
16
|
-
|
|
17
|
-
`runtime.validate()` returns diagnostics for all current failed rules in source order. It reports `local.validation.failed` when `when` is false and `local.validation.expression` when an expression cannot be evaluated. Errors count as failures. One failed rule does not stop independent rules. The result is cached for the current runtime revision and returned as defensive copies. Edits, atomic slot updates and resets refresh calculations and validation together.
|
|
18
|
-
|
|
19
|
-
The renderer displays failed-rule hints and associates them with the corresponding controls or their accessible groups using `aria-describedby` and `aria-invalid`. Hint IDs belong to the mounted document instance. Success removes the error text. Rules in hidden content remain active, as do their bindings; hidden hints do not announce themselves. A document supports at most 100 validation rules, including hidden regions. Diagnostic messages retain the existing 2,000-character bound.
|
|
20
|
-
|
|
21
|
-
## Conditional visual emphasis
|
|
22
|
-
|
|
23
|
-
```markdown
|
|
24
|
-
<badge tone="{{ total <= budget ? 'success' : 'warning' }}">{{ formatCurrency(total) }}</badge>
|
|
25
|
-
<alert tone="{{ total <= budget ? 'info' : 'warning' }}">Review the estimate before booking.</alert>
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
This build supports dynamic `tone` on two existing components. `badge` accepts `neutral`, `success` and `warning`; `alert` accepts `info`, `success` and `warning`. Static values retain their existing behavior. Dynamic expressions must have string type, and the renderer checks the returned value against the component's allowed tones. An error or unsupported value uses the default tone and a visible computation hint. CSS classes, author styles and colors remain outside this conditional API.
|
|
29
|
-
|
|
30
|
-
The limited enum covers warning and success emphasis while keeping theme colors and contrast under host control. More general conditional styling would need separate accessibility and theme rules.
|
|
31
|
-
|
|
32
|
-
## Arrays and indexing
|
|
33
|
-
|
|
34
|
-
```markdown
|
|
35
|
-
<const name="fixedFees" value="{{ [100, 20, 5] }}">
|
|
36
|
-
<computed name="fees" value="{{ [people * 80, includeKit ? people * 20 : 0] }}">
|
|
37
|
-
|
|
38
|
-
First fee: {{ fees[0] }}. Total: {{ sum(fees) }}.
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Array literals contain only strings or only finite numbers. Expressions inside a literal can read the document's named values. Nested arrays, mixed element types, booleans and null elements are rejected. `[]` is an empty numeric array by default; a direct empty literal in a conditional branch adopts a string-array sibling's kind, so `show ? subjects : []` works for string selections. Existing checklist and multi-select values remain string arrays. The public type `LocalValue` gains `number[]`, and `LocalValueKind` gains `number-array`; its existing `array` kind continues to mean a string array.
|
|
42
|
-
|
|
43
|
-
Indexing is zero based: `values[0]` reads the first element. The index must be a finite safe integer. An integer outside the collection, including a negative index, returns null; a non-integer index is an error. Indexing a string or a scalar is a type error. Arrays themselves cannot be interpolated as text; use an index, `len`, an aggregate or a data table.
|
|
44
|
-
|
|
45
|
-
`len` counts both kinds of array. `contains` accepts a string member for a string array and a number for a numeric array. String containment keeps its existing behavior. Every array is bounded by 10,000 elements; array literals also consume the existing expression-length, AST-node and evaluation budgets. The total contents of a string array keep the existing character budget. Returned runtime arrays are copied, and snapshot arrays are frozen.
|
|
46
|
-
|
|
47
|
-
## Numeric aggregates
|
|
48
|
-
|
|
49
|
-
| Function | Result |
|
|
50
|
-
| --- | --- |
|
|
51
|
-
| `sum(values)` | Sum of a numeric array; empty input gives `0` |
|
|
52
|
-
| `average(values)` | Arithmetic mean; empty input gives null |
|
|
53
|
-
| `minOf(values)` | Smallest numeric member; empty input gives null |
|
|
54
|
-
| `maxOf(values)` | Largest numeric member; empty input gives null |
|
|
55
|
-
|
|
56
|
-
Aggregates accept numeric arrays, with no conversion of numeric-looking strings. Results use finite JavaScript numbers; an overflow in the sum or result is an expression error. `average` divides the sum by the count, so an overflowing intermediate sum is also an error. Null from an empty aggregate follows existing nullable-expression rules, so guard it before numeric operations when needed. Scalar `min`, `max` and `round` keep their existing signatures and behavior.
|
|
57
|
-
|
|
58
|
-
## Generate table rows from local values
|
|
59
|
-
|
|
60
|
-
```markdown
|
|
61
|
-
<const name="subjects" value="{{ ['mathematics', 'physics', 'chemistry'] }}">
|
|
62
|
-
|
|
63
|
-
<data-table label="Subject preparation" rows="{{ subjects }}" key="{{ item }}" max-rows="3">
|
|
64
|
-
<column label="Subject" value="{{ item }}">
|
|
65
|
-
<column label="Position" value="{{ index + 1 }}">
|
|
66
|
-
<column label="Preparation" value="{{ item == 'chemistry' ? 'Prepare materials' : 'Prepare examples' }}">
|
|
67
|
-
</data-table>
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
`data-table` is a paired block component. It requires an accessible `label`, a complete array expression in `rows`, and a complete string or number expression in `key`. Its only children are 1 to 20 void `column` declarations. A column requires a static `label` and a complete scalar `value` expression. Cells output plain text; null gives an empty cell.
|
|
71
|
-
|
|
72
|
-
Each row has two local names: `item` is the current string or number, and `index` is its current zero-based position. These names shadow same-named document values inside column expressions. Other references use the normal document environment. The source `rows` expression uses ordinary document scope.
|
|
73
|
-
|
|
74
|
-
Keys depend only on `item`, including at least one reference to it. They cannot depend on `index` or other document values. The table checks that every evaluated key is a nonempty string or finite number and is unique. Numeric and string keys have distinct identities; signed numeric zeros have the same identity. React rows retain their identities when their items reorder. For records with repeated display values, use a collection of distinct local IDs as `rows` and derive the display cells from those IDs. Duplicate or invalid keys produce a whole-table error.
|
|
75
|
-
|
|
76
|
-
`max-rows` is a static integer from 1 to 1,000, defaulting to 100. Across the document, declared row budgets may total at most 2,000 and row budgets multiplied by their column counts may total at most 10,000 cells. Hidden tables count too. Each generated table also has a budget of 100,000 UTF-16 code units for header text, cell text, error/empty placeholders and encoded row keys. The renderer checks budgets before exposing rows; exceeding them reports an error without truncating the data.
|
|
77
|
-
|
|
78
|
-
An empty array displays the headers and an empty-state message. Failure of the row source or a key clears the table's rows and displays an error. A cell-expression failure is shown in that cell while independent cells remain readable. The component creates native table rows, not arbitrary repeated document sections or controls. Markdown tables and their existing chart inputs keep their original behavior.
|
|
79
|
-
|
|
80
|
-
## Integration and compatibility
|
|
81
|
-
|
|
82
|
-
`LocalDocument.validations` contains serializable `LocalValidation` metadata. Hosts can call `runtime.validate()` at any time without changing state. The compiled document remains transferable to a Worker with `structuredClone`. The [core integration guide](../integration/local-integration-en.md) describes its lifetime and persistence rules.
|
|
83
|
-
|
|
84
|
-
The aggregate names `sum`, `average`, `minOf` and `maxOf` become reserved identifiers. Rename conflicting bindings and their references before using this release. Hosts with exhaustive `LocalValueKind` checks should add `number-array`; writable multi-select and checklist controls still require strings. The preview is published on the `next` channel.
|
|
1
|
+
# Constraints, conditional tones and local collections
|
|
2
|
+
|
|
3
|
+
These additions extend `0.4.0`, which uses `local-draft-2`. The [workshop example](../../examples/local/recipes/workshop.reactive.md) combines a participant budget, a date interval and a selected-subject table. [Named calculations](computations-formatting-en.md) hold its intermediate results.
|
|
4
|
+
|
|
5
|
+
## Cross-field validation
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
<number bind="people" label="Participants" initial="4" min="1" max="20">
|
|
9
|
+
<number bind="capacity" label="Room capacity" initial="3" min="1" max="20">
|
|
10
|
+
<validation when="{{ people <= capacity }}" message="Choose a room for at least {{ people }} people." fields="people capacity">
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`validation` is a void block component with required `when`, `message` and `fields` attributes. `when` must contain one complete boolean expression. `message` is text with optional ordinary interpolations. `fields` lists distinct public writable binding names, separated by whitespace; names from multi-slot controls are accepted too. A rule can read controls and computed values, including values declared later. Fields identify the controls associated with its hint; read-only declarations cannot be fields.
|
|
14
|
+
|
|
15
|
+
Constraints report the current consistency of a document. Readers can still edit an inconsistent plan, including one whose initial values fail a cross-field rule. For example, changing the participant count from four to six can temporarily exceed room capacity; changing the room then clears the hint. Existing individual control rules, such as numeric bounds and invalid calendar dates, keep their write-rejection behavior.
|
|
16
|
+
|
|
17
|
+
`runtime.validate()` returns diagnostics for all current failed rules in source order. It reports `local.validation.failed` when `when` is false and `local.validation.expression` when an expression cannot be evaluated. Errors count as failures. One failed rule does not stop independent rules. The result is cached for the current runtime revision and returned as defensive copies. Edits, atomic slot updates and resets refresh calculations and validation together.
|
|
18
|
+
|
|
19
|
+
The renderer displays failed-rule hints and associates them with the corresponding controls or their accessible groups using `aria-describedby` and `aria-invalid`. Hint IDs belong to the mounted document instance. Success removes the error text. Rules in hidden content remain active, as do their bindings; hidden hints do not announce themselves. A document supports at most 100 validation rules, including hidden regions. Diagnostic messages retain the existing 2,000-character bound.
|
|
20
|
+
|
|
21
|
+
## Conditional visual emphasis
|
|
22
|
+
|
|
23
|
+
```markdown
|
|
24
|
+
<badge tone="{{ total <= budget ? 'success' : 'warning' }}">{{ formatCurrency(total) }}</badge>
|
|
25
|
+
<alert tone="{{ total <= budget ? 'info' : 'warning' }}">Review the estimate before booking.</alert>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This build supports dynamic `tone` on two existing components. `badge` accepts `neutral`, `success` and `warning`; `alert` accepts `info`, `success` and `warning`. Static values retain their existing behavior. Dynamic expressions must have string type, and the renderer checks the returned value against the component's allowed tones. An error or unsupported value uses the default tone and a visible computation hint. CSS classes, author styles and colors remain outside this conditional API.
|
|
29
|
+
|
|
30
|
+
The limited enum covers warning and success emphasis while keeping theme colors and contrast under host control. More general conditional styling would need separate accessibility and theme rules.
|
|
31
|
+
|
|
32
|
+
## Arrays and indexing
|
|
33
|
+
|
|
34
|
+
```markdown
|
|
35
|
+
<const name="fixedFees" value="{{ [100, 20, 5] }}">
|
|
36
|
+
<computed name="fees" value="{{ [people * 80, includeKit ? people * 20 : 0] }}">
|
|
37
|
+
|
|
38
|
+
First fee: {{ fees[0] }}. Total: {{ sum(fees) }}.
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Array literals contain only strings or only finite numbers. Expressions inside a literal can read the document's named values. Nested arrays, mixed element types, booleans and null elements are rejected. `[]` is an empty numeric array by default; a direct empty literal in a conditional branch adopts a string-array sibling's kind, so `show ? subjects : []` works for string selections. Existing checklist and multi-select values remain string arrays. The public type `LocalValue` gains `number[]`, and `LocalValueKind` gains `number-array`; its existing `array` kind continues to mean a string array.
|
|
42
|
+
|
|
43
|
+
Indexing is zero based: `values[0]` reads the first element. The index must be a finite safe integer. An integer outside the collection, including a negative index, returns null; a non-integer index is an error. Indexing a string or a scalar is a type error. Arrays themselves cannot be interpolated as text; use an index, `len`, an aggregate or a data table.
|
|
44
|
+
|
|
45
|
+
`len` counts both kinds of array. `contains` accepts a string member for a string array and a number for a numeric array. String containment keeps its existing behavior. Every array is bounded by 10,000 elements; array literals also consume the existing expression-length, AST-node and evaluation budgets. The total contents of a string array keep the existing character budget. Returned runtime arrays are copied, and snapshot arrays are frozen.
|
|
46
|
+
|
|
47
|
+
## Numeric aggregates
|
|
48
|
+
|
|
49
|
+
| Function | Result |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| `sum(values)` | Sum of a numeric array; empty input gives `0` |
|
|
52
|
+
| `average(values)` | Arithmetic mean; empty input gives null |
|
|
53
|
+
| `minOf(values)` | Smallest numeric member; empty input gives null |
|
|
54
|
+
| `maxOf(values)` | Largest numeric member; empty input gives null |
|
|
55
|
+
|
|
56
|
+
Aggregates accept numeric arrays, with no conversion of numeric-looking strings. Results use finite JavaScript numbers; an overflow in the sum or result is an expression error. `average` divides the sum by the count, so an overflowing intermediate sum is also an error. Null from an empty aggregate follows existing nullable-expression rules, so guard it before numeric operations when needed. Scalar `min`, `max` and `round` keep their existing signatures and behavior.
|
|
57
|
+
|
|
58
|
+
## Generate table rows from local values
|
|
59
|
+
|
|
60
|
+
```markdown
|
|
61
|
+
<const name="subjects" value="{{ ['mathematics', 'physics', 'chemistry'] }}">
|
|
62
|
+
|
|
63
|
+
<data-table label="Subject preparation" rows="{{ subjects }}" key="{{ item }}" max-rows="3">
|
|
64
|
+
<column label="Subject" value="{{ item }}">
|
|
65
|
+
<column label="Position" value="{{ index + 1 }}">
|
|
66
|
+
<column label="Preparation" value="{{ item == 'chemistry' ? 'Prepare materials' : 'Prepare examples' }}">
|
|
67
|
+
</data-table>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
`data-table` is a paired block component. It requires an accessible `label`, a complete array expression in `rows`, and a complete string or number expression in `key`. Its only children are 1 to 20 void `column` declarations. A column requires a static `label` and a complete scalar `value` expression. Cells output plain text; null gives an empty cell.
|
|
71
|
+
|
|
72
|
+
Each row has two local names: `item` is the current string or number, and `index` is its current zero-based position. These names shadow same-named document values inside column expressions. Other references use the normal document environment. The source `rows` expression uses ordinary document scope.
|
|
73
|
+
|
|
74
|
+
Keys depend only on `item`, including at least one reference to it. They cannot depend on `index` or other document values. The table checks that every evaluated key is a nonempty string or finite number and is unique. Numeric and string keys have distinct identities; signed numeric zeros have the same identity. React rows retain their identities when their items reorder. For records with repeated display values, use a collection of distinct local IDs as `rows` and derive the display cells from those IDs. Duplicate or invalid keys produce a whole-table error.
|
|
75
|
+
|
|
76
|
+
`max-rows` is a static integer from 1 to 1,000, defaulting to 100. Across the document, declared row budgets may total at most 2,000 and row budgets multiplied by their column counts may total at most 10,000 cells. Hidden tables count too. Each generated table also has a budget of 100,000 UTF-16 code units for header text, cell text, error/empty placeholders and encoded row keys. The renderer checks budgets before exposing rows; exceeding them reports an error without truncating the data.
|
|
77
|
+
|
|
78
|
+
An empty array displays the headers and an empty-state message. Failure of the row source or a key clears the table's rows and displays an error. A cell-expression failure is shown in that cell while independent cells remain readable. The component creates native table rows, not arbitrary repeated document sections or controls. Markdown tables and their existing chart inputs keep their original behavior.
|
|
79
|
+
|
|
80
|
+
## Integration and compatibility
|
|
81
|
+
|
|
82
|
+
`LocalDocument.validations` contains serializable `LocalValidation` metadata. Hosts can call `runtime.validate()` at any time without changing state. The compiled document remains transferable to a Worker with `structuredClone`. The [core integration guide](../integration/local-integration-en.md) describes its lifetime and persistence rules.
|
|
83
|
+
|
|
84
|
+
The aggregate names `sum`, `average`, `minOf` and `maxOf` become reserved identifiers. Rename conflicting bindings and their references before using this release. Hosts with exhaustive `LocalValueKind` checks should add `number-array`; writable multi-select and checklist controls still require strings. The preview is published on the `next` channel.
|