@portone/docx-editor 0.6.5 → 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 (56) hide show
  1. package/CHANGELOG.md +14 -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/importParagraph.js +5 -3
  27. package/dist/docx/invariants.js +19 -6
  28. package/dist/docx/packageParts.d.ts +2 -0
  29. package/dist/docx/packageParts.js +5 -1
  30. package/dist/docx/partPlan.d.ts +12 -0
  31. package/dist/docx/partPlan.js +19 -1
  32. package/dist/docx/protectionPolicy.d.ts +33 -5
  33. package/dist/docx/protectionPolicy.js +93 -47
  34. package/dist/docx/sequenceDiff.d.ts +56 -0
  35. package/dist/docx/sequenceDiff.js +252 -0
  36. package/dist/docx/storyProjection.d.ts +2 -0
  37. package/dist/docx/storyProjection.js +10 -9
  38. package/dist/docx/textDiff.d.ts +24 -0
  39. package/dist/docx/textDiff.js +38 -0
  40. package/dist/editor/commands/comments/editing.js +56 -20
  41. package/dist/editor/commands/comments/model.d.ts +3 -1
  42. package/dist/editor/editorDocument.d.ts +4 -0
  43. package/dist/editor/editorDocument.js +10 -0
  44. package/dist/editor/plugins/commentDecorations.js +6 -2
  45. package/dist/ooxml/errors.d.ts +1 -1
  46. package/dist/ooxml/names.d.ts +2 -0
  47. package/dist/ooxml/names.js +2 -0
  48. package/dist/ooxml/partSplice.d.ts +13 -3
  49. package/dist/ooxml/partSplice.js +45 -18
  50. package/dist/schema/attrRoles.js +3 -1
  51. package/dist/schema/docxSchema.js +10 -3
  52. package/dist/schema/protection.js +2 -1
  53. package/dist/ui/CommentsPanel.js +8 -13
  54. package/dist/ui/comments/commentDate.d.ts +7 -0
  55. package/dist/ui/comments/commentDate.js +17 -0
  56. package/package.json +6 -6
