Search code examples
javascriptinitializationtinymcetinymce-plugins

Set TinyMCE editor option after Initialization


How to set TinyMCE templates option after initialization? Is it possible in TinyMCE 4?

I've seen in a source code of Template plugin that this option is read every time user clicks on 'Insert template' button. So I think it could pick up a new value on every click.

ps. A possible solution could be to use URL in templates, but let's pretend it's not.


Solution

  • UPDATE: In TinyMCE v4.3.3 Templates plugin was updated, so templates setting can be a function that gets a callback that can provide templates. Good news, you don't have to patch plugin =)

    Initialization:

    $scope.tinyMceOptions = {
        plugins: 'template',
        ...
        templates: function(callback) {
            // Here you can do whatever you want with callback;
            // for example, you can provide different arguments on every click
            callback($scope.variableTemplates);
        }
    

    (For previous TinyMCE versions) My solution is to patch Template plugin that I can specify a function as templates provider. Here's what I've added to createTemplateList() function:

    else if (typeof templateList === 'function') {
        callback(templateList());
    }
    

    And during initialization I specify a function in templates:

    $scope.tinyMceOptions = {
        plugins: 'template',
        ...
        templates: function() {
            return $scope.variableTemplates;
        }