@wiris/mathtype-ckeditor5 8.6.0 → 8.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wiris/mathtype-ckeditor5",
3
- "version": "8.6.0",
3
+ "version": "8.7.0",
4
4
  "description": "MathType Web for CKEditor5 editor",
5
5
  "keywords": [
6
6
  "chem",
@@ -39,6 +39,6 @@
39
39
  "@ckeditor/ckeditor5-engine": ">=23.0.0",
40
40
  "@ckeditor/ckeditor5-ui": ">=23.0.0",
41
41
  "@ckeditor/ckeditor5-widget": ">=23.0.0",
42
- "@wiris/mathtype-html-integration-devkit": "1.14.0"
42
+ "@wiris/mathtype-html-integration-devkit": "1.14.1"
43
43
  }
44
44
  }
@@ -146,9 +146,12 @@ export default class CKEditor5Integration extends IntegrationModel {
146
146
  // This returns the value returned by the callback function (writer => {...})
147
147
  return this.editorObject.model.change((writer) => {
148
148
  const core = this.getCore();
149
+ const selection = this.editorObject.model.document.selection;
149
150
 
150
- const modelElementNew = writer.createElement('mathml', { formula: mathml });
151
- modelElementNew.data = mathml;
151
+ const modelElementNew = writer.createElement('mathml', {
152
+ formula: mathml,
153
+ ...Object.fromEntries(selection.getAttributes()), // To keep the format, such as style and font
154
+ });
152
155
 
153
156
  // Obtain the DOM <span><img ... /></span> object corresponding to the formula
154
157
  if (core.editionProperties.isNewElement) {
package/src/plugin.js CHANGED
@@ -257,6 +257,12 @@ export default class MathType extends Plugin {
257
257
  // And obtain the complete formula
258
258
  formula = Util.htmlSanitize(`<math${mathAttributes}>${formula}</math>`);
259
259
 
260
+ // Replaces the < & > characters to its HTMLEntity to avoid render issues.
261
+ formula = formula.replace('"<"', '"&lt;"')
262
+ .replace('">"', '"&gt;"')
263
+ .replace('><<', '>&lt;<');
264
+
265
+
260
266
  /* Model node that contains what's going to actually be inserted. This can be either:
261
267
  - A <mathml> element with a formula attribute set to the given formula, or
262
268
  - If the original <math> had a LaTeX annotation, then the annotation surrounded by "$$...$$" */
@@ -401,8 +407,8 @@ export default class MathType extends Plugin {
401
407
  viewToModelPositionOutsideModelElement(editor.model, (viewElement) => viewElement.hasClass('ck-math-widget')),
402
408
  );
403
409
 
404
- // Keep a reference to the original get function
405
- const { get } = editor.data;
410
+ // Keep a reference to the original get and set function.
411
+ const { get, set } = editor.data;
406
412
 
407
413
  /**
408
414
  * Hack to transform $$latex$$ into <math> in editor.getData()'s output.
@@ -414,6 +420,31 @@ export default class MathType extends Plugin {
414
420
  let imageFormula = Parser.initParse(output);
415
421
  return Parser.endParse(imageFormula);
416
422
  };
423
+
424
+ /**
425
+ * Hack to transform <math> with LaTeX into $$LaTeX$$ in editor.setData().
426
+ */
427
+ editor.data.set = (data) => {
428
+ // Retrieve the data to be set on the CKEditor.
429
+ let modifiedData = data;
430
+ // Regex to find all mathml formulas.
431
+ const regexp = /<math(.*?)<\/math>/gm;
432
+
433
+ // Get all MathML formulas and store them in an array.
434
+ let formulas = [...data.matchAll(regexp)];
435
+
436
+ // Loop to find LaTeX formulas and replace the MathML for the LaTeX notation.
437
+ formulas.forEach((formula) => {
438
+ let mathml = formula[0];
439
+ if (mathml.includes('encoding="LaTeX"')) { // LaTeX found.
440
+ let latex = '$$$' + Latex.getLatexFromMathML(mathml) + '$$$'; // We add $$$ instead of $$ because the replace function ignores one $.
441
+ modifiedData = modifiedData.replace(mathml, latex);
442
+ }
443
+ });
444
+
445
+ // Run the setData code from CKEditor5 with the modified string.
446
+ set.bind(editor.data)(modifiedData);
447
+ };
417
448
  }
418
449
 
419
450
  /**