docx-diff-editor 1.0.24 → 1.0.27

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.
package/dist/index.d.mts CHANGED
@@ -228,12 +228,16 @@ declare function mergeDocuments(docA: ProseMirrorNode, docB: ProseMirrorNode, di
228
228
 
229
229
  /**
230
230
  * Create a trackInsert mark.
231
+ * @param author - The author of the change
232
+ * @param id - Optional ID to use (for linking with corresponding delete in replacements)
231
233
  */
232
- declare function createTrackInsertMark(author?: TrackChangeAuthor): ProseMirrorMark;
234
+ declare function createTrackInsertMark(author?: TrackChangeAuthor, id?: string): ProseMirrorMark;
233
235
  /**
234
236
  * Create a trackDelete mark.
237
+ * @param author - The author of the change
238
+ * @param id - Optional ID to use (for linking with corresponding insert in replacements)
235
239
  */
236
- declare function createTrackDeleteMark(author?: TrackChangeAuthor): ProseMirrorMark;
240
+ declare function createTrackDeleteMark(author?: TrackChangeAuthor, id?: string): ProseMirrorMark;
237
241
  /**
238
242
  * Create a trackFormat mark.
239
243
  */
package/dist/index.d.ts CHANGED
@@ -228,12 +228,16 @@ declare function mergeDocuments(docA: ProseMirrorNode, docB: ProseMirrorNode, di
228
228
 
229
229
  /**
230
230
  * Create a trackInsert mark.
231
+ * @param author - The author of the change
232
+ * @param id - Optional ID to use (for linking with corresponding delete in replacements)
231
233
  */
232
- declare function createTrackInsertMark(author?: TrackChangeAuthor): ProseMirrorMark;
234
+ declare function createTrackInsertMark(author?: TrackChangeAuthor, id?: string): ProseMirrorMark;
233
235
  /**
234
236
  * Create a trackDelete mark.
237
+ * @param author - The author of the change
238
+ * @param id - Optional ID to use (for linking with corresponding insert in replacements)
235
239
  */
236
- declare function createTrackDeleteMark(author?: TrackChangeAuthor): ProseMirrorMark;
240
+ declare function createTrackDeleteMark(author?: TrackChangeAuthor, id?: string): ProseMirrorMark;
237
241
  /**
238
242
  * Create a trackFormat mark.
239
243
  */
package/dist/index.js CHANGED
@@ -280,11 +280,11 @@ function diffDocuments(docA, docB) {
280
280
  summary
281
281
  };
282
282
  }
283
- function createTrackInsertMark(author = DEFAULT_AUTHOR) {
283
+ function createTrackInsertMark(author = DEFAULT_AUTHOR, id) {
284
284
  return {
285
285
  type: "trackInsert",
286
286
  attrs: {
287
- id: uuid.v4(),
287
+ id: id ?? uuid.v4(),
288
288
  author: author.name,
289
289
  authorEmail: author.email,
290
290
  authorImage: "",
@@ -292,11 +292,11 @@ function createTrackInsertMark(author = DEFAULT_AUTHOR) {
292
292
  }
293
293
  };
294
294
  }
295
- function createTrackDeleteMark(author = DEFAULT_AUTHOR) {
295
+ function createTrackDeleteMark(author = DEFAULT_AUTHOR, id) {
296
296
  return {
297
297
  type: "trackDelete",
298
298
  attrs: {
299
- id: uuid.v4(),
299
+ id: id ?? uuid.v4(),
300
300
  author: author.name,
301
301
  authorEmail: author.email,
302
302
  authorImage: "",
@@ -337,17 +337,30 @@ function mergeDocuments(docA, docB, diffResult, author = DEFAULT_AUTHOR) {
337
337
  return null;
338
338
  }
339
339
  let docAOffset = 0;
340
- for (const segment of diffResult.segments) {
340
+ const segments = diffResult.segments;
341
+ for (let segIdx = 0; segIdx < segments.length; segIdx++) {
342
+ const segment = segments[segIdx];
341
343
  if (segment.type === "equal") {
342
344
  for (let i = 0; i < segment.text.length; i++) {
343
345
  charStates[docAOffset + i] = { type: "equal" };
344
346
  }
345
347
  docAOffset += segment.text.length;
346
348
  } else if (segment.type === "delete") {
349
+ const nextSegment = segments[segIdx + 1];
350
+ const isReplacement = nextSegment && nextSegment.type === "insert";
351
+ const replacementId = isReplacement ? uuid.v4() : void 0;
347
352
  for (let i = 0; i < segment.text.length; i++) {
348
- charStates[docAOffset + i] = { type: "delete" };
353
+ charStates[docAOffset + i] = { type: "delete", replacementId };
349
354
  }
350
355
  docAOffset += segment.text.length;
356
+ if (isReplacement && nextSegment) {
357
+ insertions.push({
358
+ afterOffset: docAOffset,
359
+ text: nextSegment.text,
360
+ replacementId
361
+ });
362
+ segIdx++;
363
+ }
351
364
  } else if (segment.type === "insert") {
352
365
  insertions.push({
353
366
  afterOffset: docAOffset,
@@ -368,7 +381,7 @@ function mergeDocuments(docA, docB, diffResult, author = DEFAULT_AUTHOR) {
368
381
  result.push({
369
382
  type: "text",
370
383
  text: ins.text,
371
- marks: [...node.marks || [], createTrackInsertMark(author)]
384
+ marks: [...node.marks || [], createTrackInsertMark(author, ins.replacementId)]
372
385
  });
373
386
  }
374
387
  const currentFormatChange = getFormatChangeAt(nodeOffset + i);
@@ -384,7 +397,7 @@ function mergeDocuments(docA, docB, diffResult, author = DEFAULT_AUTHOR) {
384
397
  const chunk = text.substring(i, j);
385
398
  let marks = [...node.marks || []];
386
399
  if (charState.type === "delete") {
387
- marks.push(createTrackDeleteMark(author));
400
+ marks.push(createTrackDeleteMark(author, charState.replacementId));
388
401
  } else if (charState.type === "equal") {
389
402
  if (currentFormatChange) {
390
403
  const trackFormatMark = createTrackFormatMark(
@@ -408,7 +421,7 @@ function mergeDocuments(docA, docB, diffResult, author = DEFAULT_AUTHOR) {
408
421
  result.push({
409
422
  type: "text",
410
423
  text: ins.text,
411
- marks: [...node.marks || [], createTrackInsertMark(author)]
424
+ marks: [...node.marks || [], createTrackInsertMark(author, ins.replacementId)]
412
425
  });
413
426
  }
414
427
  insertions = insertions.filter(
@@ -453,7 +466,7 @@ function mergeDocuments(docA, docB, diffResult, author = DEFAULT_AUTHOR) {
453
466
  {
454
467
  type: "text",
455
468
  text: ins.text,
456
- marks: [createTrackInsertMark(author)]
469
+ marks: [createTrackInsertMark(author, ins.replacementId)]
457
470
  }
458
471
  ]
459
472
  }
@@ -845,8 +858,8 @@ var DocxDiffEditor = react.forwardRef(
845
858
  console.error("Failed to initialize SuperDoc:", err);
846
859
  handleError(err instanceof Error ? err : new Error("Failed to load editor"));
847
860
  setIsLoading(false);
861
+ initRef.current = false;
848
862
  }
849
- initRef.current = false;
850
863
  }, [
851
864
  initialSource,
852
865
  showRulers,
@@ -861,12 +874,14 @@ var DocxDiffEditor = react.forwardRef(
861
874
  ]);
862
875
  react.useEffect(() => {
863
876
  mountedRef.current = true;
864
- initialize();
877
+ if (!initRef.current) {
878
+ initialize();
879
+ }
865
880
  return () => {
866
881
  mountedRef.current = false;
867
882
  destroySuperdoc();
868
883
  };
869
- }, [initialize, destroySuperdoc]);
884
+ }, []);
870
885
  react.useImperativeHandle(
871
886
  ref,
872
887
  () => ({