@portone/docx-editor 0.6.4 → 0.6.6

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 (80) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/CONTRIBUTING.md +4 -4
  3. package/dist/DocxEditor.d.ts +11 -0
  4. package/dist/DocxEditor.js +7 -3
  5. package/dist/core.d.ts +7 -0
  6. package/dist/core.js +2 -0
  7. package/dist/docx/commentOnlyChange.d.ts +4 -4
  8. package/dist/docx/comments/constants.d.ts +7 -0
  9. package/dist/docx/comments/constants.js +13 -1
  10. package/dist/docx/comments/dates.d.ts +22 -0
  11. package/dist/docx/comments/dates.js +102 -0
  12. package/dist/docx/comments/durable.d.ts +22 -0
  13. package/dist/docx/comments/durable.js +138 -0
  14. package/dist/docx/comments/grammar.d.ts +10 -1
  15. package/dist/docx/comments/grammar.js +49 -2
  16. package/dist/docx/comments/model.d.ts +10 -2
  17. package/dist/docx/comments/model.js +38 -8
  18. package/dist/docx/comments/parts.d.ts +53 -7
  19. package/dist/docx/comments/parts.js +163 -20
  20. package/dist/docx/comments/people.d.ts +1 -1
  21. package/dist/docx/comments/people.js +5 -15
  22. package/dist/docx/comments/reading.d.ts +26 -0
  23. package/dist/docx/comments/reading.js +109 -49
  24. package/dist/docx/comments/writing.d.ts +2 -7
  25. package/dist/docx/comments/writing.js +30 -43
  26. package/dist/docx/compareDocx.d.ts +154 -0
  27. package/dist/docx/compareDocx.js +218 -0
  28. package/dist/docx/importParagraph.js +5 -3
  29. package/dist/docx/invariants.js +19 -6
  30. package/dist/docx/packageParts.d.ts +2 -0
  31. package/dist/docx/packageParts.js +5 -1
  32. package/dist/docx/partPlan.d.ts +12 -0
  33. package/dist/docx/partPlan.js +19 -1
  34. package/dist/docx/protectionPolicy.d.ts +33 -5
  35. package/dist/docx/protectionPolicy.js +93 -47
  36. package/dist/docx/sequenceDiff.d.ts +56 -0
  37. package/dist/docx/sequenceDiff.js +252 -0
  38. package/dist/docx/storyProjection.d.ts +2 -0
  39. package/dist/docx/storyProjection.js +10 -9
  40. package/dist/docx/textDiff.d.ts +24 -0
  41. package/dist/docx/textDiff.js +38 -0
  42. package/dist/editor/commands/comments/editing.js +56 -20
  43. package/dist/editor/commands/comments/model.d.ts +3 -1
  44. package/dist/editor/createEditor.d.ts +4 -1
  45. package/dist/editor/createEditor.js +14 -2
  46. package/dist/editor/editRefusal.d.ts +68 -0
  47. package/dist/editor/editRefusal.js +177 -0
  48. package/dist/editor/editorDocument.d.ts +4 -0
  49. package/dist/editor/editorDocument.js +10 -0
  50. package/dist/editor/plugins/commentDecorations.js +6 -2
  51. package/dist/editor/plugins/controlContents.d.ts +24 -0
  52. package/dist/editor/plugins/controlContents.js +260 -0
  53. package/dist/editor/plugins/controlLifecycle.js +3 -5
  54. package/dist/editor/stories/storyState.js +2 -0
  55. package/dist/index.d.ts +2 -0
  56. package/dist/ooxml/errors.d.ts +1 -1
  57. package/dist/ooxml/names.d.ts +2 -0
  58. package/dist/ooxml/names.js +2 -0
  59. package/dist/ooxml/partSplice.d.ts +13 -3
  60. package/dist/ooxml/partSplice.js +45 -18
  61. package/dist/schema/attrRoles.js +3 -1
  62. package/dist/schema/controlAttrs.d.ts +6 -0
  63. package/dist/schema/controlAttrs.js +4 -0
  64. package/dist/schema/docxSchema.js +10 -3
  65. package/dist/schema/guards.d.ts +8 -2
  66. package/dist/schema/guards.js +14 -8
  67. package/dist/schema/locks.d.ts +25 -2
  68. package/dist/schema/locks.js +67 -11
  69. package/dist/schema/protection.js +2 -1
  70. package/dist/styles/classNames.d.ts +2 -0
  71. package/dist/styles/classNames.js +2 -0
  72. package/dist/styles.css +65 -14
  73. package/dist/ui/CommentsPanel.js +8 -13
  74. package/dist/ui/MenuNote.d.ts +12 -0
  75. package/dist/ui/MenuNote.js +14 -0
  76. package/dist/ui/TableMenu.js +38 -19
  77. package/dist/ui/TextMenu.js +31 -25
  78. package/dist/ui/comments/commentDate.d.ts +7 -0
  79. package/dist/ui/comments/commentDate.js +17 -0
  80. package/package.json +7 -7
