Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE: How to add plugin after init?

In the Voyager project they allow you to modify TinyMCE though a callback function:

function tinymce_init_callback(editor)
{
    //...
}

The methods of the editor are listed here.

I know that one usually list the plugins on init:

tinymce.init({
  plugins: [
    'image textcolor'
  ],

But is it possible to add a plugin like image with the editor object after the initialization? I couldn't find such a function in the docs.

like image 284
Adam Avatar asked Jul 19 '18 12:07

Adam


People also ask

How do you add a custom plugin to TinyMCE?

Using custom plugins with TinyMCE Copy code into plugins folder: Copy the entry point file (and any other files) into the plugins folder of the distributed TinyMCE code. The plugin can then be used by including it in the list of plugins specified by the plugins option.

How do I add TinyMCE plugin to WordPress?

Log in to your WordPress Dashboard, click Plugins, then Add New. Search for the Advanced TinyMCE Configuration plugin, then click Install Now. Once the plugin is installed, click Activate.

How do I add API key to TinyMCE?

Step 1. Getting the editor working on a page is as simple as including the TinyMCE script in the <head> and initializing it on a page. Example HTML code is provided on your account dashboard with your API key already inserted in the script. Copy the code directly from there to create an HTML file hosted on your server.


2 Answers

This is my solution:

function tinymce_init_callback(editor)
{
  editor.remove();
  editor = null;

  tinymce.init({
    selector: 'textarea.richTextBox',
    skin: 'voyager',
    min_height: 600,
    resize: 'vertical',
    plugins: 'print preview fullpage searchreplace autolink directionality visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists textcolor wordcount imagetools contextmenu colorpicker textpattern',
    extended_valid_elements: 'input[id|name|value|type|class|style|required|placeholder|autocomplete|onclick]',
    file_browser_callback: function (field_name, url, type, win) {
        if (type == 'image') {
            $('#upload_file').trigger('click');
        }
    },
    toolbar: 'styleselect bold italic underline | forecolor backcolor | alignleft aligncenter alignright | bullist numlist outdent indent | link image table youtube giphy | codesample code',
    convert_urls: false,
    image_caption: true,
    image_title: true
  });
}

First I remove the existing instance of TinyMCE editor (created by Voyager) and later I create a new one with the plugins and parameters I want.

When the page loads and the new instance is created, TinyMCE searches for plugins in 'public/vendor/tcg/voyager/assets/js/plugins'. TinyMCE searches for plugin JavaScript files using the name 'plugin.js', but many of these plugin files are named 'plugin.min.js', causing many errors that disable the editor. One solution for this inconvenience is to rename all the plugin files to 'plugin.js'.

like image 58
Guillermo Espert Avatar answered Oct 04 '22 03:10

Guillermo Espert


TinyMCE does not allow you load additional plugins after the editor is initialized. If you wanted to do this you would need to use the remove() API to remove the editor then you can use init() again with your new configuration to reload the editor.

like image 28
Michael Fromin Avatar answered Oct 04 '22 03:10

Michael Fromin