reactive-md 0.5.3 → 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 +195 -165
- package/CHANGELOG.md +195 -165
- package/CONTRIBUTING-ch.md +67 -62
- package/CONTRIBUTING.md +69 -64
- package/README-ch.md +157 -155
- package/README-en.md +184 -181
- package/README.md +184 -181
- package/SECURITY-ch.md +81 -81
- package/SECURITY.md +81 -81
- package/THIRD_PARTY_NOTICES.md +585 -1
- 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-DJJjKYg9.js → authoring-BEVyth5X.js} +58 -58
- package/dist-library/compiler-Cm3ppMxG.js +46044 -0
- package/dist-library/react-Bh7_oxZp.js +59655 -0
- package/dist-library/reactive-md-authoring.js +2 -2
- package/dist-library/reactive-md-editor.js +13 -0
- 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 +123 -123
- package/dist-library/{runtime-DXtle_6c.js → runtime-DlFYQ4s_.js} +151 -151
- package/dist-library/types/editor/types.d.ts +2 -0
- package/dist-library/types/local/code-block.d.ts +3 -1
- package/dist-library/types/local/code-languages.d.ts +7 -0
- package/dist-library/types/local/compiled-data.d.ts +6 -0
- package/dist-library/types/local/compiler.d.ts +1 -0
- package/dist-library/types/local/diagnostics.d.ts +31 -0
- package/dist-library/types/local/diagram-layout.d.ts +69 -0
- package/dist-library/types/local/html-names.d.ts +6 -0
- package/dist-library/types/local/html-table.d.ts +25 -0
- package/dist-library/types/local/math-display.d.ts +22 -22
- package/dist-library/types/local/math-view.d.ts +6 -7
- package/dist-library/types/local/table-surface.d.ts +10 -0
- package/dist-library/types/local/types.d.ts +2 -0
- package/dist-library/types/local/visuals.d.ts +9 -0
- package/dist-standalone/reactive-md-standalone.css +1 -1
- package/dist-standalone/reactive-md-standalone.js +331 -51
- package/docs/README.md +84 -0
- package/docs/{compatibility-ch.md → compatibility/compatibility-ch.md} +121 -109
- package/docs/{compatibility-en.md → compatibility/compatibility-en.md} +121 -109
- package/docs/{source-versioning-ch.md → compatibility/source-versioning-ch.md} +159 -159
- package/docs/{source-versioning-en.md → compatibility/source-versioning-en.md} +159 -159
- package/docs/{component-development-ch.md → development/component-development-ch.md} +210 -210
- package/docs/{component-development-en.md → development/component-development-en.md} +210 -210
- package/docs/{component-protocol-ch.md → development/component-protocol-ch.md} +747 -747
- package/docs/{component-protocol-en.md → development/component-protocol-en.md} +747 -747
- package/docs/{performance-ch.md → development/performance-ch.md} +76 -76
- package/docs/{performance-en.md → development/performance-en.md} +76 -76
- package/docs/{releasing-ch.md → development/releasing-ch.md} +108 -108
- package/docs/{releasing-en.md → development/releasing-en.md} +108 -108
- package/docs/guides/local-guide-ch.md +513 -0
- package/docs/guides/local-guide-en.md +513 -0
- package/docs/{author-tools-ch.md → integration/author-tools-ch.md} +57 -57
- package/docs/{author-tools-en.md → integration/author-tools-en.md} +57 -57
- package/docs/{editor-ch.md → integration/editor-ch.md} +7 -6
- package/docs/{editor-en.md → integration/editor-en.md} +7 -6
- package/docs/{local-integration-ch.md → integration/local-integration-ch.md} +33 -15
- package/docs/{local-integration-en.md → integration/local-integration-en.md} +33 -15
- package/docs/{shareable-export-ch.md → integration/shareable-export-ch.md} +221 -209
- package/docs/{shareable-export-en.md → integration/shareable-export-en.md} +223 -211
- package/docs/{computations-formatting-ch.md → reference/computations-formatting-ch.md} +87 -87
- package/docs/{computations-formatting-en.md → reference/computations-formatting-en.md} +87 -87
- package/docs/{constraints-collections-ch.md → reference/constraints-collections-ch.md} +84 -84
- package/docs/{constraints-collections-en.md → reference/constraints-collections-en.md} +84 -84
- package/docs/{diagnostics-counting-ch.md → reference/diagnostics-counting-ch.md} +104 -84
- package/docs/{diagnostics-counting-en.md → reference/diagnostics-counting-en.md} +104 -84
- package/docs/{html-compatibility-ch.md → reference/html-compatibility-ch.md} +42 -4
- package/docs/{html-compatibility-en.md → reference/html-compatibility-en.md} +42 -4
- package/docs/{local-components-v2-ch.md → reference/local-components-v2-ch.md} +138 -138
- package/docs/{local-components-v2-en.md → reference/local-components-v2-en.md} +104 -104
- package/docs/{local-interaction-syntax-ch.md → reference/local-interaction-syntax-ch.md} +515 -492
- package/docs/{local-interaction-syntax-en.md → reference/local-interaction-syntax-en.md} +511 -488
- package/docs/{theme-protocol-ch.md → reference/theme-protocol-ch.md} +107 -107
- package/docs/{theme-protocol-en.md → reference/theme-protocol-en.md} +107 -107
- package/docs/{release-0.4.0-ch.md → releases/release-0.4.0-ch.md} +89 -89
- package/docs/{release-0.4.0-en.md → releases/release-0.4.0-en.md} +89 -89
- package/docs/{release-0.4.1-ch.md → releases/release-0.4.1-ch.md} +23 -23
- package/docs/{release-0.4.1-en.md → releases/release-0.4.1-en.md} +23 -23
- package/docs/{release-0.5.0-ch.md → releases/release-0.5.0-ch.md} +4 -4
- package/docs/{release-0.5.0-en.md → releases/release-0.5.0-en.md} +4 -4
- package/docs/{release-0.5.1-ch.md → releases/release-0.5.1-ch.md} +4 -4
- package/docs/{release-0.5.1-en.md → releases/release-0.5.1-en.md} +4 -4
- package/docs/{release-0.5.2-ch.md → releases/release-0.5.2-ch.md} +3 -3
- package/docs/{release-0.5.2-en.md → releases/release-0.5.2-en.md} +3 -3
- package/docs/{release-0.5.3-ch.md → releases/release-0.5.3-ch.md} +1 -1
- package/docs/{release-0.5.3-en.md → releases/release-0.5.3-en.md} +1 -1
- package/docs/releases/release-0.6.0-ch.md +57 -0
- package/docs/releases/release-0.6.0-en.md +57 -0
- 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 -45
- 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/html-compat.reactive.md +19 -0
- 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 -145
- package/dist-library/compiler-UyPwGzJZ.js +0 -22109
- package/dist-library/react-fizgU3i8.js +0 -12764
- package/docs/local-guide-ch.md +0 -967
- package/docs/local-guide-en.md +0 -967
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
# 复用计算与格式化数值
|
|
2
|
-
|
|
3
|
-
这些能力属于使用 `local-draft-2` 的 `0.4.0`。完整语法见[语法规范](local-interaction-syntax-ch.md),宿主接口见[集成说明](local-integration-ch.md),可运行文档见[账单示例](
|
|
4
|
-
|
|
5
|
-
## 命名值
|
|
6
|
-
|
|
7
|
-
```markdown
|
|
8
|
-
<const name="unitPrice" value="{{ 12.5 }}">
|
|
9
|
-
<computed name="total" value="{{ quantity * unitPrice }}">
|
|
10
|
-
<computed name="invoice" value="{{ formatCurrency(total, 'CNY', 2, 'zh-CN') }}">
|
|
11
|
-
|
|
12
|
-
<number bind="quantity" label="数量" initial="2" min="0" max="100">
|
|
13
|
-
|
|
14
|
-
账单:**{{ invoice }}**。
|
|
15
|
-
|
|
16
|
-
<if when="{{ total >= 100 }}">
|
|
17
|
-
达到批量配送门槛。
|
|
18
|
-
</if>
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
`computed` 和 `const` 是不可见的无结束标签声明。每条声明独占文档顶层的一行,也接受 `/>`。两者都必须提供 `name` 和完整的 `value="{{ 表达式 }}"`。值支持数字、字符串、布尔值、null、字符串数组或数字数组。编译器推断类型,无需类型标注,也不进行隐式转换。数组字面量与聚合见[集合说明](constraints-collections-ch.md)。
|
|
22
|
-
|
|
23
|
-
名称沿用 `bind` 的标识符和保留字规则。控件、槽位绑定与声明共用命名空间,声明不能覆盖其他声明或控件。容器内声明是编译错误。代码块、行内代码和注释内的声明保持原有的字面显示或惰性行为。
|
|
24
|
-
|
|
25
|
-
声明在整个文档中可见,可以先引用后声明。`computed` 可以读取控件、常量和其他计算值。`const` 可以读取其他常量和 `pi` 等内置常量,必须独立于控件与计算值。因此,固定单价、阈值等参数无需显示禁用输入框。常量不提供编辑或重置控件。
|
|
26
|
-
|
|
27
|
-
编译器从表达式 AST 提取全部分支的依赖,按依赖顺序推断类型。未知名称、重名、自引用和间接循环均产生带源码位置的错误;未执行分支里的环也属于编译错误。运算符和函数沿用普通表达式的类型规则。运行时仍可能出现域错误,例如读者把作为分母的控件改为零。
|
|
28
|
-
|
|
29
|
-
正文、动态属性、条件和图表表格中的数值单元格都可以读取命名值。可计算 LaTeX 公式中的多字母数字名称写为 `\mathrm{total}`,例如 `y = \mathrm{total} x`。格式化字符串用于正文或标签;图表数值单元格和曲线参数使用原始数字。
|
|
30
|
-
|
|
31
|
-
## 更新与错误
|
|
32
|
-
|
|
33
|
-
每个运行时独立缓存计算结果。同一修订内复用中间值;控件修改、原子槽位修改或面板重置成功后更新计算。常量在编译时求值,创建运行时时复制。隐藏内容保留其输入与计算;重置范围沿用现有控件子树规则。
|
|
34
|
-
|
|
35
|
-
消费者读取失败的计算值时,会收到其表达式错误,不会得到零、null 或上次成功值。独立消费者继续工作;有效输入可让失败的消费者恢复。逻辑与条件运算符保留运行时短路语义,未访问的分支不会读取其中的失败值。静态值非法的常量直接产生编译错误。
|
|
36
|
-
|
|
37
|
-
`document.bindings` 与 `document.initialState` 描述可写控件。`document.computations` 保存可序列化的声明元数据。`runtime.snapshot().state` 还包含本次修订成功求出的命名值,失败值缺席;需要保留错误语义时使用 `runtime.evaluate(name)`。`set` 和 `setMany` 不能修改声明。对外数组继续防止外部修改污染运行时。
|
|
38
|
-
|
|
39
|
-
## 数字格式化
|
|
40
|
-
|
|
41
|
-
四个函数都返回字符串,可选参数按位置传入。
|
|
42
|
-
|
|
43
|
-
| 函数 | 默认值与示例 |
|
|
44
|
-
| --- | --- |
|
|
45
|
-
| `formatNumber(value, digits, locale, grouping)` | `digits=2`、`locale="en-US"`、`grouping=true`;`formatNumber(1234.5)` 得到 `"1,234.50"` |
|
|
46
|
-
| `formatPercent(value, digits, locale)` | `digits=0`、`locale="en-US"`;`formatPercent(0.125, 1)` 得到 `"12.5%"` |
|
|
47
|
-
| `formatScientific(value, digits, locale)` | `digits=2`、`locale="en-US"`;`formatScientific(1234)` 得到 `"1.23E3"` |
|
|
48
|
-
| `formatCurrency(value, currency, digits, locale)` | `currency="USD"`、`digits=2`、`locale="en-US"`;`formatCurrency(12.5)` 得到 `"$12.50"` |
|
|
49
|
-
|
|
50
|
-
`digits` 必须是 0 至 20 的整数,同时固定最少与最多小数位;JPY 等货币也遵守该规则。`grouping` 必须是布尔值。地区只接受 `en-US`、`zh-CN`、`de-DE`、`fr-FR`、`ja-JP`,省略时固定为 `en-US`,与界面语言、设备设置无关;不支持的地区会报错。货币代码必须是 `Intl.NumberFormat` 接受的三个大写 ASCII 字母。这是代码形式检查,不是流通货币名录验证。
|
|
51
|
-
|
|
52
|
-
格式化采用十进制 `halfExpand` 舍入,中点向远离零的方向舍入:`formatNumber(1.005)` 为 `"1.01"`,`formatNumber(-1.005)` 为 `"-1.01"`。零不显示负号。表达式运算仍使用有限的 JavaScript 数字;原有 `round(x)` 保留 `Math.round` 的中点向正无穷规则。格式化只改变显示,不改变源数值。
|
|
53
|
-
|
|
54
|
-
null 格式化为空字符串,其余参数严格检查类型,不把字符串自动转为数字。地区对应的标点、货币符号与空格来自宿主的 `Intl` 数据;跨平台需要逐字一致时,宿主应固定运行时及地区数据版本。
|
|
55
|
-
|
|
56
|
-
## 字符串
|
|
57
|
-
|
|
58
|
-
| 函数 | 规则 |
|
|
59
|
-
| --- | --- |
|
|
60
|
-
| `trim(text)` | 去除首尾 JavaScript Unicode 空白 |
|
|
61
|
-
| `lower(text)`、`upper(text)` | 使用不依赖地区的默认 Unicode 大小写映射 |
|
|
62
|
-
| `startsWith(text, search)`、`endsWith(text, search)` | 区分大小写的字面匹配,空搜索串匹配成功 |
|
|
63
|
-
| `replace(text, search, replacement)` | 替换全部不重叠的字面匹配;空搜索串报错;`$&` 等替换文本保持字面含义 |
|
|
64
|
-
| `slice(text, start, end)` | 按 Unicode 码点索引,终点不包含在结果内,默认到末尾;负索引从末尾计算;索引必须为整数,超界时截到边界 |
|
|
65
|
-
|
|
66
|
-
这些函数要求字符串,null 也会产生类型错误。函数不接受正则表达式或属性访问。码点不等于字素簇,一个组合表情或带组合重音的字母可能包含多个码点。所有结果沿用 100,000 个 UTF-16 代码单元的字符串预算;替换函数在分配超大结果前检查扩张长度。
|
|
67
|
-
|
|
68
|
-
## 日期
|
|
69
|
-
|
|
70
|
-
`dateCompare(a, b)` 返回 `-1`、`0` 或 `1`。`dateDiff(a, b, unit)` 返回有符号的 **a 减 b**,默认单位为 `"days"`,还支持 `"hours"`、`"minutes"`、`"seconds"`、`"milliseconds"`。
|
|
71
|
-
|
|
72
|
-
两项输入必须是有效的 `YYYY-MM-DD` 字符串,年份为 `0001` 至 `9999`,与日期控件一致,按公历校验闰日。每个日期表示 UTC 零点,因此设备时区和夏令时不会改变结果:`dateDiff("2026-03-09", "2026-03-08", "hours")` 为 `24`。时间戳、时区后缀和有歧义的地区日期格式会被拒绝。
|
|
73
|
-
|
|
74
|
-
空日期控件的值为 `""`,表示未选择日期。空串或非法日期产生域错误,null 产生类型错误。可选日期应先判断:
|
|
75
|
-
|
|
76
|
-
```markdown
|
|
77
|
-
<computed name="days" value="{{ startDate == '' || endDate == '' ? null : dateDiff(endDate, startDate) }}">
|
|
78
|
-
天数:{{ days == null ? '请选择两个日期' : formatNumber(days, 0) }}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## 诊断与预算
|
|
82
|
-
|
|
83
|
-
声明诊断采用 `local.computation.*`:`placement`、`name`、`reserved`、`duplicate`、`value`、`unknown`、`cycle`、`const`、`expression`、`budget`。表达式类型错误沿用 `local.expression.*`。诊断包含源码范围、行号和列号。计算图采用迭代式依赖排序,长链不依赖递归求值。源码、语法节点、表达式和值继续遵守现有预算,循环诊断数量也受诊断预算限制。
|
|
84
|
-
|
|
85
|
-
## 兼容性
|
|
86
|
-
|
|
87
|
-
现有表达式和 `round(x)` 保持原有行为。新增函数名称成为本版本的保留标识符;升级前应重命名使用这些名称的旧绑定,并同步修改引用。[变更记录](
|
|
1
|
+
# 复用计算与格式化数值
|
|
2
|
+
|
|
3
|
+
这些能力属于使用 `local-draft-2` 的 `0.4.0`。完整语法见[语法规范](local-interaction-syntax-ch.md),宿主接口见[集成说明](../integration/local-integration-ch.md),可运行文档见[账单示例](../../examples/local/recipes/invoice-zh.reactive.md)。
|
|
4
|
+
|
|
5
|
+
## 命名值
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
<const name="unitPrice" value="{{ 12.5 }}">
|
|
9
|
+
<computed name="total" value="{{ quantity * unitPrice }}">
|
|
10
|
+
<computed name="invoice" value="{{ formatCurrency(total, 'CNY', 2, 'zh-CN') }}">
|
|
11
|
+
|
|
12
|
+
<number bind="quantity" label="数量" initial="2" min="0" max="100">
|
|
13
|
+
|
|
14
|
+
账单:**{{ invoice }}**。
|
|
15
|
+
|
|
16
|
+
<if when="{{ total >= 100 }}">
|
|
17
|
+
达到批量配送门槛。
|
|
18
|
+
</if>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`computed` 和 `const` 是不可见的无结束标签声明。每条声明独占文档顶层的一行,也接受 `/>`。两者都必须提供 `name` 和完整的 `value="{{ 表达式 }}"`。值支持数字、字符串、布尔值、null、字符串数组或数字数组。编译器推断类型,无需类型标注,也不进行隐式转换。数组字面量与聚合见[集合说明](constraints-collections-ch.md)。
|
|
22
|
+
|
|
23
|
+
名称沿用 `bind` 的标识符和保留字规则。控件、槽位绑定与声明共用命名空间,声明不能覆盖其他声明或控件。容器内声明是编译错误。代码块、行内代码和注释内的声明保持原有的字面显示或惰性行为。
|
|
24
|
+
|
|
25
|
+
声明在整个文档中可见,可以先引用后声明。`computed` 可以读取控件、常量和其他计算值。`const` 可以读取其他常量和 `pi` 等内置常量,必须独立于控件与计算值。因此,固定单价、阈值等参数无需显示禁用输入框。常量不提供编辑或重置控件。
|
|
26
|
+
|
|
27
|
+
编译器从表达式 AST 提取全部分支的依赖,按依赖顺序推断类型。未知名称、重名、自引用和间接循环均产生带源码位置的错误;未执行分支里的环也属于编译错误。运算符和函数沿用普通表达式的类型规则。运行时仍可能出现域错误,例如读者把作为分母的控件改为零。
|
|
28
|
+
|
|
29
|
+
正文、动态属性、条件和图表表格中的数值单元格都可以读取命名值。可计算 LaTeX 公式中的多字母数字名称写为 `\mathrm{total}`,例如 `y = \mathrm{total} x`。格式化字符串用于正文或标签;图表数值单元格和曲线参数使用原始数字。
|
|
30
|
+
|
|
31
|
+
## 更新与错误
|
|
32
|
+
|
|
33
|
+
每个运行时独立缓存计算结果。同一修订内复用中间值;控件修改、原子槽位修改或面板重置成功后更新计算。常量在编译时求值,创建运行时时复制。隐藏内容保留其输入与计算;重置范围沿用现有控件子树规则。
|
|
34
|
+
|
|
35
|
+
消费者读取失败的计算值时,会收到其表达式错误,不会得到零、null 或上次成功值。独立消费者继续工作;有效输入可让失败的消费者恢复。逻辑与条件运算符保留运行时短路语义,未访问的分支不会读取其中的失败值。静态值非法的常量直接产生编译错误。
|
|
36
|
+
|
|
37
|
+
`document.bindings` 与 `document.initialState` 描述可写控件。`document.computations` 保存可序列化的声明元数据。`runtime.snapshot().state` 还包含本次修订成功求出的命名值,失败值缺席;需要保留错误语义时使用 `runtime.evaluate(name)`。`set` 和 `setMany` 不能修改声明。对外数组继续防止外部修改污染运行时。
|
|
38
|
+
|
|
39
|
+
## 数字格式化
|
|
40
|
+
|
|
41
|
+
四个函数都返回字符串,可选参数按位置传入。
|
|
42
|
+
|
|
43
|
+
| 函数 | 默认值与示例 |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `formatNumber(value, digits, locale, grouping)` | `digits=2`、`locale="en-US"`、`grouping=true`;`formatNumber(1234.5)` 得到 `"1,234.50"` |
|
|
46
|
+
| `formatPercent(value, digits, locale)` | `digits=0`、`locale="en-US"`;`formatPercent(0.125, 1)` 得到 `"12.5%"` |
|
|
47
|
+
| `formatScientific(value, digits, locale)` | `digits=2`、`locale="en-US"`;`formatScientific(1234)` 得到 `"1.23E3"` |
|
|
48
|
+
| `formatCurrency(value, currency, digits, locale)` | `currency="USD"`、`digits=2`、`locale="en-US"`;`formatCurrency(12.5)` 得到 `"$12.50"` |
|
|
49
|
+
|
|
50
|
+
`digits` 必须是 0 至 20 的整数,同时固定最少与最多小数位;JPY 等货币也遵守该规则。`grouping` 必须是布尔值。地区只接受 `en-US`、`zh-CN`、`de-DE`、`fr-FR`、`ja-JP`,省略时固定为 `en-US`,与界面语言、设备设置无关;不支持的地区会报错。货币代码必须是 `Intl.NumberFormat` 接受的三个大写 ASCII 字母。这是代码形式检查,不是流通货币名录验证。
|
|
51
|
+
|
|
52
|
+
格式化采用十进制 `halfExpand` 舍入,中点向远离零的方向舍入:`formatNumber(1.005)` 为 `"1.01"`,`formatNumber(-1.005)` 为 `"-1.01"`。零不显示负号。表达式运算仍使用有限的 JavaScript 数字;原有 `round(x)` 保留 `Math.round` 的中点向正无穷规则。格式化只改变显示,不改变源数值。
|
|
53
|
+
|
|
54
|
+
null 格式化为空字符串,其余参数严格检查类型,不把字符串自动转为数字。地区对应的标点、货币符号与空格来自宿主的 `Intl` 数据;跨平台需要逐字一致时,宿主应固定运行时及地区数据版本。
|
|
55
|
+
|
|
56
|
+
## 字符串
|
|
57
|
+
|
|
58
|
+
| 函数 | 规则 |
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| `trim(text)` | 去除首尾 JavaScript Unicode 空白 |
|
|
61
|
+
| `lower(text)`、`upper(text)` | 使用不依赖地区的默认 Unicode 大小写映射 |
|
|
62
|
+
| `startsWith(text, search)`、`endsWith(text, search)` | 区分大小写的字面匹配,空搜索串匹配成功 |
|
|
63
|
+
| `replace(text, search, replacement)` | 替换全部不重叠的字面匹配;空搜索串报错;`$&` 等替换文本保持字面含义 |
|
|
64
|
+
| `slice(text, start, end)` | 按 Unicode 码点索引,终点不包含在结果内,默认到末尾;负索引从末尾计算;索引必须为整数,超界时截到边界 |
|
|
65
|
+
|
|
66
|
+
这些函数要求字符串,null 也会产生类型错误。函数不接受正则表达式或属性访问。码点不等于字素簇,一个组合表情或带组合重音的字母可能包含多个码点。所有结果沿用 100,000 个 UTF-16 代码单元的字符串预算;替换函数在分配超大结果前检查扩张长度。
|
|
67
|
+
|
|
68
|
+
## 日期
|
|
69
|
+
|
|
70
|
+
`dateCompare(a, b)` 返回 `-1`、`0` 或 `1`。`dateDiff(a, b, unit)` 返回有符号的 **a 减 b**,默认单位为 `"days"`,还支持 `"hours"`、`"minutes"`、`"seconds"`、`"milliseconds"`。
|
|
71
|
+
|
|
72
|
+
两项输入必须是有效的 `YYYY-MM-DD` 字符串,年份为 `0001` 至 `9999`,与日期控件一致,按公历校验闰日。每个日期表示 UTC 零点,因此设备时区和夏令时不会改变结果:`dateDiff("2026-03-09", "2026-03-08", "hours")` 为 `24`。时间戳、时区后缀和有歧义的地区日期格式会被拒绝。
|
|
73
|
+
|
|
74
|
+
空日期控件的值为 `""`,表示未选择日期。空串或非法日期产生域错误,null 产生类型错误。可选日期应先判断:
|
|
75
|
+
|
|
76
|
+
```markdown
|
|
77
|
+
<computed name="days" value="{{ startDate == '' || endDate == '' ? null : dateDiff(endDate, startDate) }}">
|
|
78
|
+
天数:{{ days == null ? '请选择两个日期' : formatNumber(days, 0) }}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 诊断与预算
|
|
82
|
+
|
|
83
|
+
声明诊断采用 `local.computation.*`:`placement`、`name`、`reserved`、`duplicate`、`value`、`unknown`、`cycle`、`const`、`expression`、`budget`。表达式类型错误沿用 `local.expression.*`。诊断包含源码范围、行号和列号。计算图采用迭代式依赖排序,长链不依赖递归求值。源码、语法节点、表达式和值继续遵守现有预算,循环诊断数量也受诊断预算限制。
|
|
84
|
+
|
|
85
|
+
## 兼容性
|
|
86
|
+
|
|
87
|
+
现有表达式和 `round(x)` 保持原有行为。新增函数名称成为本版本的保留标识符;升级前应重命名使用这些名称的旧绑定,并同步修改引用。[变更记录](../../CHANGELOG-ch.md)记载了此次扩展。
|
|
@@ -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](local-integration-en.md) and [working example](
|
|
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](
|
|
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`。[工作坊示例](
|
|
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。对象生命周期和持久化规则见[集成说明](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` 渠道。
|