I’m encountering issues with destroying and recreating CKEditor 5 instances in my application. Here’s a summary of what I’m trying to achieve and the problems I’m facing:
I need to destroy an existing CKEditor 5 instance when opening a modal and then create a new instance within that modal.
function initializeEditor() {
if (window.editorInstance) {
window.editorInstance.destroy();
}
ClassicEditor
.create(document.querySelector('#email_signature_address'), {
toolbar: ['link'],
editorConfig: {
emailMode: true
}
})
.then(editor => {
window.editorInstance = editor;
})
.catch(error => {
console.error('CKEditor initialization error: ', error);
});
}
$('#createPersonModal').on('shown.bs.modal', function() {
initializeEditor();
});
Problem:
- When calling window.editorInstance.destroy(), I get an error: TypeError: Cannot read properties of undefined (reading ‘attributes’). This indicates an issue with destroying the editor instance.
- Direct manipulation of the editor’s UI elements (e.g., window.editorInstance.ui.view.element.remove()) does not seem to work
2
Answers
In my case help this solution. I add off('shown.bs.modal') before
Ensure that the editorInstance exists before calling the destroy method and the editor is correctly initialized in the modal.