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,113 +1,113 @@
|
|
|
1
|
-
# Shareable documents and static export
|
|
2
|
-
|
|
3
|
-
This guide covers the single-file HTML exporter introduced with `local-draft-2`. It complements [Local mode integration](local-integration-en.md), the [syntax reference](../reference/local-interaction-syntax-en.md), [Source revisions and migration](../compatibility/source-versioning-en.md) and the [0.4.0 release notes](../releases/release-0.4.0-en.md).
|
|
4
|
-
|
|
5
|
-
## Output modes
|
|
6
|
-
|
|
7
|
-
The rules below describe default ReactiveMD documents. Native HTML interactive export preserves `compileOptions` and whole-document HTML routing; static export disables author JavaScript. These documents have no ReactiveMD parameters. See [HTML compatibility](../reference/html-compatibility-en.md) for capability flags, isolation and the temporary routing policy.
|
|
8
|
-
|
|
9
|
-
**Interactive HTML** is one self-contained `.html` file. It embeds the original source, the standalone browser runtime, React 19, the current stylesheet and print styles. Readers can open it through `file://` without Node.js, React, the reactive-md package or network access.
|
|
10
|
-
|
|
11
|
-
The first rendered pass is static. After the embedded runtime hydrates it, controls become interactive. With JavaScript disabled the same static values, tables, formulas and charts remain readable. Refreshing restores the exported baseline; the reader's changes are not persisted.
|
|
12
|
-
|
|
13
|
-
**Static HTML** (`--mode static`) contains read-only values and no executable script cargo. It is intended for archives, static sites and email attachments. Interactive action buttons, form widgets and the reset action are removed.
|
|
14
|
-
|
|
15
|
-
Both modes use the same renderer as the React integration, so conditional content, interpolation, tables, formulas, charts and layout follow the runtime state.
|
|
16
|
-
|
|
17
|
-
## CLI
|
|
18
|
-
|
|
19
|
-
The package exposes the `reactive-md` command. HTML builds require React 19 and React DOM in the project that installs the package; `lint` and `migrate` do not.
|
|
20
|
-
|
|
21
|
-
```sh
|
|
22
|
-
reactive-md build doc.reactive.md -o doc.html
|
|
23
|
-
reactive-md build doc.reactive.md -o doc.html --mode static
|
|
24
|
-
reactive-md build doc.reactive.md -o doc.html --state-file doc.state.json
|
|
25
|
-
reactive-md build doc.reactive.md -o doc.html --assets omit
|
|
26
|
-
reactive-md lint "docs/**/*.reactive.md"
|
|
27
|
-
reactive-md lint "docs/**/*.reactive.md" --json --strict
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Build options:
|
|
31
|
-
|
|
32
|
-
| Option | Meaning |
|
|
33
|
-
| --- | --- |
|
|
34
|
-
| `-o, --output <file>` | Required output path. Its parent directory must already exist. |
|
|
35
|
-
| `--mode interactive\|static` | Output mode. Default: `interactive`. |
|
|
36
|
-
| `--state-file <json>` | Use a validated `reactive-md-state/v1` file as the starting and reset baseline. |
|
|
37
|
-
| `--assets inline\|omit` | Inline verified local raster images (default) or leave the renderer's alt fallback. |
|
|
38
|
-
| `--asset-root <dir>` | Additional local directory that relative image paths may resolve inside. |
|
|
39
|
-
| `--locale en\|zh` | Renderer labels and validation messages. Default: `en`. |
|
|
40
|
-
| `--color-scheme auto\|light\|dark` | Document color mode. Default: `auto`. |
|
|
41
|
-
| `--title <text>` | Override the HTML title. The first heading or the input filename is used otherwise. |
|
|
42
|
-
| `--strict` | Fail on compiler warnings; with `--assets omit`, fail when images are omitted. |
|
|
43
|
-
|
|
44
|
-
Exit codes are separate so build automation can distinguish failures:
|
|
45
|
-
|
|
46
|
-
| Code | Meaning |
|
|
47
|
-
| --- | --- |
|
|
48
|
-
| `0` | Success |
|
|
49
|
-
| `2` | Usage or argument error |
|
|
50
|
-
| `3` | Document compile, render, React, or packaged-artifact failure |
|
|
51
|
-
| `4` | Input/output file failure |
|
|
52
|
-
| `5` | Asset or security-policy failure |
|
|
53
|
-
|
|
54
|
-
The CLI does not use a current preview implicitly. `--state-file` is the only way to select a non-initial baseline. `format` is not part of this release.
|
|
55
|
-
|
|
56
|
-
## Migrating source
|
|
57
|
-
|
|
58
|
-
`reactive-md migrate` moves one source revision declaration to `local-draft-2`:
|
|
59
|
-
|
|
60
|
-
```sh
|
|
61
|
-
reactive-md migrate old.reactive.md --from local-draft-1 -o new.reactive.md
|
|
62
|
-
reactive-md migrate old.reactive.md --check --json
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
By default it prints the migrated source to standard output. `-o` creates a new file with exclusive creation and refuses any existing path, including the input. `--check` never writes. `--json` prints a structured report and nothing else. `--accept-semantic-changes` allows the text-control length change and reports warnings; `--locale en|zh` selects the display language.
|
|
66
|
-
|
|
67
|
-
A migration exits `1` when blocked and `0` on success. `--check` exits `1` when changes are needed or blocked, and `0` when the source is current. Usage and I/O errors exit `2`. The command does not overwrite in place and has no batch or `--write` mode. See [Source revisions and migration](../compatibility/source-versioning-en.md) for the checks, preserved bytes and state-digest consequences.
|
|
68
|
-
|
|
69
|
-
## Starting values and state files
|
|
70
|
-
|
|
71
|
-
By default, the exported baseline is the document's author initial values. This keeps builds repeatable and avoids leaking trial inputs.
|
|
72
|
-
|
|
73
|
-
A state file has this versioned shape:
|
|
74
|
-
|
|
75
|
-
```json
|
|
76
|
-
{
|
|
77
|
-
"format": "reactive-md-state/v1",
|
|
78
|
-
"syntax": "local-draft-2",
|
|
79
|
-
"sourceDigest": "fnv1a32:0123abcd",
|
|
80
|
-
"parameters": {
|
|
81
|
-
"people": 7,
|
|
82
|
-
"plan": "pro"
|
|
83
|
-
},
|
|
84
|
-
"generator": { "name": "example-host", "version": "1.0.0" }
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
Only public, serializable binding values are stored. The file identifies the document through `sourceDigest` and the language revision through `syntax`. Internal bindings (names beginning with `@`), revisions, node IDs, AST data, timers, open panels and focus are never persisted; internal snapshot keys and IDs are implementation details, not a storage format.
|
|
89
|
-
|
|
90
|
-
Resolution verifies the source digest, syntax revision, exact public parameter set and every value against the document's constraints. Unknown, missing or invalid parameters reject the file with a named diagnostic; a mismatched file is never partially applied. Slot controls such as `<range-slider>` are validated atomically.
|
|
91
|
-
|
|
92
|
-
The selected state becomes the document's reset baseline. Opening, refreshing and the panel reset action therefore return to the exported values, not to a different hidden default.
|
|
93
|
-
|
|
94
|
-
Hosts can create and apply the same file without the CLI:
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
import { exportLocalState, importLocalState } from 'reactive-md';
|
|
98
|
-
|
|
99
|
-
const file = exportLocalState(runtime, { name: 'example-host', version: '1.0.0' });
|
|
100
|
-
const saved = JSON.stringify(file);
|
|
101
|
-
|
|
102
|
-
const diagnostics = importLocalState(runtime, saved);
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
`exportLocalState` reads public parameters only. `importLocalState` validates the source digest, syntax, parameter set and constraints, then applies the values; a failed import leaves the runtime unchanged. Both are explicit host calls: the runtime still does not save or send state by itself. Use `resolveLocalState(document, input)` to validate without touching a live runtime.
|
|
106
|
-
|
|
107
|
-
The demo's export dialog can download the current preview parameters as a state file for a later CLI build. The lower-level `createLocalStateFile`, `parseLocalState`, `applyLocalState` and `serializeLocalState` remain available from `reactive-md`.
|
|
108
|
-
|
|
109
|
-
## Images
|
|
110
|
-
|
|
1
|
+
# Shareable documents and static export
|
|
2
|
+
|
|
3
|
+
This guide covers the single-file HTML exporter introduced with `local-draft-2`. It complements [Local mode integration](local-integration-en.md), the [syntax reference](../reference/local-interaction-syntax-en.md), [Source revisions and migration](../compatibility/source-versioning-en.md) and the [0.4.0 release notes](../releases/release-0.4.0-en.md).
|
|
4
|
+
|
|
5
|
+
## Output modes
|
|
6
|
+
|
|
7
|
+
The rules below describe default ReactiveMD documents. Native HTML interactive export preserves `compileOptions` and whole-document HTML routing; static export disables author JavaScript. These documents have no ReactiveMD parameters. See [HTML compatibility](../reference/html-compatibility-en.md) for capability flags, isolation and the temporary routing policy.
|
|
8
|
+
|
|
9
|
+
**Interactive HTML** is one self-contained `.html` file. It embeds the original source, the standalone browser runtime, React 19, the current stylesheet and print styles. Readers can open it through `file://` without Node.js, React, the reactive-md package or network access.
|
|
10
|
+
|
|
11
|
+
The first rendered pass is static. After the embedded runtime hydrates it, controls become interactive. With JavaScript disabled the same static values, tables, formulas and charts remain readable. Refreshing restores the exported baseline; the reader's changes are not persisted.
|
|
12
|
+
|
|
13
|
+
**Static HTML** (`--mode static`) contains read-only values and no executable script cargo. It is intended for archives, static sites and email attachments. Interactive action buttons, form widgets and the reset action are removed.
|
|
14
|
+
|
|
15
|
+
Both modes use the same renderer as the React integration, so conditional content, interpolation, tables, formulas, charts and layout follow the runtime state.
|
|
16
|
+
|
|
17
|
+
## CLI
|
|
18
|
+
|
|
19
|
+
The package exposes the `reactive-md` command. HTML builds require React 19 and React DOM in the project that installs the package; `lint` and `migrate` do not.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
reactive-md build doc.reactive.md -o doc.html
|
|
23
|
+
reactive-md build doc.reactive.md -o doc.html --mode static
|
|
24
|
+
reactive-md build doc.reactive.md -o doc.html --state-file doc.state.json
|
|
25
|
+
reactive-md build doc.reactive.md -o doc.html --assets omit
|
|
26
|
+
reactive-md lint "docs/**/*.reactive.md"
|
|
27
|
+
reactive-md lint "docs/**/*.reactive.md" --json --strict
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Build options:
|
|
31
|
+
|
|
32
|
+
| Option | Meaning |
|
|
33
|
+
| --- | --- |
|
|
34
|
+
| `-o, --output <file>` | Required output path. Its parent directory must already exist. |
|
|
35
|
+
| `--mode interactive\|static` | Output mode. Default: `interactive`. |
|
|
36
|
+
| `--state-file <json>` | Use a validated `reactive-md-state/v1` file as the starting and reset baseline. |
|
|
37
|
+
| `--assets inline\|omit` | Inline verified local raster images (default) or leave the renderer's alt fallback. |
|
|
38
|
+
| `--asset-root <dir>` | Additional local directory that relative image paths may resolve inside. |
|
|
39
|
+
| `--locale en\|zh` | Renderer labels and validation messages. Default: `en`. |
|
|
40
|
+
| `--color-scheme auto\|light\|dark` | Document color mode. Default: `auto`. |
|
|
41
|
+
| `--title <text>` | Override the HTML title. The first heading or the input filename is used otherwise. |
|
|
42
|
+
| `--strict` | Fail on compiler warnings; with `--assets omit`, fail when images are omitted. |
|
|
43
|
+
|
|
44
|
+
Exit codes are separate so build automation can distinguish failures:
|
|
45
|
+
|
|
46
|
+
| Code | Meaning |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| `0` | Success |
|
|
49
|
+
| `2` | Usage or argument error |
|
|
50
|
+
| `3` | Document compile, render, React, or packaged-artifact failure |
|
|
51
|
+
| `4` | Input/output file failure |
|
|
52
|
+
| `5` | Asset or security-policy failure |
|
|
53
|
+
|
|
54
|
+
The CLI does not use a current preview implicitly. `--state-file` is the only way to select a non-initial baseline. `format` is not part of this release.
|
|
55
|
+
|
|
56
|
+
## Migrating source
|
|
57
|
+
|
|
58
|
+
`reactive-md migrate` moves one source revision declaration to `local-draft-2`:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
reactive-md migrate old.reactive.md --from local-draft-1 -o new.reactive.md
|
|
62
|
+
reactive-md migrate old.reactive.md --check --json
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
By default it prints the migrated source to standard output. `-o` creates a new file with exclusive creation and refuses any existing path, including the input. `--check` never writes. `--json` prints a structured report and nothing else. `--accept-semantic-changes` allows the text-control length change and reports warnings; `--locale en|zh` selects the display language.
|
|
66
|
+
|
|
67
|
+
A migration exits `1` when blocked and `0` on success. `--check` exits `1` when changes are needed or blocked, and `0` when the source is current. Usage and I/O errors exit `2`. The command does not overwrite in place and has no batch or `--write` mode. See [Source revisions and migration](../compatibility/source-versioning-en.md) for the checks, preserved bytes and state-digest consequences.
|
|
68
|
+
|
|
69
|
+
## Starting values and state files
|
|
70
|
+
|
|
71
|
+
By default, the exported baseline is the document's author initial values. This keeps builds repeatable and avoids leaking trial inputs.
|
|
72
|
+
|
|
73
|
+
A state file has this versioned shape:
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{
|
|
77
|
+
"format": "reactive-md-state/v1",
|
|
78
|
+
"syntax": "local-draft-2",
|
|
79
|
+
"sourceDigest": "fnv1a32:0123abcd",
|
|
80
|
+
"parameters": {
|
|
81
|
+
"people": 7,
|
|
82
|
+
"plan": "pro"
|
|
83
|
+
},
|
|
84
|
+
"generator": { "name": "example-host", "version": "1.0.0" }
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Only public, serializable binding values are stored. The file identifies the document through `sourceDigest` and the language revision through `syntax`. Internal bindings (names beginning with `@`), revisions, node IDs, AST data, timers, open panels and focus are never persisted; internal snapshot keys and IDs are implementation details, not a storage format.
|
|
89
|
+
|
|
90
|
+
Resolution verifies the source digest, syntax revision, exact public parameter set and every value against the document's constraints. Unknown, missing or invalid parameters reject the file with a named diagnostic; a mismatched file is never partially applied. Slot controls such as `<range-slider>` are validated atomically.
|
|
91
|
+
|
|
92
|
+
The selected state becomes the document's reset baseline. Opening, refreshing and the panel reset action therefore return to the exported values, not to a different hidden default.
|
|
93
|
+
|
|
94
|
+
Hosts can create and apply the same file without the CLI:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { exportLocalState, importLocalState } from 'reactive-md';
|
|
98
|
+
|
|
99
|
+
const file = exportLocalState(runtime, { name: 'example-host', version: '1.0.0' });
|
|
100
|
+
const saved = JSON.stringify(file);
|
|
101
|
+
|
|
102
|
+
const diagnostics = importLocalState(runtime, saved);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`exportLocalState` reads public parameters only. `importLocalState` validates the source digest, syntax, parameter set and constraints, then applies the values; a failed import leaves the runtime unchanged. Both are explicit host calls: the runtime still does not save or send state by itself. Use `resolveLocalState(document, input)` to validate without touching a live runtime.
|
|
106
|
+
|
|
107
|
+
The demo's export dialog can download the current preview parameters as a state file for a later CLI build. The lower-level `createLocalStateFile`, `parseLocalState`, `applyLocalState` and `serializeLocalState` remain available from `reactive-md`.
|
|
108
|
+
|
|
109
|
+
## Images
|
|
110
|
+
|
|
111
111
|
### Browser
|
|
112
112
|
|
|
113
113
|
Use **Document images** in the demo to select files, or paste an image into the source editor. Accepted files create a Markdown reference at the selection and appear in the resource list. The list shows each source URL, format, size and whether the document references it; files can be replaced or removed. Imports are checked as a batch: an invalid file leaves the source and manifest unchanged.
|
|
@@ -120,104 +120,104 @@ These browser resource controls apply to the ReactiveMD route, including P0 `<im
|
|
|
120
120
|
|
|
121
121
|
### CLI
|
|
122
122
|
|
|
123
|
-
Offline export defaults to `--assets inline`:
|
|
124
|
-
|
|
125
|
-
- Only relative paths that resolve inside the document directory, or an explicit `--asset-root`, are read.
|
|
126
|
-
- Absolute paths, URL schemes, protocol-relative URLs, backslashes, path traversal and symlink escapes are rejected.
|
|
127
|
-
- Remote images are never fetched.
|
|
128
|
-
- PNG, JPEG, GIF and WebP are detected by file contents; SVG, forged MIME types and unknown formats are rejected.
|
|
129
|
-
- Files are limited to 8 MiB each, 24 MiB total and 64 distinct payloads. Identical content is deduplicated.
|
|
130
|
-
- Alt text is preserved. A failure stops the build by default. `--assets omit` is the explicit opt-out and leaves the alt fallback.
|
|
131
|
-
|
|
132
|
-
The renderer's global security boundary is unchanged: images are still rejected by default unless a host URL policy explicitly allows them. The exporter supplies a host policy and image resolver after it has verified each local file.
|
|
133
|
-
|
|
134
|
-
## Single-file security and printing
|
|
135
|
-
|
|
136
|
-
The HTML shell includes:
|
|
137
|
-
|
|
138
|
-
- a CSP that disallows connections, frames, objects, base URL changes and form submission while allowing the inline stylesheet/runtime and `data:` images;
|
|
139
|
-
- HTML- and script-context escaping for JSON, source, titles and license notices;
|
|
140
|
-
- the MIT and third-party notices in a non-executed `<template>`;
|
|
141
|
-
- no CDN or remote stylesheet, font or runtime.
|
|
142
|
-
|
|
143
|
-
Clicking an ordinary external link can still navigate, but opening the document itself performs no remote request. The exporter is not a sandbox for fully untrusted authors; use a sandboxed iframe when that threat model applies.
|
|
144
|
-
|
|
145
|
-
Printing does not require a temporary state switch. The current runtime values are rendered in print-only nodes, so `Ctrl+P` prints the state the reader currently sees. Print CSS forces a light paper theme, hides toolbars and controls, uses printer-owned page margins, keeps headings with following content, repeats table headers, wraps long code and URLs, and constrains charts to page width. Static HTML already contains only read-only values.
|
|
146
|
-
|
|
147
|
-
## Standalone runtime API
|
|
148
|
-
|
|
149
|
-
The build emits `dist-standalone/reactive-md-standalone.js` (IIFE, global `ReactiveMD`) and `reactive-md-standalone.css`. Load the stylesheet and script as classic browser assets:
|
|
150
|
-
|
|
151
|
-
```html
|
|
152
|
-
<link rel="stylesheet" href="reactive-md-standalone.css">
|
|
153
|
-
<script src="reactive-md-standalone.js"></script>
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
Programmatic mounting:
|
|
157
|
-
|
|
158
|
-
```js
|
|
159
|
-
const handle = ReactiveMD.mount(document.getElementById('doc'), {
|
|
160
|
-
source: '# Hello\n\n<slider bind="n" label="Count" initial="2">\n\nValue: {{ n }}',
|
|
161
|
-
locale: 'en',
|
|
162
|
-
colorScheme: 'auto',
|
|
163
|
-
});
|
|
164
|
-
handle.getState(); // reactive-md-state/v1 snapshot
|
|
165
|
-
handle.unmount();
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
`ReactiveMD.mountAll(root)` scans only once for safe auto-mount payloads:
|
|
169
|
-
|
|
170
|
-
```html
|
|
171
|
-
<div id="doc"></div>
|
|
172
|
-
<script type="application/reactive-md+json" data-reactive-md-auto>
|
|
173
|
-
{"version":1,"target":"doc","source":"# Hello","state":{"format":"reactive-md-state/v1","syntax":"local-draft-2","sourceDigest":"fnv1a32:01209266","parameters":{}},"locale":"en","colorScheme":"auto","hydrate":false}
|
|
174
|
-
</script>
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
Targets are resolved by `id`, never by an interpolated selector. Payload source is data, not executable markup. Raw `<script type="text/reactive-md">` text is deliberately not interpreted.
|
|
178
|
-
|
|
179
|
-
### `<reactive-md-doc>`
|
|
180
|
-
|
|
181
|
-
The IIFE registers a light-DOM custom element. Simple options are attributes; complex options are DOM properties.
|
|
182
|
-
|
|
183
|
-
```html
|
|
184
|
-
<reactive-md-doc src="./doc.reactive.md" locale="en" color-scheme="auto"></reactive-md-doc>
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
- Attributes: `src`, `source`, `locale`, `color-scheme`, `theme`.
|
|
188
|
-
- Properties: `source`, `state`, `urlPolicy`, `imageResolver`, `themes`.
|
|
189
|
-
- Events: `reactive-md-diagnostics` and `reactive-md-state-change`.
|
|
190
|
-
- `src` uses `fetch` with cancellation; an explicit source avoids network access.
|
|
191
|
-
- Disconnecting aborts fetches, removes subscriptions and the React root, and disposes the runtime. Two elements with the same source have independent state.
|
|
192
|
-
|
|
193
|
-
The element uses Light DOM so the existing document CSS, heading anchors and print styles apply. Shadow DOM is intentionally out of scope for this release.
|
|
194
|
-
|
|
195
|
-
## Security boundaries
|
|
196
|
-
|
|
197
|
-
- Core and React entry points keep React external. Only the standalone IIFE bundles React.
|
|
198
|
-
- The core entry remains React-free.
|
|
199
|
-
- State export and import use only public binding names and validated values.
|
|
200
|
-
- Exported HTML does not persist a reader's changes.
|
|
201
|
-
- The demo previews and exports images supplied through its document resource list. Missing references block HTML export until files are selected.
|
|
202
|
-
- The export format version is `reactive-md-html/v1`; inspect it in the generated meta tags.
|
|
203
|
-
|
|
204
|
-
## Bundle baseline
|
|
205
|
-
|
|
206
|
-
`npm run test:shareable` records the standalone artifact size and fails above these regression alarms:
|
|
207
|
-
|
|
208
|
-
| Artifact | Current baseline | Alarm |
|
|
209
|
-
| --- | --- | --- |
|
|
210
|
-
| `reactive-md-standalone.js` | ≈925 kB (≈276 kB gzip) | 1024 KiB / 320 KiB gzip |
|
|
211
|
-
| `reactive-md-standalone.css` | ≈81.2 kB (≈13.5 kB gzip) | 80 KiB / 20 KiB gzip |
|
|
212
|
-
|
|
213
|
-
The 0.5 baseline includes the syntax highlighter and common language grammars for offline use.
|
|
214
|
-
|
|
215
|
-
Sizes vary slightly with the minifier and React patch release. The alarm is intentionally well above normal noise while catching a React or renderer duplication regression. The full generated HTML also contains the source, notices and inline images, so it is larger than this baseline.
|
|
216
|
-
|
|
217
|
-
## Current scope
|
|
218
|
-
|
|
219
|
-
Included: CLI `build`/`lint`/`migrate`, interactive and static HTML, state files, local raster inlining, print output, standalone mount APIs, auto-mount, and the Web Component.
|
|
220
|
-
|
|
221
|
-
Not included in this release: `format`, UMD, remote image fetching, SVG inlining, framework plugins (Vite, Astro, Docusaurus, Next.js), Shadow DOM, and general SSR hydration guarantees outside the single-file export path.
|
|
222
|
-
|
|
223
|
-
Single-file static-first hydration is covered by the Playwright Chromium checks and local Edge checks. Firefox, Safari/WebKit and general server-side hydration remain unverified and are not claimed as supported.
|
|
123
|
+
Offline export defaults to `--assets inline`:
|
|
124
|
+
|
|
125
|
+
- Only relative paths that resolve inside the document directory, or an explicit `--asset-root`, are read.
|
|
126
|
+
- Absolute paths, URL schemes, protocol-relative URLs, backslashes, path traversal and symlink escapes are rejected.
|
|
127
|
+
- Remote images are never fetched.
|
|
128
|
+
- PNG, JPEG, GIF and WebP are detected by file contents; SVG, forged MIME types and unknown formats are rejected.
|
|
129
|
+
- Files are limited to 8 MiB each, 24 MiB total and 64 distinct payloads. Identical content is deduplicated.
|
|
130
|
+
- Alt text is preserved. A failure stops the build by default. `--assets omit` is the explicit opt-out and leaves the alt fallback.
|
|
131
|
+
|
|
132
|
+
The renderer's global security boundary is unchanged: images are still rejected by default unless a host URL policy explicitly allows them. The exporter supplies a host policy and image resolver after it has verified each local file.
|
|
133
|
+
|
|
134
|
+
## Single-file security and printing
|
|
135
|
+
|
|
136
|
+
The HTML shell includes:
|
|
137
|
+
|
|
138
|
+
- a CSP that disallows connections, frames, objects, base URL changes and form submission while allowing the inline stylesheet/runtime and `data:` images;
|
|
139
|
+
- HTML- and script-context escaping for JSON, source, titles and license notices;
|
|
140
|
+
- the MIT and third-party notices in a non-executed `<template>`;
|
|
141
|
+
- no CDN or remote stylesheet, font or runtime.
|
|
142
|
+
|
|
143
|
+
Clicking an ordinary external link can still navigate, but opening the document itself performs no remote request. The exporter is not a sandbox for fully untrusted authors; use a sandboxed iframe when that threat model applies.
|
|
144
|
+
|
|
145
|
+
Printing does not require a temporary state switch. The current runtime values are rendered in print-only nodes, so `Ctrl+P` prints the state the reader currently sees. Print CSS forces a light paper theme, hides toolbars and controls, uses printer-owned page margins, keeps headings with following content, repeats table headers, wraps long code and URLs, and constrains charts to page width. Static HTML already contains only read-only values.
|
|
146
|
+
|
|
147
|
+
## Standalone runtime API
|
|
148
|
+
|
|
149
|
+
The build emits `dist-standalone/reactive-md-standalone.js` (IIFE, global `ReactiveMD`) and `reactive-md-standalone.css`. Load the stylesheet and script as classic browser assets:
|
|
150
|
+
|
|
151
|
+
```html
|
|
152
|
+
<link rel="stylesheet" href="reactive-md-standalone.css">
|
|
153
|
+
<script src="reactive-md-standalone.js"></script>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Programmatic mounting:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
const handle = ReactiveMD.mount(document.getElementById('doc'), {
|
|
160
|
+
source: '# Hello\n\n<slider bind="n" label="Count" initial="2">\n\nValue: {{ n }}',
|
|
161
|
+
locale: 'en',
|
|
162
|
+
colorScheme: 'auto',
|
|
163
|
+
});
|
|
164
|
+
handle.getState(); // reactive-md-state/v1 snapshot
|
|
165
|
+
handle.unmount();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
`ReactiveMD.mountAll(root)` scans only once for safe auto-mount payloads:
|
|
169
|
+
|
|
170
|
+
```html
|
|
171
|
+
<div id="doc"></div>
|
|
172
|
+
<script type="application/reactive-md+json" data-reactive-md-auto>
|
|
173
|
+
{"version":1,"target":"doc","source":"# Hello","state":{"format":"reactive-md-state/v1","syntax":"local-draft-2","sourceDigest":"fnv1a32:01209266","parameters":{}},"locale":"en","colorScheme":"auto","hydrate":false}
|
|
174
|
+
</script>
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Targets are resolved by `id`, never by an interpolated selector. Payload source is data, not executable markup. Raw `<script type="text/reactive-md">` text is deliberately not interpreted.
|
|
178
|
+
|
|
179
|
+
### `<reactive-md-doc>`
|
|
180
|
+
|
|
181
|
+
The IIFE registers a light-DOM custom element. Simple options are attributes; complex options are DOM properties.
|
|
182
|
+
|
|
183
|
+
```html
|
|
184
|
+
<reactive-md-doc src="./doc.reactive.md" locale="en" color-scheme="auto"></reactive-md-doc>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- Attributes: `src`, `source`, `locale`, `color-scheme`, `theme`.
|
|
188
|
+
- Properties: `source`, `state`, `urlPolicy`, `imageResolver`, `themes`.
|
|
189
|
+
- Events: `reactive-md-diagnostics` and `reactive-md-state-change`.
|
|
190
|
+
- `src` uses `fetch` with cancellation; an explicit source avoids network access.
|
|
191
|
+
- Disconnecting aborts fetches, removes subscriptions and the React root, and disposes the runtime. Two elements with the same source have independent state.
|
|
192
|
+
|
|
193
|
+
The element uses Light DOM so the existing document CSS, heading anchors and print styles apply. Shadow DOM is intentionally out of scope for this release.
|
|
194
|
+
|
|
195
|
+
## Security boundaries
|
|
196
|
+
|
|
197
|
+
- Core and React entry points keep React external. Only the standalone IIFE bundles React.
|
|
198
|
+
- The core entry remains React-free.
|
|
199
|
+
- State export and import use only public binding names and validated values.
|
|
200
|
+
- Exported HTML does not persist a reader's changes.
|
|
201
|
+
- The demo previews and exports images supplied through its document resource list. Missing references block HTML export until files are selected.
|
|
202
|
+
- The export format version is `reactive-md-html/v1`; inspect it in the generated meta tags.
|
|
203
|
+
|
|
204
|
+
## Bundle baseline
|
|
205
|
+
|
|
206
|
+
`npm run test:shareable` records the standalone artifact size and fails above these regression alarms:
|
|
207
|
+
|
|
208
|
+
| Artifact | Current baseline | Alarm |
|
|
209
|
+
| --- | --- | --- |
|
|
210
|
+
| `reactive-md-standalone.js` | ≈925 kB (≈276 kB gzip) | 1024 KiB / 320 KiB gzip |
|
|
211
|
+
| `reactive-md-standalone.css` | ≈81.2 kB (≈13.5 kB gzip) | 80 KiB / 20 KiB gzip |
|
|
212
|
+
|
|
213
|
+
The 0.5 baseline includes the syntax highlighter and common language grammars for offline use.
|
|
214
|
+
|
|
215
|
+
Sizes vary slightly with the minifier and React patch release. The alarm is intentionally well above normal noise while catching a React or renderer duplication regression. The full generated HTML also contains the source, notices and inline images, so it is larger than this baseline.
|
|
216
|
+
|
|
217
|
+
## Current scope
|
|
218
|
+
|
|
219
|
+
Included: CLI `build`/`lint`/`migrate`, interactive and static HTML, state files, local raster inlining, print output, standalone mount APIs, auto-mount, and the Web Component.
|
|
220
|
+
|
|
221
|
+
Not included in this release: `format`, UMD, remote image fetching, SVG inlining, framework plugins (Vite, Astro, Docusaurus, Next.js), Shadow DOM, and general SSR hydration guarantees outside the single-file export path.
|
|
222
|
+
|
|
223
|
+
Single-file static-first hydration is covered by the Playwright Chromium checks and local Edge checks. Firefox, Safari/WebKit and general server-side hydration remain unverified and are not claimed as supported.
|
|
@@ -1,87 +1,87 @@
|
|
|
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
|
+
# 复用计算与格式化数值
|
|
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)记载了此次扩展。
|