@@ -0,0 +1,218 @@
1
+ // src/docx/compareDocx.ts
2
+ import { withXmlParser } from "../ooxml/xml.js";
3
+ import { SDT_BLOCK_NODE, SDT_EMPTY_BLOCK_NODE } from "../schema/controlAttrs.js";
4
+ import { withoutComments } from "../schema/protection.js";
5
+ import { commentReferencesIn } from "./comments/model.js";
6
+ import { commentsPolicy } from "./comments/policy.js";
7
+ import { importDocx } from "./importDocx.js";
8
+ import { sameBytes } from "./packageParts.js";
9
+ import {
10
+ aroundTheBlocks,
11
+ declarationsKept,
12
+ policyPartPaths
13
+ } from "./protectionPolicy.js";
14
+ import { alignSequences } from "./sequenceDiff.js";
15
+ import { storyKey, storyOf, storyText } from "./story.js";
16
+ import { comparableBlock } from "./storyProjection.js";
17
+ import { diffText } from "./textDiff.js";
18
+ var BLOCK_KINDS = {
19
+ paragraph: "paragraph",
20
+ table: "table",
21
+ [SDT_BLOCK_NODE]: "control",
22
+ [SDT_EMPTY_BLOCK_NODE]: "control",
23
+ rawBlock: "preserved"
24
+ };
25
+ function blockKind(node) {
26
+ const kind = BLOCK_KINDS[node.type.name];
27
+ if (kind === void 0) {
28
+ throw new Error(`no block kind for ${node.type.name}`);
29
+ }
30
+ return kind;
31
+ }
32
+ function blocksOf(story, side) {
33
+ const blocks = [];
34
+ story.doc.forEach((node, pos, index) => {
35
+ blocks.push({
36
+ node,
37
+ pos,
38
+ block: blockKind(node),
39
+ text: storyText(node),
40
+ identity: comparableBlock(node, story.session, withoutComments) ?? `\0${side}:${index}`
41
+ });
42
+ });
43
+ return blocks;
44
+ }
45
+ function isDifference(step) {
46
+ return step.kind !== "kept";
47
+ }
48
+ function rowAt({ index, cells }) {
49
+ return { index, cells };
50
+ }
51
+ function rowsOf(table) {
52
+ return table.children.map((row, index) => {
53
+ const cells = row.children.map(storyText);
54
+ const text = JSON.stringify(cells);
55
+ return { index, cells, identity: text, text };
56
+ });
57
+ }
58
+ function rowChanges(original, revised) {
59
+ const was = rowsOf(original);
60
+ const now = rowsOf(revised);
61
+ const rowChange = (step) => {
62
+ switch (step.kind) {
63
+ case "added":
64
+ return { kind: "added", ...rowAt(now[step.revised]) };
65
+ case "removed":
66
+ return { kind: "removed", ...rowAt(was[step.original]) };
67
+ case "changed":
68
+ return {
69
+ kind: "changed",
70
+ original: rowAt(was[step.original]),
71
+ revised: rowAt(now[step.revised])
72
+ };
73
+ }
74
+ };
75
+ return alignSequences(was, now).filter(isDifference).map(rowChange);
76
+ }
77
+ function blockAt({ pos, text }) {
78
+ return { pos, text };
79
+ }
80
+ function changedBlock(from, to, change) {
81
+ const original = blockAt(from);
82
+ const revised = blockAt(to);
83
+ if (to.block === "table") {
84
+ const rows = rowChanges(from.node, to.node);
85
+ return change === "text" ? {
86
+ kind: "changed",
87
+ block: to.block,
88
+ original,
89
+ revised,
90
+ change,
91
+ edits: diffText(from.text, to.text),
92
+ rows
93
+ } : { kind: "changed", block: to.block, original, revised, change, rows };
94
+ }
95
+ return change === "text" ? {
96
+ kind: "changed",
97
+ block: to.block,
98
+ original,
99
+ revised,
100
+ change,
101
+ edits: diffText(from.text, to.text)
102
+ } : { kind: "changed", block: to.block, original, revised, change };
103
+ }
104
+ function blockChanges(original, revised) {
105
+ const was = blocksOf(original, "original");
106
+ const now = blocksOf(revised, "revised");
107
+ const blockChange = (step) => {
108
+ switch (step.kind) {
109
+ case "added": {
110
+ const block = now[step.revised];
111
+ return { kind: "added", block: block.block, ...blockAt(block) };
112
+ }
113
+ case "removed": {
114
+ const block = was[step.original];
115
+ return { kind: "removed", block: block.block, ...blockAt(block) };
116
+ }
117
+ case "changed":
118
+ return changedBlock(was[step.original], now[step.revised], step.change);
119
+ }
120
+ };
121
+ return alignSequences(was, now, (block) => block.block).filter(isDifference).map(blockChange);
122
+ }
123
+ function commentsOf(doc) {
124
+ const read = /* @__PURE__ */ new Map();
125
+ const add = (comment) => {
126
+ if (read.has(comment.id)) return;
127
+ read.set(comment.id, {
128
+ id: comment.id,
129
+ author: comment.author,
130
+ authorId: comment.authorId,
131
+ text: storyText(storyOf(doc, storyKey("comment", comment.id)))
132
+ });
133
+ };
134
+ for (const comment of commentReferencesIn(doc).values()) {
135
+ add(comment);
136
+ comment.replies.forEach(add);
137
+ }
138
+ return read;
139
+ }
140
+ function sameAuthor(original, revised) {
141
+ return original.authorId === null && revised.authorId === null ? original.author === revised.author : original.authorId === revised.authorId;
142
+ }
143
+ function commentChanges(original, revised) {
144
+ const was = commentsOf(original);
145
+ const now = commentsOf(revised);
146
+ const changes = [];
147
+ for (const [id, comment] of was) {
148
+ const current = now.get(id);
149
+ if (current === void 0 || !sameAuthor(comment, current)) {
150
+ changes.push({ kind: "removed", ...comment });
151
+ } else if (current.text !== comment.text) {
152
+ changes.push({
153
+ kind: "changed",
154
+ id,
155
+ author: current.author,
156
+ authorId: current.authorId,
157
+ original: comment.text,
158
+ revised: current.text
159
+ });
160
+ }
161
+ }
162
+ for (const [id, comment] of now) {
163
+ const earlier = was.get(id);
164
+ if (earlier === void 0 || !sameAuthor(earlier, comment)) {
165
+ changes.push({ kind: "added", ...comment });
166
+ }
167
+ }
168
+ return changes;
169
+ }
170
+ function partChanges(original, revised) {
171
+ const was = original.session;
172
+ const now = revised.session;
173
+ const sameMainPart = was.mainPartPath === now.mainPartPath;
174
+ const compared = /* @__PURE__ */ new Set([
175
+ was.mainPartPath,
176
+ now.mainPartPath,
177
+ ...policyPartPaths(commentsPolicy, was),
178
+ ...policyPartPaths(commentsPolicy, now)
179
+ ]);
180
+ const declarations = sameMainPart ? declarationsKept(commentsPolicy, was, now, "gained or dropped") : /* @__PURE__ */ new Map();
181
+ const changes = sameMainPart ? [] : [
182
+ { part: was.mainPartPath, kind: "removed" },
183
+ { part: now.mainPartPath, kind: "added" }
184
+ ];
185
+ for (const part of /* @__PURE__ */ new Set([...was.parts.keys(), ...now.parts.keys()])) {
186
+ if (compared.has(part) || declarations.get(part) === true) continue;
187
+ const arrived = was.parts.get(part);
188
+ const current = now.parts.get(part);
189
+ if (arrived !== void 0 && current !== void 0 && sameBytes(arrived, current)) {
190
+ continue;
191
+ }
192
+ changes.push({
193
+ part,
194
+ kind: arrived === void 0 ? "added" : current === void 0 ? "removed" : "changed"
195
+ });
196
+ }
197
+ if (sameMainPart && aroundTheBlocks(original) !== aroundTheBlocks(revised)) {
198
+ changes.push({ part: was.mainPartPath, kind: "changed" });
199
+ }
200
+ return changes.sort(
201
+ (a, b) => a.part < b.part ? -1 : a.part > b.part ? 1 : 0
202
+ );
203
+ }
204
+ function compareDocx(original, revised, { xmlParser } = {}) {
205
+ return withXmlParser(xmlParser, () => {
206
+ const was = importDocx(original);
207
+ const now = importDocx(revised);
208
+ return {
209
+ blocks: blockChanges(was, now),
210
+ comments: commentChanges(was.doc, now.doc),
211
+ parts: partChanges(was, now)
212
+ };
213
+ });
214
+ }
215
+ export {
216
+ BLOCK_KINDS,
217
+ compareDocx
218
+ };
@@ -9,7 +9,7 @@ import {
9
9
  serializeXml
10
10
  } from "../ooxml/xml.js";
