@wiris/mathtype-ckeditor5 8.10.0 → 8.11.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/dist/index.js CHANGED
@@ -123,6 +123,8 @@ import Telemeter from '@wiris/mathtype-html-integration-devkit/src/telemeter.js'
123
123
  openNewFormulaEditor() {
124
124
  // Store the editor selection as it will be lost upon opening the modal
125
125
  this.core.editionProperties.selection = this.editorObject.editing.view.document.selection;
126
+ // Focus on the selected editor when multiple editor instances are present
127
+ WirisPlugin.currentInstance = this;
126
128
  return super.openNewFormulaEditor();
127
129
  }
128
130
  /**
@@ -357,7 +359,7 @@ var mathIcon = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Generator: Adob
357
359
  var chemIcon = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Generator: Adobe Illustrator 22.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->\n<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\t viewBox=\"0 0 40.3 49.5\" style=\"enable-background:new 0 0 40.3 49.5;\" xml:space=\"preserve\">\n<style type=\"text/css\">\n\t.st0{fill:#A4CF61;}\n</style>\n<path class=\"st0\" d=\"M39.2,12.1c0-1.9-1.1-3.6-2.7-4.4L24.5,0.9l0,0c-0.7-0.4-1.5-0.6-2.4-0.6c-0.9,0-1.7,0.2-2.4,0.6l0,0L2.3,10.8\n\tl0,0C0.9,11.7,0,13.2,0,14.9h0v19.6h0c0,1.7,0.9,3.3,2.3,4.1l0,0l17.4,9.9l0,0c0.7,0.4,1.5,0.6,2.4,0.6c0.9,0,1.7-0.2,2.4-0.6l0,0\n\tl12.2-6.9h0c1.5-0.8,2.6-2.5,2.6-4.3c0-2.7-2.2-4.9-4.9-4.9c-0.9,0-1.8,0.3-2.5,0.7l0,0l-9.7,5.6l-12.3-7V17.8l12.3-7l9.9,5.7l0,0\n\tc0.7,0.4,1.5,0.6,2.4,0.6C37,17,39.2,14.8,39.2,12.1\"/>\n</svg>\n";
358
360
 
359
361
  var name = "@wiris/mathtype-ckeditor5";
360
- var version = "8.10.0";
362
+ var version = "8.11.1";
361
363
  var description = "MathType Web for CKEditor5 editor";
362
364
  var keywords = [
363
365
  "chem",
@@ -392,8 +394,8 @@ var main = "src/plugin.js";
392
394
  var type = "module";
393
395
  var exports = {
394
396
  ".": "./src/plugin.js",
395
- "./dist/*.css": "./dist/*.css",
396
397
  "./dist/*": "./dist/*",
398
+ "./browser/*": null,
397
399
  "./src/*": "./src/*",
398
400
  "./theme/*": "./theme/*",
399
401
  "./package.json": "./package.json"
@@ -404,14 +406,14 @@ var scripts = {
404
406
  prepare: "npm run build:dist"
405
407
  };
406
408
  var dependencies = {
407
- "@wiris/mathtype-html-integration-devkit": "1.17.3"
409
+ "@wiris/mathtype-html-integration-devkit": "1.17.5"
408
410
  };
409
411
  var devDependencies = {
410
- "@ckeditor/ckeditor5-dev-build-tools": "^40.2.0",
411
- ckeditor5: ">=42.0.0"
412
+ "@ckeditor/ckeditor5-dev-build-tools": "^42.1.0",
413
+ ckeditor5: ">=43.0.0"
412
414
  };
413
415
  var peerDependencies = {
414
- ckeditor5: ">=42.0.0"
416
+ ckeditor5: ">=43.0.0"
415
417
  };
416
418
  var packageInfo = {
417
419
  name: name,
@@ -573,7 +575,8 @@ class MathType extends Plugin {
573
575
  schema.register("mathml", {
574
576
  inheritAllFrom: "$inlineObject",
575
577
  allowAttributes: [
576
- "formula"
578
+ "formula",
579
+ "htmlContent"
577
580
  ]
578
581
  });
579
582
  }
@@ -657,6 +660,50 @@ class MathType extends Plugin {
657
660
  data.modelCursor = data.modelRange.end;
658
661
  }
659
662
  });
663
+ // Data view -> Model
664
+ editor.data.upcastDispatcher.on("element:img", (evt, data, conversionApi)=>{
665
+ const { consumable, writer } = conversionApi;
666
+ const { viewItem } = data;
667
+ // Only upcast when is wiris formula
668
+ if (viewItem.getClassNames().next().value !== "Wirisformula") {
669
+ return;
670
+ }
671
+ const mathAttributes = [
672
+ ...viewItem.getAttributes()
673
+ ].map(([key, value])=>` ${key}="${value}"`).join("");
674
+ let htmlContent = Util.htmlSanitize(`<img${mathAttributes}>`);
675
+ const modelNode = writer.createElement("mathml", {
676
+ htmlContent
677
+ });
678
+ // Find allowed parent for element that we are going to insert.
679
+ // If current parent does not allow to insert element but one of the ancestors does
680
+ // then split nodes to allowed parent.
681
+ const splitResult = conversionApi.splitToAllowedParent(modelNode, data.modelCursor);
682
+ // When there is no split result it means that we can't insert element to model tree, so let's skip it.
683
+ if (!splitResult) {
684
+ return;
685
+ }
686
+ // Insert element on allowed position.
687
+ conversionApi.writer.insert(modelNode, splitResult.position);
688
+ // Consume appropriate value from consumable values list.
689
+ consumable.consume(viewItem, {
690
+ name: true
691
+ });
692
+ const parts = conversionApi.getSplitParts(modelNode);
693
+ // Set conversion result range.
694
+ data.modelRange = writer.createRange(conversionApi.writer.createPositionBefore(modelNode), conversionApi.writer.createPositionAfter(parts[parts.length - 1]));
695
+ // Now we need to check where the `modelCursor` should be.
696
+ if (splitResult.cursorParent) {
697
+ // If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
698
+ //
699
+ // before: <allowed><notAllowed>foo[]</notAllowed></allowed>
700
+ // after: <allowed><notAllowed>foo</notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>
701
+ data.modelCursor = conversionApi.writer.createPositionAt(splitResult.cursorParent, 0);
702
+ } else {
703
+ // Otherwise just continue after inserted element.
704
+ data.modelCursor = data.modelRange.end;
705
+ }
706
+ });
660
707
  /**
661
708
  * Whether the given view <math> element has a LaTeX annotation element.
662
709
  * @param {*} math
@@ -685,12 +732,25 @@ class MathType extends Plugin {
685
732
  }
686
733
  function createViewImage(modelItem, { writer: viewWriter }) {
687
734
  const htmlDataProcessor = new HtmlDataProcessor(viewWriter.document);
688
- const mathString = modelItem.getAttribute("formula").replaceAll('ref="<"', 'ref="&lt;"');
689
- const imgHtml = Parser.initParse(mathString, integration.getLanguage());
690
- const imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
735
+ const formula = modelItem.getAttribute("formula");
736
+ const htmlContent = modelItem.getAttribute("htmlContent");
737
+ if (!formula && !htmlContent) {
738
+ return null;
739
+ }
740
+ let imgElement = null;
741
+ if (htmlContent) {
742
+ imgElement = htmlDataProcessor.toView(htmlContent).getChild(0);
743
+ } else if (formula) {
744
+ const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
745
+ const imgHtml = Parser.initParse(mathString, integration.getLanguage());
746
+ imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
747
+ // Add HTML element (<img>) to model
748
+ viewWriter.setAttribute("htmlContent", imgHtml, modelItem);
749
+ }
691
750
  /* Although we use the HtmlDataProcessor to obtain the attributes,
692
- we must create a new EmptyElement which is independent of the
693
- DataProcessor being used by this editor instance */ if (imgElement) {
751
+ * we must create a new EmptyElement which is independent of the
752
+ * DataProcessor being used by this editor instance
753
+ */ if (imgElement) {
694
754
  return viewWriter.createEmptyElement("img", imgElement.getAttributes(), {
695
755
  renderUnsafeAttributes: [
696
756
  "src"
@@ -731,7 +791,8 @@ class MathType extends Plugin {
731
791
  }
732
792
  function createDataString(modelItem, { writer: viewWriter }) {
733
793
  const htmlDataProcessor = new HtmlDataProcessor(viewWriter.document);
734
- let mathString = Parser.endParseSaveMode(modelItem.getAttribute("formula"));
794
+ // Load img element
795
+ let mathString = modelItem.getAttribute("htmlContent");
735
796
  const sourceMathElement = htmlDataProcessor.toView(mathString).getChild(0);
736
797
  return clone(viewWriter, sourceMathElement);
737
798
  }
@@ -743,36 +804,40 @@ class MathType extends Plugin {
743
804
  * Hack to transform $$latex$$ into <math> in editor.getData()'s output.
744
805
  */ editor.data.on("get", (e)=>{
745
806
  let output = e.return;
746
- // This line cleans all the semantics stuff, including the handwritten data points and returns the MathML IF there is any.
747
- // For text or latex formulas, it returns the original output.
748
- e.return = MathML.removeSemantics(output, "application/json");
807
+ const parsedResult = Parser.endParse(output);
808
+ // Cleans all the semantics tag for safexml
809
+ // including the handwritten data points
810
+ e.return = MathML.removeSafeXMLSemantics(parsedResult);
749
811
  }, {
750
812
  priority: "low"
751
813
  });
752
814
  /**
753
- * Hack to transform <math> with LaTeX into $$LaTeX$$ in editor.setData().
815
+ * Hack to transform <math> with LaTeX into $$LaTeX$$ and formula images in editor.setData().
754
816
  */ editor.data.on("set", (e, args)=>{
755
817
  // Retrieve the data to be set on the CKEditor.
756
818
  let modifiedData = args[0];
757
819
  // Regex to find all mathml formulas.
758
- const regexp = /<math(.*?)<\/math>/gm;
759
- // Get all MathML formulas and store them in an array.
760
- // Using the conditional operator on data.main because the data parameter has different types depending on:
761
- // editor.data.set can be used directly or by the source editing plugin.
762
- // With the source editor plugin, data is an object with the key `main` which contains the source code string.
763
- // When using the editor.data.set method, the data is a string with the content to be set to the editor.
764
- let formulas = Object.values(modifiedData)[0] ? [
765
- ...Object.values(modifiedData)[0].matchAll(regexp)
766
- ] : [
767
- ...modifiedData.matchAll(regexp)
768
- ];
769
- // Loop to find LaTeX formulas and replace the MathML for the LaTeX notation.
820
+ const regexp = /(<img\b[^>]*>)|(<math(.*?)<\/math>)/gm;
821
+ const formulas = [];
822
+ let formula;
823
+ // Both data.set from the source plugin and console command are taken into account as the data received is MathML or an image containing the MathML.
824
+ while((formula = regexp.exec(modifiedData)) !== null){
825
+ formulas.push(formula[0]);
826
+ }
827
+ // Loop to find LaTeX and formula images and replace the MathML for the both.
770
828
  formulas.forEach((formula)=>{
771
- let mathml = formula[0];
772
- if (mathml.includes('encoding="LaTeX"')) {
829
+ if (formula.includes('encoding="LaTeX"')) {
773
830
  // LaTeX found.
774
- let latex = "$$$" + Latex.getLatexFromMathML(mathml) + "$$$"; // We add $$$ instead of $$ because the replace function ignores one $.
775
- modifiedData = modifiedData.replace(mathml, latex);
831
+ let latex = "$$$" + Latex.getLatexFromMathML(formula) + "$$$"; // We add $$$ instead of $$ because the replace function ignores one $.
832
+ modifiedData = modifiedData.replace(formula, latex);
833
+ } else if (formula.includes("<img")) {
834
+ // If we found a formula image, we should find MathML data, and then substitute the entire image.
835
+ const regexp = /«math\b[^»]*»(.*?)«\/math»/g;
836
+ const safexml = formula.match(regexp);
837
+ if (safexml !== null) {
838
+ let decodeXML = MathML.safeXmlDecode(safexml[0]);
839
+ modifiedData = modifiedData.replace(formula, decodeXML);
840
+ }
776
841
  }
777
842
  });
778
843
  args[0] = modifiedData;