@wiris/mathtype-ckeditor5 8.14.0 → 8.15.1

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
 
@@ -43,7 +44,10 @@ export default class MathType extends Plugin {
43
44
  currentInstance = integration;
44
45
 
45
46
  // Add the MathType and ChemType commands to the editor
46
- this._addCommands();
47
+ this._addCommands(integration);
48
+
49
+ // Add the track changes feature integration
50
+ this._addTrackChangesIntegration(integration);
47
51
 
48
52
  // Add the buttons for MathType and ChemType
49
53
  this._addViews(integration);
@@ -428,7 +432,9 @@ export default class MathType extends Plugin {
428
432
  } else if (formula) {
429
433
  const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
430
434
 
431
- 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
+
432
438
  imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
433
439
 
434
440
  // Add HTML element (<img>) to model
@@ -504,17 +510,79 @@ export default class MathType extends Plugin {
504
510
  // Keep a reference to the original get and set function.
505
511
  const { get, set } = editor.data;
506
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
+
507
533
  /**
508
- * 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.
509
543
  */
510
544
  editor.data.on(
511
545
  "get",
512
546
  (e) => {
513
547
  const output = e.return;
548
+
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
+ // In save mode, convert formula images back to clean MathML
514
582
  const parsedResult = Parser.endParse(output);
515
583
 
516
- // Cleans all the semantics tag for safexml
517
- // including the handwritten data points
584
+ // Remove all semantic annotations (including handwritten data points)
585
+ // to ensure clean, standard MathML format for storage
518
586
  e.return = MathML.removeSafeXMLSemantics(parsedResult);
519
587
  },
520
588
  { priority: "low" },
@@ -579,4 +647,25 @@ export default class MathType extends Plugin {
579
647
  Latex,
580
648
  };
581
649
  }
650
+
651
+ _addTrackChangesIntegration(integration) {
652
+ const { editor } = this;
653
+
654
+ if (editor.plugins.has("TrackChangesEditing")) {
655
+ const trackChangesEditing = editor.plugins.get("TrackChangesEditing");
656
+
657
+ // Makes MathType and ChemType buttons available when editor is in the track changes mode
658
+ trackChangesEditing.enableCommand("MathType");
659
+ trackChangesEditing.enableCommand("ChemType");
660
+
661
+ // Adds custom label replacing the default 'mathml'.
662
+ // Handles both singular and plural forms.
663
+ trackChangesEditing.descriptionFactory.registerElementLabel(
664
+ "mathml",
665
+ (quantity) =>
666
+ (quantity > 1 ? `${quantity} ` : "") +
667
+ StringManager.get(quantity > 1 ? "formulas" : "formula", integration?.getLanguage() || "en"),
668
+ );
669
+ }
670
+ }
582
671
  }
@@ -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
+ }