11
11
  import { docxSchema } from "../schema/index.js";
12
- import { commentParaId, importedCommentReplies } from "./comments/model.js";
12
+ import { importedCommentReplies, seededHexId } from "./comments/model.js";
13
13
  import { NO_COMMENTS } from "./comments/reading.js";
14
14
  import { readParagraphFormat, readRunFormat } from "./formatting.js";
15
15
  import { NO_LINK_TARGETS } from "./hyperlink.js";
@@ -75,7 +75,7 @@ function buildModelledRunChild(el, marks, images, comments) {
75
75
  const id = annotationId(el);
76
76
  if (id === null) return null;
77
77
  const comment = comments.byId.get(id);
78
- const paraId = comment?.paraId ?? commentParaId(`comment-${id}`);
78
+ const paraId = comment?.paraId ?? seededHexId(`comment-${id}`);
79
79
  return [
80
80
  docxSchema.nodes.commentReference.create(
81
81
  {
@@ -89,7 +89,9 @@ function buildModelledRunChild(el, marks, images, comments) {
89
89
  resolved: comment?.resolved ?? false,
90
90
  extensionXml: comment?.extensionXml ?? null,
91
91
  threadImported: true,
92
- replies: importedCommentReplies(comments, id)
92
+ replies: importedCommentReplies(comments, id),
93
+ durableId: comment?.durableId ?? null,
94
+ dateUtc: comment?.dateUtc ?? null
93
95
  },
94
96
  null,
95
97
  marks
@@ -24,17 +24,15 @@ import {
24
24
  extensionsChanged,
25
25
  extensionsRootProblem
26
26
  } from "./comments.js";
27
+ import { rewrittenDurableParts } from "./comments/durable.js";
28
+ import { currentCommentBodies } from "./comments/model.js";
27
29
  import {
28
30
  commentsExtendedPart,
29
31
  commentsPart,
30
32
  peoplePart
31
33
  } from "./comments/parts.js";
32
34
  import { unrecordedAuthors } from "./comments/people.js";
33
- import {
34
- COMMENT_MARKUP,
35
- currentCommentBodies,
36
- EXTENSIONS_MARKUP
37
- } from "./comments/writing.js";
35
+ import { commentsMarkup, EXTENSIONS_MARKUP } from "./comments/writing.js";
38
36
  import { HEADER_MARKUP } from "./headersFooters.js";
39
37
  import { identityProblems, identityProblemsInStories } from "./identities.js";
40
38
  import { insertedImageSrcs } from "./media.js";
@@ -379,6 +377,16 @@ var commentPartRoots = {
379
377
  });
380
378
  }
381
379
  }
380
+ if (commentsChanged(doc, session)) {
381
+ for (const part of rewrittenDurableParts(doc, session)) {
382
+ if (part.rootProblem === null) continue;
383
+ problems.push({
384
+ code: "malformed-xml",
385
+ message: part.rootProblem,
386
+ reason: { kind: "unwritable-part-root", part: part.name }
387
+ });
388
+ }
389
+ }
382
390
  return problems;
383
391
  }
