tinymce - 是否可以从工具栏外部调用自定义插件函数?

时间:2014-05-25 09:57:58

标签: javascript tinymce-4

行。我知道以前可能会问这个问题:

Here

但我的问题是如何从外部按钮调用插件功能而不是从工具栏按钮调用。

我添加了自定义插件:

tinymce.PluginManager.add('example', function(e) {
        function customfunction(){
                    e.focus(true);
                    alert('Hello TinyMce');
            }
        }
);

Check this on Fiddle

我从其他函数调用此customfunction,当我点击Custom Button时调用该函数。 像这样:

function clickme()
{
   tinymce.get('textareaid').plugins.example.customfunction();

}

按钮:

<button onclick="clickme()" >Custom Button</button>

但它对我不起作用?

通过这种方式致电custom plugin function,我做得对吗?

我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

一种可能性是向工具栏添加一个带有唯一ID的按钮,并调用该按钮的click事件。插件看起来像这样:

tinymce.PluginManager.add('example', function(e) {

        function customfunction() {
                    e.focus(true);
                    alert('Hello TinyMce');
            }


        e.addButton('testButton', {
            id: "testButton",
            text: 'Example',
            icon: false,
            onclick: function() {
                    // calls the custom function
                    customfunction();
                }
            });
    }
);

然后像这样初始化tinymce编辑器:

tinymce.init({
    selector: "textarea",
    plugins: "example",
    // show the button
    toolbar: "testButton undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});

最后调用按钮点击事件:

function clickme()
{
   document.getElementById("testButton").click();
}

请勿使用add_filter。完整的代码构成了你的小小提琴:

<script type="text/javascript">
tinymce.PluginManager.add('example', function(e) {
        function customfunction() {
                    e.focus(true);
                    alert('Hello TinyMce');
            }


    e.addButton('testButton', {
        id: "testButton",
        text: 'Example',
        icon: false,
        onclick: function() {
                customfunction();
            }
        });
}
);

tinymce.init({
    selector: "textarea",
    plugins: "example",
    toolbar: "testButton undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
//add_filter('mce_external_plugins', 'example');
function clickme()
{
   document.getElementById("testButton").click();
}

</script>

<form method="post" action="">
    <textarea name="content" id="textareaid"></textarea>
</form>

<button onclick="clickme();" >abc</button>

答案 1 :(得分:0)

您的功能存储在

tinymce.PluginManager.items[0]
检查你可以看到那个功能。试着提醒它

相关问题