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.mjs CHANGED
@@ -2874,14 +2874,38 @@ var DocxDiffEditor = forwardRef(
2874
2874
  const instanceId = useRef(`dde-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`);
2875
2875
  const editorId = `dde-editor-${instanceId.current}`;
2876
2876
  const toolbarId = `dde-toolbar-${instanceId.current}`;
2877
+ const sanitizeJson = useCallback((node) => {
2878
+ if (node.type === "text") {
2879
+ if (!node.text || node.text === "") {
2880
+ return null;
2881
+ }
2882
+ return node;
2883
+ }
2884
+ if (node.content && Array.isArray(node.content)) {
2885
+ const cleanedContent = node.content.map((child) => sanitizeJson(child)).filter((child) => child !== null);
2886
+ if (node.type === "run" && cleanedContent.length === 0) {
2887
+ return null;
2888
+ }
2889
+ return {
2890
+ ...node,
2891
+ content: cleanedContent.length > 0 ? cleanedContent : void 0
2892
+ };
2893
+ }
2894
+ return node;
2895
+ }, []);
2877
2896
  const setEditorContent = useCallback((editor, json) => {
2878
2897
  const { state, view } = editor;
2879
2898
  if (state?.doc && view && json.content) {
2880
- const newDoc = state.schema.nodeFromJSON(json);
2899
+ const sanitized = sanitizeJson(json);
2900
+ if (!sanitized || !sanitized.content) {
2901
+ console.warn("[DocxDiffEditor] Sanitized JSON has no content");
2902
+ return;
2903
+ }
2904
+ const newDoc = state.schema.nodeFromJSON(sanitized);
2881
2905
  const tr = state.tr.replaceWith(0, state.doc.content.size, newDoc.content);
2882
2906
  view.dispatch(tr);
2883
2907
  }
2884
- }, []);
2908
+ }, [sanitizeJson]);
2885
2909
  const enableReviewMode = useCallback((sd) => {
2886
2910
  if (sd.setTrackedChangesPreferences) {
2887
2911
  sd.setTrackedChangesPreferences({ mode: "review", enabled: true });
@@ -3254,7 +3278,8 @@ var DocxDiffEditor = forwardRef(
3254
3278
  }
3255
3279
  newJson = content;
3256
3280
  }
3257
- const normalizedNewJson = normalizeRunProperties(newJson);
3281
+ const cleanNewJson = acceptAllChangesInJson(newJson) || { type: "doc", content: [] };
3282
+ const normalizedNewJson = normalizeRunProperties(cleanNewJson);
3258
3283
  const structuralResult = mergeWithStructuralAwareness(
3259
3284
  cleanBaseline,
3260
3285
  normalizedNewJson,
@@ -3263,7 +3288,7 @@ var DocxDiffEditor = forwardRef(
3263
3288
  const merged = structuralResult.mergedDoc;
3264
3289
  const structInfos = structuralResult.structuralInfos;
3265
3290
  setMergedJson(merged);
3266
- const diff = diffDocuments(cleanBaseline, newJson);
3291
+ const diff = diffDocuments(cleanBaseline, cleanNewJson);
3267
3292
  setDiffResult(diff);
3268
3293
  if (superdocRef.current?.activeEditor) {
3269
3294
  setEditorContent(superdocRef.current.activeEditor, merged);