froala / django-froala-editor

Package to integrate Froala WYSIWYG HTML rich text editor with Django.
https://froala.com/wysiwyg-editor
283 stars 72 forks source link

Django 3.0, admin inlines, throws JS error when new inline formset is added #97

Open jturmel opened 4 years ago

jturmel commented 4 years ago

Screen Shot 2020-08-06 at 11 42 35 AM

jturmel commented 4 years ago

Screen Shot 2020-08-06 at 11 46 58 AM

StevenMapes commented 2 years ago

I overload the froala-django.js file within my projects with this version that fixes the issue. The problem is the invalid called to jQuery along with the javascript on the page not allowing inline formsets to generate the editors as the prefix is not being replace in the cloned script tag. This fixes that

function getCookie(name) {
  var cookieValue = null;
  if (document.cookie && document.cookie != '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
      var cookie = cookies[i].trim();
      // Does this cookie string begin with the name we want?
      if (cookie.substring(0, name.length + 1) == (name + '=')) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
        break;
      }
    }
  }
  return cookieValue;
}

if (typeof django !== 'undefined' && typeof django.jQuery !== 'undefined') {
  (function ($) {
    $(document).on('formset:added', function (event, $row, formsetName) {
      $row.find('textarea').each(function () {
        var parts = this.closest('tr').id.split('-');
        var froala_script = this.nextElementSibling.innerText.replace('__prefix__', parts[parts.length-1]);
        froala_script = froala_script.replace("\n","");
        this.parentElement.innerHTML = this.outerHTML;

        var additional_script = document.createElement("script");
        additional_script.textContent = froala_script;
        document.head.appendChild((additional_script));
      });
    });
  })(django.jQuery);
}