@@ -8,6 +8,8 @@ var NAMESPACES = {
8
8
  r: R_NS,
9
9
  w14: "http://schemas.microsoft.com/office/word/2010/wordml",
10
10
  w15: "http://schemas.microsoft.com/office/word/2012/wordml",
11
+ w16cid: "http://schemas.microsoft.com/office/word/2016/wordml/cid",
12
+ w16cex: "http://schemas.microsoft.com/office/word/2018/wordml/cex",
11
13
  mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
12
14
  wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
13
15
  a: "http://schemas.openxmlformats.org/drawingml/2006/main",
@@ -8,22 +8,32 @@
8
8
  * comment for the end of the part, and miss the root altogether when it is written under a
9
9
  * prefix or closes on itself.
10
10
  */
11
+ import { type XmlAttr } from "./element";
11
12
  import { type KnownPrefix } from "./names";
12
13
  /** One child to put into a part, named by the local part of its tag so the child order can place it */
13
14
  export interface PartChild {
14
15
  name: string;
15
16
  xml: string;
16
17
  }
18
+ /** A child the part already holds, as `keep` is handed it */
19
+ export interface HeldChild extends PartChild {
20
+ /** As its tag spells them, and null where they cannot be read */
21
+ attrs: readonly XmlAttr[] | null;
22
+ }
17
23
  /**
18
24
  * What goes into a part's root. Every field is optional and they compose: the children are
19
- * replaced first, then the inserts are placed among them, then the prepend and the append go on
20
- * either end, and last the opening tag is rewritten.
25
+ * replaced first, then the ones `keep` turns down are taken out, then the inserts are placed among
26
+ * them, then the prepend and the append go on either end, and last the opening tag is rewritten.
21
27
  */
22
28
  export interface PartSplice {
23
29
  /** The root's local name. Any prefix, a self-closing root, a prolog and comments are all handled */
24
30
  root: string;
25
- /** Children put at the spot `CHILD_ORDER[root]` lays down, ahead of the first child that order puts after them */
31
+ /** Takes out each child this answers false for, leaving the text between children */
32
+ keep?: (child: HeldChild) => boolean;
33
+ /** Children put at the spot `order` lays down, ahead of the first child that order puts after them */
26
34
  insert?: readonly PartChild[];
35
+ /** The order `insert` places by, for a root outside `wml.xsd`, which `CHILD_ORDER` is held to */
36
+ order?: readonly string[];
27
37
  /** Put in as the last children */
28
38
  append?: string;
29
39
  /** Put in right after the opening tag */
@@ -38,6 +38,11 @@ function partRootProblem(xml, root) {
38
38
  const reading = readRoot(xml, root);
39
39
  return "problem" in reading ? reading.problem : null;
40
40
  }
41
+ function tagAttributes(text, tag) {
42
+ return parseAttrs(
43
+ text.slice(tag.nameEnd, tag.end - (tag.kind === "empty" ? 2 : 1))
44
+ );
45
+ }
41
46
  function directChildren(inner) {
42
47
  const children = [];
43
48
  let depth = 0;
@@ -48,29 +53,55 @@ function directChildren(inner) {
48
53
  const tag = readTag(inner, lt);
49
54
  if (tag === null) return children;
50
55
  if (depth === 0 && (tag.kind === "open" || tag.kind === "empty")) {
51
- children.push({ name: localPart(tag.name), at: lt });
56
+ children.push({
57
+ name: localPart(tag.name),
58
+ at: lt,
59
+ end: tag.end,
60
+ open: tag
61
+ });
52
62
  }
53
63
  if (tag.kind === "open") depth += 1;
54
- if (tag.kind === "close") depth -= 1;
64
+ if (tag.kind === "close") {
65
+ depth -= 1;
66
+ const last = children.at(-1);
67
+ if (depth === 0 && last !== void 0) last.end = tag.end;
68
+ }
55
69
  at = tag.end;
56
70
  }
57
71
  }
58
- function insertionAt(children, name, order, root, end) {
72
+ function withoutChildren(inner, keep) {
73
+ let out = "";
74
+ let from = 0;
75
+ for (const { name, at, end, open } of directChildren(inner)) {
76
+ const xml = inner.slice(at, end);
77
+ if (keep({ name, xml, attrs: tagAttributes(inner, open) })) continue;
78
+ out += inner.slice(from, at);
79
+ from = end;
80
+ }
81
+ return out + inner.slice(from);
82
+ }
83
+ function insertionAt(children, name, order, root, end, fromRegistry) {
59
84
  const target = order.indexOf(name);
60
85
  if (target === -1) {
61
- throw new Error(
62
- `${name} is not a child the order of ${root} knows; add it to CHILD_ORDER`
63
- );
86
+ const fix = fromRegistry ? "; add it to CHILD_ORDER" : "";
87
+ throw new Error(`${name} is not a child the order of ${root} knows${fix}`);
64
88
  }
65
89
  const after = children.find((child) => order.indexOf(child.name) > target);
66
90
  return after === void 0 ? end : after.at;
67
91
  }
68
- function withInserted(inner, inserts, root) {
92
+ function withInserted(inner, inserts, root, given) {
69
93
  if (inserts.length === 0) return inner;
70
- const order = childOrderOf(root);
94
+ const order = given ?? childOrderOf(root);
71
95
  const children = directChildren(inner);
72
96
  const placed = inserts.map((child) => ({
73
- at: insertionAt(children, child.name, order, root, inner.length),
97
+ at: insertionAt(
98
+ children,
99
+ child.name,
100
+ order,
101
+ root,
102
+ inner.length,
103
+ given === void 0
104
+ ),
74
105
  xml: child.xml,
75
106
  rank: order.indexOf(child.name)
76
107
  })).sort((a, b) => a.at - b.at || a.rank - b.rank);
@@ -89,11 +120,9 @@ function splicePart(xml, splice) {
89
120
  }
90
121
  const { openAt, open, closeAt } = reading.root;
91
122
  const inner = closeAt === null ? "" : xml.slice(open.end, closeAt);
92
- const children = (splice.prepend ?? "") + withInserted(
93
- splice.replaceChildren ?? inner,
94
- splice.insert ?? [],
95
- splice.root
96
- ) + (splice.append ?? "");
123
+ const replaced = splice.replaceChildren ?? inner;
124
+ const kept = splice.keep === void 0 ? replaced : withoutChildren(replaced, splice.keep);
125
+ const children = (splice.prepend ?? "") + withInserted(kept, splice.insert ?? [], splice.root, splice.order) + (splice.append ?? "");
97
126
  const openTag = xml.slice(openAt, open.end);
98
127
  const opened = closeAt === null && children !== "" ? `${openTag.slice(0, -2)}>` : openTag;
99
128
  const head = xml.slice(0, openAt) + (splice.rootTag?.(opened) ?? opened);
@@ -124,16 +153,14 @@ function rootBindingConflict(xml, declarations) {
124
153
  const at = rootTagAt(xml);
125
154
  const tag = at === -1 ? null : readTag(xml, at);
126
155
  if (tag === null || tag.kind === "close") return null;
127
- const attrs = parseAttrs(
128
- xml.slice(tag.nameEnd, tag.end - (tag.kind === "empty" ? 2 : 1))
129
- );
156
+ const attrs = tagAttributes(xml, tag);
130
157
  return attrs === null ? null : conflictingPrefix(new Map(attrs), declarations);
131
158
  }
132
159
  function withDeclarations(openTag, declarations) {
133
160
  const { namespaces, ignorable } = declarations;
134
161
  const tag = readTag(openTag, 0);
135
162
  const closeLength = tag?.kind === "empty" ? 2 : 1;
136
- const attrs = tag === null ? null : parseAttrs(openTag.slice(tag.nameEnd, tag.end - closeLength));
163
+ const attrs = tag === null ? null : tagAttributes(openTag, tag);
137
164
  if (tag === null || attrs === null) {
138
165
  throw new DocxExportError(
139
166
  "malformed-xml",
@@ -148,7 +148,9 @@ var NODE_ATTR_ROLES = {
148
148
  extensionXml: { role: "source", class: "preserved" },
149
149
  replies: { role: "source", class: "model" },
150
150
  // Read to decide whether the extended comment part is rewritten at all; it is never written
151
- threadImported: { role: "session", class: "derived" }
151
+ threadImported: { role: "session", class: "derived" },
152
+ durableId: { role: "source", class: "identity" },
153
+ dateUtc: { role: "source", class: "model" }
152
154
  },
153
155
  noteReference: {
154
156
  kind: { role: "source", class: "model" },
@@ -864,7 +864,10 @@ var docxSchema = new Schema({
864
864
  resolved: { default: false },
865
865
  extensionXml: { default: null },
866
866
  threadImported: { default: false },
867
- replies: { default: [] }
867
+ replies: { default: [] },
868
+ // `date` is the author's wall clock and `dateUtc` the instant (`docx/comments/dates`)
869
+ durableId: { default: null },
870
+ dateUtc: { default: null }
868
871
  },
869
872
  toDOM(node) {
870
873
  return [
@@ -882,7 +885,9 @@ var docxSchema = new Schema({
882
885
  "data-comment-resolved": node.attrs.resolved === true ? "1" : void 0,
883
886
  "data-comment-extension-xml": text(node.attrs.extensionXml),
884
887
  "data-comment-thread-imported": node.attrs.threadImported === true ? "1" : void 0,
885
- "data-comment-replies": JSON.stringify(node.attrs.replies ?? [])
888
+ "data-comment-replies": JSON.stringify(node.attrs.replies ?? []),
889
+ "data-comment-durable-id": text(node.attrs.durableId),
890
+ "data-comment-date-utc": text(node.attrs.dateUtc)
886
891
  }
887
892
  ];
888
893
  },
@@ -915,7 +920,9 @@ var docxSchema = new Schema({
915
920
  resolved: dom.getAttribute("data-comment-resolved") === "1",
916
921
  extensionXml,
917
922
  threadImported: dom.getAttribute("data-comment-thread-imported") === "1",
918
- replies
923
+ replies,
924
+ durableId: dom.getAttribute("data-comment-durable-id"),
925
+ dateUtc: dom.getAttribute("data-comment-date-utc")
919
926
  };
920
927
  }
921
928
  }
@@ -39,6 +39,7 @@ function bodyOf(attrs, doc) {
39
39
  author: stringOrNull(attrs.author),
40
40
  initials: stringOrNull(attrs.initials),
41
41
  date: stringOrNull(attrs.date),
42
+ dateUtc: stringOrNull(attrs.dateUtc),
42
43
  body: id === null ? null : storyNodeOf(doc, storyKey("comment", id))
43
44
  };
44
45
  }
@@ -100,7 +101,7 @@ function sameIdentity(was, now) {
100
101
  return was.authorId === now.authorId && was.author === now.author;
101
102
  }
102
103
  function sameWords(was, now) {
103
- return sameStory(was.body, now.body) && was.initials === now.initials && was.date === now.date;
104
+ return sameStory(was.body, now.body) && was.initials === now.initials && was.date === now.date && was.dateUtc === now.dateUtc;
104
105
  }
105
106
  function commentIdentitiesKept(before, after) {
106
107
  const now = threadsIn(after);
@@ -20,18 +20,13 @@ import { commentOwned } from "../schema/protection.js";
20
20
  import { protectionOf } from "../schema/protectionState.js";
21
21
  import { editorClassNames } from "../styles/classNames.js";
22
22
  import { CommentComposer, FocusedTextarea } from "./comments/CommentComposer.js";
23
+ import { commentTime, shownCommentDate } from "./comments/commentDate.js";
23
24
  import {
24
25
  COMPOSER_POSITION,
25
26
  useCommentRailLayout
26
27
  } from "./comments/useCommentRailLayout.js";
27
28
  import { tooltipAttribute } from "./Tooltip.js";
28
29
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
29
- function shownDate(value) {
30
- if (value === null) return null;
31
- const date = new Date(value);
32
- if (Number.isNaN(date.valueOf())) return value;
33
- return `${date.toISOString().slice(0, 16).replace("T", " ")} UTC`;
34
- }
35
30
  function run(view, command) {
36
31
  const applied = command(
37
32
  view.state,
@@ -50,7 +45,7 @@ function shownBesideThePage(comment) {
50
45
  function CommentIdentity({
51
46
  comment
52
47
  }) {
53
- const date = shownDate(comment.date);
48
+ const date = shownCommentDate(comment.date);
54
49
  return /* @__PURE__ */ jsxs(Fragment, { children: [
55
50
  /* @__PURE__ */ jsx("span", { className: editorClassNames.commentAuthor, children: comment.author ?? "Unknown author" }),
56
51
  date && /* @__PURE__ */ jsx("span", { className: editorClassNames.commentDate, children: date })
@@ -79,13 +74,13 @@ function CommentsPanel({
79
74
  const composerSelectionPos = composerRange?.from ?? -1;
80
75
  const visible = useMemo(
81
76
  () => allCommentsOpen ? [...comments].sort((left, right) => {
82
- const leftTime = Date.parse(left.date ?? "");
83
- const rightTime = Date.parse(right.date ?? "");
84
- if (Number.isNaN(leftTime) && Number.isNaN(rightTime)) {
77
+ const leftTime = commentTime(left.date);
78
+ const rightTime = commentTime(right.date);
79
+ if (leftTime === null && rightTime === null) {
85
80
  return right.referencePos - left.referencePos;
86
81
  }
87
- if (Number.isNaN(leftTime)) return 1;
88
- if (Number.isNaN(rightTime)) return -1;
82
+ if (leftTime === null) return 1;
83
+ if (rightTime === null) return -1;
89
84
  return rightTime - leftTime;
90
85
  }) : comments.filter(shownBesideThePage),
91
86
  [allCommentsOpen, comments]
@@ -281,7 +276,7 @@ function CommentsPanel({
281
276
  children: [
282
277
  /* @__PURE__ */ jsxs("div", { className: editorClassNames.commentMeta, children: [
283
278
  /* @__PURE__ */ jsx("span", { className: editorClassNames.commentAuthor, children: reply.author ?? "Unknown author" }),
284
- shownDate(reply.date) && /* @__PURE__ */ jsx("span", { className: editorClassNames.commentDate, children: shownDate(reply.date) })
279
+ shownCommentDate(reply.date) && /* @__PURE__ */ jsx("span", { className: editorClassNames.commentDate, children: shownCommentDate(reply.date) })
285
280
  ] }),
286
281
  isEditing ? /* @__PURE__ */ jsx(
287
282
  FocusedTextarea,
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Null for a date that is not an instant as `toISOString` spells one: that is a date the document
3
+ * wrote as no time, which the browser would still read, `2026-02-30` as the second of March.
4
+ */
5
+ export declare function commentTime(value: string | null): number | null;
6
+ /** In the reader's zone and browser language; a date naming no instant is shown as written */
7
+ export declare function shownCommentDate(value: string | null, locales?: Intl.LocalesArgument): string | null;
@@ -0,0 +1,17 @@
1
+ // src/ui/comments/commentDate.ts
2
+ function commentTime(value) {
3
+ if (value === null) return null;
4
+ const time = Date.parse(value);
5
+ return Number.isNaN(time) || new Date(time).toISOString() !== value ? null : time;
6
+ }
7
+ function shownCommentDate(value, locales) {
8
+ const time = commentTime(value);
9
+ return time === null ? value : new Intl.DateTimeFormat(locales, {
10
+ dateStyle: "medium",
11
+ timeStyle: "short"
12
+ }).format(time);
13
+ }
14
+ export {
15
+ commentTime,
16
+ shownCommentDate
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portone/docx-editor",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "A simple DOCX editor for React, built directly on OOXML.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -73,15 +73,15 @@
73
73
  "react": ">=18"
74
74
  },
75
75
  "devDependencies": {
76
- "@biomejs/biome": "2.5.10",
76
+ "@biomejs/biome": "2.5.14",
77
77
  "@changesets/changelog-github": "^1.0.0",
78
78
  "@changesets/cli": "^3.0.1",
79
79
  "@microsoft/api-extractor": "7.59.1",
80
80
  "@playwright/test": "^1.62.1",
81
81
  "@types/jsdom": "^30.0.0",
82
82
  "@types/node": "^26.2.0",
83
- "@types/react": "19.2.18",
84
- "@types/react-dom": "19.2.7",
83
+ "@types/react": "19.3.0",
84
+ "@types/react-dom": "19.3.0",
85
85
  "@vitejs/plugin-react": "^6.0.5",
86
86
  "esbuild": "^0.28.1",
87
87
  "jsdom": "^30.0.1",
@@ -95,8 +95,8 @@
95
95
  "prosemirror-tables": "^1.8.5",
96
96
  "prosemirror-transform": "^1.12.0",
97
97
  "prosemirror-view": "^1.41.6",
98
- "react": "^19.2.8",
99
- "react-dom": "^19.2.8",
98
+ "react": "^19.3.0",
99
+ "react-dom": "^19.3.0",
100
100
  "typescript": "^7.0.2",
101
101
  "vite": "^8.0.12",
102
102
  "vitest": "^5.0.0"