docx-diff-editor 1.0.52 → 1.0.54

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/README.md CHANGED
@@ -51,6 +51,10 @@ function App() {
51
51
  );
52
52
 
53
53
  console.log(`Found ${result.totalChanges} changes`);
54
+
55
+ // Note: Subsequent compareWith calls compare against current editor state
56
+ // (with track changes accepted), not the original source.
57
+ // To compare against original again, call setSource() first.
54
58
  };
55
59
 
56
60
  return (
@@ -117,7 +121,8 @@ interface DocxDiffEditorRef {
117
121
  // Set the source/base document
118
122
  setSource(content: DocxContent): Promise<void>;
119
123
 
120
- // Compare and show track changes
124
+ // Compare current editor content with new content, show track changes
125
+ // Note: Compares against current editor state (not original source)
121
126
  compareWith(content: DocxContent): Promise<ComparisonResult>;
122
127
 
123
128
  // Get diff data
package/dist/index.js CHANGED
@@ -2882,14 +2882,38 @@ var DocxDiffEditor = react.forwardRef(
2882
2882
  const instanceId = react.useRef(`dde-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`);
2883
2883
  const editorId = `dde-editor-${instanceId.current}`;
2884
2884
  const toolbarId = `dde-toolbar-${instanceId.current}`;
2885
+ const sanitizeJson = react.useCallback((node) => {
2886
+ if (node.type === "text") {
2887
+ if (!node.text || node.text === "") {
2888
+ return null;
2889
+ }
2890
+ return node;
2891
+ }
2892
+ if (node.content && Array.isArray(node.content)) {
2893
+ const cleanedContent = node.content.map((child) => sanitizeJson(child)).filter((child) => child !== null);
2894
+ if (node.type === "run" && cleanedContent.length === 0) {
2895
+ return null;
2896
+ }
2897
+ return {
2898
+ ...node,
2899
+ content: cleanedContent.length > 0 ? cleanedContent : void 0
2900
+ };
2901
+ }
2902
+ return node;
2903
+ }, []);
2885
2904
  const setEditorContent = react.useCallback((editor, json) => {
2886
2905
  const { state, view } = editor;
2887
2906
  if (state?.doc && view && json.content) {
2888
- const newDoc = state.schema.nodeFromJSON(json);
2907
+ const sanitized = sanitizeJson(json);
2908
+ if (!sanitized || !sanitized.content) {
2909
+ console.warn("[DocxDiffEditor] Sanitized JSON has no content");
2910
+ return;
2911
+ }
2912
+ const newDoc = state.schema.nodeFromJSON(sanitized);
2889
2913
  const tr = state.tr.replaceWith(0, state.doc.content.size, newDoc.content);
2890
2914
  view.dispatch(tr);
2891
2915
  }
2892
- }, []);
2916
+ }, [sanitizeJson]);
2893
2917
  const enableReviewMode = react.useCallback((sd) => {
2894
2918
  if (sd.setTrackedChangesPreferences) {
2895
2919
  sd.setTrackedChangesPreferences({ mode: "review", enabled: true });
@@ -3185,17 +3209,27 @@ var DocxDiffEditor = react.forwardRef(
3185
3209
  }
3186
3210
  },
3187
3211
  /**
3188
- * Compare source with new content, show track changes
3212
+ * Compare current editor content with new content, show track changes.
3213
+ *
3214
+ * The comparison uses the current editor state (with any existing track
3215
+ * changes accepted/stripped) as the baseline. This means if you've made
3216
+ * edits or accepted previous comparisons, those become the new baseline.
3217
+ *
3218
+ * To compare against the original source document, call setSource() again
3219
+ * before compareWith().
3189
3220
  */
3190
3221
  async compareWith(content) {
3191
3222
  if (!SuperDocRef.current) {
3192
3223
  throw new Error("Editor not initialized");
3193
3224
  }
3194
- if (!sourceJson) {
3195
- throw new Error("No source document set. Call setSource() first.");
3225
+ if (!superdocRef.current?.activeEditor) {
3226
+ throw new Error("Editor not ready. Ensure a document is loaded first.");
3196
3227
  }
3197
3228
  setIsLoading(true);
3198
3229
  try {
3230
+ const currentEditorJson = superdocRef.current.activeEditor.getJSON();
3231
+ const cleanBaseline = acceptAllChangesInJson(currentEditorJson) || { type: "doc", content: [] };
3232
+ setSourceJson(cleanBaseline);
3199
3233
  const contentType = detectContentType(content);
3200
3234
  let newJson;
3201
3235
  if (contentType === "file") {
@@ -3254,14 +3288,14 @@ var DocxDiffEditor = react.forwardRef(
3254
3288
  }
3255
3289
  const normalizedNewJson = normalizeRunProperties(newJson);
3256
3290
  const structuralResult = mergeWithStructuralAwareness(
3257
- sourceJson,
3291
+ cleanBaseline,
3258
3292
  normalizedNewJson,
3259
3293
  author
3260
3294
  );
3261
3295
  const merged = structuralResult.mergedDoc;
3262
3296
  const structInfos = structuralResult.structuralInfos;
3263
3297
  setMergedJson(merged);
3264
- const diff = diffDocuments(sourceJson, newJson);
3298
+ const diff = diffDocuments(cleanBaseline, newJson);
3265
3299
  setDiffResult(diff);
3266
3300
  if (superdocRef.current?.activeEditor) {
3267
3301
  setEditorContent(superdocRef.current.activeEditor, merged);