@portone/docx-editor 0.3.0 → 0.5.0

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 (217) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/assets/editor.png +0 -0
  3. package/dist/DocxEditor.d.ts +1 -1
  4. package/dist/DocxEditor.js +45 -25
  5. package/dist/core.d.ts +1 -1
  6. package/dist/docx/cloning.js +4 -9
  7. package/dist/docx/commentOnlyChange.d.ts +5 -3
  8. package/dist/docx/comments/grammar.d.ts +27 -10
  9. package/dist/docx/comments/grammar.js +18 -67
  10. package/dist/docx/comments/model.d.ts +5 -6
  11. package/dist/docx/comments/model.js +0 -9
  12. package/dist/docx/comments/parts.d.ts +16 -10
  13. package/dist/docx/comments/parts.js +65 -13
  14. package/dist/docx/comments/people.d.ts +11 -3
  15. package/dist/docx/comments/people.js +20 -6
  16. package/dist/docx/comments/policy.js +7 -1
  17. package/dist/docx/comments/reading.d.ts +4 -2
  18. package/dist/docx/comments/reading.js +1 -14
  19. package/dist/docx/comments/writing.js +78 -35
  20. package/dist/docx/exportDocx.js +30 -18
  21. package/dist/docx/exportRefs.d.ts +8 -3
  22. package/dist/docx/exportRefs.js +2 -1
  23. package/dist/docx/fidelity.js +24 -10
  24. package/dist/docx/fields.d.ts +42 -0
  25. package/dist/docx/fields.js +76 -0
  26. package/dist/docx/formatting/attrs.d.ts +20 -2
  27. package/dist/docx/formatting/attrs.js +57 -2
  28. package/dist/docx/formatting/context.d.ts +10 -2
  29. package/dist/docx/formatting/context.js +23 -5
  30. package/dist/docx/formatting/resolve.d.ts +10 -4
  31. package/dist/docx/formatting/resolve.js +31 -11
  32. package/dist/docx/formatting/styles.d.ts +25 -2
  33. package/dist/docx/formatting/styles.js +84 -3
  34. package/dist/docx/headersFooters.d.ts +89 -20
  35. package/dist/docx/headersFooters.js +163 -172
  36. package/dist/docx/hyperlink.d.ts +14 -4
  37. package/dist/docx/hyperlink.js +4 -1
  38. package/dist/docx/importDocx.d.ts +1 -1
  39. package/dist/docx/importDocx.js +135 -74
  40. package/dist/docx/importParagraph.d.ts +16 -4
  41. package/dist/docx/importParagraph.js +87 -93
  42. package/dist/docx/importPolicy.d.ts +70 -0
  43. package/dist/docx/importPolicy.js +190 -0
  44. package/dist/docx/importPreserved.d.ts +30 -0
  45. package/dist/docx/importPreserved.js +54 -0
  46. package/dist/docx/importTable.d.ts +2 -2
  47. package/dist/docx/importTable.js +107 -66
  48. package/dist/docx/invariants.js +42 -31
  49. package/dist/docx/newLists.d.ts +30 -0
  50. package/dist/docx/newLists.js +24 -1
  51. package/dist/docx/notes.d.ts +6 -1
  52. package/dist/docx/notes.js +6 -16
  53. package/dist/docx/numberingPlanner.d.ts +6 -3
  54. package/dist/docx/numberingPlanner.js +38 -7
  55. package/dist/docx/pageGeometry.d.ts +5 -14
  56. package/dist/docx/pageGeometry.js +0 -12
  57. package/dist/docx/protectionPolicy.d.ts +13 -5
  58. package/dist/docx/protectionPolicy.js +32 -22
  59. package/dist/docx/scan.d.ts +18 -8
  60. package/dist/docx/scan.js +17 -11
  61. package/dist/docx/sections.d.ts +126 -0
  62. package/dist/docx/sections.js +207 -0
  63. package/dist/docx/serializeBlock.d.ts +13 -4
  64. package/dist/docx/serializeBlock.js +16 -18
  65. package/dist/docx/serializeParagraph.d.ts +4 -0
  66. package/dist/docx/serializeParagraph.js +1 -0
  67. package/dist/docx/serializePreserved.d.ts +14 -0
  68. package/dist/docx/serializePreserved.js +24 -0
  69. package/dist/docx/serializeStory.d.ts +17 -0
  70. package/dist/docx/serializeStory.js +17 -0
  71. package/dist/docx/serializeTable.js +10 -13
  72. package/dist/docx/session.d.ts +24 -7
  73. package/dist/docx/session.js +37 -6
  74. package/dist/docx/story.d.ts +140 -0
  75. package/dist/docx/story.js +237 -0
  76. package/dist/docx/storyProjection.js +6 -1
  77. package/dist/docx/tableFormatting/conditions.d.ts +76 -0
  78. package/dist/docx/tableFormatting/conditions.js +196 -0
  79. package/dist/docx/tableFormatting/editing.d.ts +7 -3
  80. package/dist/docx/tableFormatting/editing.js +4 -5
  81. package/dist/docx/tableFormatting/reading.d.ts +49 -19
  82. package/dist/docx/tableFormatting/reading.js +109 -35
  83. package/dist/docx/tableFormatting.d.ts +1 -0
  84. package/dist/docx/tableFormatting.js +1 -0
  85. package/dist/docx/tableTemplate.js +12 -12
  86. package/dist/editor/clipboard/htmlReader.d.ts +30 -0
  87. package/dist/editor/clipboard/htmlReader.js +302 -0
  88. package/dist/editor/clipboard/internalChannel.d.ts +57 -0
  89. package/dist/editor/clipboard/internalChannel.js +58 -0
  90. package/dist/editor/clipboard/normalizers.d.ts +99 -0
  91. package/dist/editor/clipboard/normalizers.js +199 -0
  92. package/dist/editor/clipboard/parser.d.ts +34 -0
  93. package/dist/editor/clipboard/parser.js +58 -0
  94. package/dist/editor/clipboard/plugin.d.ts +21 -0
  95. package/dist/editor/clipboard/plugin.js +320 -0
  96. package/dist/editor/clipboard/readContext.d.ts +24 -0
  97. package/dist/editor/clipboard/readContext.js +19 -0
  98. package/dist/editor/clipboard/readers.d.ts +33 -0
  99. package/dist/editor/clipboard/readers.js +37 -0
  100. package/dist/editor/commands/comments/editing.d.ts +26 -6
  101. package/dist/editor/commands/comments/editing.js +175 -105
  102. package/dist/editor/commands/comments/model.d.ts +34 -17
  103. package/dist/editor/commands/comments/model.js +11 -0
  104. package/dist/editor/commands/comments/reading.d.ts +5 -3
  105. package/dist/editor/commands/comments/reading.js +8 -58
  106. package/dist/editor/commands/formatting/propertyCommands.js +6 -1
  107. package/dist/editor/commands/index.d.ts +2 -2
  108. package/dist/editor/commands/index.js +2 -0
  109. package/dist/editor/commands/listCommands.d.ts +12 -3
  110. package/dist/editor/commands/listCommands.js +76 -30
  111. package/dist/editor/commands/noteQueries.d.ts +22 -5
  112. package/dist/editor/commands/noteQueries.js +34 -6
  113. package/dist/editor/commands/paragraphCommands.js +6 -1
  114. package/dist/editor/createEditor.js +14 -4
  115. package/dist/editor/documentStyles.d.ts +26 -4
  116. package/dist/editor/documentStyles.js +9 -4
  117. package/dist/editor/editorDocument.d.ts +7 -6
  118. package/dist/editor/editorDocument.js +18 -6
  119. package/dist/editor/imageFiles.d.ts +2 -2
  120. package/dist/editor/imageFiles.js +2 -0
  121. package/dist/editor/insertTable.d.ts +1 -1
  122. package/dist/editor/insertTable.js +2 -2
  123. package/dist/editor/paragraphEdits.d.ts +16 -1
  124. package/dist/editor/paragraphEdits.js +13 -5
  125. package/dist/editor/paragraphPlacement.d.ts +11 -0
  126. package/dist/editor/paragraphPlacement.js +18 -0
  127. package/dist/editor/plainText.d.ts +12 -0
  128. package/dist/editor/plainText.js +2 -1
  129. package/dist/editor/plugins/columnResize.js +6 -3
  130. package/dist/editor/plugins/commentComposer.d.ts +28 -0
  131. package/dist/editor/plugins/commentComposer.js +46 -0
  132. package/dist/editor/plugins/commentDecorations.d.ts +29 -2
  133. package/dist/editor/plugins/commentDecorations.js +89 -15
  134. package/dist/editor/plugins/documentProjection.d.ts +31 -0
  135. package/dist/editor/plugins/documentProjection.js +22 -0
  136. package/dist/editor/plugins/imagePaste.js +20 -10
  137. package/dist/editor/plugins/linkPanel.d.ts +7 -3
  138. package/dist/editor/plugins/linkPanel.js +13 -27
  139. package/dist/editor/plugins/numberingDecorations.d.ts +10 -4
  140. package/dist/editor/plugins/numberingDecorations.js +43 -2
  141. package/dist/editor/plugins/panelState.d.ts +67 -0
  142. package/dist/editor/plugins/panelState.js +54 -0
  143. package/dist/editor/plugins/paragraphDisplay.js +9 -3
  144. package/dist/editor/plugins/tableContextMenu.d.ts +1 -1
  145. package/dist/editor/plugins/tableContextMenu.js +30 -47
  146. package/dist/editor/plugins/tableDisplay.js +19 -5
  147. package/dist/editor/plugins/textContextMenu.d.ts +1 -1
  148. package/dist/editor/plugins/textContextMenu.js +30 -47
  149. package/dist/model/format.d.ts +50 -0
  150. package/dist/model/format.js +65 -0
  151. package/dist/numbering/listRegistry.d.ts +37 -0
  152. package/dist/numbering/listRegistry.js +112 -0
  153. package/dist/numbering/listTemplate.d.ts +26 -17
  154. package/dist/numbering/listTemplate.js +66 -20
  155. package/dist/numbering/markers.d.ts +15 -6
  156. package/dist/numbering/markers.js +26 -59
  157. package/dist/numbering/parseNumbering.d.ts +70 -5
  158. package/dist/numbering/parseNumbering.js +100 -26
  159. package/dist/numbering/spellers.d.ts +15 -0
  160. package/dist/numbering/spellers.js +96 -0
  161. package/dist/numbering/writeNumbering.d.ts +5 -3
  162. package/dist/numbering/writeNumbering.js +15 -15
  163. package/dist/ooxml/conformance.d.ts +28 -0
  164. package/dist/ooxml/conformance.js +20 -0
  165. package/dist/ooxml/errors.d.ts +5 -5
  166. package/dist/ooxml/fragment.d.ts +11 -0
  167. package/dist/ooxml/fragment.js +14 -4
  168. package/dist/ooxml/image.d.ts +3 -4
  169. package/dist/ooxml/image.js +50 -5
  170. package/dist/ooxml/names.d.ts +15 -0
  171. package/dist/ooxml/names.js +10 -1
  172. package/dist/ooxml/partSplice.d.ts +1 -1
  173. package/dist/ooxml/partSplice.js +10 -3
  174. package/dist/ooxml/props.d.ts +8 -0
  175. package/dist/ooxml/props.js +14 -4
  176. package/dist/ooxml/rangeMarkers.d.ts +5 -0
  177. package/dist/ooxml/rangeMarkers.js +24 -0
  178. package/dist/ooxml/xml.d.ts +8 -9
  179. package/dist/ooxml/xml.js +6 -6
  180. package/dist/page/PageGuides.d.ts +8 -4
  181. package/dist/page/PageGuides.js +13 -37
  182. package/dist/page/pageLayout.d.ts +62 -13
  183. package/dist/page/pageLayout.js +91 -37
  184. package/dist/page/usePageLayout.d.ts +14 -11
  185. package/dist/page/usePageLayout.js +21 -29
  186. package/dist/schema/attrRoles.js +44 -19
  187. package/dist/schema/docxSchema.d.ts +9 -1
  188. package/dist/schema/docxSchema.js +220 -139
  189. package/dist/schema/editGuard.d.ts +1 -1
  190. package/dist/schema/guards.js +2 -2
  191. package/dist/schema/index.d.ts +1 -0
  192. package/dist/schema/preservedFragments.d.ts +12 -0
  193. package/dist/schema/preservedFragments.js +38 -0
  194. package/dist/schema/preservedGuards.d.ts +9 -7
  195. package/dist/schema/preservedGuards.js +112 -11
  196. package/dist/schema/protection.d.ts +34 -5
  197. package/dist/schema/protection.js +42 -14
  198. package/dist/schema/rendering.js +3 -1
  199. package/dist/schema/stories.d.ts +49 -0
  200. package/dist/schema/stories.js +78 -0
  201. package/dist/styles/classNames.d.ts +16 -7
  202. package/dist/styles/classNames.js +16 -7
  203. package/dist/styles.css +90 -30
  204. package/dist/table/cellFormatting.js +1 -2
  205. package/dist/table/gridBorders.d.ts +11 -15
  206. package/dist/table/gridBorders.js +32 -18
  207. package/dist/table/resize.d.ts +5 -3
  208. package/dist/ui/CommentsPanel.d.ts +1 -3
  209. package/dist/ui/CommentsPanel.js +24 -15
  210. package/dist/ui/NotesPanel.js +2 -2
  211. package/dist/ui/TextMenu.d.ts +1 -3
  212. package/dist/ui/TextMenu.js +10 -6
  213. package/dist/ui/comments/CommentComposer.d.ts +6 -1
  214. package/dist/ui/comments/CommentComposer.js +26 -3
  215. package/package.json +1 -1
  216. package/dist/editor/externalClipboard.d.ts +0 -11
  217. package/dist/editor/externalClipboard.js +0 -456
