@portone/docx-editor 0.2.1 → 0.3.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 (225) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/CONTRIBUTING.md +5 -1
  3. package/dist/DocxEditor.d.ts +6 -0
  4. package/dist/DocxEditor.js +12 -29
  5. package/dist/core.d.ts +23 -8
  6. package/dist/core.js +9 -4
  7. package/dist/docx/cloning.d.ts +38 -0
  8. package/dist/docx/cloning.js +74 -0
  9. package/dist/docx/commentOnlyChange.d.ts +12 -7
  10. package/dist/docx/commentOnlyChange.js +9 -156
  11. package/dist/docx/comments/constants.d.ts +0 -1
  12. package/dist/docx/comments/constants.js +0 -2
  13. package/dist/docx/comments/grammar.d.ts +10 -4
  14. package/dist/docx/comments/grammar.js +12 -2
  15. package/dist/docx/comments/{verifying.d.ts → parts.d.ts} +26 -18
  16. package/dist/docx/comments/parts.js +279 -0
  17. package/dist/docx/comments/people.d.ts +11 -2
  18. package/dist/docx/comments/people.js +16 -92
  19. package/dist/docx/comments/policy.d.ts +7 -0
  20. package/dist/docx/comments/policy.js +43 -0
  21. package/dist/docx/comments/reading.js +8 -10
  22. package/dist/docx/comments/writing.d.ts +19 -7
  23. package/dist/docx/comments/writing.js +52 -111
  24. package/dist/docx/documentSettings.d.ts +7 -0
  25. package/dist/docx/documentSettings.js +10 -2
  26. package/dist/docx/exportDocx.d.ts +40 -5
  27. package/dist/docx/exportDocx.js +69 -71
  28. package/dist/docx/exportRefs.d.ts +5 -2
  29. package/dist/docx/exportRefs.js +3 -1
  30. package/dist/docx/fidelity.d.ts +43 -0
  31. package/dist/docx/fidelity.js +64 -0
  32. package/dist/docx/formatting/attrs.d.ts +27 -0
  33. package/dist/docx/formatting/attrs.js +31 -0
  34. package/dist/docx/formatting/context.d.ts +31 -0
  35. package/dist/docx/formatting/context.js +42 -0
  36. package/dist/docx/formatting/direct.d.ts +6 -5
  37. package/dist/docx/formatting/direct.js +52 -78
  38. package/dist/docx/formatting/resolve.d.ts +45 -0
  39. package/dist/docx/formatting/resolve.js +140 -0
  40. package/dist/docx/formatting/runProperties.d.ts +93 -0
  41. package/dist/docx/formatting/runProperties.js +316 -0
  42. package/dist/docx/formatting/styles.js +3 -3
  43. package/dist/docx/formatting/tabStops.js +7 -22
  44. package/dist/docx/formatting.d.ts +4 -1
  45. package/dist/docx/formatting.js +4 -1
  46. package/dist/docx/headersFooters.js +6 -13
  47. package/dist/docx/identities.d.ts +67 -0
  48. package/dist/docx/identities.js +174 -0
  49. package/dist/docx/importDocx.d.ts +12 -1
  50. package/dist/docx/importDocx.js +72 -79
  51. package/dist/docx/importParagraph.d.ts +1 -1
  52. package/dist/docx/importParagraph.js +4 -3
  53. package/dist/docx/importTable.d.ts +1 -1
  54. package/dist/docx/importTable.js +17 -1
  55. package/dist/docx/invariants.d.ts +33 -0
  56. package/dist/docx/invariants.js +256 -0
  57. package/dist/docx/media.d.ts +6 -4
  58. package/dist/docx/media.js +6 -37
  59. package/dist/docx/newLists.d.ts +20 -0
  60. package/dist/docx/newLists.js +36 -0
  61. package/dist/docx/notes.js +3 -7
  62. package/dist/docx/numberingPlanner.d.ts +8 -0
  63. package/dist/docx/numberingPlanner.js +19 -0
  64. package/dist/docx/packageParts.d.ts +42 -0
  65. package/dist/docx/packageParts.js +135 -0
  66. package/dist/docx/pageGeometry.d.ts +2 -0
  67. package/dist/docx/pageGeometry.js +18 -9
  68. package/dist/docx/paraProps.d.ts +10 -22
  69. package/dist/docx/paraProps.js +47 -76
  70. package/dist/docx/partPlan.d.ts +36 -0
  71. package/dist/docx/partPlan.js +59 -0
  72. package/dist/docx/protectionPolicy.d.ts +127 -0
  73. package/dist/docx/protectionPolicy.js +169 -0
  74. package/dist/docx/relationships.d.ts +1 -1
  75. package/dist/docx/relationships.js +8 -13
  76. package/dist/docx/runProps.d.ts +9 -22
  77. package/dist/docx/runProps.js +15 -168
  78. package/dist/docx/scan.js +20 -52
  79. package/dist/docx/sdt.js +9 -34
  80. package/dist/docx/sdtProps.d.ts +8 -1
  81. package/dist/docx/sdtProps.js +10 -0
  82. package/dist/docx/serializeBlock.d.ts +2 -0
  83. package/dist/docx/serializeBlock.js +8 -5
  84. package/dist/docx/serializeTable.js +32 -15
  85. package/dist/docx/session.d.ts +42 -14
  86. package/dist/docx/session.js +43 -17
  87. package/dist/docx/tableFormatting/editing.js +108 -139
  88. package/dist/docx/tableFormatting/reading.d.ts +16 -6
  89. package/dist/docx/tableFormatting/reading.js +40 -20
  90. package/dist/docx/tableTemplate.js +18 -7
  91. package/dist/download.d.ts +8 -5
  92. package/dist/download.js +2 -0
  93. package/dist/editor/clipboard/inlineFormatting.js +19 -30
  94. package/dist/editor/commands/comments/editing.d.ts +6 -2
  95. package/dist/editor/commands/comments/editing.js +19 -31
  96. package/dist/editor/commands/exportQueries.d.ts +15 -0
  97. package/dist/editor/commands/exportQueries.js +14 -0
  98. package/dist/editor/commands/fidelityQueries.d.ts +12 -0
  99. package/dist/editor/commands/fidelityQueries.js +8 -0
  100. package/dist/editor/commands/formatting/editing.d.ts +2 -2
  101. package/dist/editor/commands/formatting/editing.js +26 -108
  102. package/dist/editor/commands/formatting/propertyCommands.d.ts +21 -0
  103. package/dist/editor/commands/formatting/propertyCommands.js +103 -0
  104. package/dist/editor/commands/formatting/shared.d.ts +3 -3
  105. package/dist/editor/commands/formatting/shared.js +5 -2
  106. package/dist/editor/commands/indentCommands.js +5 -4
  107. package/dist/editor/commands/index.d.ts +11 -0
  108. package/dist/editor/commands/index.js +5 -0
  109. package/dist/editor/commands/linkCommands.js +5 -6
  110. package/dist/editor/commands/listCommands.js +8 -10
  111. package/dist/editor/commands/lockCommands.d.ts +7 -1
  112. package/dist/editor/commands/lockCommands.js +35 -46
  113. package/dist/editor/commands/paragraphCommands.js +28 -31
  114. package/dist/editor/commands/spacingCommands.js +1 -1
  115. package/dist/editor/createEditor.d.ts +17 -29
  116. package/dist/editor/createEditor.js +58 -58
  117. package/dist/editor/documentStyles.d.ts +11 -35
  118. package/dist/editor/documentStyles.js +9 -52
  119. package/dist/editor/editorDocument.d.ts +56 -0
  120. package/dist/editor/editorDocument.js +74 -0
  121. package/dist/editor/externalClipboard.js +16 -45
  122. package/dist/editor/insertTable.js +4 -3
  123. package/dist/editor/paragraphEdits.d.ts +11 -18
  124. package/dist/editor/paragraphEdits.js +7 -16
  125. package/dist/editor/plugins/displayDerivation.d.ts +47 -0
  126. package/dist/editor/plugins/displayDerivation.js +78 -0
  127. package/dist/editor/plugins/imagePaste.js +4 -3
  128. package/dist/editor/plugins/keymap.js +18 -3
  129. package/dist/editor/plugins/numberingDecorations.d.ts +5 -11
  130. package/dist/editor/plugins/numberingDecorations.js +7 -17
  131. package/dist/editor/plugins/paragraphDisplay.d.ts +7 -0
  132. package/dist/editor/plugins/paragraphDisplay.js +51 -0
  133. package/dist/editor/plugins/tabLayout.js +1 -1
  134. package/dist/editor/plugins/tableDisplay.d.ts +6 -0
  135. package/dist/editor/plugins/tableDisplay.js +16 -0
  136. package/dist/index.d.ts +2 -0
  137. package/dist/model/format.d.ts +30 -8
  138. package/dist/model/format.js +35 -22
  139. package/dist/model/tabStops.d.ts +9 -0
  140. package/dist/model/tabStops.js +18 -0
  141. package/dist/numbering/listTemplate.js +24 -7
  142. package/dist/numbering/parseNumbering.d.ts +14 -1
  143. package/dist/numbering/parseNumbering.js +35 -22
  144. package/dist/numbering/writeNumbering.d.ts +3 -4
  145. package/dist/numbering/writeNumbering.js +14 -25
  146. package/dist/ooxml/childOrder.d.ts +34 -0
  147. package/dist/ooxml/childOrder.js +496 -0
  148. package/dist/ooxml/element.d.ts +24 -11
  149. package/dist/ooxml/element.js +31 -12
  150. package/dist/ooxml/errors.d.ts +3 -2
  151. package/dist/ooxml/image.d.ts +4 -0
  152. package/dist/ooxml/image.js +2 -5
  153. package/dist/ooxml/partSplice.d.ts +67 -0
  154. package/dist/ooxml/partSplice.js +169 -0
  155. package/dist/ooxml/props.d.ts +112 -0
  156. package/dist/{docx/propsXml.js → ooxml/props.js} +72 -200
  157. package/dist/ooxml/simpleTypes.d.ts +103 -0
  158. package/dist/ooxml/simpleTypes.js +182 -0
  159. package/dist/ooxml/tabStops.js +8 -27
  160. package/dist/ooxml/tagScan.d.ts +34 -0
  161. package/dist/ooxml/tagScan.js +108 -0
  162. package/dist/ooxml/units.d.ts +25 -13
  163. package/dist/ooxml/units.js +65 -31
  164. package/dist/ooxml/xml.d.ts +27 -6
  165. package/dist/ooxml/xml.js +37 -5
  166. package/dist/page/blockKinds.d.ts +61 -0
  167. package/dist/page/blockKinds.js +11 -0
  168. package/dist/page/kinds/index.d.ts +6 -0
  169. package/dist/page/kinds/index.js +10 -0
  170. package/dist/page/kinds/paragraphKind.d.ts +10 -0
  171. package/dist/page/kinds/paragraphKind.js +71 -0
  172. package/dist/page/kinds/tableKind.d.ts +10 -0
  173. package/dist/page/kinds/tableKind.js +178 -0
  174. package/dist/page/measureBlocks.d.ts +3 -14
  175. package/dist/page/measureBlocks.js +24 -45
  176. package/dist/page/pageDecorations.d.ts +27 -44
  177. package/dist/page/pageDecorations.js +70 -152
  178. package/dist/page/pageLayout.d.ts +2 -19
  179. package/dist/page/pageLayout.js +36 -6
  180. package/dist/page/usePageLayout.d.ts +2 -17
  181. package/dist/page/usePageLayout.js +3 -39
  182. package/dist/schema/attrRoles.d.ts +25 -18
  183. package/dist/schema/attrRoles.js +102 -67
  184. package/dist/schema/displayDerivation.d.ts +82 -0
  185. package/dist/schema/displayDerivation.js +130 -0
  186. package/dist/schema/docxSchema.d.ts +3 -0
  187. package/dist/schema/docxSchema.js +26 -14
  188. package/dist/schema/editGuard.d.ts +1 -1
  189. package/dist/schema/guards.d.ts +37 -14
  190. package/dist/schema/guards.js +29 -3
  191. package/dist/schema/index.d.ts +2 -0
  192. package/dist/schema/index.js +2 -0
  193. package/dist/schema/locks.d.ts +11 -44
  194. package/dist/schema/locks.js +0 -9
  195. package/dist/schema/preservedGuards.d.ts +26 -6
  196. package/dist/schema/preservedGuards.js +31 -1
  197. package/dist/schema/protection.d.ts +7 -1
  198. package/dist/schema/protection.js +2 -1
  199. package/dist/schema/sourceEquality.d.ts +1 -9
  200. package/dist/schema/sourceEquality.js +1 -27
  201. package/dist/styles/inlineStyle.js +15 -6
  202. package/dist/table/cellFormatting.d.ts +8 -1
  203. package/dist/table/cellFormatting.js +10 -18
  204. package/dist/table/commands.js +20 -18
  205. package/dist/table/format.d.ts +3 -0
  206. package/dist/table/format.js +3 -9
  207. package/dist/table/gridBorders.d.ts +14 -16
  208. package/dist/table/gridBorders.js +3 -41
  209. package/dist/table/merge.d.ts +2 -6
  210. package/dist/table/merge.js +5 -5
  211. package/package.json +7 -3
  212. package/dist/docx/comments/contentTypes.d.ts +0 -7
  213. package/dist/docx/comments/contentTypes.js +0 -38
  214. package/dist/docx/comments/verifying.js +0 -206
  215. package/dist/docx/formatting/effectiveParagraph.d.ts +0 -15
  216. package/dist/docx/formatting/effectiveParagraph.js +0 -81
  217. package/dist/docx/propsXml.d.ts +0 -67
  218. package/dist/docx/uniqueControls.d.ts +0 -14
  219. package/dist/docx/uniqueControls.js +0 -62
  220. package/dist/editor/plugins/commentReservations.d.ts +0 -5
  221. package/dist/editor/plugins/commentReservations.js +0 -26
  222. package/dist/editor/plugins/styledParagraphs.d.ts +0 -15
  223. package/dist/editor/plugins/styledParagraphs.js +0 -65
  224. package/dist/page/tableMeasurements.d.ts +0 -18
  225. package/dist/page/tableMeasurements.js +0 -119
