@portone/docx-editor 0.6.5 → 0.6.7

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 (86) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/CONTRIBUTING.md +4 -4
  3. package/dist/core.d.ts +7 -0
  4. package/dist/core.js +2 -0
  5. package/dist/docx/commentOnlyChange.d.ts +4 -4
  6. package/dist/docx/comments/constants.d.ts +7 -0
  7. package/dist/docx/comments/constants.js +13 -1
  8. package/dist/docx/comments/dates.d.ts +22 -0
  9. package/dist/docx/comments/dates.js +102 -0
  10. package/dist/docx/comments/durable.d.ts +22 -0
  11. package/dist/docx/comments/durable.js +138 -0
  12. package/dist/docx/comments/grammar.d.ts +10 -1
  13. package/dist/docx/comments/grammar.js +49 -2
  14. package/dist/docx/comments/model.d.ts +10 -2
  15. package/dist/docx/comments/model.js +38 -8
  16. package/dist/docx/comments/parts.d.ts +53 -7
  17. package/dist/docx/comments/parts.js +163 -20
  18. package/dist/docx/comments/people.d.ts +1 -1
  19. package/dist/docx/comments/people.js +5 -15
  20. package/dist/docx/comments/reading.d.ts +26 -0
  21. package/dist/docx/comments/reading.js +109 -49
  22. package/dist/docx/comments/writing.d.ts +2 -7
  23. package/dist/docx/comments/writing.js +30 -43
  24. package/dist/docx/compareDocx.d.ts +154 -0
  25. package/dist/docx/compareDocx.js +218 -0
  26. package/dist/docx/fidelity.js +2 -4
  27. package/dist/docx/importParagraph.js +6 -9
  28. package/dist/docx/invariants.js +19 -6
  29. package/dist/docx/packageParts.d.ts +2 -0
  30. package/dist/docx/packageParts.js +5 -1
  31. package/dist/docx/partPlan.d.ts +12 -0
  32. package/dist/docx/partPlan.js +19 -1
  33. package/dist/docx/protectionPolicy.d.ts +33 -5
  34. package/dist/docx/protectionPolicy.js +93 -47
  35. package/dist/docx/runProps.d.ts +3 -0
  36. package/dist/docx/runProps.js +9 -0
  37. package/dist/docx/sequenceDiff.d.ts +56 -0
  38. package/dist/docx/sequenceDiff.js +252 -0
  39. package/dist/docx/serializeParagraph.js +29 -3
  40. package/dist/docx/storyProjection.d.ts +2 -0
  41. package/dist/docx/storyProjection.js +10 -9
  42. package/dist/docx/textDiff.d.ts +24 -0
  43. package/dist/docx/textDiff.js +38 -0
  44. package/dist/editor/blockControlEdits.js +4 -4
  45. package/dist/editor/commands/comments/editing.js +59 -22
  46. package/dist/editor/commands/comments/model.d.ts +3 -1
  47. package/dist/editor/createEditor.js +6 -9
  48. package/dist/editor/editorDocument.d.ts +4 -0
  49. package/dist/editor/editorDocument.js +10 -0
  50. package/dist/editor/formatSource.d.ts +17 -0
  51. package/dist/editor/formatSource.js +74 -0
  52. package/dist/editor/plugins/caretFormat.d.ts +10 -0
  53. package/dist/editor/plugins/caretFormat.js +39 -0
  54. package/dist/editor/plugins/commentDecorations.js +6 -2
  55. package/dist/editor/plugins/controlContents.d.ts +5 -0
  56. package/dist/editor/plugins/controlContents.js +120 -13
  57. package/dist/editor/plugins/emptyRunPointer.d.ts +9 -0
  58. package/dist/editor/plugins/emptyRunPointer.js +22 -0
  59. package/dist/editor/plugins/inlinePlugins.d.ts +6 -0
  60. package/dist/editor/plugins/inlinePlugins.js +19 -0
  61. package/dist/editor/stories/storyState.js +4 -8
  62. package/dist/editor/views/runMarkView.d.ts +5 -2
  63. package/dist/editor/views/runMarkView.js +12 -1
  64. package/dist/ooxml/errors.d.ts +1 -1
  65. package/dist/ooxml/names.d.ts +2 -0
  66. package/dist/ooxml/names.js +2 -0
  67. package/dist/ooxml/partSplice.d.ts +13 -3
  68. package/dist/ooxml/partSplice.js +45 -18
  69. package/dist/schema/attrRoles.js +4 -1
  70. package/dist/schema/clipboard.js +3 -0
  71. package/dist/schema/docxSchema.d.ts +1 -1
  72. package/dist/schema/docxSchema.js +52 -4
  73. package/dist/schema/emptyRuns.d.ts +3 -0
  74. package/dist/schema/emptyRuns.js +7 -0
  75. package/dist/schema/locks.d.ts +11 -0
  76. package/dist/schema/locks.js +2 -0
  77. package/dist/schema/protection.js +2 -1
  78. package/dist/styles/classNames.d.ts +4 -0
  79. package/dist/styles/classNames.js +4 -0
  80. package/dist/styles/inlineStyle.d.ts +2 -0
  81. package/dist/styles/inlineStyle.js +10 -4
  82. package/dist/styles.css +15 -0
  83. package/dist/ui/CommentsPanel.js +8 -13
  84. package/dist/ui/comments/commentDate.d.ts +7 -0
  85. package/dist/ui/comments/commentDate.js +17 -0
  86. package/package.json +7 -7
