jazzband / django-tinymce

TinyMCE integration for Django
http://django-tinymce.readthedocs.org/
MIT License
1.28k stars 317 forks source link

Allow formset integration #385

Closed shagi closed 2 years ago

shagi commented 2 years ago

I'm working on a formset with a formfield using tinymce, outside django-admin.

Could the initTinyMCE javascript function made reachable? Maybe with another name, to ensure its unique in context?

Thats the only change needed to suport integration with django formsets in public views.

I could made the pull-request if wanted, just curious if there is a reason I overlook to keep this function outside scope.

claudep commented 2 years ago

Could you elaborate a bit more about the issue you have currently with your use case?

shagi commented 2 years ago

I have a formset and using the https://github.com/nortigo/jquery-formset javscript plugin to create and delete rows in the formset.

When creating new rows I have to initialize tinymce in one of the form fields. As the initialization code is out of scope the unique way I found to use tiniymce in those fields is duplicating the initTinyMCE function an call it on a callback function.

Basically I need some way to tell django-tinymce that a new field has been added to de DOM and that it must initialize tinimce on that field.

The jquery-formset library is just an usage example, as in the near future I want to drop jquery from the code.

claudep commented 2 years ago

Did you try triggering the formset:added event yourself when you are adding the new row? Like the Django admin is doing: https://github.com/django/django/blob/main/django/contrib/admin/static/admin/js/inlines.js#L91

shagi commented 2 years ago

It could work, but for this I need to get django.jQuery in scope.

Managing this event without the need of django.jQuery could be another fine way to get what I need.

    // initialize the TinyMCE editor after adding an inline in the django admin context.
    if (typeof(django) !== 'undefined' && typeof(django.jQuery) !== 'undefined') {
      django.jQuery(document).on('formset:added', function(event, $row, formsetName) {
        $row.find('textarea.tinymce').each(function() {
          initTinyMCE(this);
        });
      });
    }
claudep commented 2 years ago

You may insert the admin/js/jquery.init.js file to have django.jQuery defined. Hopefully Django will generate vanilla events soon to be able to get rid of such tricks. https://code.djangoproject.com/ticket/33328

shagi commented 2 years ago

Ok. I undertand that when django generates vanilla events my problem will be solved without modifications to this app.

It remains dificult to integrate in some other workflows, for example when attaching forms with javascript after the page gets ready. The capacity to initialize tinymce on any momment is interesting, and it needs little change.

But this is another history, close the ticket if you want.

claudep commented 2 years ago

I think I wouldn't oppose to expose initTinyMCE in the global space, but others may disagree. You can always try to make a PR and we'll see...

claudep commented 2 years ago

FYI, Django 4.1 will produce JS native formset:added events.

claudep commented 2 years ago

And now with a28d68e9, django-tinymce should support native JS formset:added event on Django 4.1+