@@ -1,11 +1,21 @@
1
1
  // src/docx/sdtProps.ts
2
+ import { elementXml, openTagXml } from "../ooxml/element.js";
3
+ import { wName } from "../ooxml/names.js";
2
4
  import { editSdtPrefix } from "./sdt.js";
3
5
  var LOCK_BOTH_CLAUSES = '<w:lock w:val="sdtContentLocked"/>';
6
+ function lockedControlPrefix(id) {
7
+ return openTagXml(wName("sdt"), null) + elementXml(
8
+ wName("sdtPr"),
9
+ [],
10
+ [elementXml(wName("id"), [[wName("val"), `${id}`]]), LOCK_BOTH_CLAUSES]
11
+ );
12
+ }
4
13
  function withContentLock(sdtPrefix, locked) {
5
14
  return editSdtPrefix(sdtPrefix, [
6
15
  ["lock", locked ? LOCK_BOTH_CLAUSES : null]
7
16
  ]);
8
17
  }
9
18
  export {
19
+ lockedControlPrefix,
10
20
  withContentLock
11
21
  };
@@ -1,4 +1,6 @@
1
1
  import type { Node as PMNode } from "prosemirror-model";
2
2
  import { type ExportRefs } from "./exportRefs";
3
3
  import { type SessionStore } from "./session";