@@ -9,10 +9,11 @@ import {
9
9
  unattributedCommentAuthors
10
10
  } from "../schema/protection.js";
11
11
  import { importDocx } from "./importDocx.js";
12
- import { CONTENT_TYPES_PATH } from "./packageParts.js";
12
+ import { CONTENT_TYPES_PATH, sameBytes } from "./packageParts.js";
13
13
  import {
14
14
  readRelationships,
15
- relsPathOf
15
+ relsPathOf,
16
+ resolveTarget
16
17
  } from "./relationships.js";
17
18
  import { isModelledBlock } from "./storyProjection.js";
18
19
  var policies = /* @__PURE__ */ new Map();
@@ -23,56 +24,96 @@ function registerProtectionPolicy(policy) {
23
24
  function protectionPolicyFor(level) {
24
25
  return policies.get(level);
25
26
  }
26
- function sameBytes(before, after) {
27
- return before.length === after.length && before.every((byte, index) => byte === after[index]);
28
- }
29
27
  function partPathsOf(policy, session) {
30
28
  return policy.parts.map((kind) => kind.pathIn(session));
31
29
  }
32
- function excusedPaths(policy, session) {
30
+ function policyPartPaths(policy, session) {
33
31
  return partPathsOf(policy, session).filter(
34
32
  (path) => path !== null
35
33
  );
36
34
  }
37
- function aroundTheStory(story) {
38
- const preserved = story.session.blocks.filter((block) => !isModelledBlock(block.node)).map((block) => block.xml).join("");
35
+ function aroundTheBlocks(story) {
39
36
  const sectPr = story.doc.attrs.sectPr;
40
- return story.session.documentPrefix + preserved + (typeof sectPr === "string" ? sectPr : "") + story.session.documentSuffix;
37
+ return story.session.documentPrefix + (typeof sectPr === "string" ? sectPr : "") + story.session.documentSuffix;
38
+ }
39
+ function preservedBlocks(story) {
40
+ return story.session.blocks.filter((block) => !isModelledBlock(block.node)).map((block) => block.xml).join("");
41
+ }
42
+ function partKindAt(policy, session, path, matches) {
43
+ return partPathsOf(policy, session).some(
44
+ (at, index) => at === path && matches(policy.parts[index])
45
+ );
41
46
  }
42
- function relationshipsKept(before, after, relTypes) {
43
- if (new Set(before.map((entry) => entry.id)).size !== before.length || new Set(after.map((entry) => entry.id)).size !== after.length) {
47
+ function relationshipsKept(policy, before, after, allowed) {
48
+ const relsPath = relsPathOf(before.mainPartPath);
49
+ const was = readRelationships(before.parts, relsPath);
50
+ const now = readRelationships(after.parts, relsPath);
51
+ if (new Set(was.map((entry) => entry.id)).size !== was.length || new Set(now.map((entry) => entry.id)).size !== now.length) {
44
52
  return false;
45
53
  }
46
- const now = new Map(after.map((entry) => [entry.id, entry]));
47
- const kept = before.every((entry) => {
48
- const current = now.get(entry.id);
49
- return current !== void 0 && current.type === entry.type && current.target === entry.target && current.external === entry.external;
54
+ const relTypes = policy.parts.map((kind) => kind.relType);
55
+ const heldByRevised = (entry) => after.parts.has(resolveTarget(before.mainPartPath, entry.target));
56
+ const current = new Map(now.map((entry) => [entry.id, entry]));
57
+ const kept = was.every((entry) => {
58
+ const other = current.get(entry.id);
59
+ if (other === void 0) {
60
+ return allowed === "gained or dropped" && relTypes.includes(entry.type) && !entry.external && !heldByRevised(entry);
61
+ }
62
+ return other.type === entry.type && other.target === entry.target && other.external === entry.external;
50
63
  });
51
- const ids = new Set(before.map((entry) => entry.id));
52
- const parts = after.filter((entry) => !entry.external);
53
- return kept && after.filter((entry) => !ids.has(entry.id)).every(
54
- (entry) => relTypes.includes(entry.type) && !entry.external && parts.filter((other) => other.type === entry.type).length === 1
55
- );
64
+ const ids = new Set(was.map((entry) => entry.id));
65
+ const parts = now.filter((entry) => !entry.external);
66
+ return kept && now.filter((entry) => !ids.has(entry.id)).every(
67
+ (entry) => relTypes.includes(entry.type) && !entry.external && heldByRevised(entry) && parts.filter((other) => other.type === entry.type).length === 1
68
+ ) && gainedPartsAreNew(policy, before, after);
56
69
  }
57
70
  function contentTypes(bytes) {
58
- if (bytes === void 0) return /* @__PURE__ */ new Map();
59
- const declared = /* @__PURE__ */ new Map();
71
+ const declared = { defaults: /* @__PURE__ */ new Map(), overrides: /* @__PURE__ */ new Map() };
72
+ if (bytes === void 0) return declared;
60
73
  for (const el of elementChildren(
61
74
  parseXml(decodeUtf8(bytes).text).documentElement
62
75
  )) {
63
- const key = el.localName === "Default" ? el.getAttribute("Extension") : el.localName === "Override" ? el.getAttribute("PartName") : null;
64
- if (key !== null) declared.set(key, el.getAttribute("ContentType") ?? "");
76
+ const type = el.getAttribute("ContentType") ?? "";
77
+ const extension = el.getAttribute("Extension");
78
+ const partName = el.getAttribute("PartName");
79
+ if (el.localName === "Default" && extension !== null) {
80
+ declared.defaults.set(extension, type);
81
+ } else if (el.localName === "Override" && partName !== null) {
82
+ declared.overrides.set(partName, type);
83
+ }
65
84
  }
66
85
  return declared;
67
86
  }
68
- function contentTypesKept(before, after, declarable) {
69
- for (const [key, type] of before) {
70
- if (after.get(key) !== type) return false;
71
- }
72
- for (const [key, type] of after) {
73
- if (!before.has(key) && !declarable.includes(type)) return false;
74
- }
75
- return true;
87
+ function sameDeclarations(before, after) {
88
+ return before.size === after.size && Array.from(before).every(([key, type]) => after.get(key) === type);
89
+ }
90
+ function contentTypesKept(policy, before, after, allowed) {
91
+ const was = contentTypes(before.parts.get(CONTENT_TYPES_PATH));
92
+ const now = contentTypes(after.parts.get(CONTENT_TYPES_PATH));
93
+ const pathOf = (partName) => partName.replace(/^\//, "");
94
+ const dropped = Array.from(was.overrides).filter(
95
+ ([partName]) => !now.overrides.has(partName)
96
+ );
97
+ const gained = Array.from(now.overrides).filter(
98
+ ([partName]) => !was.overrides.has(partName)
99
+ );
100
+ return sameDeclarations(was.defaults, now.defaults) && Array.from(was.overrides).every(
101
+ ([partName, type]) => !now.overrides.has(partName) || now.overrides.get(partName) === type
102
+ ) && dropped.every(
103
+ ([partName, type]) => allowed === "gained or dropped" && !after.parts.has(pathOf(partName)) && partKindAt(
104
+ policy,
105
+ before,
106
+ pathOf(partName),
107
+ (kind) => kind.contentType === type
108
+ )
109
+ ) && gained.every(
110
+ ([partName, type]) => after.parts.has(pathOf(partName)) && partKindAt(
111
+ policy,
112
+ after,
113
+ pathOf(partName),
114
+ (kind) => kind.contentType === type
115
+ )
116
+ );
76
117
  }
77
118
  function gainedPartsAreNew(policy, before, after) {
78
119
  const had = partPathsOf(policy, before);
@@ -80,6 +121,15 @@ function gainedPartsAreNew(policy, before, after) {
80
121
  (path, kind) => path === null || path === had[kind] || !before.parts.has(path)
81
122
  );
82
123
  }
124
+ function declarationsKept(policy, before, after, allowed) {
125
+ return /* @__PURE__ */ new Map([
126
+ [
127
+ relsPathOf(before.mainPartPath),
128
+ relationshipsKept(policy, before, after, allowed)
129
+ ],
130
+ [CONTENT_TYPES_PATH, contentTypesKept(policy, before, after, allowed)]
131
+ ]);
132
+ }
83
133
  function packageKept(policy, before, after) {
84
134
  const was = before.session;
85
135
  const now = after.session;
@@ -91,8 +141,8 @@ function packageKept(policy, before, after) {
91
141
  was.mainPartPath,
92
142
  relsPath,
93
143
  CONTENT_TYPES_PATH,
94
- ...excusedPaths(policy, was),
95
- ...excusedPaths(policy, now)
144
+ ...policyPartPaths(policy, was),
145
+ ...policyPartPaths(policy, now)
96
146
  ]);
97
147
  for (const path of /* @__PURE__ */ new Set([...was.parts.keys(), ...now.parts.keys()])) {
98
148
  if (untouched.has(path)) continue;
@@ -102,21 +152,14 @@ function packageKept(policy, before, after) {
102
152
  return { ok: false, reason: "part-changed", part: path };
103
153
  }
104
154
  }
105
- if (!relationshipsKept(
106
- readRelationships(was.parts, relsPath),
107
- readRelationships(now.parts, relsPath),
108
- policy.parts.map((kind) => kind.relType)
109
- ) || !gainedPartsAreNew(policy, was, now)) {
155
+ const declarations = declarationsKept(policy, was, now, "gained");
156
+ if (declarations.get(relsPath) !== true) {
110
157
  return { ok: false, reason: "relationship-changed", part: relsPath };
111
158
  }
112
- if (!contentTypesKept(
113
- contentTypes(was.parts.get(CONTENT_TYPES_PATH)),
114
- contentTypes(now.parts.get(CONTENT_TYPES_PATH)),
115
- policy.parts.map((kind) => kind.contentType)
116
- )) {
159
+ if (declarations.get(CONTENT_TYPES_PATH) !== true) {
117
160
  return { ok: false, reason: "part-changed", part: CONTENT_TYPES_PATH };
118
161
  }
119
- if (aroundTheStory(before) !== aroundTheStory(after)) {
162
+ if (preservedBlocks(before) !== preservedBlocks(after) || aroundTheBlocks(before) !== aroundTheBlocks(after)) {
120
163
  return { ok: false, reason: "part-changed", part: was.mainPartPath };
121
164
  }
122
165
  return { ok: true };
@@ -140,7 +183,7 @@ function partKept(kind, before, after, authorId, options) {
140
183
  original?.el ?? null,
141
184
  authorId,
142
185
  options,
143
- after.session,
186
+ { arrived: before.session, submitted: after.session },
144
187
  unattributed
145
188
  )) {
146
189
  return false;
@@ -148,7 +191,7 @@ function partKept(kind, before, after, authorId, options) {
148
191
  }
149
192
  const stoodBehindBefore = kind.referents(before);
150
193
  return Array.from(arrived.keys()).every(
151
- (id) => stoodBehindBefore.has(id) || submitted.has(id)
194
+ (id) => submitted.has(id) || stoodBehindBefore.has(id) && !stoodBehindNow.has(id)
152
195
  );
153
196
  }
154
197
  function partsKept(policy, before, after, authorId, options) {
@@ -172,7 +215,10 @@ function verifyChange(policy, original, submitted, authorId, { xmlParser, ...opt
172
215
  });
173
216
  }
174
217
  export {
218
+ aroundTheBlocks,
219
+ declarationsKept,
175
220
  partsKept,
221
+ policyPartPaths,
176
222
  protectionPolicyFor,
177
223
  registerProtectionPolicy,
178
224
  verifyChange
@@ -11,6 +11,7 @@
11
11
  * The display values produced here are used on screen only. What goes back into the document is
12
12
  * always the rPr string.
13
13
  */
14
+ import type { Mark } from "prosemirror-model";
14
15
  import { type RunFormat } from "../model/format";
15
16
  import { type EditableRunKey, type RunEdit } from "./formatting";
16
17
  export type { RunEdit } from "./formatting";
@@ -25,6 +26,8 @@ export interface RunProps {
25
26
  }
26
27
  /** Derives the display values again from the rPr we operated on. The same rPr always yields the same display values */
27
28
  export declare function readRunProps(rPr: string | null): RunFormat | null;
29
+ /** The colour a run's own properties paint behind its characters, whatever it inherits. null for none */
30
+ export declare function ownBackground(run: Mark): string | null;
28
31
  /**
29
32
  * The rPr and display values after changing one piece of character formatting.
30
33
  *
@@ -5,6 +5,7 @@ import {
5
5
  parsePropsXml,
6
6
  renderProps
7
7
  } from "../ooxml/props.js";
8
+ import { runBackground } from "../styles/inlineStyle.js";
8
9
  import {
9
10
  EMPTY_RUN_PROPS,
10
11
  RUN_PROPERTIES,
@@ -19,6 +20,13 @@ function propsOf(rPr) {
19
20
  function readRunProps(rPr) {
20
21
  return rPr === null ? null : readRunFormat(parsePropsXml(rPr));
21
22
  }
23
+ function ownBackground(run) {
24
+ const format = toRunFormat(run.attrs.format);
25
+ if (format === null || runBackground(format) === null) return null;
26
+ const rPr = run.attrs.rPr;
27
+ const own = readRunProps(typeof rPr === "string" ? rPr : null);
28
+ return own === null ? null : runBackground(own);
29
+ }
22
30
  function inheritedFormat(current) {
23
31
  const direct = new Set(Object.keys(readRunProps(current.rPr) ?? {}));
24
32
  const inherited = {};
@@ -50,5 +58,6 @@ export {
50
58
  editRunProps,
51
59
  isRunToggleOn,
52
60
  matchesRunEdit,
61
+ ownBackground,
53
62
  readRunProps
54
63
  };
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The elements two sequences share, as the shortest edit between them, and the block-by-block
3
+ * alignment `compareDocx` builds on it.
4
+ *
5
+ * The search is Myers' in linear space (the middle-snake bisection), so a long sequence rewritten
6
+ * throughout costs time rather than a table the size of both sequences multiplied. The time is
7
+ * bounded too: past `COMPARISON_BUDGET` the search gives up and reports everything between the
8
+ * shared head and tail as removed and added, which is a correct edit if not the shortest one.
9
+ */
10
+ export type Match = readonly [original: number, revised: number];
11
+ /**
12
+ * How many element comparisons and diagonal probes one search may spend. It is a count rather
13
+ * than a deadline so that the same two inputs always get the same answer. Five million keeps a
14
+ * search under a tenth of a second, and still finds a thousand edits scattered through a text of
15
+ * forty thousand characters.
16
+ */
17
+ export declare const COMPARISON_BUDGET = 5000000;
18
+ type Same = (original: number, revised: number) => boolean;
19
+ interface Budget {
20
+ left: number;
21
+ }
22
+ /**
23
+ * The index pairs of a longest common subsequence of two sequences under `same`, in order.
24
+ *
25
+ * Where the search runs past its budget, only the shared head and tail are matched. Searches that
26
+ * pass one budget between them share it, so a caller running many bounds them all together.
27
+ */
28
+ export declare function commonSubsequence(originalLength: number, revisedLength: number, same: Same, budget?: Budget): Match[];
29
+ /** One element of a sequence to align: what it would be written as, and what it reads as */
30
+ export interface Alignable {
31
+ identity: string;
32
+ text: string;
33
+ }
34
+ export type Alignment = {
35
+ kind: "kept";
36
+ original: number;
37
+ revised: number;
38
+ } | {
39
+ kind: "changed";
40
+ original: number;
41
+ revised: number;
42
+ change: "text" | "formatting";
43
+ } | {
44
+ kind: "removed";
45
+ original: number;
46
+ } | {
47
+ kind: "added";
48
+ revised: number;
49
+ };
50
+ /**
51
+ * Lines two sequences up: elements written the same are kept, then elements reading the same are
52
+ * changed in formatting, and the elements left between two of those are paired as changed in
53
+ * text (`pairLeftovers`). Elements of different kinds are never paired.
54
+ */
55
+ export declare function alignSequences<T extends Alignable>(original: readonly T[], revised: readonly T[], kindOf?: (item: T) => string): Alignment[];
56
+ export {};
@@ -0,0 +1,252 @@
1
+ // src/docx/sequenceDiff.ts
2
+ var COMPARISON_BUDGET = 5e6;
3
+ function bisect(originalFrom, n, revisedFrom, m, same, budget) {
4
+ const maxD = Math.ceil((n + m) / 2);
5
+ const offset = maxD;
6
+ const length = 2 * maxD + 2;
7
+ const forward = new Int32Array(length).fill(-1);
8
+ const backward = new Int32Array(length).fill(-1);
9
+ forward[offset + 1] = 0;
10
+ backward[offset + 1] = 0;
11
+ const delta = n - m;
12
+ const front = delta % 2 !== 0;
13
+ let forwardStart = 0;
14
+ let forwardEnd = 0;
15
+ let backwardStart = 0;
16
+ let backwardEnd = 0;
17
+ for (let d = 0; d < maxD; d += 1) {
18
+ for (let k = -d + forwardStart; k <= d - forwardEnd; k += 2) {
19
+ budget.left -= 1;
20
+ const at = offset + k;
21
+ let x = k === -d || k !== d && forward[at - 1] < forward[at + 1] ? forward[at + 1] : forward[at - 1] + 1;
22
+ let y = x - k;
23
+ const start = x;
24
+ while (x < n && y < m && same(originalFrom + x, revisedFrom + y)) {
25
+ x += 1;
26
+ y += 1;
27
+ }
28
+ budget.left -= x - start;
29
+ forward[at] = x;
30
+ if (x > n) forwardEnd += 2;
31
+ else if (y > m) forwardStart += 2;
32
+ else if (front) {
33
+ const other = offset + delta - k;
34
+ if (other >= 0 && other < length && backward[other] !== -1) {
35
+ if (x >= n - backward[other]) return [x, y];
36
+ }
37
+ }
38
+ }
39
+ for (let k = -d + backwardStart; k <= d - backwardEnd; k += 2) {
40
+ budget.left -= 1;
41
+ const at = offset + k;
42
+ let x = k === -d || k !== d && backward[at - 1] < backward[at + 1] ? backward[at + 1] : backward[at - 1] + 1;
43
+ let y = x - k;
44
+ const start = x;
45
+ while (x < n && y < m && same(originalFrom + n - x - 1, revisedFrom + m - y - 1)) {
46
+ x += 1;
47
+ y += 1;
48
+ }
49
+ budget.left -= x - start;
50
+ backward[at] = x;
51
+ if (x > n) backwardEnd += 2;
52
+ else if (y > m) backwardStart += 2;
53
+ else if (!front) {
54
+ const other = offset + delta - k;
55
+ if (other >= 0 && other < length && forward[other] !== -1) {
56
+ const meet = forward[other];
57
+ if (meet >= n - x) return [meet, offset + meet - other];
58
+ }
59
+ }
60
+ }
61
+ if (budget.left <= 0) return "exhausted";
62
+ }
63
+ return null;
64
+ }
65
+ function matchesIn(originalFrom, originalTo, revisedFrom, revisedTo, same, budget, found) {
66
+ while (originalFrom < originalTo && revisedFrom < revisedTo && same(originalFrom, revisedFrom)) {
67
+ found.push([originalFrom, revisedFrom]);
68
+ originalFrom += 1;
69
+ revisedFrom += 1;
70
+ }
71
+ const tail = [];
72
+ while (originalFrom < originalTo && revisedFrom < revisedTo && same(originalTo - 1, revisedTo - 1)) {
73
+ originalTo -= 1;
74
+ revisedTo -= 1;
75
+ tail.push([originalTo, revisedTo]);
76
+ }
77
+ if (originalFrom < originalTo && revisedFrom < revisedTo) {
78
+ const split = bisect(
79
+ originalFrom,
80
+ originalTo - originalFrom,
81
+ revisedFrom,
82
+ revisedTo - revisedFrom,
83
+ same,
84
+ budget
85
+ );
86
+ if (split === "exhausted") return false;
87
+ if (split !== null) {
88
+ const [x, y] = split;
89
+ const kept = matchesIn(
90
+ originalFrom,
91
+ originalFrom + x,
92
+ revisedFrom,
93
+ revisedFrom + y,
94
+ same,
95
+ budget,
96
+ found
97
+ ) && matchesIn(
98
+ originalFrom + x,
99
+ originalTo,
100
+ revisedFrom + y,
101
+ revisedTo,
102
+ same,
103
+ budget,
104
+ found
105
+ );
106
+ if (!kept) return false;
107
+ }
108
+ }
109
+ found.push(...tail.reverse());
110
+ return true;
111
+ }
112
+ function commonSubsequence(originalLength, revisedLength, same, budget = { left: COMPARISON_BUDGET }) {
113
+ let head = 0;
114
+ while (head < originalLength && head < revisedLength && same(head, head)) {
115
+ head += 1;
116
+ }
117
+ let tail = 0;
118
+ while (tail < originalLength - head && tail < revisedLength - head && same(originalLength - 1 - tail, revisedLength - 1 - tail)) {
119
+ tail += 1;
120
+ }
121
+ const matched = (from, to, shift) => Array.from({ length: to - from }, (_, at) => [
122
+ from + at,
123
+ from + at + shift
124
+ ]);
125
+ const middle = [];
126
+ const found = matchesIn(
127
+ head,
128
+ originalLength - tail,
129
+ head,
130
+ revisedLength - tail,
131
+ same,
132
+ budget,
133
+ middle
134
+ );
135
+ return [
136
+ ...matched(0, head, 0),
137
+ ...found ? middle : [],
138
+ ...matched(
139
+ originalLength - tail,
140
+ originalLength,
141
+ revisedLength - originalLength
142
+ )
143
+ ];
144
+ }
145
+ function keyed(original, revised) {
146
+ const ids = /* @__PURE__ */ new Map();
147
+ const idOf = (key) => {
148
+ const known = ids.get(key);
149
+ if (known !== void 0) return known;
150
+ ids.set(key, ids.size);
151
+ return ids.size - 1;
152
+ };
153
+ return [Int32Array.from(original, idOf), Int32Array.from(revised, idOf)];
154
+ }
155
+ function matchBetween(anchors, original, revised, key) {
156
+ const [was, now] = keyed(original.map(key), revised.map(key));
157
+ const budget = { left: COMPARISON_BUDGET };
158
+ const found = [];
159
+ let from = 0;
160
+ let to = 0;
161
+ for (const anchor of [
162
+ ...anchors,
163
+ [original.length, revised.length]
164
+ ]) {
165
+ const matches = commonSubsequence(
166
+ anchor[0] - from,
167
+ anchor[1] - to,
168
+ (i, j) => was[from + i] === now[to + j],
169
+ budget
170
+ );
171
+ for (const [i, j] of matches) found.push([from + i, to + j]);
172
+ if (anchor[0] < original.length) found.push(anchor);
173
+ from = anchor[0] + 1;
174
+ to = anchor[1] + 1;
175
+ }
176
+ return found;
177
+ }
178
+ function pairLeftovers(original, revised, kindOf, [originalFrom, originalTo], [revisedFrom, revisedTo]) {
179
+ const steps = [];
180
+ let cursor = originalFrom;
181
+ let added = revisedFrom;
182
+ for (let at = revisedFrom; at < revisedTo; at += 1) {
183
+ const kind = kindOf(revised[at]);
184
+ let partner = cursor;
185
+ while (partner < originalTo && kindOf(original[partner]) !== kind) {
186
+ partner += 1;
187
+ }
188
+ if (partner === originalTo) continue;
189
+ for (; cursor < partner; cursor += 1) {
190
+ steps.push({ kind: "removed", original: cursor });
191
+ }
192
+ for (; added < at; added += 1)
193
+ steps.push({ kind: "added", revised: added });
194
+ steps.push({
195
+ kind: "changed",
196
+ original: partner,
197
+ revised: at,
198
+ change: "text"
199
+ });
200
+ cursor = partner + 1;
201
+ added = at + 1;
202
+ }
203
+ for (; cursor < originalTo; cursor += 1) {
204
+ steps.push({ kind: "removed", original: cursor });
205
+ }
206
+ for (; added < revisedTo; added += 1)
207
+ steps.push({ kind: "added", revised: added });
208
+ return steps;
209
+ }
210
+ function alignSequences(original, revised, kindOf = () => "") {
211
+ const exact = matchBetween(
212
+ [],
213
+ original,
214
+ revised,
215
+ (item) => `${kindOf(item)}\0${item.identity}`
216
+ );
217
+ const matched = matchBetween(
218
+ exact,
219
+ original,
220
+ revised,
221
+ (item) => `${kindOf(item)}\0${item.text}`
222
+ );
223
+ const alignment = [];
224
+ let from = 0;
225
+ let to = 0;
226
+ for (const [was, now] of [
227
+ ...matched,
228
+ [original.length, revised.length]
229
+ ]) {
230
+ alignment.push(
231
+ ...pairLeftovers(original, revised, kindOf, [from, was], [to, now])
232
+ );
233
+ if (was < original.length) {
234
+ alignment.push(
235
+ original[was].identity === revised[now].identity ? { kind: "kept", original: was, revised: now } : {
236
+ kind: "changed",
237
+ original: was,
238
+ revised: now,
239
+ change: "formatting"
240
+ }
241
+ );
242
+ }
243
+ from = was + 1;
244
+ to = now + 1;
245
+ }
246
+ return alignment;
247
+ }
248
+ export {
249
+ COMPARISON_BUDGET,
250
+ alignSequences,
251
+ commonSubsequence
252
+ };
@@ -12,7 +12,15 @@ import {
12
12
  withExtent
13
13
  } from "../ooxml/image.js";
14
14
  import { wName } from "../ooxml/names.js";
15
- import { escapeXml } from "../ooxml/xml.js";
15
+ import { parsePropsXml } from "../ooxml/props.js";
16
+ import {
17
+ attrString,
18
+ childByLocalName,
19
+ elementChildren,
20
+ escapeXml,
21
+ serializeXml
22
+ } from "../ooxml/xml.js";
23
+ import { isEmptyRun } from "../schema/emptyRuns.js";
16
24
  import { wrapperMarks } from "../schema/wrappers.js";
17
25
  import { NO_EXPORT_REFS } from "./exportRefs.js";
18
26
  import { emptyWrapperXml, wrapperKindOf } from "./wrappers.js";
@@ -113,11 +121,29 @@ function preservedXml(node) {
113
121
  }
114
122
  return xml;
115
123
  }
124
+ function readWith(run, mark) {
125
+ const rPr = childByLocalName(run, "rPr");
126
+ return attrString(run) === rawAttrsOf(mark?.attrs.rAttrs) && (rPr ? serializeXml(rPr) : null) === rawAttrsOf(mark?.attrs.rPr);
127
+ }
128
+ function emptyRunPart(node) {
129
+ const xml = rawAttrsOf(node.attrs.xml);
130
+ const run = xml === null ? null : parsePropsXml(xml);
131
+ const mark = markOf(node, "run");
132
+ if (xml !== null && run !== null && readWith(run, mark)) {
133
+ return { kind: "raw", xml };
134
+ }
135
+ const pieces = (run ? elementChildren(run) : []).filter((child) => child.localName !== "rPr").map(serializeXml);
136
+ return { kind: "run", mark, pieces, sealed: true };
137
+ }
116
138
  function addInline(parts, child, images) {
117
139
  if (child.type.name === "rawInline") {
118
140
  parts.push({ kind: "raw", xml: preservedXml(child) });
119
141
  return;
120
142
  }
143
+ if (isEmptyRun(child)) {
144
+ parts.push(emptyRunPart(child));
145
+ return;
146
+ }
121
147
  const empty = emptyWrapperXml(child);
122
148
  if (empty !== null) {
123
149
  parts.push({ kind: "raw", xml: empty });
@@ -146,10 +172,10 @@ function addInline(parts, child, images) {
146
172
  const piece = renderInline(child, images);
147
173
  const mark = markOf(child, "run");
148
174
  const last = parts.at(-1);
149
- if (last?.kind === "run" && sameMark(last.mark, mark)) {
175
+ if (last?.kind === "run" && !last.sealed && sameMark(last.mark, mark)) {
150
176
  last.pieces.push(piece);
151
177
  } else {
152
- parts.push({ kind: "run", mark, pieces: [piece] });
178
+ parts.push({ kind: "run", mark, pieces: [piece], sealed: false });
153
179
  }
154
180
  }
155
181
  function renderParagraphPart(part) {
@@ -34,3 +34,5 @@ export declare function isModelledBlock(node: PMNode): boolean;
34
34
  * over: a caller judging two files answers such a story the way it answers a changed one.
35
35
  */
36
36
  export declare function comparableStory(story: Story, strip: (node: PMNode) => PMNode): readonly string[] | null;
37
+ /** One block as the writer puts it out, and null where it cannot be written back at all */
38
+ export declare function comparableBlock(block: PMNode, session: SessionStore, strip: (node: PMNode) => PMNode): string | null;
@@ -7,22 +7,23 @@ function isModelledBlock(node) {
7
7
  }
8
8
  function comparableStory(story, strip) {
9
9
  const blocks = [];
10
+ for (const block of story.doc.children) {
11
+ const xml = comparableBlock(block, story.session, strip);
12
+ if (xml === null) return null;
13
+ blocks.push(xml);
14
+ }
15
+ return blocks;
16
+ }
17
+ function comparableBlock(block, session, strip) {
10
18
  try {
11
- story.doc.forEach((block) => {
12
- blocks.push(
13
- serializeBlock(strip(block), {
14
- ...NO_EXPORT_REFS,
15
- session: story.session
16
- })
17
- );
18
- });
19
+ return serializeBlock(strip(block), { ...NO_EXPORT_REFS, session });
19
20
  } catch (error) {
20
21
  if (error instanceof DocxExportError) return null;
21
22
  throw error;
22
23
  }
23
- return blocks;
24
24
  }
25
25
  export {
26
+ comparableBlock,
26
27
  comparableStory,
27
28
  isModelledBlock
28
29
  };