Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textarea not updating on editor.save() #343

Open
gusarg81 opened this issue Mar 4, 2021 · 5 comments
Open

Textarea not updating on editor.save() #343

gusarg81 opened this issue Mar 4, 2021 · 5 comments

Comments

@gusarg81
Copy link

gusarg81 commented Mar 4, 2021

Hi,

Before I save content to django model on the field that contains TinyMCE, I search for all active editors and then apply editor.save(). The textarea is not being updated by editor content. This is what I do:

$.each(tinyMCE.editors, function(index, editor){
    editor.save();
});

NOTE: I never had to do this with django-tinymce4-lite. I did in this case since I saw textarea was not getting updated.

Could someone explain me what is going on? Thanks.

@gusarg81
Copy link
Author

gusarg81 commented Mar 4, 2021

Never mind... Since I use ajax, I was doing this after sending FormData() instead of before.

But still the question is why I had to manually do this since with the another lib (django-tinymce4-lite) I didn't have to.

Thanks.

@nikolaysm
Copy link

You can also add setup option to TINYMCE_DEFAULT_CONFIG.

The below code:

setup: function (editor) {
    editor.on('change', function () {
        editor.save();
    });
}

TINYMCE_DEFAULT_CONFIG looks than like:


TINYMCE_DEFAULT_CONFIG = {
    "theme": "silver",
    ...
    ...
    "setup": "function(editor){editor.on('change', function(){editor.save();});}",
}

@gusarg81
Copy link
Author

@nikolaysm Hi, thanks for the tip! I will test it later.

@some1ataplace
Copy link

tinymce.init({
  selector: '#id_content', // Django form field ID
  setup: function(editor) {
    editor.on('save', function() {
      tinymce.triggerSave(); // Update the textarea
    });
  },
  // Additional TinyMCE configuration options...
});

In this example, we're using the editor.on() method to listen for the save event, and then calling the tinymce.triggerSave() method to update the hidden textarea element used by Django's form field. By default, TinyMCE replaces the textarea completely when it loads, so this allows us to keep the textarea element updated with the current state of the editor.


tinymce.init({
  selector: 'textarea',
});

// To update the textarea on editor.save()

editor.on('save', function () {
  tinymce.triggerSave();
});

In the above code, we first initialize the TinyMCE editor with the selector textarea. And then on the editor.save() event, we call the tinymce.triggerSave() method to trigger a DOM event that tells TinyMCE to update the underlying textarea with the current HTML content.

@MortenKaae
Copy link

Thanks for your suggestion, @nikolaysm. This solved my issues as well.

This solution was useful as I don't call tinymce.init directly, so I needed to be able to supply a Javascript function to the setup-option in TINYMCE_DEFAULT_CONFIG.

Initially I thought this wasn't possible as the source code (link) has a comment mentioning that "There is no way to pass a JavaScript function as an option because all options are serialized as JSON.".

I think this could easily be misinterpreted as it is possible to pass a JS-function as long as it's provided in a string. Unless I'm misunderstanding something, I think this comment should perhaps be updated as I reckon this information is helpful to others?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants