Skip to content

Commit 8afcf8b

Browse files
feat: handle multiple ck5 instances
1 parent eb74695 commit 8afcf8b

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

packages/ckeditor5/src/plugin.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import chemIcon from "../theme/icons/ckeditor5-chem.svg";
2626
import packageInfo from "../package.json";
2727

2828
export let currentInstance = null; // eslint-disable-line import/no-mutable-exports
29+
export const integration = null; // eslint-disable-line import/no-mutable-exports
2930

3031
export default class MathType extends Plugin {
3132
static get requires() {
@@ -38,20 +39,19 @@ export default class MathType extends Plugin {
3839

3940
init() {
4041
// Create the MathType API Integration object
41-
const integration = this._addIntegration();
42-
currentInstance = integration;
42+
this.integration = this._addIntegration();
4343

4444
// Add the MathType and ChemType commands to the editor
4545
this._addCommands();
4646

4747
// Add the buttons for MathType and ChemType
48-
this._addViews(integration);
48+
this._addViews(this.integration);
4949

5050
// Registers the <mathml> element in the schema
5151
this._addSchema();
5252

5353
// Add the downcast and upcast converters
54-
this._addConverters(integration);
54+
this._addConverters(this.integration);
5555

5656
// Expose the WirisPlugin variable to the window
5757
this._exposeWiris();
@@ -61,8 +61,7 @@ export default class MathType extends Plugin {
6161
* Inherited from Plugin class: Executed when CKEditor5 is destroyed
6262
*/
6363
destroy() {
64-
// eslint-disable-line class-methods-use-this
65-
currentInstance.destroy();
64+
this.integration.destroy();
6665
}
6766

6867
/**
@@ -113,6 +112,16 @@ export default class MathType extends Plugin {
113112
},
114113
{ priority: "highest" },
115114
);
115+
116+
this.listenTo(editor.editing.view.document, "change:isFocused", (_evt, _data, isFocused) => {
117+
if (isFocused) {
118+
currentInstance = integration;
119+
}
120+
});
121+
122+
if (editor.editing.view.document.isFocused) {
123+
currentInstance = integration;
124+
}
116125
}
117126

118127
return integration;
@@ -393,15 +402,15 @@ export default class MathType extends Plugin {
393402
const formula = modelItem.getAttribute("formula");
394403
const htmlContent = modelItem.getAttribute("htmlContent");
395404

396-
if(!formula && !htmlContent){
405+
if (!formula && !htmlContent) {
397406
return null;
398407
}
399408

400409
let imgElement = null;
401410

402411
if (htmlContent) {
403412
imgElement = htmlDataProcessor.toView(htmlContent).getChild(0);
404-
} else if(formula) {
413+
} else if (formula) {
405414
const mathString = formula.replaceAll('ref="<"', 'ref="&lt;"');
406415

407416
const imgHtml = Parser.initParse(mathString, integration.getLanguage());
@@ -412,9 +421,9 @@ export default class MathType extends Plugin {
412421
}
413422

414423
/* Although we use the HtmlDataProcessor to obtain the attributes,
415-
* we must create a new EmptyElement which is independent of the
416-
* DataProcessor being used by this editor instance
417-
*/
424+
* we must create a new EmptyElement which is independent of the
425+
* DataProcessor being used by this editor instance
426+
*/
418427
if (imgElement) {
419428
return viewWriter.createEmptyElement("img", imgElement.getAttributes(), {
420429
renderUnsafeAttributes: ["src"],
@@ -523,7 +532,7 @@ export default class MathType extends Plugin {
523532
// If we found a formula image, we should find MathML data, and then substitute the entire image.
524533
const regexp = /«math\b[^»]*»(.*?)«\/math»/g;
525534
const safexml = formula.match(regexp);
526-
if(safexml !== null) {
535+
if (safexml !== null) {
527536
let decodeXML = MathML.safeXmlDecode(safexml[0]);
528537
modifiedData = modifiedData.replace(formula, decodeXML);
529538
}

0 commit comments

Comments
 (0)