docx-diff-editor 1.0.59 → 1.0.61

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
@@ -822,17 +822,6 @@ async function parseHtmlToJson(html, SuperDoc) {
822
822
  }
823
823
  }, TIMEOUTS.CLEANUP_DELAY);
824
824
  };
825
- const createMockPasteEvent = (htmlContent) => {
826
- const dataTransfer = new DataTransfer();
827
- dataTransfer.setData("text/html", htmlContent);
828
- dataTransfer.setData("text/plain", "");
829
- const event = new ClipboardEvent("paste", {
830
- bubbles: true,
831
- cancelable: true,
832
- clipboardData: dataTransfer
833
- });
834
- return event;
835
- };
836
825
  const tryPasteApproach = (sd, onSuccess, onFail) => {
837
826
  try {
838
827
  const editor = sd?.activeEditor;
@@ -962,6 +951,40 @@ async function parseHtmlToJson(html, SuperDoc) {
962
951
  }, 50);
963
952
  });
964
953
  }
954
+ function syncNumberingToParent(childEditor, parentEditor) {
955
+ try {
956
+ const childNumbering = childEditor?.converter?.numbering;
957
+ const parentNumbering = parentEditor?.converter?.numbering;
958
+ if (!childNumbering || !parentNumbering) {
959
+ return;
960
+ }
961
+ if (childNumbering.definitions) {
962
+ parentNumbering.definitions = {
963
+ ...parentNumbering.definitions,
964
+ ...childNumbering.definitions
965
+ };
966
+ }
967
+ if (childNumbering.abstracts) {
968
+ parentNumbering.abstracts = {
969
+ ...parentNumbering.abstracts,
970
+ ...childNumbering.abstracts
971
+ };
972
+ }
973
+ parentEditor.converter.numbering = parentNumbering;
974
+ } catch (err) {
975
+ console.warn("[syncNumberingToParent] Failed to sync numbering definitions:", err);
976
+ }
977
+ }
978
+ function createMockPasteEvent(htmlContent) {
979
+ const dataTransfer = new DataTransfer();
980
+ dataTransfer.setData("text/html", htmlContent);
981
+ dataTransfer.setData("text/plain", "");
982
+ return new ClipboardEvent("paste", {
983
+ bubbles: true,
984
+ cancelable: true,
985
+ clipboardData: dataTransfer
986
+ });
987
+ }
965
988
  async function parseHtmlWithLinkedEditor(html, mainEditor) {
966
989
  const container = document.createElement("div");
967
990
  container.style.cssText = "position:absolute;top:-9999px;left:-9999px;width:800px;height:600px;visibility:hidden;";
@@ -983,16 +1006,26 @@ async function parseHtmlWithLinkedEditor(html, mainEditor) {
983
1006
  }
984
1007
  }, TIMEOUTS.CLEANUP_DELAY);
985
1008
  };
986
- try {
987
- mainEditor.createChildEditor({
988
- element: container,
989
- html,
990
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
991
- onCreate: ({ editor: localEditor }) => {
1009
+ const pasteAndExtract = (editor) => {
1010
+ try {
1011
+ if (!editor?.view?.pasteHTML) {
1012
+ throw new Error("pasteHTML not available on child editor");
1013
+ }
1014
+ editor.commands?.focus?.();
1015
+ if (editor.commands?.selectAll && editor.commands?.deleteSelection) {
1016
+ editor.commands.selectAll();
1017
+ editor.commands.deleteSelection();
1018
+ }
1019
+ const mockEvent = createMockPasteEvent(html);
1020
+ editor.view.pasteHTML(html, mockEvent);
1021
+ setTimeout(() => {
992
1022
  if (resolved) return;
993
1023
  try {
994
- childEditor = localEditor;
995
- const json = localEditor.getJSON();
1024
+ syncNumberingToParent(editor, mainEditor);
1025
+ const json = editor.getJSON();
1026
+ if (!json?.content?.length) {
1027
+ throw new Error("Paste produced empty document");
1028
+ }
996
1029
  const normalizedJson = normalizeRunProperties(json);
997
1030
  resolved = true;
998
1031
  cleanup();
@@ -1002,6 +1035,23 @@ async function parseHtmlWithLinkedEditor(html, mainEditor) {
1002
1035
  cleanup();
1003
1036
  reject(err);
1004
1037
  }
1038
+ }, 100);
1039
+ } catch (err) {
1040
+ resolved = true;
1041
+ cleanup();
1042
+ reject(err);
1043
+ }
1044
+ };
1045
+ try {
1046
+ mainEditor.createChildEditor({
1047
+ element: container,
1048
+ html: "<p></p>",
1049
+ // Minimal empty document - actual HTML pasted via pasteHTML
1050
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1051
+ onCreate: ({ editor: localEditor }) => {
1052
+ if (resolved) return;
1053
+ childEditor = localEditor;
1054
+ pasteAndExtract(localEditor);
1005
1055
  },
1006
1056
  onError: (error) => {
1007
1057
  if (resolved) return;