@wiris/mathtype-ckeditor5 8.15.1 → 8.15.2

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/src/plugin.js CHANGED
@@ -557,7 +557,7 @@ export default class MathType extends Plugin {
557
557
  const previewOutput = output.replace(/<img([^>]*class="Wirisformula"[^>]*)>/g, (match, attributes) => {
558
558
  // Extract Track Changes attributes
559
559
  const trackChangesAttrs = [];
560
-
560
+
561
561
  attributesToPreserve.forEach((prefix) => {
562
562
  const regex = new RegExp(`(${prefix}[^=]*="[^"]*")`, "g");
563
563
  let attrMatch;
@@ -578,8 +578,11 @@ export default class MathType extends Plugin {
578
578
  return;
579
579
  }
580
580
 
581
- // In save mode, convert formula images back to clean MathML
582
- const parsedResult = Parser.endParse(output);
581
+ // Clean track changes markers only from LaTeX content before converting to MathML.
582
+ const latexParsedOutput = this._endParseEditModeWithTrackChangesSupport(output);
583
+
584
+ // Convert formula images to MathML. It's important to use the save mode to prevent issues.
585
+ const parsedResult = Parser.endParseSaveMode(latexParsedOutput);
583
586
 
584
587
  // Remove all semantic annotations (including handwritten data points)
585
588
  // to ensure clean, standard MathML format for storage
@@ -629,6 +632,45 @@ export default class MathType extends Plugin {
629
632
  );
630
633
  }
631
634
 
635
+ /**
636
+ * When track changes markers are present inside a LaTeX block:
637
+ * - The LaTeX is preserved as text (not converted to MathML) to maintain suggestions.
638
+ * - It also prevents the issue where the suggestions were placed as MathML tags.
639
+ *
640
+ * When no track changes markers are inside a LaTeX block:
641
+ * - The LaTeX is converted to MathML normally. (previous default behavior)
642
+ *
643
+ * This is to ensure that:
644
+ * 1. LaTeX without suggestions gets converted to MathML for final output.
645
+ * 2. LaTeX with pending suggestions is preserved so setData(getData()) works correctly.
646
+ */
647
+ _endParseEditModeWithTrackChangesSupport(code) {
648
+ if (!Configuration.get("parseModes").includes("latex")) {
649
+ return code;
650
+ }
651
+
652
+ const latexBlockRegex = /\$\$([\s\S]*?)\$\$/g;
653
+ const trackChangesRegex = /<(suggestion|comment)-(start|end)/i;
654
+
655
+ //TODO: Validate if replace all is needed instead of just replace when it is all implemented.
656
+ return code.replace(latexBlockRegex, (fullMatch, latexContent) => {
657
+ // Check if this LaTeX contains track changes markers to prevent conversion.
658
+ if (trackChangesRegex.test(latexContent)) {
659
+ return fullMatch;
660
+ }
661
+
662
+ // When LaTeX has no suggestion, it can be converted to MathML.
663
+ const decodedLatex = Util.htmlEntitiesDecode(latexContent);
664
+ let mathml = Util.htmlSanitize(Latex.getMathMLFromLatex(decodedLatex, true));
665
+
666
+ if (!Configuration.get("saveHandTraces")) {
667
+ mathml = MathML.removeAnnotation(mathml, "application/json");
668
+ }
669
+
670
+ return mathml;
671
+ });
672
+ }
673
+
632
674
  /**
633
675
  * Expose the WirisPlugin variable to the window
634
676
  */
@@ -666,6 +708,24 @@ export default class MathType extends Plugin {
666
708
  (quantity > 1 ? `${quantity} ` : "") +
667
709
  StringManager.get(quantity > 1 ? "formulas" : "formula", integration?.getLanguage() || "en"),
668
710
  );
711
+
712
+ this._registerLatexTrackChangesAdapter(integration);
669
713
  }
670
714
  }
715
+
716
+ /**
717
+ * Register a custom adapter for handling LaTeX text changes.
718
+ * This ensures that LaTeX formulas ($$...$$) are treated as atomic units
719
+ * when used by the track changes feature and avoid partial edits.
720
+ */
721
+ _registerLatexTrackChangesAdapter(integration) {
722
+ const { editor } = this;
723
+
724
+ editor.model.document.on("change:data", () => {
725
+ if (integration) {
726
+ integration._trackChangesEnabled =
727
+ editor.commands.get("trackChanges")?.value ?? false;
728
+ }
729
+ });
730
+ }
671
731
  }