4
+ /** Why there is no original to write, which a block that came in from another document answers differently */
5
+ export declare function lostOriginal(node: PMNode, session: SessionStore): string;
4
6
  export declare function serializeBlock(node: PMNode, session: SessionStore, refs?: ExportRefs): string;
@@ -3,14 +3,16 @@ import { DocxExportError } from "../ooxml/errors.js";
3
3
  import { NO_EXPORT_REFS } from "./exportRefs.js";
4
4
  import { preservedXml, serializeParagraph } from "./serializeParagraph.js";
5
5
  import { serializeTable } from "./serializeTable.js";
6
- import { originalBlock } from "./session.js";
6
+ import { originalBlock, splitBlockKey } from "./session.js";
7
+ function lostOriginal(node, session) {
8
+ const srcId = node.attrs.srcId;
9
+ const key = typeof srcId === "string" ? splitBlockKey(srcId) : null;
10
+ return key === null || key.sessionId === session.sessionId ? "a preserved block has lost its original XML" : `a preserved block comes from another document (${key.sessionId})`;
11
+ }
7
12
  function serializeRaw(node, session) {
8
13
  const imported = originalBlock(node, session);
9
14
  if (!imported) {
10
- throw new DocxExportError(
11
- "lost-original",
12
- "a preserved block has lost its original XML"
13
- );
15
+ throw new DocxExportError("lost-original", lostOriginal(node, session));
14
16
  }
15
17
  return imported.xml;
16
18
  }
@@ -25,5 +27,6 @@ function serializeBlock(node, session, refs = NO_EXPORT_REFS) {
25
27
  );
26
28
  }
27
29
  export {
30
+ lostOriginal,
28
31
  serializeBlock
29
32
  };
@@ -8,16 +8,15 @@ import {
8
8
  import { elementXml, openTagXml } from "../ooxml/element.js";
9
9
  import { DocxExportError } from "../ooxml/errors.js";
10
10
  import { wName } from "../ooxml/names.js";
11
- import { NO_EXPORT_REFS } from "./exportRefs.js";
12
11
  import {
13
12
  innerXml,
14
13
  parseProps,
15
14
  propsChild,
16
15
  renderProps,
17
- setPropsChild,
18
- TBL_PR_ORDER,
19
- TC_PR_ORDER
20
- } from "./propsXml.js";
16
+ setChild
17
+ } from "../ooxml/props.js";
18
+ import { ST_MeasurementOrPercent, ST_TwipsMeasure } from "../ooxml/simpleTypes.js";
19
+ import { NO_EXPORT_REFS } from "./exportRefs.js";
21
20
  import {
22
21
  preservedXml,
23
22
  rawAttrsOf,
@@ -35,31 +34,49 @@ function modelled(name, attrs, replacing) {
35
34
  const inner = replacing === void 0 ? "" : innerXml(replacing);
36
35
  return elementXml(wName(name), attrs, inner === "" ? [] : [inner]);
37
36
  }
37
+ function widthText(width) {
38
+ const value = widthNumber(width);
39
+ if (value === null || !Number.isFinite(value)) return "0";
40
+ return ST_MeasurementOrPercent.format({
41
+ kind: "number",
42
+ value: Math.round(value)
43
+ }) ?? "0";
44
+ }
38
45
  function widthXml(name, width, replacing) {
39
46
  return modelled(
40
47
  name,
41
48
  [
42
- [wName("w"), `${widthNumber(width) ?? 0}`],
49
+ [wName("w"), widthText(width)],
43
50
  [wName("type"), width.type]
44
51
  ],
45
52
  replacing
46
53
  );
47
54
  }
48
- function withWidth(props, name, width, order) {
55
+ function withWidth(props, name, width) {
49
56
  if (!width) return props;
50
57
  const replacing = propsChild(props.children, name)?.xml;
51
- return setPropsChild(props, name, widthXml(name, width, replacing), order);
58
+ return setChild(props, name, widthXml(name, width, replacing));
52
59
  }
53
60
  function tablePropsXml(table) {
54
61
  const props = propsOf(table.attrs.tblPr, "w:tblPr");
55
62
  const width = toTableWidth(table.attrs.tblW);
56
- return renderProps(withWidth(props, "tblW", width, TBL_PR_ORDER));
63
+ return renderProps(withWidth(props, "tblW", width)) || elementXml(wName("tblPr"), []);
57
64
  }
58
65
  function tableGridXml(table) {
59
66
  const gridCols = toGridCols(table.attrs.gridCols);
60
- if (gridCols.length === 0) return "";
61
- const cols = gridCols.map((w) => elementXml(wName("gridCol"), [[wName("w"), `${w}`]])).join("");
62
- return elementXml(wName("tblGrid"), [], [cols]);
67
+ const gridChange = typeof table.attrs.gridChange === "string" ? table.attrs.gridChange : "";
68
+ if (gridCols.length === 0 && gridChange === "") return "";
69
+ const cols = gridCols.map((w) => {
70
+ const width = ST_TwipsMeasure.format(Math.round(w));
71
+ if (width === null) {
72
+ throw new DocxExportError(
73
+ "invalid-table",
74
+ "a grid column has no writable width"
75
+ );
76
+ }
77
+ return elementXml(wName("gridCol"), [[wName("w"), width]]);
78
+ }).join("");
79
+ return elementXml(wName("tblGrid"), [], [cols + gridChange]);
63
80
  }
64
81
  function cellPropsXml(cell, role) {
65
82
  const props = propsOf(cell.attrs.tcPr, "w:tcPr");
@@ -75,10 +92,10 @@ function cellPropsXml(cell, role) {
75
92
  [[wName("val"), "restart"]],
76
93
  propsChild(props.children, "vMerge")?.xml
77
94
  ) : null;
78
- const spanned = setPropsChild(props, "gridSpan", gridSpan, TC_PR_ORDER);
79
- const merged = setPropsChild(spanned, "vMerge", vMerge, TC_PR_ORDER);
95
+ const spanned = setChild(props, "gridSpan", gridSpan);
96
+ const merged = setChild(spanned, "vMerge", vMerge);
80
97
  const width = toTableWidth(cell.attrs.tcW);
81
- return renderProps(withWidth(merged, "tcW", width, TC_PR_ORDER));
98
+ return renderProps(withWidth(merged, "tcW", width));
82
99
  }
83
100
  function cellBlockXml(block, refs) {
84
101
  if (block.type.name === "paragraph") return serializeParagraph(block, refs);
@@ -9,9 +9,9 @@
9
9
  */
10
10
  import type { Node as PMNode } from "prosemirror-model";
11
11
  import type { DocumentDefaults } from "../model/format";
12
- import { type Numbering } from "../numbering/parseNumbering";
12
+ import { type Numbering, type NumberingOptions } from "../numbering/parseNumbering";
13
13
  import type { ImportedComments } from "./comments";
14
- import type { ParagraphFormatLayer, ParagraphStyleOption, StyleTable } from "./formatting";
14
+ import type { FormattingContext, ParagraphStyleOption } from "./formatting";
15
15
  import type { HeadersFooters } from "./headersFooters";
16
16
  import type { PageGeometry } from "./pageGeometry";
17
17
  export interface ImportedBlock {
@@ -28,9 +28,23 @@ export interface ImportedBlock {
28
28
  export interface DocxSession {
29
29
  readonly kind: "docxSession";
30
30
  }
31
+ /** What names one open document, which a block key is written against */
32
+ export interface SessionIdentity {
33
+ readonly sessionId: string;
34
+ }
35
+ /**
36
+ * A name for one open document.
37
+ *
38
+ * Opening the same bytes twice opens two documents, so this is drawn fresh each time rather than
39
+ * worked out from what was opened. It carries no `:`, which is what lets a block key be read back
40
+ * however many colons the story it names holds.
41
+ */
42
+ export declare function newSessionId(): string;
31
43
  /** What the package's own layers read the open document off */
32
- export declare class SessionStore implements DocxSession {
44
+ export declare class SessionStore implements DocxSession, SessionIdentity {
33
45
  readonly kind = "docxSession";
46
+ /** Which open document this is. Every block key of it names it, so a block of another document is not taken for one of this one */
47
+ readonly sessionId: string;
34
48
  readonly parts: Map<string, Uint8Array>;
35
49
  readonly mainPartPath: string;
36
50
  readonly documentPrefix: string;
@@ -41,19 +55,12 @@ export declare class SessionStore implements DocxSession {
41
55
  readonly defaults: DocumentDefaults;
42
56
  /** The effective automatic tab interval read from settings.xml. Used for display only. */
43
57
  readonly defaultTabStopPt: number;
44
- /** The paragraph properties at the base of the OOXML formatting hierarchy. */
45
- readonly paragraphDefaults: ParagraphFormatLayer;
46
58
  /** The paper this document is written on, read from the first section. Used for display only: the `w:sectPr` itself goes back out in the preserved tail */
47
59
  readonly geometry: PageGeometry;
48
- /** The style chain from styles.xml. Used only to fold the style a paragraph points at into its displayed values */
49
- readonly styles: StyleTable;
60
+ /** Everything the display values of a paragraph or a run are resolved against: the style chain, the defaults, the list definitions */
61
+ readonly formatting: FormattingContext;
50
62
  /** The paragraph styles this document defines, in the order styles.xml lists them. What the style picker offers */
51
63
  readonly paragraphStyles: ParagraphStyleOption[];
52
- /**
53
- * The paragraph style every paragraph with no `w:pStyle` of its own wears (`w:default="1"`,
54
- * usually Normal). null when the document marks none
55
- */
56
- readonly defaultParagraphStyleId: string | null;
57
64
  /** The raw text of numbering.xml. Kept around so list numbers can be drawn on screen. null if there is none */
58
65
  readonly numberingXml: string | null;
59
66
  /** Where numbering.xml sits inside the zip. When a new list is exported it is rewritten at that spot. null if there is none */
@@ -76,6 +83,27 @@ export declare function sessionOf(session: DocxSession): SessionStore;
76
83
  /** Where the body part sits inside the package, which is the one part an export rewrites */
77
84
  export declare function documentPartPath(session: DocxSession): string;
78
85
  /** The list definitions the document carries, which is what a paragraph's numbering points into */
79
- export declare function documentNumbering(session: DocxSession): Numbering;
80
- /** Finds which original block a node came from. undefined for a node newly created during editing */
86
+ export declare function documentNumbering(session: DocxSession, options?: NumberingOptions): Numbering;
87
+ /** The story a block key names when the block stood in the main document body */
88
+ export declare const BODY_STORY_KEY = "body";
89
+ /** A block key read back: which open document the block came from, which story of it, and where in that story */
90
+ export interface BlockKey {
91
+ sessionId: string;
92
+ storyKey: string;
93
+ index: number;
94
+ }
95
+ /** Where a block stands, as the one string a node carries on its `srcId` */
96
+ export declare function blockKey(session: SessionIdentity, storyKey: string, index: number): string;
97
+ /**
98
+ * The three parts of a block key, or null for a string that is not one.
99
+ *
100
+ * A story key holds colons of its own (`comment:4`), so the key is read from both ends rather
101
+ * than split: the first segment is the document, the last is the place in the story, and
102
+ * everything between them is the story itself.
103
+ */
104
+ export declare function splitBlockKey(key: string): BlockKey | null;
105
+ /**
106
+ * Finds which original block a node came from. undefined for a node newly created during editing,
107
+ * and for one that came in from another document, whose original is in that file and not this one.
108
+ */
81
109
  export declare function originalBlock(node: PMNode, session: SessionStore): ImportedBlock | undefined;
@@ -1,7 +1,16 @@
1
1
  // src/docx/session.ts
2
- import { parseNumbering } from "../numbering/parseNumbering.js";
2
+ import {
3
+ parseNumbering
4
+ } from "../numbering/parseNumbering.js";
5
+ var sessionsOpened = 0;
6
+ function newSessionId() {
7
+ sessionsOpened += 1;
8
+ return `d${sessionsOpened}-${Math.random().toString(36).slice(2, 10)}`;
9
+ }
3
10
  var SessionStore = class {
4
11
  kind = "docxSession";
12
+ /** Which open document this is. Every block key of it names it, so a block of another document is not taken for one of this one */
13
+ sessionId;
5
14
  parts;
6
15
  mainPartPath;
7
16
  documentPrefix;
@@ -12,19 +21,12 @@ var SessionStore = class {
12
21
  defaults;
13
22
  /** The effective automatic tab interval read from settings.xml. Used for display only. */
14
23
  defaultTabStopPt;
15
- /** The paragraph properties at the base of the OOXML formatting hierarchy. */
16
- paragraphDefaults;
17
24
  /** The paper this document is written on, read from the first section. Used for display only: the `w:sectPr` itself goes back out in the preserved tail */
18
25
  geometry;
19
- /** The style chain from styles.xml. Used only to fold the style a paragraph points at into its displayed values */
20
- styles;
26
+ /** Everything the display values of a paragraph or a run are resolved against: the style chain, the defaults, the list definitions */
27
+ formatting;
21
28
  /** The paragraph styles this document defines, in the order styles.xml lists them. What the style picker offers */
22
29
  paragraphStyles;
23
- /**
24
- * The paragraph style every paragraph with no `w:pStyle` of its own wears (`w:default="1"`,
25
- * usually Normal). null when the document marks none
26
- */
27
- defaultParagraphStyleId;
28
30
  /** The raw text of numbering.xml. Kept around so list numbers can be drawn on screen. null if there is none */
29
31
  numberingXml;
30
32
  /** Where numbering.xml sits inside the zip. When a new list is exported it is rewritten at that spot. null if there is none */
@@ -36,6 +38,7 @@ var SessionStore = class {
36
38
  /** First-section header and footer stories projected for the page preview. */
37
39
  headersFooters;
38
40
  constructor(opened) {
41
+ this.sessionId = opened.sessionId;
39
42
  this.parts = opened.parts;
40
43
  this.mainPartPath = opened.mainPartPath;
41
44
  this.documentPrefix = opened.documentPrefix;
@@ -44,11 +47,9 @@ var SessionStore = class {
44
47
  this.blocks = opened.blocks;
45
48
  this.defaults = opened.defaults;
46
49
  this.defaultTabStopPt = opened.defaultTabStopPt;
47
- this.paragraphDefaults = opened.paragraphDefaults;
48
50
  this.geometry = opened.geometry;
49
- this.styles = opened.styles;
51
+ this.formatting = opened.formatting;
50
52
  this.paragraphStyles = opened.paragraphStyles;
51
- this.defaultParagraphStyleId = opened.defaultParagraphStyleId;
52
53
  this.numberingXml = opened.numberingXml;
53
54
  this.numberingPartPath = opened.numberingPartPath;
54
55
  this.comments = opened.comments;
@@ -63,17 +64,42 @@ function sessionOf(session) {
63
64
  function documentPartPath(session) {
64
65
  return sessionOf(session).mainPartPath;
65
66
  }
66
- function documentNumbering(session) {
67
- return parseNumbering(sessionOf(session).numberingXml);
67
+ function documentNumbering(session, options) {
68
+ return parseNumbering(sessionOf(session).numberingXml, options);
69
+ }
70
+ var BODY_STORY_KEY = "body";
71
+ function blockKey(session, storyKey, index) {
72
+ return `${session.sessionId}:${storyKey}:${index}`;
73
+ }
74
+ function splitBlockKey(key) {
75
+ const first = key.indexOf(":");
76
+ const last = key.lastIndexOf(":");
77
+ if (first < 1 || last <= first + 1) return null;
78
+ const indexText = key.slice(last + 1);
79
+ if (!/^\d+$/.test(indexText)) return null;
80
+ const index = Number(indexText);
81
+ if (!Number.isSafeInteger(index)) return null;
82
+ return {
83
+ sessionId: key.slice(0, first),
84
+ storyKey: key.slice(first + 1, last),
85
+ index
86
+ };
68
87
  }
69
88
  function originalBlock(node, session) {
70
89
  const srcId = node.attrs.srcId;
71
- return typeof srcId === "number" ? session.blocks[srcId] : void 0;
90
+ if (typeof srcId !== "string") return void 0;
91
+ const key = splitBlockKey(srcId);
92
+ if (key === null || key.sessionId !== session.sessionId) return void 0;
93
+ return key.storyKey === BODY_STORY_KEY ? session.blocks[key.index] : void 0;
72
94
  }
73
95
  export {
96
+ BODY_STORY_KEY,
74
97
  SessionStore,
98
+ blockKey,
75
99
  documentNumbering,
76
100
  documentPartPath,
101
+ newSessionId,
77
102
  originalBlock,
78
- sessionOf
103
+ sessionOf,
104
+ splitBlockKey
79
105
  };