Skip to content

Commit

Permalink
Merge pull request #119 from hvlads/112_ckeditor_5_in_inline_formsets
Browse files Browse the repository at this point in the history
resolve #112 init ckeditor5 in inline formset after adding row.
  • Loading branch information
hvlads authored Jan 21, 2023
2 parents ea76122 + 9e77936 commit 151c2bc
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions django_ckeditor_5/static/django_ckeditor_5/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import ClassicEditor from './src/ckeditor';
import './src/override-django.css';


let editors = [];
let editorsIds = [];

function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
Expand All @@ -17,11 +20,14 @@ function getCookie(name) {
return cookieValue;
}

document.addEventListener("DOMContentLoaded", () => {
let editors = [];
function createEditors() {
const allEditors = document.querySelectorAll('.django_ckeditor_5');
for (let i = 0; i < allEditors.length; ++i) {
const script_id = `${allEditors[i].id}_script`;
if (editorsIds.indexOf(script_id) !== -1){
continue;
}
allEditors[i].nextSibling.remove();
const upload_url = document.getElementById(
`ck-editor-5-upload-url-${script_id}`
).getAttribute('data-upload-url');
Expand All @@ -46,15 +52,25 @@ document.addEventListener("DOMContentLoaded", () => {
ClassicEditor.create(
allEditors[i],
config
).then( editor => {
).then(editor => {
const wordCountPlugin = editor.plugins.get('WordCount');
const wordCountWrapper = document.getElementById(`word-count-${script_id}`);
wordCountWrapper.innerHTML = '';
wordCountWrapper.appendChild(wordCountPlugin.wordCountContainer);
editors.push(editor);
}).catch(error => {

console.error((error));
});
editorsIds.push(script_id);
}
window.editors = editors;
window.ClassicEditor = ClassicEditor;
}

document.addEventListener("DOMContentLoaded", () => {
createEditors();
if (typeof django === "object" && django.jQuery) {
django.jQuery(document).on("formset:added", createEditors);
}
});

0 comments on commit 151c2bc

Please sign in to comment.