docx-diff-editor 1.0.44 → 1.0.45

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
@@ -436,6 +436,8 @@ declare function isProseMirrorJSON(content: unknown): boolean;
436
436
  * 3. Select all content and delete it (start fresh)
437
437
  * 4. Use editor.view.pasteHTML(html) - this uses the paste path which preserves styles
438
438
  * 5. Return the resulting JSON
439
+ *
440
+ * Falls back to the standard import approach if paste fails.
439
441
  */
440
442
  declare function parseHtmlToJson(html: string, SuperDoc: SuperDocConstructor): Promise<ProseMirrorJSON>;
441
443
  /**
package/dist/index.d.ts CHANGED
@@ -436,6 +436,8 @@ declare function isProseMirrorJSON(content: unknown): boolean;
436
436
  * 3. Select all content and delete it (start fresh)
437
437
  * 4. Use editor.view.pasteHTML(html) - this uses the paste path which preserves styles
438
438
  * 5. Return the resulting JSON
439
+ *
440
+ * Falls back to the standard import approach if paste fails.
439
441
  */
440
442
  declare function parseHtmlToJson(html: string, SuperDoc: SuperDocConstructor): Promise<ProseMirrorJSON>;
441
443
  /**
package/dist/index.js CHANGED
@@ -490,6 +490,89 @@ async function parseHtmlToJson(html, SuperDoc) {
490
490
  }
491
491
  }, TIMEOUTS.CLEANUP_DELAY);
492
492
  };
493
+ const createMockPasteEvent = (htmlContent) => {
494
+ const dataTransfer = new DataTransfer();
495
+ dataTransfer.setData("text/html", htmlContent);
496
+ dataTransfer.setData("text/plain", "");
497
+ const event = new ClipboardEvent("paste", {
498
+ bubbles: true,
499
+ cancelable: true,
500
+ clipboardData: dataTransfer
501
+ });
502
+ return event;
503
+ };
504
+ const tryPasteApproach = (sd, onSuccess, onFail) => {
505
+ try {
506
+ const editor = sd?.activeEditor;
507
+ if (!editor?.view?.pasteHTML) {
508
+ onFail();
509
+ return;
510
+ }
511
+ editor.commands.focus?.();
512
+ if (editor.commands.selectAll && editor.commands.deleteSelection) {
513
+ editor.commands.selectAll();
514
+ editor.commands.deleteSelection();
515
+ }
516
+ const mockEvent = createMockPasteEvent(html);
517
+ editor.view.pasteHTML(html, mockEvent);
518
+ setTimeout(() => {
519
+ try {
520
+ const json = editor.getJSON();
521
+ if (json?.content?.length > 0) {
522
+ onSuccess(json);
523
+ } else {
524
+ onFail();
525
+ }
526
+ } catch {
527
+ onFail();
528
+ }
529
+ }, 100);
530
+ } catch (err) {
531
+ console.warn("[parseHtmlToJson] Paste approach error:", err);
532
+ onFail();
533
+ }
534
+ };
535
+ const fallbackToImport = () => {
536
+ if (superdoc) {
537
+ try {
538
+ superdoc.destroy?.();
539
+ } catch {
540
+ }
541
+ superdoc = null;
542
+ }
543
+ superdoc = new SuperDoc({
544
+ selector: container,
545
+ html,
546
+ // Use the actual HTML content
547
+ documentMode: "viewing",
548
+ rulers: false,
549
+ user: { name: "Parser", email: "parser@local" },
550
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
551
+ onReady: ({ superdoc: sd }) => {
552
+ if (resolved) return;
553
+ try {
554
+ const editor = sd?.activeEditor;
555
+ if (!editor) {
556
+ throw new Error("No active editor found");
557
+ }
558
+ const json = editor.getJSON();
559
+ resolved = true;
560
+ cleanup();
561
+ resolve(json);
562
+ } catch (err) {
563
+ resolved = true;
564
+ cleanup();
565
+ reject(err);
566
+ }
567
+ },
568
+ onException: ({ error: err }) => {
569
+ if (resolved) return;
570
+ resolved = true;
571
+ cleanup();
572
+ reject(err);
573
+ }
574
+ });
575
+ };
493
576
  setTimeout(async () => {
494
577
  if (resolved) return;
495
578
  try {
@@ -504,42 +587,27 @@ async function parseHtmlToJson(html, SuperDoc) {
504
587
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
505
588
  onReady: ({ superdoc: sd }) => {
506
589
  if (resolved) return;
507
- try {
508
- const editor = sd?.activeEditor;
509
- if (!editor) {
510
- throw new Error("No active editor found");
511
- }
512
- const view = editor.view;
513
- if (!view) {
514
- throw new Error("No editor view found");
515
- }
516
- editor.commands.selectAll();
517
- editor.commands.deleteSelection();
518
- view.pasteHTML(html);
519
- setTimeout(() => {
590
+ tryPasteApproach(
591
+ sd,
592
+ // Success callback
593
+ (json) => {
520
594
  if (resolved) return;
521
- try {
522
- const json = editor.getJSON();
523
- resolved = true;
524
- cleanup();
525
- resolve(json);
526
- } catch (err) {
527
- resolved = true;
528
- cleanup();
529
- reject(err);
530
- }
531
- }, 50);
532
- } catch (err) {
533
- resolved = true;
534
- cleanup();
535
- reject(err);
536
- }
595
+ resolved = true;
596
+ cleanup();
597
+ resolve(json);
598
+ },
599
+ // Fail callback - try fallback
600
+ () => {
601
+ if (resolved) return;
602
+ console.warn("[parseHtmlToJson] Paste approach failed, falling back to import");
603
+ fallbackToImport();
604
+ }
605
+ );
537
606
  },
538
607
  onException: ({ error: err }) => {
539
608
  if (resolved) return;
540
- resolved = true;
541
- cleanup();
542
- reject(err);
609
+ console.warn("[parseHtmlToJson] Paste approach exception, falling back:", err);
610
+ fallbackToImport();
543
611
  }
544
612
  });
545
613
  setTimeout(() => {
@@ -550,8 +618,12 @@ async function parseHtmlToJson(html, SuperDoc) {
550
618
  }
551
619
  }, TIMEOUTS.PARSE_TIMEOUT);
552
620
  } catch (err) {
553
- cleanup();
554
- reject(err);
621
+ try {
622
+ fallbackToImport();
623
+ } catch (fallbackErr) {
624
+ cleanup();
625
+ reject(fallbackErr);
626
+ }
555
627
  }
556
628
  }, 50);
557
629
  });