@@ -38,8 +38,7 @@ export default class MathType extends Plugin {
3838
3939 init ( ) {
4040 // Create the MathType API Integration object
41- const integration = this . _addIntegration ( ) ;
42- currentInstance = integration ;
41+ this . integration = this . _addIntegration ( ) ;
4342
4443 // Add the MathType and ChemType commands to the editor
4544 this . _addCommands ( ) ;
@@ -61,8 +60,7 @@ export default class MathType extends Plugin {
6160 * Inherited from Plugin class: Executed when CKEditor5 is destroyed
6261 */
6362 destroy ( ) {
64- // eslint-disable-line class-methods-use-this
65- currentInstance . destroy ( ) ;
63+ this . integration . destroy ( ) ;
6664 }
6765
6866 /**
@@ -113,6 +111,16 @@ export default class MathType extends Plugin {
113111 } ,
114112 { priority : "highest" } ,
115113 ) ;
114+
115+ this . listenTo ( editor . editing . view . document , "change:isFocused" , ( _evt , _data , isFocused ) => {
116+ if ( isFocused ) {
117+ currentInstance = integration ;
118+ }
119+ } ) ;
120+
121+ if ( editor . editing . view . document . isFocused ) {
122+ currentInstance = integration ;
123+ }
116124 }
117125
118126 return integration ;
@@ -393,15 +401,15 @@ export default class MathType extends Plugin {
393401 const formula = modelItem . getAttribute ( "formula" ) ;
394402 const htmlContent = modelItem . getAttribute ( "htmlContent" ) ;
395403
396- if ( ! formula && ! htmlContent ) {
404+ if ( ! formula && ! htmlContent ) {
397405 return null ;
398406 }
399407
400408 let imgElement = null ;
401409
402410 if ( htmlContent ) {
403411 imgElement = htmlDataProcessor . toView ( htmlContent ) . getChild ( 0 ) ;
404- } else if ( formula ) {
412+ } else if ( formula ) {
405413 const mathString = formula . replaceAll ( 'ref="<"' , 'ref="<"' ) ;
406414
407415 const imgHtml = Parser . initParse ( mathString , integration . getLanguage ( ) ) ;
@@ -412,9 +420,9 @@ export default class MathType extends Plugin {
412420 }
413421
414422 /* 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- */
423+ * we must create a new EmptyElement which is independent of the
424+ * DataProcessor being used by this editor instance
425+ */
418426 if ( imgElement ) {
419427 return viewWriter . createEmptyElement ( "img" , imgElement . getAttributes ( ) , {
420428 renderUnsafeAttributes : [ "src" ] ,
@@ -523,7 +531,7 @@ export default class MathType extends Plugin {
523531 // If we found a formula image, we should find MathML data, and then substitute the entire image.
524532 const regexp = / « m a t h \b [ ^ » ] * » ( .* ?) « \/ m a t h » / g;
525533 const safexml = formula . match ( regexp ) ;
526- if ( safexml !== null ) {
534+ if ( safexml !== null ) {
527535 let decodeXML = MathML . safeXmlDecode ( safexml [ 0 ] ) ;
528536 modifiedData = modifiedData . replace ( formula , decodeXML ) ;
529537 }
@@ -550,7 +558,9 @@ export default class MathType extends Plugin {
550558 Configuration,
551559 Listeners,
552560 IntegrationModel,
553- currentInstance,
561+ get currentInstance ( ) {
562+ return currentInstance ;
563+ } ,
554564 Latex,
555565 } ;
556566 }
0 commit comments