@yudaocode/univer-docs 1.6.215 → 1.6.216

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.
@@ -4,6 +4,15 @@ This document defines the canonical cross-language (`TypeScript` & `Java`) contr
4
4
 
5
5
  Golden cross-language test vectors conforming to this specification are published in [`fixtures/word-template-protocol-v1.json`](./fixtures/word-template-protocol-v1.json).
6
6
 
7
+ ### Protocol Version & Package Version Mapping
8
+
9
+ The golden fixture file retains `"version": "1.0"` for strict cross-language schema compatibility while tracking additive protocol revisions via `"protocolRevision"` and `"npmVersionMapping"`:
10
+
11
+ | `protocolRevision` | Fixture `"version"` | `@yudaocode/univer-docs` Range | Summary |
12
+ | :--- | :--- | :--- | :--- |
13
+ | `1.0` | `1.0` | `>=1.6.211` | Baseline `IDocumentData` layout invariants, inline tokens, `<w:sdt>` (`rangeType = 2`), and single-level `tableRowLoops` |
14
+ | `1.1` | `1.0` | `>=1.6.216` | Deterministic `""` for missing/null/undefined bound fields, explicit `UNSUPPORTED_ROW_LOOP_STRUCTURE` error on `vMerge` / nested-table loop rows, root scalar vs dotted path lookup rules, multi-loop tables, and header/footer `PAGE` / `NUMPAGES` evaluation |
15
+
7
16
  ---
8
17
 
9
18
  ## 1. `IDocumentData` Core Layout & Structural Invariants
@@ -53,7 +62,11 @@ Both TypeScript (`fillDatasetIntoDocSnapshot`) and Java template compilers MUST
53
62
 
54
63
  ## 2. Field Bindings: Inline Tokens & SDT Controls
55
64
 
56
- Data payloads are JSON objects (`Record<string, unknown>`). Nested property paths (`a.b.c` and bracket indices `a[0].b`) are resolved via `resolvePathValue(source, path)`, checking exact literal keys on `source` first before dot/index traversal.
65
+ Data payloads are JSON objects (`Record<string, unknown>`). Value lookup via `resolvePathValue(source, path)` follows strict deterministic precedence:
66
+
67
+ 1. **Unprefixed Root-Level Scalar (`${reportTitle}`, `#{remark}`)**: When `path` contains neither `.` nor `[...]`, resolves directly against the root payload `data[path]`.
68
+ 2. **Dotted / Indexed Nested Path (`${order.no}`, `${items[0].name}`)**: When `path` contains `.` or `[...]`, traverses nested object/array segments (`data.order.no`) first; if nested traversal yields `undefined`, falls back to an exact literal key lookup `data['order.no']` on `source` when present.
69
+ 3. **Missing / `null` / `undefined` Bound Values**: Any bound inline token (`${...}` / `#{...}`) whose resolved value is `undefined`, `null`, or missing in `data` resolves deterministically to empty string `""` (never leaving raw `${...}` or `#{...}` placeholder text in filled output).
57
70
 
58
71
  ### 2.1 Inline Value Tokens
59
72
 
@@ -64,16 +77,16 @@ Inline expressions are supported across `body`, `headers`, `footers`, and table
64
77
  - **Explicit list-item syntax** (inside detail table rows or paragraph loops): `${#listKey.field}`, `#{#listKey.field}`
65
78
  - **1-based row sequence token** (inside detail table rows or paragraph loops): `${listKey._index}`, `#{listKey._index}`, `${#listKey._index}`, `#{#listKey._index}`, and (when explicit `listKey` is bound on the row) `${_index}`, `#{_index}`
66
79
 
67
- Outside loop rows, scalar values (`string | number | boolean`) replace the token span while preserving surrounding run styles; unresolved scalar tokens outside explicit loop rows remain untouched.
80
+ Outside loop rows, scalar values (`string | number | boolean`) replace the token span while preserving surrounding run styles, and missing/`null`/`undefined` bound values resolve deterministically to `""`.
68
81
 
69
82
  ### 2.2 Structured Document Tag (`<w:sdt>`) Controls
70
83
 
71
- SDT controls live in `story.customRanges[]` with `rangeType = 7` (`CustomRangeType.SDT`):
84
+ SDT controls live in `story.customRanges[]` with `rangeType = 2` (`CustomRangeType.SDT`):
72
85
 
73
86
  ```json
74
87
  {
75
88
  "rangeId": "sdt_contract_no",
76
- "rangeType": 7,
89
+ "rangeType": 2,
77
90
  "startIndex": 5,
78
91
  "endIndex": 14,
79
92
  "wholeEntity": false,
@@ -106,6 +119,8 @@ SDT controls live in `story.customRanges[]` with `rangeType = 7` (`CustomRangeTy
106
119
 
107
120
  ## 3. Single-Level `tableRowLoops` Expansion Protocol
108
121
 
122
+ > **Single-Level Row Loops Only**: Nested row loops are **NOT** supported. `tableRowLoops` operates strictly on single-level detail rows (including multiple independent single-level loop sections within the same table). If a detail template row contains vertical cell merges (`rowSpan > 1` or `vMerge` continuation `rowSpan === 0`) or nested `<w:tbl>` tables inside its `startIndex..endIndex` span, template expansion throws `DocTemplateProtocolError` with `code: 'UNSUPPORTED_ROW_LOOP_STRUCTURE'`.
123
+
109
124
  ### 3.1 Configuration Schema
110
125
 
111
126
  Row loop bindings are stored in `YUDAO_DOCS_TEMPLATE_RESOURCE` under `metadata.tableRowLoops`, keyed by `"${tableId}:${rowIndex}"`:
@@ -138,8 +153,9 @@ export interface IDocTableRowLoopConfig {
138
153
  - Otherwise auto-detect from `${#listKey.field}` / `#{#listKey.field}` tokens, dotted `${set.field}` / `#{set.field}` tokens where `Array.isArray(resolvePathValue(data, set))`, or SDT `bindingPath` / `tag` prefixes inside the row where `Array.isArray(resolvePathValue(data, candidatePrefix))` is `true`.
139
154
  - Resolve list items via `rawItems = resolvePathValue(data, detectedListKey) ?? data[detectedListKey]`. Any `null`, `undefined`, or non-array value is treated as `[]`.
140
155
  3. **`repeatHeaderRow` Semantics**:
141
- - When `explicitCfg?.repeatHeaderRow === false`: set `tableSource[tableId].tableRows[h].repeatHeaderRow = 0` (`BooleanNumber.FALSE`) for `0 <= h < Math.max(1, rIdx)`, and delete `metadata.tableRepeatHeaders[tableId]`. Exported `.docx` MUST NOT emit `<w:tblHeader/>` on that table.
142
- - When `repeatHeader` is `true` (`explicitCfg?.repeatHeaderRow ?? true`) and `rIdx > 0`: set `tableSource[tableId].tableRows[h].repeatHeaderRow = 1` (`BooleanNumber.TRUE`) for all header rows `0 <= h < rIdx` (and `isFirstRow = 1` on row `0`).
156
+ - Repeating table headers (`<w:tblHeader/>`) apply strictly to contiguous leading rows `0 <= h < firstLoopRowIndex` preceding the earliest detail loop row in that table.
157
+ - When `explicitCfg?.repeatHeaderRow === false` on the leading loop row: set `tableSource[tableId].tableRows[h].repeatHeaderRow = 0` (`BooleanNumber.FALSE`) for `0 <= h < Math.max(1, firstLoopRowIndex)`, and delete `metadata.tableRepeatHeaders[tableId]`. Exported `.docx` MUST NOT emit `<w:tblHeader/>` on that table.
158
+ - When `repeatHeader` is `true` (`explicitCfg?.repeatHeaderRow ?? true`) and `firstLoopRowIndex > 0`: set `tableSource[tableId].tableRows[h].repeatHeaderRow = 1` (`BooleanNumber.TRUE`) for all leading header rows `0 <= h < firstLoopRowIndex` (and `isFirstRow = 1` on row `0`). Sub-header rows situated between two separate loop sections in the same table keep `repeatHeaderRow = 0`.
143
159
  4. **Empty List Strategies (`items.length === 0`)**:
144
160
  - `'remove'`: If `remainingRowCount > 1`, splice out the row from `body.dataStream` and `tableSource[tableId].tableRows`, shift all downstream offsets by `-removedLen`, and decrement `remainingRowCount`. **Safety fallback**: if `remainingRowCount <= 1` (e.g., a 1-row table or all rows in the table are empty `'remove'` loop rows), fall back to `'keep-blank'` (1 blank row) so `<w:tbl>` never ends up with 0 rows.
145
161
  - `'placeholder'`: Render 1 row (`itemObj = null`) where cell `0` contains `explicitCfg.emptyPlaceholderText || "暂无明细数据"` and cells `1 .. colCount - 1` contain `""`.
@@ -205,8 +221,9 @@ When exporting an **unfilled template** `IDocumentData` to `.docx` via `exportDo
205
221
 
206
222
  ### 5.4 Dynamic Document Fields (`PAGE` / `NUMPAGES`)
207
223
 
208
- - Complex `<w:fldChar w:fldCharType="begin\|separate\|end"/>` + `<w:instrText>` and `<w:fldSimple>` fields in body, table cells, headers, and footers are parsed into `yudaoBlockKind: "docField"` custom ranges (`fieldInstr`, `fieldSimple`), preserving cached display runs for Univer canvas preview and re-emitting live OOXML fields plus `<w:updateFields w:val="true"/>` in `word/settings.xml` on `.docx` export.
209
- - Browser `decodeDocx` / `parseDocxBytesWithReport` emits an explicit compatibility warning (`code: "UNSUPPORTED_FIELD"`) whenever dynamic `PAGE` / `NUMPAGES` fields are detected so hosts and users know canvas preview displays cached static text rather than live page numbers.
224
+ - Complex `<w:fldChar w:fldCharType="begin\|separate\|end"/>` + `<w:instrText>` and `<w:fldSimple>` fields in body, table cells, headers, and footers are parsed into `yudaoBlockKind: "docField"` custom ranges (`fieldInstr`, `fieldSimple`), preserving cached display runs for Univer canvas preview, surviving `fillDatasetIntoDocSnapshot` intact, and re-emitting live OOXML fields plus `<w:updateFields w:val="true"/>` in `word/settings.xml` on `.docx` export.
225
+ - During vector PDF compilation (`exportDocToPdfBytes`), header and footer stories (`snapshot.headers`, `snapshot.footers`) evaluate `PAGE` and `NUMPAGES` dynamic field ranges per page (`1 .. totalPages`, honoring `startPageNo` and numeral formatting) and suppress duplicate synthetic footers whenever an explicit document footer story is present (unless `options.pageNumberFormat` is explicitly specified).
226
+ - Browser `decodeDocx` / `parseDocxBytesWithReport` emits an explicit compatibility warning (`code: "UNSUPPORTED_FIELD"`) whenever dynamic `PAGE` / `NUMPAGES` fields are detected so hosts and users know Univer canvas preview displays cached static text rather than live page numbers.
210
227
 
211
228
  ### 5.5 Table Cell Margins (`w:tcMar` / `w:tblCellMar`), Table `borderDefaults`, SDT Shading & DEFLATE Packaging
212
229