384
392
  };
@@ -408,11 +416,16 @@ function rewrittenParts(doc, session) {
408
416
  }
409
417
  const { xml, extendedXml, extendedPartPath } = session.comments;
410
418
  if (commentsChanged(doc, session)) {
411
- add(commentsPart.pathIn(session), xml, COMMENT_MARKUP);
419
+ add(commentsPart.pathIn(session), xml, commentsMarkup(doc, session));
412
420
  }
413
421
  if (extensionsChanged(doc, session) && (commentReferencesIn(doc).size > 0 || extendedPartPath !== null)) {
414
422
  add(commentsExtendedPart.pathIn(session), extendedXml, EXTENSIONS_MARKUP);
415
423
  }
424
+ if (commentsChanged(doc, session)) {
425
+ for (const part of rewrittenDurableParts(doc, session)) {
426
+ add(part.path, part.xml, part.declarations);
427
+ }
428
+ }
416
429
  if (startedLists(doc, session).defined.size > 0) {
417
430
  const path = session.numberingPartPath;
418
431
  add(path, readPart(session.parts, path), NUMBERING_MARKUP);
@@ -8,6 +8,8 @@
8
8
  * related part lived in five places.
9
9
  */
10
10
  export declare const CONTENT_TYPES_PATH = "[Content_Types].xml";
11
+ /** Whether two parts hold the same bytes, which is the only sameness a part nothing reads has */
12
+ export declare function sameBytes(before: Uint8Array, after: Uint8Array): boolean;
11
13
  /**
12
14
  * Where the part the main part relates under this type sits. null for no such relationship, and
13
15
  * for one that points outside the package, which no part of it can stand behind.
@@ -15,6 +15,9 @@ import {
15
15
  resolveTarget
16
16
  } from "./relationships.js";
17
17
  var CONTENT_TYPES_PATH = "[Content_Types].xml";
18
+ function sameBytes(before, after) {
19
+ return before.length === after.length && before.every((byte, index) => byte === after[index]);
20
+ }
18
21
  function relatedPartPath(parts, mainPartPath, type) {
19
22
  const relationship = readRelationships(parts, relsPathOf(mainPartPath)).find(
20
23
  (entry) => entry.type === type && !entry.external
@@ -147,5 +150,6 @@ export {
147
150
  declaredXmlParts,
148
151
  markupParts,
149
152
  readPart,
150
- relatedPartPath
153
+ relatedPartPath,
154
+ sameBytes
151
155
  };
@@ -17,6 +17,18 @@ export interface PartPlanContext {
17
17
  /** Where a story writer records an approximation it had to make, beside the body writer's */
18
18
  readonly notes: FidelityCollector;
19
19
  }
