@portone/docx-editor 0.6.2 → 0.6.3

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/headersFooters.js +4 -14
  4. package/dist/docx/identities.d.ts +8 -2
  5. package/dist/docx/identities.js +31 -11
  6. package/dist/docx/importPolicy.d.ts +2 -2
  7. package/dist/docx/importPolicy.js +12 -5
  8. package/dist/docx/importPreserved.d.ts +3 -3
  9. package/dist/docx/importSdtBlock.d.ts +24 -0
  10. package/dist/docx/importSdtBlock.js +42 -0
  11. package/dist/docx/importTable.js +35 -24
  12. package/dist/docx/notes/numbering.js +4 -4
  13. package/dist/docx/notes/references.d.ts +0 -2
  14. package/dist/docx/notes/references.js +2 -7
  15. package/dist/docx/sdt.d.ts +52 -9
  16. package/dist/docx/sdt.js +108 -26
  17. package/dist/docx/sections.d.ts +13 -13
  18. package/dist/docx/sections.js +34 -20
  19. package/dist/docx/serializeBlock.js +9 -1
  20. package/dist/docx/serializeSdtBlock.d.ts +24 -0
  21. package/dist/docx/serializeSdtBlock.js +15 -0
  22. package/dist/docx/serializeTable.d.ts +8 -2
  23. package/dist/docx/serializeTable.js +14 -20
  24. package/dist/docx/story.d.ts +10 -4
  25. package/dist/docx/story.js +25 -20
  26. package/dist/docx/storyBlocks.d.ts +32 -0
  27. package/dist/docx/storyBlocks.js +65 -0
  28. package/dist/docx/wrappers.d.ts +10 -7
  29. package/dist/docx/wrappers.js +15 -27
  30. package/dist/editor/blockControlEdits.d.ts +15 -0
  31. package/dist/editor/blockControlEdits.js +79 -0
  32. package/dist/editor/clipboard/normalizers.js +1 -1
  33. package/dist/editor/commands/lockCommands.d.ts +5 -1
  34. package/dist/editor/commands/lockCommands.js +27 -19
  35. package/dist/editor/createEditor.js +4 -0
  36. package/dist/editor/notes/noteSurface.js +4 -3
  37. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  38. package/dist/editor/plugins/controlLifecycle.js +130 -0
  39. package/dist/editor/plugins/keymap.js +23 -5
  40. package/dist/editor/plugins/rowResize.js +2 -4
  41. package/dist/editor/stories/storyState.d.ts +2 -0
  42. package/dist/editor/stories/storyState.js +2 -0
  43. package/dist/page/blockKinds.d.ts +24 -2
  44. package/dist/page/blockKinds.js +6 -1
  45. package/dist/page/kinds/index.d.ts +5 -0
  46. package/dist/page/kinds/index.js +2 -0
  47. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  48. package/dist/page/kinds/sdtBlockKind.js +143 -0
  49. package/dist/page/measureBlocks.js +4 -2
  50. package/dist/page/pageDecorations.js +3 -2
  51. package/dist/page/pageLayout.d.ts +16 -15
  52. package/dist/page/pageLayout.js +134 -67
  53. package/dist/page/usePageLayout.d.ts +2 -2
  54. package/dist/page/usePageLayout.js +5 -5
  55. package/dist/schema/attrRoles.js +45 -1
  56. package/dist/schema/clipboard.js +9 -0
  57. package/dist/schema/controlAttrs.d.ts +105 -0
  58. package/dist/schema/controlAttrs.js +129 -0
  59. package/dist/schema/controlEdges.d.ts +22 -0
  60. package/dist/schema/controlEdges.js +50 -0
  61. package/dist/schema/docxSchema.d.ts +1 -1
  62. package/dist/schema/docxSchema.js +180 -37
  63. package/dist/schema/editGuard.d.ts +1 -1
  64. package/dist/schema/guards.js +2 -0
  65. package/dist/schema/locks.d.ts +22 -6
  66. package/dist/schema/locks.js +88 -47
  67. package/dist/styles/classNames.d.ts +8 -0
  68. package/dist/styles/classNames.js +8 -0
  69. package/dist/styles.css +31 -2
  70. package/dist/table/merge.js +11 -3
  71. package/dist/table/rowResize.d.ts +9 -0
  72. package/dist/table/rowResize.js +9 -5
  73. package/dist/ui/TextMenu.js +2 -0
  74. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,67 @@
1
1
  # @portone/docx-editor
2
2
 
