@wiris/mathtype-ckeditor5 8.13.2 → 8.13.4
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/browser/index.js +32 -3
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/index.umd.js +31 -2
- package/dist/browser/index.umd.js.map +1 -1
- package/dist/index.js +32 -3
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/plugin.js +67 -44
package/src/plugin.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// CKEditor imports
|
|
2
2
|
import { Plugin } from "ckeditor5/src/core.js";
|
|
3
3
|
import { ButtonView } from "ckeditor5/src/ui.js";
|
|
4
|
-
import { ClickObserver, HtmlDataProcessor, XmlDataProcessor,
|
|
4
|
+
import { ClickObserver, HtmlDataProcessor, XmlDataProcessor, ViewUpcastWriter } from "ckeditor5/src/engine.js";
|
|
5
5
|
import { Widget, toWidget, viewToModelPositionOutsideModelElement } from "ckeditor5/src/widget.js";
|
|
6
6
|
|
|
7
7
|
// MathType API imports
|
|
@@ -62,8 +62,8 @@ export default class MathType extends Plugin {
|
|
|
62
62
|
* Inherited from Plugin class: Executed when CKEditor5 is destroyed
|
|
63
63
|
*/
|
|
64
64
|
destroy() {
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
// eslint-disable-line class-methods-use-this
|
|
66
|
+
currentInstance?.destroy();
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/**
|
|
@@ -245,7 +245,7 @@ export default class MathType extends Plugin {
|
|
|
245
245
|
|
|
246
246
|
// Only god knows why the following line makes viewItem lose all of its children,
|
|
247
247
|
// so we obtain isLatex before doing this because we need viewItem's children for that.
|
|
248
|
-
const upcastWriter = new
|
|
248
|
+
const upcastWriter = new ViewUpcastWriter(editor.editing.view.document);
|
|
249
249
|
const viewDocumentFragment = upcastWriter.createDocumentFragment(viewItem.getChildren());
|
|
250
250
|
|
|
251
251
|
// and obtain the attributes of <math> too!
|
|
@@ -306,57 +306,80 @@ export default class MathType extends Plugin {
|
|
|
306
306
|
});
|
|
307
307
|
|
|
308
308
|
// Data view -> Model
|
|
309
|
-
editor.data.upcastDispatcher.on(
|
|
310
|
-
|
|
311
|
-
|
|
309
|
+
editor.data.upcastDispatcher.on(
|
|
310
|
+
"element:img",
|
|
311
|
+
(evt, data, conversionApi) => {
|
|
312
|
+
const { consumable, writer } = conversionApi;
|
|
313
|
+
const { viewItem } = data;
|
|
314
|
+
|
|
315
|
+
// Only upcast when is wiris formula
|
|
316
|
+
if (viewItem.getClassNames().next().value !== "Wirisformula") {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
312
319
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
320
|
+
// The following code ensures that the element's name, attributes, and classes are consumed.
|
|
321
|
+
// This means that they are marked as handled so that other parts of the system or plugins don't process them again.
|
|
317
322
|
|
|
318
|
-
|
|
319
|
-
|
|
323
|
+
// Check if we can consume the element name.
|
|
324
|
+
if (!consumable.test(viewItem, { name: true })) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
320
327
|
|
|
321
|
-
|
|
328
|
+
// Consume the name, attributes, and classes so nothing else processes it.
|
|
329
|
+
consumable.consume(viewItem, { name: true });
|
|
330
|
+
for (const attrName of viewItem.getAttributes()) {
|
|
331
|
+
consumable.consume(viewItem, { attributes: [attrName] });
|
|
332
|
+
}
|
|
322
333
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const splitResult = conversionApi.splitToAllowedParent(modelNode, data.modelCursor);
|
|
334
|
+
for (const className of viewItem.getClassNames()) {
|
|
335
|
+
consumable.consume(viewItem, { classes: [className] });
|
|
336
|
+
}
|
|
327
337
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
338
|
+
const mathAttributes = [...viewItem.getAttributes()].map(([key, value]) => ` ${key}="${value}"`).join("");
|
|
339
|
+
const htmlContent = Util.htmlSanitize(`<img${mathAttributes}>`);
|
|
332
340
|
|
|
333
|
-
|
|
334
|
-
conversionApi.writer.insert(modelNode, splitResult.position);
|
|
341
|
+
const modelNode = writer.createElement("mathml", { htmlContent });
|
|
335
342
|
|
|
336
|
-
|
|
337
|
-
|
|
343
|
+
// Find allowed parent for element that we are going to insert.
|
|
344
|
+
// If current parent does not allow to insert element but one of the ancestors does
|
|
345
|
+
// then split nodes to allowed parent.
|
|
346
|
+
const splitResult = conversionApi.splitToAllowedParent(modelNode, data.modelCursor);
|
|
338
347
|
|
|
339
|
-
|
|
348
|
+
// When there is no split result it means that we can't insert element to model tree, so let's skip it.
|
|
349
|
+
if (!splitResult) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
340
352
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
conversionApi.writer.createPositionBefore(modelNode),
|
|
344
|
-
conversionApi.writer.createPositionAfter(parts[parts.length - 1]),
|
|
345
|
-
);
|
|
353
|
+
// Insert element on allowed position.
|
|
354
|
+
conversionApi.writer.insert(modelNode, splitResult.position);
|
|
346
355
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
// If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
|
|
350
|
-
//
|
|
351
|
-
// before: <allowed><notAllowed>foo[]</notAllowed></allowed>
|
|
352
|
-
// after: <allowed><notAllowed>foo</notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>
|
|
356
|
+
// Consume appropriate value from consumable values list.
|
|
357
|
+
consumable.consume(viewItem, { name: true });
|
|
353
358
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
//
|
|
357
|
-
data.
|
|
358
|
-
|
|
359
|
-
|
|
359
|
+
const parts = conversionApi.getSplitParts(modelNode);
|
|
360
|
+
|
|
361
|
+
// Set conversion result range.
|
|
362
|
+
data.modelRange = writer.createRange(
|
|
363
|
+
conversionApi.writer.createPositionBefore(modelNode),
|
|
364
|
+
conversionApi.writer.createPositionAfter(parts[parts.length - 1]),
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
// Now we need to check where the `modelCursor` should be.
|
|
368
|
+
if (splitResult.cursorParent) {
|
|
369
|
+
// If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
|
|
370
|
+
//
|
|
371
|
+
// before: <allowed><notAllowed>foo[]</notAllowed></allowed>
|
|
372
|
+
// after: <allowed><notAllowed>foo</notAllowed><converted></converted><notAllowed>[]</notAllowed></allowed>
|
|
373
|
+
|
|
374
|
+
data.modelCursor = conversionApi.writer.createPositionAt(splitResult.cursorParent, 0);
|
|
375
|
+
} else {
|
|
376
|
+
// Otherwise just continue after inserted element.
|
|
377
|
+
data.modelCursor = data.modelRange.end;
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
// Ensures MathType processes the Wiris formulas before other plugins, preventing conflicts.
|
|
381
|
+
{ priority: "high" },
|
|
382
|
+
);
|
|
360
383
|
|
|
361
384
|
/**
|
|
362
385
|
* Whether the given view <math> element has a LaTeX annotation element.
|