django-froala-editor icon indicating copy to clipboard operation
django-froala-editor copied to clipboard

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

Open jturmel opened this issue 5 years ago • 2 comments

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

jturmel avatar Aug 06 '20 16:08 jturmel

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

jturmel avatar Aug 06 '20 16:08 jturmel

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);
}

StevenMapes avatar Nov 05 '21 23:11 StevenMapes