20
+ /** A part a planner may add, as the package relates and declares it */
21
+ export interface DeclaredPart {
22
+ readonly relType: string;
23
+ readonly contentType: string;
24
+ pathIn(session: SessionStore): string | null;
25
+ writePathIn(session: SessionStore): string;
26
+ }
27
+ /**
28
+ * Where the part is written: related from the main part where the package relates none, and
29
+ * declared in `[Content_Types].xml` where its bytes did not arrive.
30
+ */
31
+ export declare function declarePart(part: DeclaredPart, session: SessionStore, context: PartPlanContext, arrivedXml: string | null): string;
20
32
  export interface PartPlanner {
21
33
  readonly name: string;
22
34
  /** The parts to write, keyed by path, and null when the document gives this planner nothing to write */
@@ -5,7 +5,24 @@ import {
5
5
  CONTENT_TYPES_PATH,
6
6
  markupParts
7
7
  } from "./packageParts.js";
8
- import { relsPathOf } from "./relationships.js";
8
+ import {
9
+ directoryOf,
10
+ relsPathOf
11
+ } from "./relationships.js";
12
+ function declarePart(part, session, context, arrivedXml) {
13
+ const adding = part.pathIn(session) === null;
14
+ const path = part.writePathIn(session);
15
+ if (adding) {
16
+ context.relationships.add({
17
+ type: part.relType,
18
+ target: path.slice(directoryOf(session.mainPartPath).length)
19
+ });
20
+ }
21
+ if (adding || arrivedXml === null) {
22
+ context.contentTypes.addOverride(path, part.contentType);
23
+ }
24
+ return path;
25
+ }
9
26
  function runPartPlanners(planners, doc, session, context, prior = /* @__PURE__ */ new Map()) {
10
27
  const owned = new Set(
11
28
  [
@@ -53,5 +70,6 @@ function assertPartsParse(replacements, contentTypes) {
53
70
  }
54
71
  export {
55
72
  assertPartsParse,
73
+ declarePart,
56
74
  runPartPlanners
57
75
  };
@@ -36,6 +36,10 @@ export interface PolicyOptions {
36
36
  export interface VerifyOptions extends PolicyOptions {
37
37
  xmlParser?: XmlParser;
38
38
  }
39
+ export interface JudgedPackages {
40
+ arrived: SessionStore;
41
+ submitted: SessionStore;
42
+ }
39
43
  /** One package part a protection lets an editor rewrite, and how a rewritten entry is judged */
40
44
  export interface StoryPartKind {
41
45
  relType: string;
@@ -75,12 +79,11 @@ export interface StoryPartKind {
75
79
  anyonesChange(entry: Element, original: Element): boolean;
76
80
  /**
77
81
  * Permission alone: whether `authorId` may have written (`original === null`) or rewritten this
78
- * entry under `options`. `session` is the submission's, so a kind can look across at a sibling
79
- * part, the way a comment's author resolves through the people part. `unattributed` is the
80
- * display names the file that arrived writes comments under while recording nobody for them,
81
- * which is what an entry claiming no identity is held against.
82
+ * entry under `options`. `packages` lets a kind look across at a sibling part or back at what
83
+ * arrived. `unattributed` is the display names the file that arrived writes comments under while
84
+ * recording nobody for them, which is what an entry claiming no identity is held against.
82
85
  */
83
- allowed(entry: Element, original: Element | null, authorId: string, options: PolicyOptions, session: SessionStore, unattributed: ReadonlySet<string>): boolean;
86
+ allowed(entry: Element, original: Element | null, authorId: string, options: PolicyOptions, packages: JudgedPackages, unattributed: ReadonlySet<string>): boolean;
84
87
  }
85
88
  /** The two reasons the package comparison answers with, whatever the policy */
86
89
  export type PackageReason = "part-changed" | "relationship-changed";
@@ -113,6 +116,31 @@ export declare function registerProtectionPolicy<S extends string, P extends str
113
116
  * `readOnly` among them.
114
117
  */
115
118
  export declare function protectionPolicyFor(level: EditingProtection): ProtectionPolicy<string, string> | undefined;
119
+ /** The paths of the policy's parts this package holds, which is what a comparison of its bytes leaves to the policy */
120
+ export declare function policyPartPaths(policy: ProtectionPolicy<string, string>, session: SessionStore): string[];
121
+ /**
122
+ * The main document part with every block of the body taken out of it: the namespaces the story
123
+ * is written under, the text around the body, and the section properties that set the paper and
124
+ * its margins. A comment is written inside a paragraph, so nothing a comment edit writes reaches
125
+ * this text.
126
+ *
127
+ * The section closing the body is read off the document node rather than out of the tail
128
+ * (`docx/sections`), and it belongs here for the same reason the rest does: a submission is free
129
+ * to rewrite the paper it is written on, and a comparison of the blocks would not see it.
130
+ */
131
+ export declare function aroundTheBlocks(story: Story): string;
132
+ /**
133
+ * Which of the policy's declarations two packages differ by: `gained` lets the revised package add
134
+ * them, the way a submission writing its first comment does, and `gained or dropped` also lets it
135
+ * give them up, the way a file that lost its last comment may.
136
+ */
137
+ export type DeclarationChange = "gained" | "gained or dropped";
138
+ /**
139
+ * Whether each of the package's two declaration parts, the main part's relationships and
140
+ * `[Content_Types].xml`, differs between the two packages in nothing but what the policy's parts
141
+ * need, keyed by the part's path.
142
+ */
143
+ export declare function declarationsKept(policy: ProtectionPolicy<string, string>, before: SessionStore, after: SessionStore, allowed: DeclarationChange): ReadonlyMap<string, boolean>;
116
144
  /**
117
145
  * Whether the parts this protection lets an edit rewrite came back holding only entries this
118
146
  * editor writes, each of them one this author was in a position to write.
@@ -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