@portone/docx-editor 0.6.2 → 0.6.4

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 (107) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/comments/writing.d.ts +8 -0
  4. package/dist/docx/comments/writing.js +9 -2
  5. package/dist/docx/headersFooters.d.ts +7 -0
  6. package/dist/docx/headersFooters.js +8 -15
  7. package/dist/docx/identities.d.ts +9 -2
  8. package/dist/docx/identities.js +45 -12
  9. package/dist/docx/importDocx.js +2 -0
  10. package/dist/docx/importParagraph.d.ts +4 -0
  11. package/dist/docx/importParagraph.js +2 -0
  12. package/dist/docx/importPolicy.d.ts +2 -2
  13. package/dist/docx/importPolicy.js +12 -5
  14. package/dist/docx/importPreserved.d.ts +3 -3
  15. package/dist/docx/importSdtBlock.d.ts +23 -0
  16. package/dist/docx/importSdtBlock.js +33 -0
  17. package/dist/docx/importTable.js +35 -24
  18. package/dist/docx/invariants.js +60 -3
  19. package/dist/docx/notes/numbering.js +4 -4
  20. package/dist/docx/notes/references.d.ts +0 -2
  21. package/dist/docx/notes/references.js +2 -7
  22. package/dist/docx/numberingPlanner.d.ts +3 -0
  23. package/dist/docx/numberingPlanner.js +8 -2
  24. package/dist/docx/packageParts.d.ts +9 -0
  25. package/dist/docx/packageParts.js +16 -0
  26. package/dist/docx/packagePrefixes.d.ts +12 -0
  27. package/dist/docx/packagePrefixes.js +18 -0
  28. package/dist/docx/partPlan.js +3 -5
  29. package/dist/docx/sdt.d.ts +79 -9
  30. package/dist/docx/sdt.js +121 -26
  31. package/dist/docx/sections.d.ts +13 -13
  32. package/dist/docx/sections.js +34 -20
  33. package/dist/docx/serializeBlock.js +10 -1
  34. package/dist/docx/serializeParagraph.d.ts +2 -1
  35. package/dist/docx/serializeParagraph.js +6 -1
  36. package/dist/docx/serializeSdtBlock.d.ts +16 -0
  37. package/dist/docx/serializeSdtBlock.js +11 -0
  38. package/dist/docx/serializeTable.d.ts +8 -2
  39. package/dist/docx/serializeTable.js +14 -20
  40. package/dist/docx/story.d.ts +10 -4
  41. package/dist/docx/story.js +25 -20
  42. package/dist/docx/storyBlocks.d.ts +32 -0
  43. package/dist/docx/storyBlocks.js +65 -0
  44. package/dist/docx/storyParts.d.ts +3 -0
  45. package/dist/docx/storyParts.js +4 -1
  46. package/dist/docx/wrappers.d.ts +33 -15
  47. package/dist/docx/wrappers.js +45 -26
  48. package/dist/editor/blockControlEdits.d.ts +15 -0
  49. package/dist/editor/blockControlEdits.js +79 -0
  50. package/dist/editor/clipboard/normalizers.js +1 -1
  51. package/dist/editor/commands/lockCommands.d.ts +5 -1
  52. package/dist/editor/commands/lockCommands.js +51 -23
  53. package/dist/editor/createEditor.js +4 -0
  54. package/dist/editor/inlineControlEdits.d.ts +20 -0
  55. package/dist/editor/inlineControlEdits.js +69 -0
  56. package/dist/editor/notes/noteSurface.js +4 -3
  57. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  58. package/dist/editor/plugins/controlLifecycle.js +130 -0
  59. package/dist/editor/plugins/keymap.js +34 -5
  60. package/dist/editor/plugins/rowResize.js +2 -4
  61. package/dist/editor/stories/storyState.d.ts +2 -0
  62. package/dist/editor/stories/storyState.js +2 -0
  63. package/dist/ooxml/element.js +3 -2
  64. package/dist/ooxml/errors.d.ts +11 -0
  65. package/dist/ooxml/fragment.js +3 -2
  66. package/dist/ooxml/names.d.ts +4 -0
  67. package/dist/ooxml/names.js +8 -2
  68. package/dist/ooxml/partPrefixes.d.ts +21 -0
  69. package/dist/ooxml/partPrefixes.js +164 -0
  70. package/dist/ooxml/partSplice.d.ts +6 -1
  71. package/dist/ooxml/partSplice.js +30 -10
  72. package/dist/ooxml/tagScan.d.ts +24 -2
  73. package/dist/ooxml/tagScan.js +26 -7
  74. package/dist/ooxml/xml.d.ts +8 -0
  75. package/dist/ooxml/xml.js +27 -5
  76. package/dist/page/blockKinds.d.ts +24 -2
  77. package/dist/page/blockKinds.js +6 -1
  78. package/dist/page/kinds/index.d.ts +5 -0
  79. package/dist/page/kinds/index.js +2 -0
  80. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  81. package/dist/page/kinds/sdtBlockKind.js +143 -0
  82. package/dist/page/measureBlocks.js +4 -2
  83. package/dist/page/pageDecorations.js +3 -2
  84. package/dist/page/pageLayout.d.ts +16 -15
  85. package/dist/page/pageLayout.js +134 -67
  86. package/dist/page/usePageLayout.d.ts +2 -2
  87. package/dist/page/usePageLayout.js +5 -5
  88. package/dist/schema/attrRoles.js +56 -1
  89. package/dist/schema/clipboard.js +11 -0
  90. package/dist/schema/controlAttrs.d.ts +119 -0
  91. package/dist/schema/controlAttrs.js +137 -0
  92. package/dist/schema/controlEdges.d.ts +22 -0
  93. package/dist/schema/controlEdges.js +50 -0
  94. package/dist/schema/docxSchema.d.ts +1 -1
  95. package/dist/schema/docxSchema.js +235 -37
  96. package/dist/schema/editGuard.d.ts +1 -1
  97. package/dist/schema/guards.js +2 -0
  98. package/dist/schema/locks.d.ts +56 -8
  99. package/dist/schema/locks.js +115 -56
  100. package/dist/styles/classNames.d.ts +10 -0
  101. package/dist/styles/classNames.js +10 -0
  102. package/dist/styles.css +44 -2
  103. package/dist/table/merge.js +11 -3
  104. package/dist/table/rowResize.d.ts +9 -0
  105. package/dist/table/rowResize.js +9 -5
  106. package/dist/ui/TextMenu.js +2 -0
  107. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,96 @@
1
1
  # @portone/docx-editor
2
2
 
3
+ ## 0.6.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#171](https://github.com/portone-io/docx-editor/pull/171) [`2a610ee`](https://github.com/portone-io/docx-editor/commit/2a610ee732cd2e024e0ce735efa5c91b1ddc9428) Thanks [@Deea222](https://github.com/Deea222)! - Draw nothing for a content control a paragraph holds with nothing inside it.
8
+
9
+ A `w:sdt` standing between the words of a paragraph whose `w:sdtContent` was empty, or which wrote no content element at all, used to open as a dotted box saying the content was preserved. A server that answers a failed condition by leaving a tagged phrase empty - a clause that does not apply, a signing date to be filled in later - therefore drew a box in the middle of a sentence, in every draft, 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 width, so the sentence reads on as the file says it should.
10
+
11
+ Everything the control states about itself is kept: its tag, its id, its lock and its data binding all ride back out untouched, a control nobody edited goes out as the bytes it arrived as, and a control standing inside another control or inside a hyperlink still has that wrapper closed around it. Backspace and Delete beside it pass over it and take the character on the far side, as if it were not there, so the control goes only with a selection that covers it, and then only if its lock does not say otherwise; a control holding nothing that carries a lock also keeps a selection covering it from being replaced or deleted. Copying a sentence it stands in leaves the control behind, since it holds nothing to carry, and a document holding one no longer reports it as content that was preserved rather than read, since nothing of it was lost.
12
+
13
+ The control a file writes with nothing inside it between paragraphs, which 0.6.3 released as one that goes only when it is selected whole, now follows the same rule: it can no longer be selected on its own, and a selection covering it is what removes it.
14
+
15
+ - [#172](https://github.com/portone-io/docx-editor/pull/172) [`d882a25`](https://github.com/portone-io/docx-editor/commit/d882a25aac4d0434e4a9d95a79447dcaee4f3b00) Thanks [@Deea222](https://github.com/Deea222)! - Open a document whose WordprocessingML is written under another prefix.
16
+
17
+ A file whose main part spells the wordprocessing namespace `ns0:` or leaves
18
+ it as the default namespace - what Python's `xml.etree` writes, and what
19
+ Word and Google Docs both open - used to be refused as content the editor
20
+ cannot write back. Such a package now opens: each part is rewritten to the
21
+ prefixes the editor writes, tag and attribute names only, before anything is
22
+ read, so a comment, a footnote, a header and the body all come through and
23
+ the file goes back out spelled `w:` throughout. A part that had already
24
+ bound `w`, `r` or another prefix the editor writes to a namespace of its own
25
+ is left exactly as it arrived, since respelling it would change what its
26
+ preserved markup means; the checks that applied before still decide such a
27
+ file, so a main part that binds `w` itself to some other namespace - the one
28
+ arrangement no respelling can reach - is refused as it was. A package
29
+ already spelling those prefixes is untouched and still exports byte for
30
+ byte.
31
+
32
+ ## 0.6.3
33
+
34
+ ### Patch Changes
35
+
36
+ - [#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.
37
+
38
+ 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.
39
+
40
+ 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.
41
+
42
+ 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.
43
+
44
+ Locks are read off a block control but not yet enforced against an edit, and a control does not yet break across a page.
45
+
46
+ - [#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.
47
+
48
+ 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.
49
+
50
+ 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.
51
+
52
+ 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.
53
+
54
+ - [#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.
55
+
56
+ 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.
57
+
58
+ - [#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.
59
+
60
+ 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.
61
+
62
+ 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`.
63
+
64
+ 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.
65
+
66
+ - [#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.
67
+
68
+ 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.
69
+
70
+ 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.
71
+
72
+ 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.
73
+
74
+ - [#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.
75
+
76
+ 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.
77
+ 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.
78
+ Footnote and endnote numbering that restarts per section, and the width a table is fitted to, follow the section the paragraph is in.
79
+
80
+ - [#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.
81
+
82
+ 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.
83
+
84
+ 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.
85
+
86
+ - [#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.
87
+
88
+ 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.
89
+
90
+ 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`.
91
+
92
+ 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.
93
+
3
94
  ## 0.6.2
4
95
 
5
96
  ### 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 });
@@ -2,6 +2,7 @@
2
2
  * Plans changed comment package parts while preserving untouched XML.
3
3
  */
4
4
  import type { Node as PMNode } from "prosemirror-model";
5
+ import { type RootDeclarations } from "../../ooxml/partSplice";
5
6
  import type { PartPlanner } from "../partPlan";
6
7
  import type { SessionStore } from "../session";
7
8
  import { type CommentReferenceData, type CommentReplyData } from "./model";
@@ -22,5 +23,12 @@ export declare function commentsRootProblem(xml: string): string | null;
22
23
  export declare function extensionsRootProblem(xml: string): string | null;
23
24
  /** Whether the extended part has to be rewritten: thread state arrived, changed, or went with a deleted comment */
24
25
  export declare function extensionsChanged(doc: PMNode, session: SessionStore): boolean;
26
+ /**
27
+ * What a part carrying comments this editor wrote has to declare. Every entry it writes is spelled
28
+ * under `w`, so the root binds it rather than each entry declaring it again.
29
+ */
30
+ export declare const COMMENT_MARKUP: RootDeclarations;
31
+ /** What a part carrying the thread an entry belongs to has to declare, its keys being `w15` ones */
32
+ export declare const EXTENSIONS_MARKUP: RootDeclarations;
25
33
  export declare function currentCommentBodies(references: ReadonlyMap<string, CommentReferenceData>): ReadonlyMap<string, CommentReferenceData | CommentReplyData>;
26
34
  export declare const commentsPlanner: PartPlanner;
@@ -102,7 +102,7 @@ function extensionsXml(references, comments, originallyReferenced) {
102
102
  root: EXTENSIONS_ROOT,
103
103
  replaceChildren: pieces.join("")
104
104
  }),
105
- { namespaces: { w15: NAMESPACES.w15 } }
105
+ EXTENSIONS_MARKUP
106
106
  );
107
107
  }
108
108
  var COMMENTS_ROOT = "comments";
@@ -163,11 +163,16 @@ function renderedComment(comment, arrivedKeyed, arrived, doc, session) {
163
163
  session
164
164
  });
165
165
  }
166
- var COMMENT_MARKUP = { namespaces: { w: NAMESPACES.w } };
166
+ var COMMENT_MARKUP = {
167
+ namespaces: { w: NAMESPACES.w }
168
+ };
167
169
  var THREAD_MARKUP = {
168
170
  namespaces: { w: NAMESPACES.w, w14: NAMESPACES.w14, mc: NAMESPACES.mc },
169
171
  ignorable: ["w14"]
170
172
  };
173
+ var EXTENSIONS_MARKUP = {
174
+ namespaces: { w15: NAMESPACES.w15 }
175
+ };
171
176
  function currentCommentBodies(references) {
172
177
  const comments = /* @__PURE__ */ new Map();
173
178
  for (const [id, comment] of references) {
@@ -309,6 +314,8 @@ var commentsPlanner = {
309
314
  plan: planCommentParts
310
315
  };
311
316
  export {
317
+ COMMENT_MARKUP,
318
+ EXTENSIONS_MARKUP,
312
319
  commentsChanged,
313
320
  commentsPlanner,
314
321
  commentsRootProblem,
@@ -12,6 +12,7 @@
12
12
  */
13
13
  import type { Node as PMNode } from "prosemirror-model";
14
14
  import { type ParagraphAlign } from "../model/format";
15
+ import { type RootDeclarations } from "../ooxml/partSplice";
15
16
  import { type StoryKey } from "../schema/stories";
16
17
  import type { PartPlanner } from "./partPlan";
17
18
  import { type DocumentSection } from "./sections";
@@ -96,6 +97,12 @@ export declare function headerFooterOn(variants: HeaderFooterVariants, headersFo
96
97
  * `PAGE` field prints (§17.16.5.45).
97
98
  */
98
99
  export declare function headerFooterText(story: PMNode, page: number, totalPages: number): string;
100
+ /**
101
+ * What a header this editor rewrote has to declare: every block it writes is spelled under `w`, so
102
+ * the part's root binds it rather than each block declaring it again. A part that already binds it
103
+ * - which every one Word writes does - is left exactly as it stands.
104
+ */
105
+ export declare const HEADER_MARKUP: RootDeclarations;
99
106
  /**
100
107
  * Writes back the header and footer parts an edit changed, and no others.
101
108
  *
@@ -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,15 +160,11 @@ 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
- var HEADER_MARKUP = { namespaces: { w: NAMESPACES.w } };
165
+ var HEADER_MARKUP = {
166
+ namespaces: { w: NAMESPACES.w }
167
+ };
176
168
  function writtenPart(imported, current, session, notes) {
177
169
  const written = withUniqueStoryIdentities([{ story: current, frozen: false }]).map(
178
170
  (story) => serializeStory(story, imported, imported, {
@@ -203,6 +195,7 @@ var headerFooterPlanner = {
203
195
  plan: (doc, session, context) => rewrittenParts(doc, session, context.notes)
204
196
  };
205
197
  export {
198
+ HEADER_MARKUP,
206
199
  displayPageNumber,
207
200
  headerFooterOn,
208
201
  headerFooterPlanner,
@@ -48,8 +48,15 @@ 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, a wrapped cell
57
+ * or row and either node a control holding nothing stands as alike, each read under its own
58
+ * attribute names: a second control claiming the first one's `w:id` is not a shape §17.5.2.18
59
+ * allows, whichever of them carries it.
53
60
  */
54
61
  export declare const controlRule: IdentityRule;
55
62
  /**
@@ -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 = [];
@@ -84,14 +94,37 @@ function rewriteParagraph(paragraph, written) {
84
94
  renamed = true;
85
95
  marks = control.copy.addToSet(control.mark.removeFromSet(marks));
86
96
  }
87
- inline.push(marks === child.marks ? child : child.mark(marks));
97
+ const names = controlAttrsOf(child);
98
+ const own = names === null ? null : claimedPrefix(child.attrs, names, written);
99
+ if (names === null || own === null) {
100
+ inline.push(marks === child.marks ? child : child.mark(marks));
101
+ return;
102
+ }
103
+ renamed = true;
104
+ inline.push(
105
+ child.type.create(
106
+ { ...child.attrs, [names.prefix]: own },
107
+ child.content,
108
+ marks
109
+ )
110
+ );
88
111
  });
89
112
  return renamed ? paragraph.copy(Fragment.fromArray(inline)) : paragraph;
90
113
  }
91
114
  var controlRule = {
92
115
  name: "control",
93
116
  visit(block, written) {
94
- return block.type === docxSchema.nodes.paragraph ? rewriteParagraph(block, written) : block;
117
+ if (block.type === docxSchema.nodes.paragraph) {
118
+ return rewriteParagraph(block, written);
119
+ }
120
+ const names = controlAttrsOf(block);
121
+ if (!names) return block;
122
+ const prefix = claimedPrefix(block.attrs, names, written);
123
+ return prefix === null ? block : block.type.create(
124
+ { ...block.attrs, [names.prefix]: prefix },
125
+ block.content,
126
+ block.marks
127
+ );
95
128
  }
96
129
  };
97
130
  var IDENTITY_RULES = [
@@ -44,6 +44,7 @@ import {
44
44
  specialNotesOf
45
45
  } from "./notes/reading.js";
46
46
  import { readPart, relatedPartPath } from "./packageParts.js";
47
+ import { normalizePackagePrefixes } from "./packagePrefixes.js";
47
48
  import { A4_PORTRAIT } from "./pageGeometry.js";
48
49
  import { readRelationships } from "./relationships.js";
49
50
  import { scanBody } from "./scan.js";
@@ -170,6 +171,7 @@ function importDocx(input, options) {
170
171
  function readDocx(input) {
171
172
  const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
172
173
  const parts = openParts(bytes);
174
+ normalizePackagePrefixes(parts);
173
175
  const mainPartPath = findMainPartPath(parts);
174
176
  const mainPart = parts.get(mainPartPath);
175
177
  if (!mainPart) {
@@ -9,6 +9,10 @@
9
9
  * is read as the file wrote it and recorded on the marks (`docx/wrappers`). A link inside a link is
10
10
  * the one arrangement no mark can record, and the inner one stays whole as it always has.
11
11
  *
12
+ * A wrapper the file wrote with nothing inside it holds no text for a mark to ride on, so the
13
+ * registry says what it opens as instead: a content control becomes a node of its own, wearing the
14
+ * wrappers around it (`docx/wrappers`).
15
+ *
12
16
  * Nothing here demotes a paragraph. What the editor has no model for - a field character, a
13
17
  * tracked insertion, a symbol, a drawing nobody could read - is kept where it stood, inside its
14
18
  * run or beside it, by the rule `docx/importPolicy` gives it for the level it stands at. The
@@ -181,6 +181,8 @@ function buildModelledInline(child, sources, depth, wrappers) {
181
181
  const kind = wrapperKindFor(child);
182
182
  if (kind) {
183
183
  if (!wrapperFits(kind, wrappers)) return null;
184
+ const empty = kind.readEmpty(child, wrappers);
185
+ if (empty) return [empty];
184
186
  const reading = kind.read(child, depth, sources);
185
187
  if (!reading) return null;
186
188
  return buildContent(reading.content, sources, depth + 1, [
@@ -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,23 @@
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, in either of the two shapes
21
+ * that state it (`./sdt`).
22
+ */
23
+ export declare function buildSdtBlock(el: Element, srcId: string | null, readBlock: BlockReader): PMNode | null;
@@ -0,0 +1,33 @@
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 { controlFactsFrom, modelsBlockContent, readSdtContents } from "./sdt.js";
9
+ import { nextKey } from "./wrappers.js";
10
+ function controlNodeAttrs(el, srcId, facts) {
11
+ return {
12
+ srcId,
13
+ key: nextKey(docxSchema.marks.sdt.name, el),
14
+ ...controlAttrs(OWN_CONTROL_ATTRS, facts)
15
+ };
16
+ }
17
+ function buildSdtBlock(el, srcId, readBlock) {
18
+ if (!modelsBlockContent(el)) return null;
19
+ const read = readSdtContents(el);
20
+ if (read === null) return null;
21
+ if (read.kind === "nothing") {
22
+ return docxSchema.nodes.sdtEmpty.create(
23
+ controlNodeAttrs(el, srcId, read.facts)
24
+ );
25
+ }
26
+ return docxSchema.nodes.sdtBlock.create(
27
+ controlNodeAttrs(el, srcId, controlFactsFrom(read.wrapper)),
28
+ elementChildren(read.wrapper.content).map(readBlock)
29
+ );
30
+ }
31
+ export {
32
+ buildSdtBlock
33
+ };