@wiris/mathtype-ckeditor5 8.15.0 → 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
@@ -23,6 +23,7 @@ import CKEditor5Integration from "./integration.js";
23
23
 
24
24
  import mathIcon from "../theme/icons/ckeditor5-formula.svg";
25
25
  import chemIcon from "../theme/icons/ckeditor5-chem.svg";
26
+ import "../theme/styles.css";
26
27
 
27
28
  import packageInfo from "../package.json";
28
29
 
@@ -431,7 +432,9 @@ export default class MathType extends Plugin {
431
432
  } else if (formula) {
432
433
  const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
433
434
 
434
- const imgHtml = Parser.initParse(mathString, integration.getLanguage());
435
+ const lang = integration?.getLanguage() || "en"; // Safe fallback to 'en' in case integration is undefined.
436
+ const imgHtml = Parser.initParse(mathString, lang);
437
+
435
438
  imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
436
439
 
437
440
  // Add HTML element (<img>) to model
@@ -507,17 +510,82 @@ export default class MathType extends Plugin {
507
510
  // Keep a reference to the original get and set function.
508
511
  const { get, set } = editor.data;
509
512
 
513
+ // Listen to the preview command execution to set a flag in localStorage.
514
+ // This flag will be used in the getData() to prevent converting formulas while generating the preview.
515
+ // This is necessary because the preview command uses editor.getData() multiple times internally.
516
+ const previewCommand = editor.commands.get("previewFinalContent");
517
+
518
+ if (previewCommand) {
519
+ this.listenTo(
520
+ previewCommand,
521
+ "execute",
522
+ () => {
523
+ localStorage.setItem("isGeneratingPreview", true);
524
+
525
+ setTimeout(() => {
526
+ localStorage.setItem("isGeneratingPreview", false);
527
+ }, 1000);
528
+ },
529
+ { priority: "high" },
530
+ );
531
+ }
532
+
510
533
  /**
511
- * Hack to transform $$latex$$ into <math> in editor.getData()'s output.
534
+ * Listener for getData() that handles Track Changes and semantics cleanup.
535
+ *
536
+ * This listener intercepts the getData() call and processes the output differently
537
+ * depending on whether we're generating a preview or performing a save operation:
538
+ *
539
+ * - Preview Mode: Removes deleted formulas (marked with Track Changes deletion attributes)
540
+ * while preserving formula images for visual representation.
541
+ * - Save Mode: Converts formulas to clean MathML by removing semantic annotations
542
+ * and handwritten data, ensuring clean storage format.
512
543
  */
513
544
  editor.data.on(
514
545
  "get",
515
546
  (e) => {
516
547
  const output = e.return;
517
- const parsedResult = Parser.endParse(output);
518
548
 
519
- // Cleans all the semantics tag for safexml
520
- // including the handwritten data points
549
+ // Check if we're in preview mode (flag set by previewFinalContent command)
550
+ const isGeneratingPreview = localStorage.getItem("isGeneratingPreview");
551
+
552
+ if (isGeneratingPreview === "true") {
553
+ // Wrap a <span> around all img elements to preserve Track Changes visibility for formulas.
554
+ // Span must contain all the img attributes to avoid render issues.
555
+ const attributesToPreserve = ["data-suggestion-", "data-comment-"];
556
+
557
+ const previewOutput = output.replace(/<img([^>]*class="Wirisformula"[^>]*)>/g, (match, attributes) => {
558
+ // Extract Track Changes attributes
559
+ const trackChangesAttrs = [];
560
+
561
+ attributesToPreserve.forEach((prefix) => {
562
+ const regex = new RegExp(`(${prefix}[^=]*="[^"]*")`, "g");
563
+ let attrMatch;
564
+
565
+ while ((attrMatch = regex.exec(attributes)) !== null) {
566
+ trackChangesAttrs.push(attrMatch[1]);
567
+ }
568
+ });
569
+
570
+ const spanAttrs = trackChangesAttrs.length > 0 ? ` ${trackChangesAttrs.join(" ")}` : "";
571
+ return `<span${spanAttrs}>${match}</span>`;
572
+ });
573
+
574
+ // Cleans all the semantics tag for safexml
575
+ // including the handwritten data points
576
+ e.return = MathML.removeSafeXMLSemantics(previewOutput);
577
+
578
+ return;
579
+ }
580
+
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);
586
+
587
+ // Remove all semantic annotations (including handwritten data points)
588
+ // to ensure clean, standard MathML format for storage
521
589
  e.return = MathML.removeSafeXMLSemantics(parsedResult);
522
590
  },
523
591
  { priority: "low" },
@@ -564,6 +632,45 @@ export default class MathType extends Plugin {
564
632
  );
565
633
  }
566
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
+
567
674
  /**
568
675
  * Expose the WirisPlugin variable to the window
569
676
  */
@@ -597,9 +704,28 @@ export default class MathType extends Plugin {
597
704
  // Handles both singular and plural forms.
598
705
  trackChangesEditing.descriptionFactory.registerElementLabel(
599
706
  "mathml",
600
- quantity => (quantity > 1 ? quantity + ' ' : '') +
601
- StringManager.get(quantity > 1 ? "formulas" : "formula", integration.getLanguage()),
707
+ (quantity) =>
708
+ (quantity > 1 ? `${quantity} ` : "") +
709
+ StringManager.get(quantity > 1 ? "formulas" : "formula", integration?.getLanguage() || "en"),
602
710
  );
711
+
712
+ this._registerLatexTrackChangesAdapter(integration);
603
713
  }
604
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
+ }
605
731
  }
@@ -0,0 +1,28 @@
1
+ /* Replace the default suggestion marker border because it's doesn't span the entire widget. */
2
+ .ck-widget.ck-math-widget.ck-suggestion-marker {
3
+ border: none;
4
+
5
+ &.ck-suggestion-marker-insertion {
6
+ & .Wirisformula {
7
+ border: 3px solid var(--ck-color-suggestion-marker-insertion-border);
8
+ }
9
+
10
+ &.ck-suggestion-marker--active {
11
+ & .Wirisformula {
12
+ border: 3px solid var(--ck-color-suggestion-marker-insertion-border-active);
13
+ }
14
+ }
15
+ }
16
+
17
+ &.ck-suggestion-marker-deletion {
18
+ & .Wirisformula {
19
+ border: 3px solid var(--ck-color-suggestion-marker-deletion-border);
20
+ }
21
+
22
+ &.ck-suggestion-marker--active {
23
+ & .Wirisformula {
24
+ border: 3px solid var(--ck-color-suggestion-marker-deletion-border-active);
25
+ }
26
+ }
27
+ }
28
+ }