@wiris/mathtype-ckeditor5 8.15.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
 
@@ -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,79 @@ 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;
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
517
582
  const parsedResult = Parser.endParse(output);
518
583
 
519
- // Cleans all the semantics tag for safexml
520
- // including the handwritten data points
584
+ // Remove all semantic annotations (including handwritten data points)
585
+ // to ensure clean, standard MathML format for storage
521
586
  e.return = MathML.removeSafeXMLSemantics(parsedResult);
522
587
  },
523
588
  { priority: "low" },
@@ -597,8 +662,9 @@ export default class MathType extends Plugin {
597
662
  // Handles both singular and plural forms.
598
663
  trackChangesEditing.descriptionFactory.registerElementLabel(
599
664
  "mathml",
600
- quantity => (quantity > 1 ? quantity + ' ' : '') +
601
- StringManager.get(quantity > 1 ? "formulas" : "formula", integration.getLanguage()),
665
+ (quantity) =>
666
+ (quantity > 1 ? `${quantity} ` : "") +
667
+ StringManager.get(quantity > 1 ? "formulas" : "formula", integration?.getLanguage() || "en"),
602
668
  );
603
669
  }
604
670
  }
@@ -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
+ }