Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically get TinyMCE settings (to apply them to another init)

Tags:

jquery

tinymce

How can I get TinyMCE settings?

I'd want to take them from the first editor and store in variable (first or last doesn't matter, as long as script in question doesn't rely on id to get settings). Then, when needed, use them with tinyMCE.init to initialize new editor.

i.e.

var mysettings = $(".tinymce").last().GETEDITORSETTINGS;
$(".button").click(function(){
   mysettings.selector = ".newtextarea";
   tinyMCE.init(mysettings);
});
like image 691
MarcinWolny Avatar asked Feb 08 '23 05:02

MarcinWolny


1 Answers

There are two basic ways you can handle this...

Option 1 - Grab the current settings

tinymce.activeEditor.settings will get you an object that contains the settings for the current TinyMCE instance. You can then manipulate this in whatever way you need to create a new init object for the new editor instance you want to instantiate. (You can also use tinymce.get().settings to target a specific instance as opposed to the active editor).

Option 2 - (Recommended) Use a default init and clone it each time you need it

The other option (and the one I would recommend) is to create a "default" init object as a JavaScript variable on your page and then reference that later. For example:

var default_init = {
    theme: "modern",
    plugins....,
    toolbar....
}

...then when you want to invoke a TinyMCE instance you can use that base object to make your init:

var first_init = jQuery.extend({}, default_init);
first_init.selector = ".first_textarea";
tinymce.init(first_init);

var second_init = jQuery.extend({}, default_init);
second_init.selector = ".second_textarea";
tinymce.init(second_init);

(If you are not using jQuery you can write your own object cloning method instead of using extend() - search SO and you will find many examples. You don't need a deep copy for a typical init so a shallow copy algorithm should suffice).

like image 179
Michael Fromin Avatar answered Feb 13 '23 05:02

Michael Fromin