@@ -1,5 +1,7 @@
1
1
  // src/ooxml/names.ts
2
- import { R_NS, W_NS } from "./xml.js";
2
+ var W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
3
+ var R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
4
+ var M_NS = "http://schemas.openxmlformats.org/officeDocument/2006/math";
3
5
  var W_PREFIX = "w";
4
6
  var NAMESPACES = {
5
7
  w: W_NS,
@@ -11,6 +13,9 @@ var NAMESPACES = {
11
13
  a: "http://schemas.openxmlformats.org/drawingml/2006/main",
12
14
  pic: "http://schemas.openxmlformats.org/drawingml/2006/picture"
13
15
  };
16
+ function isKnownPrefix(prefix) {
17
+ return Object.hasOwn(NAMESPACES, prefix);
18
+ }
14
19
  function qualify(prefix, local) {
15
20
  return `${prefix}:${local}`;
16
21
  }
@@ -21,8 +26,12 @@ function xmlnsDecl(prefix) {
21
26
  return `xmlns:${prefix}="${NAMESPACES[prefix]}"`;
22
27
  }
23
28
  export {
29
+ M_NS,
24
30
  NAMESPACES,
31
+ R_NS,
32
+ W_NS,
25
33
  W_PREFIX,
34
+ isKnownPrefix,
26
35
  qualify,
27
36
  wName,
28
37
  xmlnsDecl
@@ -62,6 +62,6 @@ export interface RootDeclarations {
62
62
  }
63
63
  /**
64
64
  * The part with its root binding every prefix named and its `mc:Ignorable` naming every token
65
- * asked for, each declared once: one the root declares already is left as it was written.
65
+ * asked for, each declared once: a matching declaration is preserved, and a conflicting binding is refused.
66
66
  */
67
67
  export declare function ensureRootDeclarations(xml: string, declarations: RootDeclarations): string;
@@ -121,12 +121,19 @@ function withDeclarations(openTag, { namespaces, ignorable }) {
121
121
  "the root element's attributes could not be read"
122
122
  );
123
123
  }
124
- const declared = new Set(attrs.map(([name]) => name));
124
+ const declared = new Map(attrs);
125
125
  const additions = [];
126
126
  for (const [prefix, namespace] of Object.entries(namespaces)) {
127
- if (namespace !== void 0 && !declared.has(`xmlns:${prefix}`)) {
128
- additions.push([`xmlns:${prefix}`, namespace]);
127
+ if (namespace === void 0) continue;
128
+ const name = `xmlns:${prefix}`;
129
+ const existing = declared.get(name);
130
+ if (existing !== void 0 && existing !== namespace) {
131
+ throw new DocxExportError(
132
+ "unsupported-content",
133
+ `the part root binds ${prefix} to a namespace the writer cannot use`
134
+ );
129
135
  }
136
+ if (existing === void 0) additions.push([name, namespace]);
130
137
  }
131
138
  const ignoring = IGNORABLE.exec(openTag);
132
139
  let tokens = null;
@@ -52,6 +52,14 @@ export declare function renderProps(props: Props): string;
52
52
  * carries children in a different order under a different parent (`pPr/rPr`).
53
53
  */
54
54
  export declare function setChild(props: Props, name: string, xml: string | null, parent?: string): Props;
55
+ /**
56
+ * Replaces every child of one name with this list of them, in the order given.
57
+ *
58
+ * `CT_SectPr` lets a section name a header and a footer once per variant, so a name standing more
59
+ * than once is a list rather than a single child, and writing one of them is writing the list. An
60
+ * empty list takes them all away.
61
+ */
62
+ export declare function setChildren(props: Props, name: string, xmls: readonly string[], parent?: string): Props;
55
63
  export declare function propsChild(children: readonly PropsChild[], name: string): PropsChild | undefined;
56
64
  /**
57
65
  * The opening tag's attributes as pairs, and null when their shape cannot be made out.
@@ -115,28 +115,37 @@ function tailWith(props, carried) {
115
115
  return tail === "" ? {} : { tail };
116
116
  }
117
117
  function setChild(props, name, xml, parent) {
118
+ return setChildren(props, name, xml === null ? [] : [xml], parent);
119
+ }
120
+ function setChildren(props, name, xmls, parent) {
118
121
  const order = childOrderOf(localPart(props.tag), parent);
119
122
  const at = props.children.findIndex((entry) => entry.name === name);
120
- if (at !== -1 && xml !== null) {
123
+ if (at !== -1 && xmls.length > 0) {
121
124
  const rest = dropChildren(props.children.slice(at + 1), name);
122
125
  return {
123
126
  ...props,
124
127
  children: [
125
128
  ...props.children.slice(0, at),
126
- { ...props.children[at], xml },
129
+ ...xmls.map(
130
+ (xml, index2) => index2 === 0 ? { ...props.children[at], xml } : { name, xml }
131
+ ),
127
132
  ...rest.kept
128
133
  ],
129
134
  ...tailWith(props, rest.carried)
130
135
  };
131
136
  }
132
137
  const { kept, carried } = dropChildren(props.children, name);
133
- if (xml === null) {
138
+ if (xmls.length === 0) {
134
139
  return { ...props, children: kept, ...tailWith(props, carried) };
135
140
  }
136
141
  const index = insertIndex(kept, name, order, props.tag);
137
142
  return {
138
143
  ...props,
139
- children: [...kept.slice(0, index), { name, xml }, ...kept.slice(index)],
144
+ children: [
145
+ ...kept.slice(0, index),
146
+ ...xmls.map((xml) => ({ name, xml })),
147
+ ...kept.slice(index)
148
+ ],
140
149
  ...tailWith(props, carried)
141
150
  };
142
151
  }
@@ -210,5 +219,6 @@ export {
210
219
  renderElement,
211
220
  renderProps,
212
221
  setChild,
222
+ setChildren,
213
223
  withAttrs
214
224
  };
@@ -0,0 +1,5 @@
1
+ /** `EG_RangeMarkupElements` less the comment markers modelled inside paragraphs and wrappers */
2
+ export declare const RANGE_MARKERS: string[];
3
+ /** The markers of `EG_RunLevelElts` that stand outside `EG_RangeMarkupElements` */
4
+ export declare const PERMISSION_MARKERS: string[];
5
+ export declare const COMMENT_RANGE_MARKERS: string[];
@@ -0,0 +1,24 @@
1
+ // src/ooxml/rangeMarkers.ts
2
+ var RANGE_MARKERS = [
3
+ "bookmarkStart",
4
+ "bookmarkEnd",
5
+ "moveFromRangeStart",
6
+ "moveFromRangeEnd",
7
+ "moveToRangeStart",
8
+ "moveToRangeEnd",
9
+ "customXmlInsRangeStart",
10
+ "customXmlInsRangeEnd",
11
+ "customXmlDelRangeStart",
12
+ "customXmlDelRangeEnd",
13
+ "customXmlMoveFromRangeStart",
14
+ "customXmlMoveFromRangeEnd",
15
+ "customXmlMoveToRangeStart",
16
+ "customXmlMoveToRangeEnd"
17
+ ];
18
+ var PERMISSION_MARKERS = ["permStart", "permEnd"];
19
+ var COMMENT_RANGE_MARKERS = ["commentRangeStart", "commentRangeEnd"];
20
+ export {
21
+ COMMENT_RANGE_MARKERS,
22
+ PERMISSION_MARKERS,
23
+ RANGE_MARKERS
24
+ };
@@ -1,10 +1,5 @@
1
- /** The wordprocessing namespace that every element we read lives in */
2
- export declare const W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
3
- /**
4
- * The relationship namespace. It is both where the `r:embed` attributes live and the base
5
- * every relationship type name is built on
6
- */
7
- export declare const R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
1
+ import { R_NS, W_NS } from "./names";
2
+ export { R_NS, W_NS };
8
3
  /**
9
4
  * The prefixes whose meaning the editor depends on, and the namespace each one has to carry.
10
5
  *
@@ -58,8 +53,12 @@ export declare function withXmlParser<T>(parser: XmlParser | undefined, work: ()
58
53
  export declare function parseXml(source: string): Document;
59
54
  /**
60
55
  * Gathers the namespace prefixes used in the fragment and declares them.
61
- * Only `w` carries real meaning; the rest are placeholders that keep the parser from stopping.
62
- * All we read are element names and `w:` attributes, so placeholders still let the values be read as they are.
56
+ *
57
+ * A prefix `NAMESPACES` knows is bound to the namespace it stands for, so an attribute read off
58
+ * the fragment by namespace answers what it would in the part the fragment came out of. A prefix
59
+ * it does not know is bound to a placeholder, which carries no meaning but keeps the parser going:
60
+ * a fragment naming `o:`, `v:` or `wne:` would otherwise fail to parse and be refused whole
61
+ * (`ooxml/fragment`), taking the node that held it with it.
63
62
  *
64
63
  * An attribute standing at the very start of the string counts too, since a fragment may be an
65
64
  * attribute list of its own (`attrString`) rather than an element.
package/dist/ooxml/xml.js CHANGED
@@ -1,7 +1,6 @@
1
1
  // src/ooxml/xml.ts
2
2
  import { DocxImportError } from "./errors.js";
3
- var W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
4
- var R_NS = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
3
+ import { isKnownPrefix, NAMESPACES, R_NS, W_NS, W_PREFIX } from "./names.js";
5
4
  var RESERVED_PREFIXES = /* @__PURE__ */ new Map([
6
5
  ["w", W_NS],
7
6
  ["r", R_NS]
@@ -74,7 +73,7 @@ function parseXml(source) {
74
73
  return doc;
75
74
  }
76
75
  function namespaceDecls(xml) {
77
- const prefixes = /* @__PURE__ */ new Set(["w"]);
76
+ const prefixes = /* @__PURE__ */ new Set([W_PREFIX]);
78
77
  for (const [, prefix] of xml.matchAll(/<\/?([A-Za-z_][\w.-]*):/g)) {
79
78
  prefixes.add(prefix);
80
79
  }
@@ -85,9 +84,10 @@ function namespaceDecls(xml) {
85
84
  }
86
85
  prefixes.delete("xml");
87
86
  prefixes.delete("xmlns");
88
- return Array.from(prefixes).map(
89
- (prefix) => `xmlns:${prefix}="${prefix === "w" ? W_NS : `urn:docx-editor:${prefix}`}"`
90
- ).join(" ");
87
+ return Array.from(prefixes).map((prefix) => {
88
+ const namespace = isKnownPrefix(prefix) ? NAMESPACES[prefix] : `urn:docx-editor:${prefix}`;
89
+ return `xmlns:${prefix}="${namespace}"`;
90
+ }).join(" ");
91
91
  }
92
92
  function elementChildren(el) {
93
93
  return Array.from(el.children);
@@ -1,16 +1,20 @@
1
1
  /**
2
- * Draws the gaps between pages on the sheet, plus a number for each page.
2
+ * Draws the gaps between pages on the sheet.
3
3
  *
4
4
  * Straddling blocks have already been moved to the next page, so there is no text under
5
5
  * these bands.
6
6
  * Only the places crossed by a block taller than one page are marked over the text, with
7
7
  * a faint dotted line.
8
8
  * The box only paints, so the mouse still reaches the text beneath it.
9
+ *
10
+ * Each page draws the header and footer of the section it opens with, since a document names its
11
+ * headers section by section (`docx/headersFooters`).
9
12
  */
10
13
  import type { ReactElement } from "react";
11
14
  import { type HeadersFooters } from "../docx/headersFooters";
12
- import type { PageOverlay } from "./usePageLayout";
13
- export declare function PageGuides({ overlay, headersFooters, }: {
15
+ import type { PageFace, PageOverlay } from "./usePageLayout";
16
+ export declare function PageGuides({ overlay, headersFootersFor, }: {
14
17
  overlay: PageOverlay;
15
- headersFooters?: HeadersFooters;
18
+ /** The stories the section this page belongs to shows. Left out while no document is open */
19
+ headersFootersFor?: (page: PageFace) => HeadersFooters | null;
16
20
  }): ReactElement;
@@ -1,17 +1,14 @@
1
1
  // src/page/PageGuides.tsx
2
2
  import {
3
3
  displayPageNumber,
4
- headerFooterAlign,
4
+ headerFooterOn,
5
5
  headerFooterText
6
6
  } from "../docx/headersFooters.js";
7
7
  import { editorClassNames } from "../styles/classNames.js";
8
8
  import { jsx, jsxs } from "react/jsx-runtime";
9
- function badgeTitle(badge) {
10
- return badge.exactPage ? `Page ${badge.page}` : `Page ${badge.page} (approximate)`;
11
- }
12
9
  function PageGuides({
13
10
  overlay,
14
- headersFooters
11
+ headersFootersFor
15
12
  }) {
16
13
  const totalPages = overlay.pages.length;
17
14
  return /* @__PURE__ */ jsxs(
@@ -37,40 +34,19 @@ function PageGuides({
37
34
  },
38
35
  mark.page
39
36
  )),
40
- overlay.badges.map((badge) => /* @__PURE__ */ jsx(
41
- "span",
42
- {
43
- className: editorClassNames.pageBadge,
44
- style: { top: `${badge.top}px` },
45
- title: badgeTitle(badge),
46
- children: badge.page
47
- },
48
- badge.page
49
- )),
50
- headersFooters && overlay.pages.flatMap((page) => {
51
- if (page.crossed) return [];
37
+ headersFootersFor && overlay.pages.flatMap((page) => {
38
+ const headersFooters = page.crossed ? null : headersFootersFor(page);
39
+ if (headersFooters === null) return [];
52
40
  const number = displayPageNumber(headersFooters, page.page);
53
- const header = headerFooterText(
54
- headersFooters.headers,
55
- headersFooters,
56
- page.page,
57
- totalPages
58
- );
59
- const footer = headerFooterText(
60
- headersFooters.footers,
61
- headersFooters,
62
- page.page,
63
- totalPages
64
- );
65
- const headerAlign = headerFooterAlign(
41
+ const header = headerFooterOn(
66
42
  headersFooters.headers,
67
43
  headersFooters,
68
- page.page
44
+ page.pageInSection
69
45
  );
70
- const footerAlign = headerFooterAlign(
46
+ const footer = headerFooterOn(
71
47
  headersFooters.footers,
72
48
  headersFooters,
73
- page.page
49
+ page.pageInSection
74
50
  );
75
51
  return [
76
52
  header === null ? null : /* @__PURE__ */ jsx(
@@ -81,10 +57,10 @@ function PageGuides({
81
57
  top: `${page.headerTop}px`,
82
58
  left: `${page.left}px`,
83
59
  width: `${page.width}px`,
84
- textAlign: headerAlign ?? void 0
60
+ textAlign: header.align ?? void 0
85
61
  },
86
62
  title: `Page ${number} header`,
87
- children: header
63
+ children: headerFooterText(header.story, number, totalPages)
88
64
  },
89
65
  `header-${page.page}`
90
66
  ),
@@ -96,10 +72,10 @@ function PageGuides({
96
72
  top: `${page.footerTop}px`,
97
73
  left: `${page.left}px`,
98
74
  width: `${page.width}px`,
99
- textAlign: footerAlign ?? void 0
75
+ textAlign: footer.align ?? void 0
100
76
  },
101
77
  title: `Page ${number} footer`,
102
- children: footer
78
+ children: headerFooterText(footer.story, number, totalPages)
103
79
  },
104
80
  `footer-${page.page}`
105
81
  )
@@ -7,9 +7,17 @@
7
7
  * the places where Word actually breaks may differ.
8
8
  *
9
9
  * The size of a page is the document's own (`docx/pageGeometry`), not a fixed A4: a Letter
10
- * document breaks at Letter's height and fits its tables to Letter's width.
10
+ * document breaks at Letter's height and fits its tables to Letter's width. A document written in
11
+ * several sections breaks each block at the height of the section that block sits in
12
+ * (`docx/sections`), and a section opens a page of its own unless it declares `continuous`, which
13
+ * carries it on down the page the section before it ends on.
14
+ *
15
+ * The width is the exception: one sheet is drawn at one width (`styles/editor.css`), which is the
16
+ * first section's, so a section on wider paper is paginated on its own height but drawn on the
17
+ * paper the sheet holds.
11
18
  */
12
19
  import { type PageGeometry } from "../docx/pageGeometry";
20
+ import type { DocumentSection, SectionProperties } from "../docx/sections";
13
21
  import type { MeasuredBlock, PageCut } from "./blockKinds";
14
22
  export type { MeasuredBlock };
15
23
  /** The paper measured in the pixels the sheet is drawn with */
@@ -39,6 +47,35 @@ export declare const PAGE_SPLIT_PX = 24;
39
47
  export declare function pagePixels(geometry: PageGeometry): PagePixels;
40
48
  /** The paper a document that names none is drawn on */
41
49
  export declare const A4_PAGE_PIXELS: PagePixels;
50
+ /** The paper one section of a document is laid out on */
51
+ export interface SectionPixels {
52
+ /**
53
+ * The last position this section covers. A section is closed by the paragraph carrying its
54
+ * `w:sectPr` (§17.6.17), so the block after that one opens the section that follows, and the
55
+ * section the body itself closes reaches past every block.
56
+ */
57
+ untilPos: number;
58
+ pixels: PagePixels;
59
+ /**
60
+ * How this section starts (§17.6.22). `continuous` carries it on down the page the section
61
+ * before it ends on; every other kind opens a page.
62
+ */
63
+ type: SectionProperties["type"];
64
+ }
65
+ /** The one section a document naming none is laid out on */
66
+ export declare const A4_SECTION_PIXELS: readonly SectionPixels[];
67
+ /**
68
+ * The paper of every section of a document, in the pixels the sheet is drawn with.
69
+ *
70
+ * Reading the sections walks every block (`docx/sections`), so this runs once per document change
71
+ * rather than once per measurement.
72
+ */
73
+ export declare function sectionPixels(sections: readonly DocumentSection[]): SectionPixels[];
74
+ /**
75
+ * The paper the block at this position is laid out on, which is the paper of every page that
76
+ * block stands on. A4 where the list names no section at all.
77
+ */
78
+ export declare function sectionPaperAt(sections: readonly SectionPixels[], pos: number): PagePixels;
42
79
  /**
43
80
  * The paper the document names, as the CSS variables the sheet is drawn from.
44
81
  *
@@ -63,11 +100,6 @@ export interface PageSplit {
63
100
  page: number;
64
101
  /** Whether the document records a break at this place */
65
102
  forced: boolean;
66
- /**
67
- * Whether every place before this one was recorded by the document, so the number can
68
- * be trusted
69
- */
70
- exactPage: boolean;
71
103
  /**
72
104
  * Whether the block is taller than one page, so it cannot be pushed and the text
73
105
  * crosses this place
@@ -80,10 +112,16 @@ export interface PageStart {
80
112
  /** Where this page's body starts, measured from the top of the body */
81
113
  bodyStart: number;
82
114
  /**
83
- * Whether every place before this one was recorded by the document, so the number can
84
- * be trusted
115
+ * The position of the block this page opens with, which is the block the text crossing into it
116
+ * belongs to. 0 for a document holding no block at all
117
+ */
118
+ pos: number;
119
+ /**
120
+ * The place this page takes within its own section, counted from 1 again at every section it
121
+ * opens. A document read with no section table at all is one section, so this counts the whole
122
+ * of it
85
123
  */
86
- exactPage: boolean;
124
+ pageInSection: number;
87
125
  /**
88
126
  * Whether text crosses over from the previous page, so this page continues with no top
89
127
  * margin
@@ -100,12 +138,21 @@ export interface PageLayout {
100
138
  pages: PageStart[];
101
139
  /** The body height with the last page filled out in full */
102
140
  bodyHeight: number;
141
+ /**
142
+ * The margin under that last page, which is its own section's. The sheet is padded from the
143
+ * first section's paper alone (`editor/createEditor`), so a document ending on a deeper bottom
144
+ * margin needs the sheet drawn down to this.
145
+ */
146
+ marginBottom: number;
103
147
  }
104
148
  export interface PageLayoutInput {
105
149
  blocks: readonly MeasuredBlock[];
106
- pageBodyHeight: number;
107
- /** From the end of the previous page's body to the top of the next page's body */
108
- pageStep: number;
150
+ /**
151
+ * The paper of each section in document order (`sectionPixels`). A block is laid out on the
152
+ * paper of the section it sits in, and the section a block opens starts a page of its own
153
+ * unless that section is `continuous`.
154
+ */
155
+ sections: readonly SectionPixels[];
109
156
  }
110
157
  /**
111
158
  * Distributes the blocks across pages.
@@ -120,5 +167,7 @@ export interface PageLayoutInput {
120
167
  * a row boundary in a table - is taken only where the piece after it would run off the page.
121
168
  * A block kept with the next one is pushed together with what it is kept with: the blocks kept
122
169
  * after it and the first piece of the block the keeps end at.
170
+ * Each block is measured against the paper of its own section, and the first block of a section
171
+ * opens a page of its own unless the section it opens declares `continuous`.
123
172
  */
124
- export declare function pageLayout({ blocks, pageBodyHeight, pageStep, }: PageLayoutInput): PageLayout;
173
+ export declare function pageLayout({ blocks, sections }: PageLayoutInput): PageLayout;
@@ -24,6 +24,23 @@ function pagePixels(geometry) {
24
24
  };
25
25
  }
26
26
  var A4_PAGE_PIXELS = pagePixels(A4_PORTRAIT);
27
+ var A4_SECTION_PIXELS = [
28
+ { untilPos: Number.POSITIVE_INFINITY, pixels: A4_PAGE_PIXELS, type: null }
29
+ ];
30
+ function sectionPixels(sections) {
31
+ return sections.map(({ anchor, props }) => ({
32
+ untilPos: anchor.kind === "paragraph" ? anchor.pos : Number.POSITIVE_INFINITY,
33
+ pixels: pagePixels(props.geometry),
34
+ type: props.type
35
+ }));
36
+ }
37
+ function sectionIndexAt(sections, pos) {
38
+ const reaching = sections.findIndex((section) => pos <= section.untilPos);
39
+ return reaching === -1 ? sections.length - 1 : reaching;
40
+ }
41
+ function sectionPaperAt(sections, pos) {
42
+ return sections[sectionIndexAt(sections, pos)]?.pixels ?? A4_PAGE_PIXELS;
43
+ }
27
44
  function px(value) {
28
45
  return `${Math.round(value * 100) / 100}px`;
29
46
  }
@@ -41,15 +58,29 @@ var TOLERANCE_PX = 0.5;
41
58
  function round(value) {
42
59
  return Math.round(value * 100) / 100;
43
60
  }
44
- function keepsWithNext(block, next) {
45
- return block.keepWithNext && block.candidates.length === 0 && !block.breakAfter && !next.breakBefore;
61
+ function withinSections(pages, sections) {
62
+ let section = null;
63
+ let within = 0;
64
+ return pages.map((page) => {
65
+ const index = sectionIndexAt(sections, page.pos);
66
+ within = index === section ? within + 1 : 1;
67
+ section = index;
68
+ return { ...page, pageInSection: within };
69
+ });
46
70
  }
47
- function keptExtents(blocks, fitsPage) {
71
+ function keepsWithNext(block, next, opensPage) {
72
+ return block.keepWithNext && !opensPage && block.candidates.length === 0 && !block.breakAfter && !next.breakBefore;
73
+ }
74
+ function keptExtents(blocks, laid) {
75
+ const fitsPage = (extent, index) => {
76
+ const height = laid[index]?.paper.bodyHeight ?? 0;
77
+ return extent <= height + TOLERANCE_PX;
78
+ };
48
79
  const extents = /* @__PURE__ */ new Map();
49
80
  let run = [];
50
81
  const settle = () => {
51
82
  const head = run.at(-1);
52
- if (head && fitsPage(head.extent)) {
83
+ if (head && fitsPage(head.extent, head.index)) {
53
84
  for (const { index, extent } of run) {
54
85
  extents.set(index, extent);
55
86
  }
@@ -59,7 +90,8 @@ function keptExtents(blocks, fitsPage) {
59
90
  for (let index = blocks.length - 2; index >= 0; index -= 1) {
60
91
  const block = blocks[index];
61
92
  const next = blocks[index + 1];
62
- if (block && next && keepsWithNext(block, next)) {
93
+ const opensPage = laid[index + 1]?.opensPage === true;
94
+ if (block && next && keepsWithNext(block, next, opensPage)) {
63
95
  const below = run.at(-1)?.extent ?? next.minFirstPiece;
64
96
  run.push({ index, extent: block.height + next.gap + below });
65
97
  } else {
@@ -69,51 +101,67 @@ function keptExtents(blocks, fitsPage) {
69
101
  settle();
70
102
  return extents;
71
103
  }
72
- function pageLayout({
73
- blocks,
74
- pageBodyHeight,
75
- pageStep
76
- }) {
104
+ function pageLayout({ blocks, sections }) {
77
105
  const pushes = [];
78
106
  const cuts = [];
79
107
  const splits = [];
80
- const firstPage = {
81
- page: 1,
82
- bodyStart: 0,
83
- exactPage: true,
84
- crossed: false
85
- };
86
- const pages = [firstPage];
87
- if (!(pageBodyHeight > 0)) {
88
- return { pushes, cuts, splits, pages, bodyHeight: 0 };
108
+ let blockPos = blocks[0]?.pos ?? 0;
109
+ const opened = [
110
+ { page: 1, bodyStart: 0, pos: blockPos, crossed: false }
111
+ ];
112
+ const roomy = sections.find(
113
+ (section) => section.pixels.bodyHeight > 0
114
+ )?.pixels;
115
+ if (roomy === void 0) {
116
+ return {
117
+ pushes,
118
+ cuts,
119
+ splits,
120
+ pages: withinSections(opened, sections),
121
+ bodyHeight: 0,
122
+ marginBottom: 0
123
+ };
89
124
  }
125
+ const blockSections = blocks.map(
126
+ (block) => sectionIndexAt(sections, block.pos)
127
+ );
128
+ const laid = blockSections.map((section, index) => {
129
+ const pixels = sections[section]?.pixels;
130
+ return {
131
+ paper: pixels && pixels.bodyHeight > 0 ? pixels : roomy,
132
+ opensPage: index > 0 && section !== blockSections[index - 1] && sections[section]?.type !== "continuous"
133
+ };
134
+ });
90
135
  let pageStart = 0;
91
136
  let cursor = 0;
92
- let exactPage = true;
93
- const split = (y, crossed, forced) => {
94
- if (!forced) exactPage = false;
137
+ let paper = laid[0]?.paper ?? roomy;
138
+ const stepTo = (opening) => paper.pageStep - paper.marginTop + opening.marginTop;
139
+ const split = (y, crossed, forced, opening = paper) => {
95
140
  const page = splits.length + 2;
96
- splits.push({ y: round(y), page, forced, exactPage, crossed });
97
- pageStart = crossed ? y : y + pageStep;
98
- pages.push({ page, bodyStart: round(pageStart), exactPage, crossed });
141
+ splits.push({ y: round(y), page, forced, crossed });
142
+ pageStart = crossed ? y : y + stepTo(opening);
143
+ opened.push({ page, bodyStart: round(pageStart), pos: blockPos, crossed });
99
144
  };
100
145
  const crossTo = (y) => {
101
- while (y > pageStart + pageBodyHeight + TOLERANCE_PX) {
102
- split(pageStart + pageBodyHeight, true, false);
146
+ while (y > pageStart + paper.bodyHeight + TOLERANCE_PX) {
147
+ split(pageStart + paper.bodyHeight, true, false);
103
148
  }
104
149
  };
105
- const fitsPage = (extent) => extent <= pageBodyHeight + TOLERANCE_PX;
106
- const kept = keptExtents(blocks, fitsPage);
150
+ const kept = keptExtents(blocks, laid);
107
151
  let breakAfterPrevious = false;
108
152
  for (const [index, block] of blocks.entries()) {
109
- const pageEnd = pageStart + pageBodyHeight;
153
+ blockPos = block.pos;
154
+ const opening = laid[index]?.paper ?? paper;
155
+ const startsSection = laid[index]?.opensPage === true;
156
+ const pageEnd = pageStart + paper.bodyHeight;
110
157
  const top = cursor + block.gap;
111
- const startsPage = (block.breakBefore || breakAfterPrevious) && top > pageStart + TOLERANCE_PX;
158
+ const startsPage = (block.breakBefore || breakAfterPrevious || startsSection) && top > pageStart + TOLERANCE_PX;
112
159
  const first = kept.get(index) ?? block.minFirstPiece;
113
160
  const overflows = top + first > pageEnd + TOLERANCE_PX;
114
- const fits = fitsPage(first);
115
- const push = startsPage || overflows && fits ? Math.max(0, pageEnd + pageStep - top) : 0;
116
- if (push > 0 || startsPage) split(pageEnd, false, startsPage);
161
+ const fits = first <= opening.bodyHeight + TOLERANCE_PX;
162
+ const push = startsPage || overflows && fits ? Math.max(0, pageEnd + stepTo(opening) - top) : 0;
163
+ if (push > 0 || startsPage) split(pageEnd, false, startsPage, opening);
164
+ paper = opening;
117
165
  if (push > 0) {
118
166
  pushes.push({
119
167
  pos: block.pos,
@@ -122,6 +170,7 @@ function pageLayout({
122
170
  });
123
171
  }
124
172
  const contentTop = top + push;
173
+ const pageBodyHeight = paper.bodyHeight;
125
174
  let added = 0;
126
175
  let pieceStart = 0;
127
176
  for (let index2 = 0; index2 <= block.candidates.length; index2 += 1) {
@@ -151,14 +200,19 @@ function pageLayout({
151
200
  pushes,
152
201
  cuts,
153
202
  splits,
154
- pages,
155
- bodyHeight: round(pageStart + pageBodyHeight)
203
+ pages: withinSections(opened, sections),
204
+ // The last page is filled out in full on the paper of the section it opens with
205
+ bodyHeight: round(pageStart + paper.bodyHeight),
206
+ marginBottom: paper.marginBottom
156
207
  };
157
208
  }
158
209
  export {
159
210
  A4_PAGE_PIXELS,
211
+ A4_SECTION_PIXELS,
160
212
  PAGE_SPLIT_PX,
161
213
  pageGeometryStyle,
162
214
  pageLayout,
163
- pagePixels
215
+ pagePixels,
216
+ sectionPaperAt,
217
+ sectionPixels
164
218
  };