@wiris/mathtype-ckeditor5 8.13.3 → 8.14.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/src/plugin.js CHANGED
@@ -306,57 +306,80 @@ export default class MathType extends Plugin {
306
306
  });
307
307
 
308
308
  // Data view -> Model
309
- editor.data.upcastDispatcher.on("element:img", (evt, data, conversionApi) => {
310
- const { consumable, writer } = conversionApi;
311
- const { viewItem } = data;
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
- // Only upcast when is wiris formula
314
- if (viewItem.getClassNames().next().value !== "Wirisformula") {
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
- const mathAttributes = [...viewItem.getAttributes()].map(([key, value]) => ` ${key}="${value}"`).join("");
319
- const htmlContent = Util.htmlSanitize(`<img${mathAttributes}>`);
323
+ // Check if we can consume the element name.
324
+ if (!consumable.test(viewItem, { name: true })) {
325
+ return;
326
+ }
320
327
 
321
- const modelNode = writer.createElement("mathml", { htmlContent });
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
- // Find allowed parent for element that we are going to insert.
324
- // If current parent does not allow to insert element but one of the ancestors does
325
- // then split nodes to allowed parent.
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
- // When there is no split result it means that we can't insert element to model tree, so let's skip it.
329
- if (!splitResult) {
330
- return;
331
- }
338
+ const mathAttributes = [...viewItem.getAttributes()].map(([key, value]) => ` ${key}="${value}"`).join("");
339
+ const htmlContent = Util.htmlSanitize(`<img${mathAttributes}>`);
332
340
 
333
- // Insert element on allowed position.
334
- conversionApi.writer.insert(modelNode, splitResult.position);
341
+ const modelNode = writer.createElement("mathml", { htmlContent });
335
342
 
336
- // Consume appropriate value from consumable values list.
337
- consumable.consume(viewItem, { name: true });
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
- const parts = conversionApi.getSplitParts(modelNode);
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
- // Set conversion result range.
342
- data.modelRange = writer.createRange(
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
- // Now we need to check where the `modelCursor` should be.
348
- if (splitResult.cursorParent) {
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
- data.modelCursor = conversionApi.writer.createPositionAt(splitResult.cursorParent, 0);
355
- } else {
356
- // Otherwise just continue after inserted element.
357
- data.modelCursor = data.modelRange.end;
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.