@wiris/mathtype-ckeditor5 8.9.1 → 8.10.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/README.md +93 -68
- package/dist/browser/index-content.css +0 -0
- package/dist/browser/index-editor.css +502 -0
- package/dist/browser/index.css +650 -0
- package/dist/browser/index.css.map +1 -0
- package/dist/browser/index.js +11211 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/index.umd.js +11215 -0
- package/dist/browser/index.umd.js.map +1 -0
- package/dist/index-content.css +0 -0
- package/dist/index-editor.css +0 -0
- package/dist/index.css +0 -0
- package/dist/index.js +803 -0
- package/dist/index.js.map +1 -0
- package/package.json +23 -8
- package/src/commands.js +17 -13
- package/src/index.js +3 -0
- package/src/integration.js +83 -86
- package/src/plugin.js +147 -147
package/src/plugin.js
CHANGED
|
@@ -1,33 +1,29 @@
|
|
|
1
1
|
// CKEditor imports
|
|
2
|
-
import Plugin from '
|
|
3
|
-
import ButtonView from '
|
|
4
|
-
import ClickObserver from '
|
|
5
|
-
import
|
|
6
|
-
import XmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/xmldataprocessor';
|
|
7
|
-
import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
|
|
8
|
-
import { toWidget, viewToModelPositionOutsideModelElement } from '@ckeditor/ckeditor5-widget/src/utils';
|
|
9
|
-
import Widget from '@ckeditor/ckeditor5-widget/src/widget';
|
|
2
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
3
|
+
import { ButtonView } from 'ckeditor5/src/ui.js';
|
|
4
|
+
import { ClickObserver, HtmlDataProcessor, XmlDataProcessor, UpcastWriter } from 'ckeditor5/src/engine.js';
|
|
5
|
+
import { Widget, toWidget, viewToModelPositionOutsideModelElement } from 'ckeditor5/src/widget.js';
|
|
10
6
|
|
|
11
7
|
// MathType API imports
|
|
12
|
-
import IntegrationModel from
|
|
13
|
-
import Core from
|
|
14
|
-
import Parser from
|
|
15
|
-
import Util from
|
|
16
|
-
import Image from
|
|
17
|
-
import Configuration from
|
|
18
|
-
import Listeners from
|
|
19
|
-
import MathML from
|
|
20
|
-
import Latex from
|
|
21
|
-
import StringManager from
|
|
8
|
+
import IntegrationModel from "@wiris/mathtype-html-integration-devkit/src/integrationmodel.js";
|
|
9
|
+
import Core from "@wiris/mathtype-html-integration-devkit/src/core.src.js";
|
|
10
|
+
import Parser from "@wiris/mathtype-html-integration-devkit/src/parser.js";
|
|
11
|
+
import Util from "@wiris/mathtype-html-integration-devkit/src/util.js";
|
|
12
|
+
import Image from "@wiris/mathtype-html-integration-devkit/src/image.js";
|
|
13
|
+
import Configuration from "@wiris/mathtype-html-integration-devkit/src/configuration.js";
|
|
14
|
+
import Listeners from "@wiris/mathtype-html-integration-devkit/src/listeners.js";
|
|
15
|
+
import MathML from "@wiris/mathtype-html-integration-devkit/src/mathml.js";
|
|
16
|
+
import Latex from "@wiris/mathtype-html-integration-devkit/src/latex.js";
|
|
17
|
+
import StringManager from "@wiris/mathtype-html-integration-devkit/src/stringmanager.js";
|
|
22
18
|
|
|
23
19
|
// Local imports
|
|
24
|
-
import { MathTypeCommand, ChemTypeCommand } from
|
|
25
|
-
import CKEditor5Integration from
|
|
20
|
+
import { MathTypeCommand, ChemTypeCommand } from "./commands.js";
|
|
21
|
+
import CKEditor5Integration from "./integration.js";
|
|
26
22
|
|
|
27
|
-
import mathIcon from
|
|
28
|
-
import chemIcon from
|
|
23
|
+
import mathIcon from "../theme/icons/ckeditor5-formula.svg";
|
|
24
|
+
import chemIcon from "../theme/icons/ckeditor5-chem.svg";
|
|
29
25
|
|
|
30
|
-
import packageInfo from
|
|
26
|
+
import packageInfo from "../package.json";
|
|
31
27
|
|
|
32
28
|
export let currentInstance = null; // eslint-disable-line import/no-mutable-exports
|
|
33
29
|
|
|
@@ -37,7 +33,7 @@ export default class MathType extends Plugin {
|
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
static get pluginName() {
|
|
40
|
-
return
|
|
36
|
+
return "MathType";
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
init() {
|
|
@@ -64,14 +60,15 @@ export default class MathType extends Plugin {
|
|
|
64
60
|
/**
|
|
65
61
|
* Inherited from Plugin class: Executed when CKEditor5 is destroyed
|
|
66
62
|
*/
|
|
67
|
-
destroy() {
|
|
63
|
+
destroy() {
|
|
64
|
+
// eslint-disable-line class-methods-use-this
|
|
68
65
|
currentInstance.destroy();
|
|
69
66
|
}
|
|
70
67
|
|
|
71
68
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
* Create the MathType API Integration object
|
|
70
|
+
* @returns {CKEditor5Integration} the integration object
|
|
71
|
+
*/
|
|
75
72
|
_addIntegration() {
|
|
76
73
|
const { editor } = this;
|
|
77
74
|
|
|
@@ -81,15 +78,15 @@ export default class MathType extends Plugin {
|
|
|
81
78
|
*/
|
|
82
79
|
const integrationProperties = {};
|
|
83
80
|
integrationProperties.environment = {};
|
|
84
|
-
integrationProperties.environment.editor =
|
|
85
|
-
integrationProperties.environment.editorVersion =
|
|
81
|
+
integrationProperties.environment.editor = "CKEditor5";
|
|
82
|
+
integrationProperties.environment.editorVersion = "5.x";
|
|
86
83
|
integrationProperties.version = packageInfo.version;
|
|
87
84
|
integrationProperties.editorObject = editor;
|
|
88
85
|
integrationProperties.serviceProviderProperties = {};
|
|
89
|
-
integrationProperties.serviceProviderProperties.URI =
|
|
90
|
-
integrationProperties.serviceProviderProperties.server =
|
|
86
|
+
integrationProperties.serviceProviderProperties.URI = "https://www.wiris.net/demo/plugins/app";
|
|
87
|
+
integrationProperties.serviceProviderProperties.server = "java";
|
|
91
88
|
integrationProperties.target = editor.sourceElement;
|
|
92
|
-
integrationProperties.scriptName =
|
|
89
|
+
integrationProperties.scriptName = "bundle.js";
|
|
93
90
|
integrationProperties.managesLanguage = true;
|
|
94
91
|
// etc
|
|
95
92
|
|
|
@@ -100,59 +97,64 @@ export default class MathType extends Plugin {
|
|
|
100
97
|
// Instance of the integration associated to this editor instance
|
|
101
98
|
integration = new CKEditor5Integration(integrationProperties);
|
|
102
99
|
integration.init();
|
|
103
|
-
integration.listeners.fire(
|
|
100
|
+
integration.listeners.fire("onTargetReady", {});
|
|
104
101
|
|
|
105
102
|
integration.checkElement();
|
|
106
103
|
|
|
107
|
-
this.listenTo(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
104
|
+
this.listenTo(
|
|
105
|
+
editor.editing.view.document,
|
|
106
|
+
"click",
|
|
107
|
+
(evt, data) => {
|
|
108
|
+
// Is Double-click
|
|
109
|
+
if (data.domEvent.detail === 2) {
|
|
110
|
+
integration.doubleClickHandler(data.domTarget, data.domEvent);
|
|
111
|
+
evt.stop();
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
{ priority: "highest" },
|
|
115
|
+
);
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
return integration;
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
* Add the MathType and ChemType commands to the editor
|
|
123
|
+
*/
|
|
122
124
|
_addCommands() {
|
|
123
125
|
const { editor } = this;
|
|
124
126
|
|
|
125
127
|
// Add command to open the formula editor
|
|
126
|
-
editor.commands.add(
|
|
128
|
+
editor.commands.add("MathType", new MathTypeCommand(editor));
|
|
127
129
|
|
|
128
130
|
// Add command to open the chemistry formula editor
|
|
129
|
-
editor.commands.add(
|
|
131
|
+
editor.commands.add("ChemType", new ChemTypeCommand(editor));
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
/**
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
* Add the buttons for MathType and ChemType
|
|
136
|
+
* @param {CKEditor5Integration} integration the integration object
|
|
137
|
+
*/
|
|
136
138
|
_addViews(integration) {
|
|
137
139
|
const { editor } = this;
|
|
138
140
|
|
|
139
141
|
// Check if MathType editor is enabled
|
|
140
|
-
if (Configuration.get(
|
|
142
|
+
if (Configuration.get("editorEnabled")) {
|
|
141
143
|
// Add button for the formula editor
|
|
142
|
-
editor.ui.componentFactory.add(
|
|
144
|
+
editor.ui.componentFactory.add("MathType", (locale) => {
|
|
143
145
|
const view = new ButtonView(locale);
|
|
144
146
|
|
|
145
147
|
// View is enabled iff command is enabled
|
|
146
|
-
view.bind(
|
|
148
|
+
view.bind("isEnabled").to(editor.commands.get("MathType"), "isEnabled");
|
|
147
149
|
view.set({
|
|
148
|
-
label: StringManager.get(
|
|
150
|
+
label: StringManager.get("insert_math", integration.getLanguage()),
|
|
149
151
|
icon: mathIcon,
|
|
150
152
|
tooltip: true,
|
|
151
153
|
});
|
|
152
154
|
|
|
153
155
|
// Callback executed once the image is clicked.
|
|
154
|
-
view.on(
|
|
155
|
-
editor.execute(
|
|
156
|
+
view.on("execute", () => {
|
|
157
|
+
editor.execute("MathType", {
|
|
156
158
|
integration, // Pass integration as parameter
|
|
157
159
|
});
|
|
158
160
|
});
|
|
@@ -162,23 +164,23 @@ export default class MathType extends Plugin {
|
|
|
162
164
|
}
|
|
163
165
|
|
|
164
166
|
// Check if ChemType editor is enabled
|
|
165
|
-
if (Configuration.get(
|
|
167
|
+
if (Configuration.get("chemEnabled")) {
|
|
166
168
|
// Add button for the chemistry formula editor
|
|
167
|
-
editor.ui.componentFactory.add(
|
|
169
|
+
editor.ui.componentFactory.add("ChemType", (locale) => {
|
|
168
170
|
const view = new ButtonView(locale);
|
|
169
171
|
|
|
170
172
|
// View is enabled iff command is enabled
|
|
171
|
-
view.bind(
|
|
173
|
+
view.bind("isEnabled").to(editor.commands.get("ChemType"), "isEnabled");
|
|
172
174
|
|
|
173
175
|
view.set({
|
|
174
|
-
label: StringManager.get(
|
|
176
|
+
label: StringManager.get("insert_chem", integration.getLanguage()),
|
|
175
177
|
icon: chemIcon,
|
|
176
178
|
tooltip: true,
|
|
177
179
|
});
|
|
178
180
|
|
|
179
181
|
// Callback executed once the image is clicked.
|
|
180
|
-
view.on(
|
|
181
|
-
editor.execute(
|
|
182
|
+
view.on("execute", () => {
|
|
183
|
+
editor.execute("ChemType", {
|
|
182
184
|
integration, // Pass integration as parameter
|
|
183
185
|
});
|
|
184
186
|
});
|
|
@@ -192,39 +194,39 @@ export default class MathType extends Plugin {
|
|
|
192
194
|
}
|
|
193
195
|
|
|
194
196
|
/**
|
|
195
|
-
|
|
196
|
-
|
|
197
|
+
* Registers the <mathml> element in the schema
|
|
198
|
+
*/
|
|
197
199
|
_addSchema() {
|
|
198
200
|
const { schema } = this.editor.model;
|
|
199
201
|
|
|
200
|
-
schema.register(
|
|
201
|
-
inheritAllFrom:
|
|
202
|
-
allowAttributes: [
|
|
202
|
+
schema.register("mathml", {
|
|
203
|
+
inheritAllFrom: "$inlineObject",
|
|
204
|
+
allowAttributes: ["formula"],
|
|
203
205
|
});
|
|
204
206
|
}
|
|
205
207
|
|
|
206
208
|
/**
|
|
207
|
-
|
|
208
|
-
|
|
209
|
+
* Add the downcast and upcast converters
|
|
210
|
+
*/
|
|
209
211
|
_addConverters(integration) {
|
|
210
212
|
const { editor } = this;
|
|
211
213
|
|
|
212
214
|
// Editing view -> Model
|
|
213
|
-
editor.conversion.for(
|
|
215
|
+
editor.conversion.for("upcast").elementToElement({
|
|
214
216
|
view: {
|
|
215
|
-
name:
|
|
216
|
-
classes:
|
|
217
|
+
name: "span",
|
|
218
|
+
classes: "ck-math-widget",
|
|
217
219
|
},
|
|
218
220
|
model: (viewElement, { writer: modelWriter }) => {
|
|
219
|
-
const formula = MathML.safeXmlDecode(viewElement.getChild(0).getAttribute(
|
|
220
|
-
return modelWriter.createElement(
|
|
221
|
+
const formula = MathML.safeXmlDecode(viewElement.getChild(0).getAttribute("data-mathml"));
|
|
222
|
+
return modelWriter.createElement("mathml", {
|
|
221
223
|
formula,
|
|
222
224
|
});
|
|
223
225
|
},
|
|
224
226
|
});
|
|
225
227
|
|
|
226
228
|
// Data view -> Model
|
|
227
|
-
editor.data.upcastDispatcher.on(
|
|
229
|
+
editor.data.upcastDispatcher.on("element:math", (evt, data, conversionApi) => {
|
|
228
230
|
const { consumable, writer } = conversionApi;
|
|
229
231
|
const { viewItem } = data;
|
|
230
232
|
|
|
@@ -246,28 +248,23 @@ export default class MathType extends Plugin {
|
|
|
246
248
|
const viewDocumentFragment = upcastWriter.createDocumentFragment(viewItem.getChildren());
|
|
247
249
|
|
|
248
250
|
// and obtain the attributes of <math> too!
|
|
249
|
-
const mathAttributes = [...viewItem.getAttributes()]
|
|
250
|
-
.map(([key, value]) => ` ${key}="${value}"`)
|
|
251
|
-
.join('');
|
|
251
|
+
const mathAttributes = [...viewItem.getAttributes()].map(([key, value]) => ` ${key}="${value}"`).join("");
|
|
252
252
|
|
|
253
253
|
// We process the document fragment
|
|
254
|
-
let formula = processor.toData(viewDocumentFragment) ||
|
|
254
|
+
let formula = processor.toData(viewDocumentFragment) || "";
|
|
255
255
|
|
|
256
256
|
// And obtain the complete formula
|
|
257
257
|
formula = Util.htmlSanitize(`<math${mathAttributes}>${formula}</math>`);
|
|
258
258
|
|
|
259
259
|
// Replaces the < & > characters to its HTMLEntity to avoid render issues.
|
|
260
|
-
formula = formula.replaceAll('"<"', '"<"')
|
|
261
|
-
.replaceAll('">"', '">"')
|
|
262
|
-
.replaceAll('><<', '><<');
|
|
263
|
-
|
|
260
|
+
formula = formula.replaceAll('"<"', '"<"').replaceAll('">"', '">"').replaceAll("><<", "><<");
|
|
264
261
|
|
|
265
262
|
/* Model node that contains what's going to actually be inserted. This can be either:
|
|
266
263
|
- A <mathml> element with a formula attribute set to the given formula, or
|
|
267
264
|
- If the original <math> had a LaTeX annotation, then the annotation surrounded by "$$...$$" */
|
|
268
265
|
const modelNode = isLatex
|
|
269
266
|
? writer.createText(Parser.initParse(formula, integration.getLanguage()))
|
|
270
|
-
: writer.createElement(
|
|
267
|
+
: writer.createElement("mathml", { formula });
|
|
271
268
|
|
|
272
269
|
// Find allowed parent for element that we are going to insert.
|
|
273
270
|
// If current parent does not allow to insert element but one of the ancestors does
|
|
@@ -308,15 +305,15 @@ export default class MathType extends Plugin {
|
|
|
308
305
|
});
|
|
309
306
|
|
|
310
307
|
/**
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
308
|
+
* Whether the given view <math> element has a LaTeX annotation element.
|
|
309
|
+
* @param {*} math
|
|
310
|
+
* @returns {bool}
|
|
311
|
+
*/
|
|
315
312
|
function mathIsLatex(math) {
|
|
316
313
|
const semantics = math.getChild(0);
|
|
317
|
-
if (!semantics || semantics.name !==
|
|
314
|
+
if (!semantics || semantics.name !== "semantics") return false;
|
|
318
315
|
for (const child of semantics.getChildren()) {
|
|
319
|
-
if (child.name ===
|
|
316
|
+
if (child.name === "annotation" && child.getAttribute("encoding") === "LaTeX") {
|
|
320
317
|
return true;
|
|
321
318
|
}
|
|
322
319
|
}
|
|
@@ -324,8 +321,8 @@ export default class MathType extends Plugin {
|
|
|
324
321
|
}
|
|
325
322
|
|
|
326
323
|
function createViewWidget(modelItem, { writer: viewWriter }) {
|
|
327
|
-
const widgetElement = viewWriter.createContainerElement(
|
|
328
|
-
class:
|
|
324
|
+
const widgetElement = viewWriter.createContainerElement("span", {
|
|
325
|
+
class: "ck-math-widget",
|
|
329
326
|
});
|
|
330
327
|
|
|
331
328
|
const mathUIElement = createViewImage(modelItem, { writer: viewWriter }); // eslint-disable-line no-use-before-define
|
|
@@ -340,7 +337,7 @@ export default class MathType extends Plugin {
|
|
|
340
337
|
function createViewImage(modelItem, { writer: viewWriter }) {
|
|
341
338
|
const htmlDataProcessor = new HtmlDataProcessor(viewWriter.document);
|
|
342
339
|
|
|
343
|
-
const mathString = modelItem.getAttribute(
|
|
340
|
+
const mathString = modelItem.getAttribute("formula").replaceAll('ref="<"', 'ref="<"');
|
|
344
341
|
const imgHtml = Parser.initParse(mathString, integration.getLanguage());
|
|
345
342
|
const imgElement = htmlDataProcessor.toView(imgHtml).getChild(0);
|
|
346
343
|
|
|
@@ -348,8 +345,8 @@ export default class MathType extends Plugin {
|
|
|
348
345
|
we must create a new EmptyElement which is independent of the
|
|
349
346
|
DataProcessor being used by this editor instance */
|
|
350
347
|
if (imgElement) {
|
|
351
|
-
return viewWriter.createEmptyElement(
|
|
352
|
-
renderUnsafeAttributes: [
|
|
348
|
+
return viewWriter.createEmptyElement("img", imgElement.getAttributes(), {
|
|
349
|
+
renderUnsafeAttributes: ["src"],
|
|
353
350
|
});
|
|
354
351
|
}
|
|
355
352
|
|
|
@@ -357,14 +354,14 @@ export default class MathType extends Plugin {
|
|
|
357
354
|
}
|
|
358
355
|
|
|
359
356
|
// Model -> Editing view
|
|
360
|
-
editor.conversion.for(
|
|
361
|
-
model:
|
|
357
|
+
editor.conversion.for("editingDowncast").elementToElement({
|
|
358
|
+
model: "mathml",
|
|
362
359
|
view: createViewWidget,
|
|
363
360
|
});
|
|
364
361
|
|
|
365
362
|
// Model -> Data view
|
|
366
|
-
editor.conversion.for(
|
|
367
|
-
model:
|
|
363
|
+
editor.conversion.for("dataDowncast").elementToElement({
|
|
364
|
+
model: "mathml",
|
|
368
365
|
view: createDataString, // eslint-disable-line no-use-before-define
|
|
369
366
|
});
|
|
370
367
|
|
|
@@ -374,26 +371,27 @@ export default class MathType extends Plugin {
|
|
|
374
371
|
* @returns {module:engine/view/node~Node} Copy of the node.
|
|
375
372
|
*/
|
|
376
373
|
function clone(viewWriter, sourceNode) {
|
|
377
|
-
if (sourceNode.is(
|
|
374
|
+
if (sourceNode.is("text")) {
|
|
378
375
|
return viewWriter.createText(sourceNode.data);
|
|
379
|
-
}
|
|
380
|
-
|
|
376
|
+
}
|
|
377
|
+
if (sourceNode.is("element")) {
|
|
378
|
+
if (sourceNode.is("emptyElement")) {
|
|
381
379
|
return viewWriter.createEmptyElement(sourceNode.name, sourceNode.getAttributes());
|
|
382
380
|
}
|
|
383
381
|
const element = viewWriter.createContainerElement(sourceNode.name, sourceNode.getAttributes());
|
|
384
382
|
for (const child of sourceNode.getChildren()) {
|
|
385
|
-
viewWriter.insert(viewWriter.createPositionAt(element,
|
|
383
|
+
viewWriter.insert(viewWriter.createPositionAt(element, "end"), clone(viewWriter, child));
|
|
386
384
|
}
|
|
387
385
|
return element;
|
|
388
386
|
}
|
|
389
387
|
|
|
390
|
-
throw new Exception(
|
|
388
|
+
throw new Exception("Given node has unsupported type."); // eslint-disable-line no-undef
|
|
391
389
|
}
|
|
392
390
|
|
|
393
391
|
function createDataString(modelItem, { writer: viewWriter }) {
|
|
394
392
|
const htmlDataProcessor = new HtmlDataProcessor(viewWriter.document);
|
|
395
393
|
|
|
396
|
-
let mathString = Parser.endParseSaveMode(modelItem.getAttribute(
|
|
394
|
+
let mathString = Parser.endParseSaveMode(modelItem.getAttribute("formula"));
|
|
397
395
|
|
|
398
396
|
const sourceMathElement = htmlDataProcessor.toView(mathString).getChild(0);
|
|
399
397
|
|
|
@@ -402,8 +400,8 @@ export default class MathType extends Plugin {
|
|
|
402
400
|
|
|
403
401
|
// This stops the view selection getting into the <span>s and messing up caret movement
|
|
404
402
|
editor.editing.mapper.on(
|
|
405
|
-
|
|
406
|
-
viewToModelPositionOutsideModelElement(editor.model, (viewElement) => viewElement.hasClass(
|
|
403
|
+
"viewToModelPosition",
|
|
404
|
+
viewToModelPositionOutsideModelElement(editor.model, (viewElement) => viewElement.hasClass("ck-math-widget")),
|
|
407
405
|
);
|
|
408
406
|
|
|
409
407
|
// Keep a reference to the original get and set function.
|
|
@@ -412,54 +410,56 @@ export default class MathType extends Plugin {
|
|
|
412
410
|
/**
|
|
413
411
|
* Hack to transform $$latex$$ into <math> in editor.getData()'s output.
|
|
414
412
|
*/
|
|
415
|
-
editor.data.
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
.
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
// We transform all the retrieved data to images and then we Parse the data.
|
|
426
|
-
let imageFormula = Parser.initParse(output);
|
|
427
|
-
return Parser.endParse(imageFormula);
|
|
428
|
-
};
|
|
413
|
+
editor.data.on(
|
|
414
|
+
"get",
|
|
415
|
+
(e) => {
|
|
416
|
+
let output = e.return;
|
|
417
|
+
// This line cleans all the semantics stuff, including the handwritten data points and returns the MathML IF there is any.
|
|
418
|
+
// For text or latex formulas, it returns the original output.
|
|
419
|
+
e.return = MathML.removeSemantics(output, "application/json");
|
|
420
|
+
},
|
|
421
|
+
{ priority: "low" },
|
|
422
|
+
);
|
|
429
423
|
|
|
430
424
|
/**
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
editor.data.
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
425
|
+
* Hack to transform <math> with LaTeX into $$LaTeX$$ in editor.setData().
|
|
426
|
+
*/
|
|
427
|
+
editor.data.on(
|
|
428
|
+
"set",
|
|
429
|
+
(e, args) => {
|
|
430
|
+
// Retrieve the data to be set on the CKEditor.
|
|
431
|
+
let modifiedData = args[0];
|
|
432
|
+
// Regex to find all mathml formulas.
|
|
433
|
+
const regexp = /<math(.*?)<\/math>/gm;
|
|
434
|
+
|
|
435
|
+
// Get all MathML formulas and store them in an array.
|
|
436
|
+
// Using the conditional operator on data.main because the data parameter has different types depending on:
|
|
437
|
+
// editor.data.set can be used directly or by the source editing plugin.
|
|
438
|
+
// With the source editor plugin, data is an object with the key `main` which contains the source code string.
|
|
439
|
+
// When using the editor.data.set method, the data is a string with the content to be set to the editor.
|
|
440
|
+
let formulas = Object.values(modifiedData)[0]
|
|
441
|
+
? [...Object.values(modifiedData)[0].matchAll(regexp)]
|
|
442
|
+
: [...modifiedData.matchAll(regexp)];
|
|
443
|
+
|
|
444
|
+
// Loop to find LaTeX formulas and replace the MathML for the LaTeX notation.
|
|
445
|
+
formulas.forEach((formula) => {
|
|
446
|
+
let mathml = formula[0];
|
|
447
|
+
if (mathml.includes('encoding="LaTeX"')) {
|
|
448
|
+
// LaTeX found.
|
|
449
|
+
let latex = "$$$" + Latex.getLatexFromMathML(mathml) + "$$$"; // We add $$$ instead of $$ because the replace function ignores one $.
|
|
450
|
+
modifiedData = modifiedData.replace(mathml, latex);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
454
453
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
454
|
+
args[0] = modifiedData;
|
|
455
|
+
},
|
|
456
|
+
{ priority: "high" },
|
|
457
|
+
);
|
|
458
458
|
}
|
|
459
459
|
|
|
460
460
|
/**
|
|
461
|
-
|
|
462
|
-
|
|
461
|
+
* Expose the WirisPlugin variable to the window
|
|
462
|
+
*/
|
|
463
463
|
// eslint-disable-next-line class-methods-use-this
|
|
464
464
|
_exposeWiris() {
|
|
465
465
|
window.WirisPlugin = {
|