Equation image added to two editors instead of intended one ckeditor in single page
📝 Provide detailed reproduction steps (if any)
@Reinmar @scofalik @oleq @Mgsy
I have two editor instances (one as question and second as answer) on a single web page and implemented a custom Add Equations plugin.
Whenever I'm trying to add an equation in the question editor, It gets added in both question and answer editor instead of only question editor. I figured out that there was a problem with my custom equation plugin. I have tried in multiple ways but unable to achieve the expected output.
(Note : Rendering two editor instance using element reference(viewChild) in angular 7)
ckeditor-tool.ts
@ViewChild('editor') public editorRef: ElementRef;
ckeditor-tool.html `
`
Both editor are rendering properly.
Custom plugin code is below :
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import { modelToViewAttributeConverter } from '@ckeditor/ckeditor5-image/src/image/converters';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
export class MathText extends Plugin {
init() {
const editor = this.editor;
const view = editor.editing.view;
const componentFactory = editor.ui.componentFactory;
this._defineSchema();
this._defineConverters();
this._defineListenerModalToCK();
view.addObserver( ClickObserver );
componentFactory.add('MathText', locale => {
const view = new ButtonView(locale);
view.set({
label: 'Add Equations',
withText: true,
tooltip: true
});
// Callback executed once the image is clicked.
this.listenTo(view, 'execute', () => {
openModel();
console.log("MathText Plugin called");
});
return view;
});
this.listenTo( editor.editing.view.document, 'click', ( evt, data ) => {
// if ( data.domEvent.detail == 2 ) {
if ( data.domEvent.detail == 1 ) { // temp
console.log("_editorToPopupdoubleClickHandler");
this._editorToPopupdoubleClickHandler( data.domTarget, data.domEvent );
evt.stop();
}
}, { priority: 'highest' } );
}
_defineSchema () {
const schema = this.editor.model.schema;
schema.extend( 'image', {
allowAttributes: [ 'data-mathml','advanced' ]
} );
}
_defineConverters() {
const conversion = this.editor.conversion;
conversion.for( 'downcast' ).add( modelToViewAttributeConverter( 'data-mathml' ) );
conversion.for( 'upcast' ).attributeToAttribute( {
view: {
name: 'img',
key: 'data-mathml'
},
model: 'data-mathml'
} );
conversion.for( 'downcast' ).add( modelToViewAttributeConverter( 'advanced' ) );
conversion.for( 'upcast' ).attributeToAttribute( {
view: {
name: 'img',
key: 'advanced'
},
model: 'advanced'
} );
}
_defineListenerModalToCK () {
var model = this.editor.model;
var selection = model.document.selection;
window.addEventListener('setDatatoCK', (data) => {
model.change( writer => {
const imageElement = writer.createElement( 'image', {
src: data.detail.imgURL,
'data-mathml': data.detail.latexFrmla,
advanced : data.detail.advanced
} );
model.insertContent( imageElement, selection );
} );
})
}
_editorToPopupdoubleClickHandler(element, event) {
if (element.nodeName.toLowerCase() == 'img') {
var latexStr = element.getAttribute("data-mathml");
var advanced = element.getAttribute("advanced");
if (typeof event.stopPropagation != 'undefined') { // old I.E compatibility.
event.stopPropagation();
} else {
event.returnValue = false;
}
loadDataFromCkEditortoPopup({latex:latexStr,advanced :advanced});
// event.stop();
}
};
}
✔️ Expected result
❌ Actual result
📃 Other details
- Browser: Google Chrome Version 78.0.3904.108 (Official Build) (64-bit)
- OS: macOS Mojave 10.14.6
- CKEditor version: CKEditor 5
- Installed CKEditor plugins: ckeditor5-editor-classic,ckeditor5-image,ckeditor5-essentials,ckeditor5-basic-styles,ckeditor5-core,ckeditor5-ui
Please help me with the same.
If you'd like to see this fixed sooner, add a 👍 reaction to this post.
I assume each editor instances load the MathText plugin.
And if that's so, my first guess is that when you defined window.addEventListener('setDatatoCK', (data) => { ... } you actually made both editors to listen to the event and update their content upon it. Simply, when setDatatoCK is fired by some modal, both editors have a listener for that event and they both gonna react.
Log directly in _defineListenerModalToCK() to see it's called twice.
@oleq Yes, You are right, When setDatatoCK is fired by some modal, both editors have a listener for that event and they both react the same. I can't log directly in_defineListenerModalToCK.
I know addEventListener event listener has to be unique for each editor instance.
So after that, I have changed _defineListenerModalToCK function
_defineEquationWriter (_counter,dataObj = '') {
const model = this.editor.model;
const selection = model.document.selection;
const options = {};
options.detail = dataObj;
options._counter = _counter;
options.editor = this.editor;
const openerMethod = 'modal';
const originalOnInit = options.onInit;
options.onInit = mathText => {
mathText.addEventListener("equation:add"+_counter, function(data){
console.log("file:choose -> " + JSON.stringify(data.target));
model.change( writer => {
const imageElement = writer.createElement( 'image', {
src: data.target.imgURL,
'data-mathml': data.target.latexFrmla,
advanced : data.target.advanced
} );
model.insertContent( imageElement, selection );
} );
});
}
window.org.ckditor.mathTextPlugin[openerMethod](options);
}
But still, I'm getting the same error. Could please help me to give some logic or more suggestion to fix my issue.
But still, I'm getting the same error.
What error? Because you didn't mention any error (or am I missing something?).
But still, I'm getting the same error.
That means, facing the same problem as mentioned below :
Whenever I'm trying to add an equation in the question editor, It gets added in both question and answer editor instead of only question editor.
After your input, I have tried in multiple ways but the image gets added to both editors. Could please help me to give some logic or more suggestion to fix my issue. @oleq
There has been no activity on this issue for the past two years. We've marked it as stale and will close it in 30 days. We understand it may be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.