docx-diff-editor 1.0.53 → 1.0.55

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.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 });
@@ -3262,7 +3286,8 @@ var DocxDiffEditor = react.forwardRef(
3262
3286
  }
3263
3287
  newJson = content;
3264
3288
  }
3265
- const normalizedNewJson = normalizeRunProperties(newJson);
3289
+ const cleanNewJson = acceptAllChangesInJson(newJson) || { type: "doc", content: [] };
3290
+ const normalizedNewJson = normalizeRunProperties(cleanNewJson);
3266
3291
  const structuralResult = mergeWithStructuralAwareness(
3267
3292
  cleanBaseline,
3268
3293
  normalizedNewJson,
@@ -3271,7 +3296,7 @@ var DocxDiffEditor = react.forwardRef(
3271
3296
  const merged = structuralResult.mergedDoc;
3272
3297
  const structInfos = structuralResult.structuralInfos;
3273
3298
  setMergedJson(merged);
3274
- const diff = diffDocuments(cleanBaseline, newJson);
3299
+ const diff = diffDocuments(cleanBaseline, cleanNewJson);
3275
3300
  setDiffResult(diff);
3276
3301
  if (superdocRef.current?.activeEditor) {
3277
3302
  setEditorContent(superdocRef.current.activeEditor, merged);