3
+ ## 0.6.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#158](https://github.com/portone-io/docx-editor/pull/158) [`c946c8c`](https://github.com/portone-io/docx-editor/commit/c946c8cf82ccd4765a3897e676b64cd5ef4fe5fa) Thanks [@Deea222](https://github.com/Deea222)! - Open a block-level content control as the blocks it holds.
8
+
9
+ A `w:sdt` standing under the body or inside a table cell used to open as a placeholder: a box saying the content was preserved, with the paragraphs and tables inside it neither readable on the page nor editable. It now opens as the content it wraps, so the text of a clause held in a rich text control reads and edits like any other text, and a control holding a table holds a table. The control's own opening XML - its id, its lock, its type, its data binding - is kept whole and written back untouched, and a control nobody edited still goes out as the bytes it arrived as.
10
+
11
+ Controls nest as the file nested them, so a `w:group` around a rich text control comes back that way round, and a control inside a table cell keeps the formatting the cell dressed it with. A control that is duplicated gets an id of its own on the way out, and its data binding stays with the first copy, so two controls never edit each other.
12
+
13
+ Two kinds are still preserved whole, because opening them would let an edit write a file Word reads differently: a `w:text` control, whose content may be no more than one paragraph, and a `w:picture` control, whose content may be no more than one picture.
14
+
15
+ Locks are read off a block control but not yet enforced against an edit, and a control does not yet break across a page.
16
+
17
+ - [#160](https://github.com/portone-io/docx-editor/pull/160) [`e62e154`](https://github.com/portone-io/docx-editor/commit/e62e154e616400b702e4516544bb2176e22f3036) Thanks [@Deea222](https://github.com/Deea222)! - Hold the edge of a block-level content control, and act on the two properties that say a control does not outlive an edit.
18
+
19
+ Backspace at the start of a control's first block and Delete at the end of its last one no longer carry a paragraph out of the control or pull the block after it in; they do nothing, and the caret stays where it was. A selection running from outside a control into it, or the other way round, is refused rather than closed by moving text across the boundary. A control left holding a single empty paragraph is removed whole instead, unless its lock says it may not be deleted.
20
+
21
+ A control carrying `w:temporary` now loses its wrapper on the first edit inside it, as the specification requires, and one carrying `w:showingPlcHdr` stops claiming that what it holds is placeholder text, so a document exported after typing into it no longer shows that text as a placeholder in Word. Both go into the history with the edit that caused them, so one undo takes them back together, and a comment left on a document changes neither.
22
+
23
+ Placeholder text itself is not selected or replaced when a control stops claiming it holds a placeholder, so it is edited and deleted like any other text.
24
+
25
+ - [#164](https://github.com/portone-io/docx-editor/pull/164) [`3054980`](https://github.com/portone-io/docx-editor/commit/30549809ae6675f22523a0b83b36e4e40d8422ff) Thanks [@Deea222](https://github.com/Deea222)! - Let a kept run inside a block-level content control go when no page can hold it.
26
+
27
+ Paragraphs inside a control that ask to be kept with the next one (`w:keepNext`) closed the boundaries between them for good, so a kept run longer than a page had nowhere to part and the control ran off the sheet. Such a boundary is now closed only while a page could hold what is kept together, and let go whole where none can, which is how a run of kept paragraphs outside a control has always been treated.
28
+
29
+ - [#159](https://github.com/portone-io/docx-editor/pull/159) [`6a41b2b`](https://github.com/portone-io/docx-editor/commit/6a41b2ba2d1aa954946f6ca14bf96b8fe0008df8) Thanks [@Deea222](https://github.com/Deea222)! - Enforce the lock a block-level content control carries, and refuse edits inside a group control.
30
+
31
+ A `w:sdt` standing under the body or inside a table cell was drawn as locked when its `w:lock` said so, and was then editable and deletable anyway. Its lock is now honored the way a wrapped cell's already was: where the control locks its contents, typing, formatting and deleting inside it are refused, a partial deletion that crosses its edge is refused with them, and so is an edit made in a table the control holds. A control locked against deletion is kept even when the selection covers it from end to end, while a `contentLocked` control may still be deleted whole and no less than whole. Unlocking from the editor now reaches a block control too, leaving the control itself standing under the id the file gave it.
32
+
33
+ A control that names `w:group` refuses edits to its contents even though it states no lock, which is what the specification asks of a group (§17.5.2.17). Its own paragraphs are refused, while a control standing inside it - a text control, a cell, or another block-level control - stays editable, and the group itself may still be deleted whole. There is no lock on such a control to lift, so the editor offers none, and `selectionLock` answers the new `shut` for a selection standing there rather than `none`.
34
+
35
+ A lock reaches every control standing inside the one that carries it, so text inside an open control held by a locked cell, by a locked block control or by a locked text control is refused as well.
36
+
37
+ - [#162](https://github.com/portone-io/docx-editor/pull/162) [`c3e5b75`](https://github.com/portone-io/docx-editor/commit/c3e5b75e301454b6950e9c1f78f0562aac5cf14b) Thanks [@Deea222](https://github.com/Deea222)! - Break a block-level content control across pages, read the sections written inside one, and draw the header text it holds.
38
+
39
+ A `w:sdt` opened as a container was measured as one unbreakable block, so a control longer than a page ran off the sheet, a page break written inside it only started a page after the whole control, and a table inside one lost the row-by-row pagination it has everywhere else. A control now pages as the blocks it holds do: a page opens between any two of them, a page break cuts where it stands, a held table keeps its rows and repeated headers, and a control inside a control is parted the same way one level down. A paragraph inside a control that asks for a page of its own now gets one at every level of nesting, a keep (`w:keepNext`) holds between two blocks a control holds as well as between the control and the block after it, and a held table still asks the page it starts on for its repeated headers and one body row. The control is drawn as one box around everything it holds, so a piece of it on a continued page still reads as standing inside the control.
40
+
41
+ A paragraph inside a control may end a section, since a control holds what the body holds (§17.5.2.34). Such a paragraph's `w:sectPr` was written back untouched but drawn as nothing: the paper, the margins, the headers and the footers of everything up to it were read off the next section instead. The break is now read, so the section it closes gets its own paper, its own header and footer stories, and note numbering restarting there where the section says so. A `w:sectPr` written inside a table cell is preserved rather than read, which is what Word makes of one.
42
+
43
+ Text modelled inside a control in a header or footer part now appears in the on-page header and footer preview, along with the page numbers its fields print, and the preview follows the alignment of the first paragraph even where a control stands around it.
44
+
45
+ - [#168](https://github.com/portone-io/docx-editor/pull/168) [`7c654c9`](https://github.com/portone-io/docx-editor/commit/7c654c98aff8e33580fd009005fb8213832ed885) Thanks [@Deea222](https://github.com/Deea222)! - End a section at the paragraph inside a content control that carries its break, and read every break a control holds.
46
+
47
+ A section break written on a paragraph inside a block-level `w:sdt` now ends its section at that paragraph, as it does for a paragraph outside a control, and every break a control holds is read.
48
+ What the control holds after a break starts the next section's page, on that section's paper and under its header and footer, as Word parts the control across the next sheets, while the control stays one control in the document.
49
+ Footnote and endnote numbering that restarts per section, and the width a table is fitted to, follow the section the paragraph is in.
50
+
51
+ - [#167](https://github.com/portone-io/docx-editor/pull/167) [`8442ce6`](https://github.com/portone-io/docx-editor/commit/8442ce6956fdce772d38b60191c5b7dec3e8f48c) Thanks [@Deea222](https://github.com/Deea222)! - Draw nothing for a content control with nothing inside it.
52
+
53
+ A `w:sdt` whose `w:sdtContent` was empty, or which wrote no content element at all, used to open as a grey box saying the content was preserved. A server that answers a failed condition by leaving a tagged control empty therefore drew a box for the clause that is not there, and the reader could neither open it nor take it away. Such a control now opens as a node of its own that holds nothing and takes no room on the page, so the clause reads as the nothing the file says it is.
54
+
55
+ Everything the control states about itself is kept: its tag, its id, its lock and its data binding all ride back out untouched, and a control nobody edited goes out as the bytes it arrived as. Backspace and Delete beside it pass over it, so it goes only when it is selected whole, and then only if its lock does not say otherwise; a copy of one gets an id of its own the way every other control does.
56
+
57
+ - [#161](https://github.com/portone-io/docx-editor/pull/161) [`1865d19`](https://github.com/portone-io/docx-editor/commit/1865d19f5c881d1a195b5603b8172bb8bd7391a7) Thanks [@Deea222](https://github.com/Deea222)! - Open a table whose row stands inside a content control, instead of standing the whole table down.
58
+
59
+ A `w:sdt` around a `w:tr` used to be markup nothing read, so one such control cost every row, cell, merge and paragraph of the table it stood in: the table opened as an uneditable placeholder. The control is now read onto the row it wraps, exactly as a control around a cell has always been read onto the cell, so the table is editable and the wrapper goes back around the same row on export. A table nobody edited still goes out byte for byte, and an edit in another row rewrites the table with the control back where it stood.
60
+
61
+ The lock such a control carries is honored on the row: typing anywhere in a locked row is refused, so is a paragraph or formatting command inside it and a change to its height, while a row beside it is edited as ever. A row deletion is the control being taken away whole, which a lock against deletion refuses; a column deletion reaches into what the control holds, which a lock over its contents refuses. A `w:group` shuts a row's contents without a lock, lifting a lock reaches a row, and a row inserted beside a wrapped one carries no control, so no second control claims the first one's `w:id`.
62
+
63
+ A control the row cannot carry back out still keeps the table whole: one holding more than the single row the specification describes, one holding another control instead of a row, and one around a row that only continues a vertical merge, since such a row is rebuilt on export rather than written from the document.
64
+
3
65
  ## 0.6.2
4
66
 
5
67
  ### Patch Changes
@@ -13,7 +13,7 @@ import { exportDocx } from "./docx/exportDocx.js";
13
13
  import { variantsFor } from "./docx/headersFooters.js";
14
14
  import { importDocx } from "./docx/importDocx.js";
15
15
  import { exportProblems } from "./docx/invariants.js";
16
- import { sectionIn, sectionsOf } from "./docx/sections.js";
16
+ import { sectionsOf } from "./docx/sections.js";
17
17
  import { activeLinkSpan } from "./editor/commands/linkCommands.js";
18
18
  import { createEditorView, editorStateForSession } from "./editor/createEditor.js";
19
19
  import { sectionGeometryAt } from "./editor/documentStyles.js";
@@ -320,7 +320,7 @@ function DocxEditorSurface({
320
320
  (key) => storyNodeOf(doc, key)
321
321
  )
322
322
  );
323
- return (face) => shown[sectionIn(sections, doc, face.pos).index] ?? null;
323
+ return (face) => shown[face.section] ?? null;
324
324
  }, [doc, session, sections]);
325
325
  if (opened?.status === "rejected") {
326
326
  return renderImportError ? renderImportError(opened.error) : /* @__PURE__ */ jsx(ImportRejection, { error: opened.error });
@@ -6,7 +6,6 @@ import {
6
6
  } from "../ooxml/partSplice.js";
7
7
  import { isOnElement } from "../ooxml/units.js";
8
8
  import { decodeUtf8, encodeUtf8, parseXml, R_NS, W_NS } from "../ooxml/xml.js";
9
- import { docxSchema } from "../schema/index.js";
10
9
  import {
11
10
  HEADER_FOOTER_KINDS,
12
11
  storyKey
@@ -24,6 +23,7 @@ import {
24
23
  readStory,
25
24
  storyLeafText
26
25
  } from "./story.js";
26
+ import { storyParagraphs } from "./storyBlocks.js";
27
27
  import { storyChangesOf } from "./storyParts.js";
28
28
  var EMPTY_VARIANTS = {
29
29
  default: null,
@@ -86,12 +86,8 @@ function readHeaderFooterStories(parts, mainPartPath, referenced, depsFor) {
86
86
  };
87
87
  }
88
88
  function firstAlign(story) {
89
- for (let at = 0; at < story.childCount; at += 1) {
90
- const block = story.child(at);
91
- if (block.type !== docxSchema.nodes.paragraph) continue;
92
- return toParagraphFormat(block.attrs.format)?.align ?? null;
93
- }
94
- return null;
89
+ const first = storyParagraphs(story).at(0);
90
+ return first === void 0 ? null : toParagraphFormat(first.attrs.format)?.align ?? null;
95
91
  }
96
92
  function variantContent(ids, kind, refs, storyOf) {
97
93
  const variants = { ...EMPTY_VARIANTS };
@@ -164,13 +160,7 @@ function paragraphText(paragraph, page, totalPages) {
164
160
  return pieces.join("");
165
161
  }
166
162
  function headerFooterText(story, page, totalPages) {
167
- const lines = [];
168
- story.forEach((block) => {
169
- if (block.type === docxSchema.nodes.paragraph) {
170
- lines.push(paragraphText(block, page, totalPages));
171
- }
172
- });
173
- return lines.join("\n");
163
+ return storyParagraphs(story).map((paragraph) => paragraphText(paragraph, page, totalPages)).join("\n");
174
164
  }
175
165
  var HEADER_MARKUP = { namespaces: { w: NAMESPACES.w } };
176
166
  function writtenPart(imported, current, session, notes) {
@@ -48,8 +48,14 @@ export declare const paragraphIdRule: IdentityRule;
48
48
  /**
49
49
  * A content control cannot cross a paragraph, so splitting a paragraph in the middle of one leaves
50
50
  * that same control standing in two, and dropping unmarked text into the middle of one breaks it
51
- * in two within the paragraph. Each piece after the first opens as a copy with a `w:id` of its
52
- * own (see `docx/sdt` for what a copy must not carry along).
51
+ * in two within the paragraph. A control an edit left standing twice one level up - a copy of it
52
+ * pasted, or the container duplicated - is the same case, and the walker reaches a control nested
53
+ * inside another. Each piece after the first opens as a copy with a `w:id` of its own (see
54
+ * `docx/sdt` for what a copy must not carry along).
55
+ *
56
+ * Every carrier `schema/controlAttrs` knows is settled here, the block container and a wrapped
57
+ * cell or row alike, each read under its own attribute names: a second control claiming the first
58
+ * one's `w:id` is not a shape §17.5.2.18 allows, whichever of them carries it.
53
59
  */
54
60
  export declare const controlRule: IdentityRule;
55
61
  /**
@@ -4,9 +4,13 @@ import { DocxExportError } from "../ooxml/errors.js";
4
4
  import { qualify } from "../ooxml/names.js";
5
5
  import { parseAttrs } from "../ooxml/tagScan.js";
6
6
  import { docxSchema } from "../schema/index.js";
7
+ import {
8
+ controlAttrsOf,
9
+ OWN_CONTROL_ATTRS
10
+ } from "../schema/controlAttrs.js";
7
11
  import { wrappersOf } from "../schema/wrappers.js";
8
12
  import { withoutParagraphIds } from "./cloning.js";
9
- import { wrapperKindOf } from "./wrappers.js";
13
+ import { copiedControlPrefix, newControlId } from "./sdt.js";
10
14
  var sourceBlockRule = {
11
15
  name: "sourceBlock",
12
16
  visit(block, written) {
@@ -55,19 +59,25 @@ var paragraphIdRule = {
55
59
  function sdtMarksOf(node) {
56
60
  return wrappersOf(node, docxSchema.marks.sdt);
57
61
  }
58
- function controlName(mark) {
59
- const key = mark.attrs.key;
60
- const prefix = mark.attrs.sdtPrefix;
62
+ function controlName(attrs, names) {
63
+ const key = attrs.key;
64
+ const prefix = attrs[names.prefix];
61
65
  return `${typeof key === "number" ? key : 0} ${typeof prefix === "string" ? prefix : ""}`;
62
66
  }
63
- function copiedMark(mark) {
64
- return wrapperKindOf(mark).copy?.(mark) ?? mark;
67
+ function claimedPrefix(attrs, names, written) {
68
+ const name = controlName(attrs, names);
69
+ const claimed = written.has(name);
70
+ written.add(name);
71
+ const prefix = attrs[names.prefix];
72
+ if (!claimed || typeof prefix !== "string") return null;
73
+ return copiedControlPrefix(prefix, newControlId());
65
74
  }
66
75
  function claim(mark, written) {
67
- const name = controlName(mark);
68
- const copy = written.has(name) ? copiedMark(mark) : null;
69
- written.add(name);
70
- return { mark, copy };
76
+ const prefix = claimedPrefix(mark.attrs, OWN_CONTROL_ATTRS, written);
77
+ return {
78
+ mark,
79
+ copy: prefix === null ? null : mark.type.create({ ...mark.attrs, sdtPrefix: prefix })
80
+ };
71
81
  }
72
82
  function rewriteParagraph(paragraph, written) {
73
83
  const inline = [];
@@ -91,7 +101,17 @@ function rewriteParagraph(paragraph, written) {
91
101
  var controlRule = {
92
102
  name: "control",
93
103
  visit(block, written) {
94
- return block.type === docxSchema.nodes.paragraph ? rewriteParagraph(block, written) : block;
104
+ if (block.type === docxSchema.nodes.paragraph) {
105
+ return rewriteParagraph(block, written);
106
+ }
107
+ const names = controlAttrsOf(block);
108
+ if (!names) return block;
109
+ const prefix = claimedPrefix(block.attrs, names, written);
110
+ return prefix === null ? block : block.type.create(
111
+ { ...block.attrs, [names.prefix]: prefix },
112
+ block.content,
113
+ block.marks
114
+ );
95
115
  }
96
116
  };
97
117
  var IDENTITY_RULES = [
@@ -14,7 +14,7 @@
14
14
  *
15
15
  * A level is a content model of `wml.xsd` rather than an element name, because the same name means
16
16
  * different things in different places: `sdt` is a control the paragraph reader unwraps, a cell
17
- * wrapper under `tr`, a block placeholder under the body, and a row wrapper nothing reads. Keys are
17
+ * wrapper under `tr`, a row wrapper under `tbl`, and a block container under the body. Keys are
18
18
  * Clark names (`{namespace}localName`) so that `m:oMath` and `w:sdt` sit in one map.
19
19
  */
20
20
  import type { PreservedDisplay } from "../schema";
@@ -51,7 +51,7 @@ export type DemotionPolicy = {
51
51
  readonly tier: "demote";
52
52
  };
53
53
  /** The levels a reader walks. Each is a content model of its own in `wml.xsd` */
54
- export type ContentLevel = "body" | "tbl" | "tr" | "tc" | "p" | "wrapper" | "r";
54
+ export type ContentLevel = "body" | "sdtContent" | "tbl" | "tr" | "tc" | "p" | "wrapper" | "r";
55
55
  export declare function clarkName(el: Element): string;
56
56
  export declare const WML_POLICY: Readonly<Record<ContentLevel, ReadonlyMap<string, ElementPolicy>>>;
57
57
  /** The levels that have a node of their own to keep a stranger in */
@@ -110,24 +110,29 @@ var WRAPPER_LEVEL = new Map([
110
110
  ...MATH.map((name) => [name, INLINE_CHIP])
111
111
  ]);
112
112
  var BLOCK_CHILDREN = [
113
- [["p", "tbl"], MODEL],
113
+ [["p", "tbl", "sdt"], MODEL],
114
114
  [RANGE_MARKERS, HIDDEN_MARKER],
115
115
  [COMMENT_RANGE_MARKERS, HIDDEN_MARKER],
116
116
  [PERMISSION_MARKERS, HIDDEN_MARKER],
117
117
  [["proofErr"], IGNORABLE],
118
118
  [REVISION_CONTAINERS, BLOCK_CHIP],
119
- [["sdt", "customXml", "altChunk"], BLOCK_CHIP]
119
+ [["customXml"], BLOCK_CHIP]
120
120
  ];
121
+ var BLOCK_LEVEL_ELTS = [...BLOCK_CHILDREN, [["altChunk"], BLOCK_CHIP]];
121
122
  var BODY_LEVEL = new Map([
122
- ...rules([...BLOCK_CHILDREN, [["sectPr"], BLOCK_CHIP]]),
123
+ ...rules([...BLOCK_LEVEL_ELTS, [["sectPr"], BLOCK_CHIP]]),
123
124
  ...MATH.map((name) => [name, BLOCK_CHIP])
124
125
  ]);
125
126
  var CELL_LEVEL = new Map([
126
- ...rules([...BLOCK_CHILDREN, [["tcPr"], MODEL]]),
127
+ ...rules([...BLOCK_LEVEL_ELTS, [["tcPr"], MODEL]]),
128
+ ...MATH.map((name) => [name, BLOCK_CHIP])
129
+ ]);
130
+ var SDT_CONTENT_LEVEL = new Map([
131
+ ...rules(BLOCK_CHILDREN),
127
132
  ...MATH.map((name) => [name, BLOCK_CHIP])
128
133
  ]);
129
134
  var TABLE_LEVEL = rules([
130
- [["tblPr", "tblGrid", "tr"], MODEL],
135
+ [["tblPr", "tblGrid", "tr", "sdt"], MODEL],
131
136
  [RANGE_MARKERS, HIDDEN_MARKER],
132
137
  [COMMENT_RANGE_MARKERS, HIDDEN_MARKER],
133
138
  [PERMISSION_MARKERS, HIDDEN_MARKER],
@@ -145,6 +150,7 @@ var WML_POLICY = {
145
150
  p: PARAGRAPH_LEVEL,
146
151
  wrapper: WRAPPER_LEVEL,
147
152
  body: BODY_LEVEL,
153
+ sdtContent: SDT_CONTENT_LEVEL,
148
154
  tc: CELL_LEVEL,
149
155
  tbl: TABLE_LEVEL,
150
156
  tr: ROW_LEVEL
@@ -154,6 +160,7 @@ var LEVEL_PRESERVATION = {
154
160
  p: INLINE_CHIP,
155
161
  wrapper: INLINE_CHIP,
156
162
  body: BLOCK_CHIP,
163
+ sdtContent: BLOCK_CHIP,
157
164
  tc: BLOCK_CHIP
158
165
  };
159
166
  function defaultPreservation(level) {
@@ -24,7 +24,7 @@ export declare function buildPreservedInline(el: Element, wrappers: readonly Mar
24
24
  * One block kept as it came, standing where it stood.
25
25
  *
26
26
  * A body block is one of the fragments the session holds, so it names that fragment and the bytes
27
- * it arrived as go back out untouched. A block inside a cell was never a fragment of its own, so
28
- * it carries its XML along with it.
27
+ * it arrived as go back out untouched. A block inside a cell or inside a content control was never
28
+ * a fragment of its own, so it carries its XML along with it.
29
29
  */
30
- export declare function buildPreservedBlock(el: Element, srcId: string | null, level: "body" | "tc"): PMNode;
30
+ export declare function buildPreservedBlock(el: Element, srcId: string | null, level: "body" | "tc" | "sdtContent"): PMNode;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Reading a block-level content control (`w:sdt` under the body, a cell, or another control) as a
3
+ * container of the blocks it holds, or as the node a control holding nothing stands as.
4
+ *
5
+ * The blocks inside a control are the ones the level around it takes, so the caller hands over the
6
+ * reader for its own level rather than this module reaching for one: a control may hold a table
7
+ * while a table may hold a control, and neither may import the other.
8
+ *
9
+ * null leaves the control to whatever held it, which preserves it whole as it was before.
10
+ */
11
+ import type { Node as PMNode } from "prosemirror-model";
12
+ /** How the level around a control reads one block of it */
13
+ export type BlockReader = (el: Element) => PMNode;
14
+ /**
15
+ * One `w:sdt` as a container node, or null for a control this editor keeps whole.
16
+ *
17
+ * `srcId` names the fragment of the session the control was sliced as; a control inside a cell or
18
+ * inside another control was never a fragment of its own and is handed null, as a nested table is.
19
+ *
20
+ * A control holding nothing is read as the atom that draws nothing: an empty `w:sdtContent` and a
21
+ * control that writes no content element at all say the same thing, since §17.5.2.34 makes that
22
+ * element a cache of what stood inside rather than the statement itself.
23
+ */
24
+ export declare function buildSdtBlock(el: Element, srcId: string | null, readBlock: BlockReader): PMNode | null;
@@ -0,0 +1,42 @@
1
+ // src/docx/importSdtBlock.ts
2
+ import { elementChildren } from "../ooxml/xml.js";
3
+ import { docxSchema } from "../schema/index.js";
4
+ import {
5
+ controlAttrs,
6
+ OWN_CONTROL_ATTRS
7
+ } from "../schema/controlAttrs.js";
8
+ import {
9
+ controlFactsFrom,
10
+ modelsBlockContent,
11
+ readEmptySdt,
12
+ readSdtWrapper
13
+ } from "./sdt.js";
14
+ import { nextKey } from "./wrappers.js";
15
+ function controlNodeAttrs(el, srcId, facts) {
16
+ return {
17
+ srcId,
18
+ key: nextKey(docxSchema.marks.sdt.name, el),
19
+ ...controlAttrs(OWN_CONTROL_ATTRS, facts)
20
+ };
21
+ }
22
+ function buildSdtBlock(el, srcId, readBlock) {
23
+ if (!modelsBlockContent(el)) return null;
24
+ const wrapper = readSdtWrapper(el);
25
+ if (!wrapper) {
26
+ const facts = readEmptySdt(el);
27
+ return facts === null ? null : docxSchema.nodes.sdtEmpty.create(controlNodeAttrs(el, srcId, facts));
28
+ }
29
+ const children = elementChildren(wrapper.content);
30
+ if (children.length === 0) {
31
+ return docxSchema.nodes.sdtEmpty.create(
32
+ controlNodeAttrs(el, srcId, controlFactsFrom(wrapper))
33
+ );
34
+ }
35
+ return docxSchema.nodes.sdtBlock.create(
36
+ controlNodeAttrs(el, srcId, controlFactsFrom(wrapper)),
37
+ children.map(readBlock)
38
+ );
39
+ }
40
+ export {
41
+ buildSdtBlock
42
+ };
@@ -7,6 +7,11 @@ import {
7
7
  serializeXml
8
8
  } from "../ooxml/xml.js";
9
9
  import { docxSchema } from "../schema/index.js";
10
+ import {
11
+ controlAttrs,
12
+ NO_CONTROL,
13
+ WRAPPED_CONTROL_ATTRS
14
+ } from "../schema/controlAttrs.js";
10
15
  import {
11
16
  layerTableFormat,
12
17
  NO_FORMATTING,
@@ -20,7 +25,8 @@ import {
20
25
  } from "./importParagraph.js";
21
26
  import { policyFor } from "./importPolicy.js";
22
27
  import { buildPreservedBlock } from "./importPreserved.js";
23
- import { readSdtWrapper } from "./sdt.js";
28
+ import { buildSdtBlock } from "./importSdtBlock.js";
29
+ import { controlFactsFrom, readSdtWrapper } from "./sdt.js";
24
30
  import {
25
31
  cellConditionsOf,
26
32
  cellDefaultsFor,
@@ -58,23 +64,20 @@ function readVerticalMerge(tcPr) {
58
64
  if (!vMerge) return null;
59
65
  return wAttr(vMerge, "val") === "restart" ? "restart" : "continue";
60
66
  }
61
- function readSdtCell(el) {
67
+ function readSdtSingle(el, localName) {
62
68
  const wrapper = readSdtWrapper(el);
63
69
  if (!wrapper) return null;
64
70
  const inner = elementChildren(wrapper.content);
65
- const tc = inner[0];
66
- if (inner.length !== 1 || tc.localName !== "tc") return null;
67
- if (readVerticalMerge(childByLocalName(tc, "tcPr")) === "continue") {
68
- return null;
69
- }
70
- return {
71
- el: tc,
72
- control: {
73
- prefix: wrapper.prefix,
74
- contentsLocked: wrapper.contentsLocked,
75
- deletionLocked: wrapper.deletionLocked
76
- }
77
- };
71
+ if (inner.length !== 1 || inner[0].localName !== localName) return null;
72
+ return { el: inner[0], control: controlFactsFrom(wrapper) };
73
+ }
74
+ function readSdtCell(el) {
75
+ const cell = readSdtSingle(el, "tc");
76
+ if (!cell) return null;
77
+ return readVerticalMerge(childByLocalName(cell.el, "tcPr")) === "continue" ? null : cell;
78
+ }
79
+ function readSdtRow(el) {
80
+ return readSdtSingle(el, "tr");
78
81
  }
79
82
  function trailCell(cells, gathered) {
80
83
  const xml = takeGathered(gathered);
@@ -84,7 +87,7 @@ function trailCell(cells, gathered) {
84
87
  last.trailingXml = (last.trailingXml ?? "") + xml;
85
88
  return true;
86
89
  }
87
- function readRow(el) {
90
+ function readRow(el, control) {
88
91
  const cells = [];
89
92
  const gathered = [];
90
93
  let tblPrEx = null;
@@ -114,7 +117,7 @@ function readRow(el) {
114
117
  });
115
118
  }
116
119
  if (cells.length === 0 || !trailCell(cells, gathered)) return null;
117
- return { el, tblPrEx, cells, leadingXml, trailingXml: null };
120
+ return { el, tblPrEx, cells, control, leadingXml, trailingXml: null };
118
121
  }
119
122
  function gridRevisionXml(grid) {
120
123
  const revision = childByLocalName(grid, "tblGridChange");
@@ -150,8 +153,9 @@ function readTableParts(el) {
150
153
  gathered.push(serializeXml(child));
151
154
  continue;
152
155
  }
153
- if (child.localName !== "tr") return null;
154
- const row = readRow(child);
156
+ const sdt = child.localName === "sdt" ? readSdtRow(child) : null;
157
+ if (!sdt && child.localName !== "tr") return null;
158
+ const row = readRow(sdt?.el ?? child, sdt?.control ?? null);
155
159
  if (!row) return null;
156
160
  const previous = rows.at(-1);
157
161
  if (previous) previous.trailingXml = takeGathered(gathered);
@@ -205,7 +209,7 @@ function gridWidthOf(rows) {
205
209
  const first = widths[0];
206
210
  return widths.every((width) => width === first) ? first : null;
207
211
  }
208
- function buildCellBlock(el, sources, context, placement) {
212
+ function buildCellBlock(el, sources, context, placement, level = "tc") {
209
213
  if (el.localName === "p") {
210
214
  return styledParagraph(
211
215
  buildParagraph(el, null, sources),
@@ -213,7 +217,15 @@ function buildCellBlock(el, sources, context, placement) {
213
217
  placement
214
218
  );
215
219
  }
216
- return buildPreservedBlock(el, null, "tc");
220
+ if (el.localName === "sdt") {
221
+ const control = buildSdtBlock(
222
+ el,
223
+ null,
224
+ (child) => buildCellBlock(child, sources, context, placement, "sdtContent")
225
+ );
226
+ if (control) return control;
227
+ }
228
+ return buildPreservedBlock(el, null, level);
217
229
  }
218
230
  function cellRect(draft) {
219
231
  return {
@@ -241,9 +253,7 @@ function buildCell(draft, table, sources, context) {
241
253
  tcPr: tcPr ? serializeXml(tcPr) : null,
242
254
  tcW: readTableWidth(tcPr, "tcW"),
243
255
  format: readCellFormat(tcPr, defaults),
244
- sdtPrefix: draft.control?.prefix ?? null,
245
- sdtContentsLocked: draft.control?.contentsLocked ?? false,
246
- sdtDeletionLocked: draft.control?.deletionLocked ?? false,
256
+ ...controlAttrs(WRAPPED_CONTROL_ATTRS, draft.control ?? NO_CONTROL),
247
257
  trailingXml: draft.trailingXml
248
258
  },
249
259
  blocks
@@ -263,6 +273,7 @@ function buildRow(row, drafts, table, sources, context) {
263
273
  tblPrEx: row.tblPrEx ? serializeXml(row.tblPrEx) : null,
264
274
  trPr: trPr ? serializeXml(trPr) : null,
265
275
  format: readRowFormat(trPr),
276
+ ...controlAttrs(WRAPPED_CONTROL_ATTRS, row.control ?? NO_CONTROL),
266
277
  leadingXml: row.leadingXml,
267
278
  trailingXml: row.trailingXml
268
279
  },
@@ -13,15 +13,15 @@ import { eachNoteReference } from "./references.js";
13
13
  var MAX_LABEL_CHARS = 64;
14
14
  function referencesBySection(doc) {
15
15
  const found = [];
16
- eachNoteReference(doc, ({ node, pos, block }) => {
17
- found.push({ block, reference: { pos, node } });
16
+ eachNoteReference(doc, ({ node, pos }) => {
17
+ found.push({ pos, node });
18
18
  });
19
19
  if (found.length === 0) return [];
20
20
  const sections = sectionsOf(doc);
21
21
  const references = sections.map(() => []);
22
22
  let at = 0;
23
- for (const { block, reference } of found) {
24
- while (block > sections[at].lastBlock) at += 1;
23
+ for (const reference of found) {
24
+ while (reference.pos > sections[at].to) at += 1;
25
25
  references[at].push(reference);
26
26
  }
27
27
  return sections.map((section, index) => ({
@@ -14,8 +14,6 @@ export interface PlacedNoteReference {
14
14
  readonly node: PMNode;
15
15
  /** Where the reference stands in the document */
16
16
  readonly pos: number;
17
- /** The top-level block it stands in, by index, which is what a section covers a run of */
18
- readonly block: number;
19
17
  /** The note it names, and null where it names no kind of note or carries no id */
20
18
  readonly key: NoteKey | null;
21
19
  }
@@ -7,17 +7,12 @@ function keyOf(node) {
7
7
  }
8
8
  function eachNoteReference(doc, visit) {
9
9
  let running = true;
10
- doc.forEach((block, offset, index) => {
10
+ doc.forEach((block, offset) => {
11
11
  if (!running) return;
12
12
  block.descendants((node, pos) => {
13
13
  if (!running) return false;
14
14
  if (node.type.name !== "noteReference") return true;
15
- running = visit({
16
- node,
17
- pos: offset + 1 + pos,
18
- block: index,
19
- key: keyOf(node)
20
- }) !== false;
15
+ running = visit({ node, pos: offset + 1 + pos, key: keyOf(node) }) !== false;
21
16
  return false;
22
17
  });
23
18
  });
@@ -7,22 +7,65 @@
7
7
  * and stands last. Everything else is null, which leaves whatever held the control preserved
8
8
  * together with its original fragment.
9
9
  *
10
- * What sits inside the control is the caller's business. A whole cell (`docx/importTable`) and
11
- * a stretch of runs inside a paragraph (`docx/importParagraph`) each read the content their
12
- * own way, and the wrapper they put back on export is the same string in both cases.
10
+ * What sits inside the control is the caller's business. A whole cell or row
11
+ * (`docx/importTable`), the blocks under a body or a cell (`docx/importSdtBlock`) and a stretch of
12
+ * runs inside a paragraph (`docx/importParagraph`) each read the content their own way, and the
13
+ * wrapper they put back on export is the same string in all of them.
13
14
  */
14
- /** The opening of a content control taken apart, and the content it wraps */
15
- export interface SdtWrapper {
15
+ import type { ControlFacts, ControlFlags } from "../schema/controlAttrs";
16
+ /**
17
+ * What a control's `w:sdtPr` states, which is all the editor reads off a control besides the XML
18
+ * it goes back out as. The carriers write it under names of their own (`schema/controlAttrs`).
19
+ */
20
+ export type SdtFacts = ControlFlags;
21
+ /** The opening of a content control taken apart, the content it wraps, and what it says */
22
+ export interface SdtWrapper extends SdtFacts {
16
23
  /** The `<w:sdt>` opening tag followed by everything that stood ahead of `<w:sdtContent>` */
17
24
  prefix: string;
18
25
  content: Element;
19
- /** Whether the control says its contents may not be edited */
20
- contentsLocked: boolean;
21
- /** Whether the control says it may not be deleted, not even whole */
22
- deletionLocked: boolean;
23
26
  }
24
27
  /** Takes a `w:sdt` apart into the wrapper to put back on export and the content to read. null for a shape we do not write ourselves */
25
28
  export declare function readSdtWrapper(el: Element): SdtWrapper | null;
29
+ /**
30
+ * A `w:sdt` that carries no `w:sdtContent` at all, as the facts its carrier records. null for
31
+ * anything else, this editor's own shape of control included.
32
+ *
33
+ * `CT_Sdt` writes the content element `minOccurs="0"`, so a control stating that what it stood
34
+ * around is not there may leave it out entirely, and §17.5.2.34 makes that element a cache of
35
+ * contents rather than the statement itself. `readSdtWrapper` turns such a control down and stays
36
+ * that way: the inline mark and a wrapped cell or row all read the content it hands back, and
37
+ * there is none here for them to stand around. Everything the control carries, `w:sdtEndPr`
38
+ * included, rides in the prefix, exactly as it does when a content element follows it.
39
+ */
40
+ export declare function readEmptySdt(el: Element): ControlFacts | null;
41
+ /**
42
+ * What the carrier of this control records about it (`schema/controlAttrs`).
43
+ *
44
+ * Every carrier derives it from the wrapper here rather than from the element again, so the inline
45
+ * mark, the block container, the node a control holding nothing stands as, a wrapped cell and a
46
+ * wrapped row cannot come to disagree about what one and the same `w:sdtPr` says.
47
+ */
48
+ export declare function controlFactsFrom(wrapper: SdtWrapper): ControlFacts;
49
+ /**
50
+ * Whether this control's content may be read as a container of blocks, which is read off the
51
+ * `w:sdtPr` alone so that a control kept whole never has its prefix serialized.
52
+ *
53
+ * Every other type - `w:richText`, which is also what a control naming no type is, `w:group`, the
54
+ * gallery and field types, and the extension types a closed `CT_SdtPr` can only carry as ignorable
55
+ * markup - says what the control means rather than what it may hold.
56
+ */
57
+ export declare function modelsBlockContent(el: Element): boolean;
58
+ /**
59
+ * The `w:sdtContent` this editor writes itself, and the control it closes after it.
60
+ *
61
+ * `readSdtWrapper` takes back apart exactly this shape, so the two stand together: a control whose
62
+ * content tag carried an attribute, or did not stand last, is one this never writes.
63
+ */
64
+ export declare const SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
65
+ /** The control's opening tag as it arrived, up to the content tag. A wrapper that lost it cannot go out */
66
+ export declare function sdtOpeningXml(prefix: unknown): string;
67
+ /** One whole control: the opening it arrived with, around content this writer owns */
68
+ export declare function sdtXml(prefix: unknown, content: string): string;
26
69
  /**
27
70
  * The number Word writes on every control. Nothing reads it back and it only has to differ
28
71
  * from the other controls in the document, so a draw out of the whole 32 bit range is enough.