如何以编程方式单击TinyMCE工具栏的按钮?

问题描述 投票:0回答:7

我想在提交按钮旁边像平常的表单按钮一样制作预览按钮(就像在大多数情况下“发布新主题”表单一样)。如何以编程方式模拟工具栏的预览按钮单击?

我尝试过

$('#article_body_preview').click()
但没有成功。 (我使用 jQuery lib,#article_body_preview 是工具栏的预览按钮元素)

javascript tinymce
7个回答
7
投票

我自己也有类似的问题。您可以在插件的源代码中找到命令的名称。例如。对于预览插件,源代码位于 jscripts/tiny_mce/plugins/preview/editor_plugin_src.js 寻找第 37 行:

ed.addButton('preview', {title : 'preview.preview_desc', cmd : 'mcePreview'});

所以命令名称不是“preview”而是“mcePreview”。

我想在 init 上设置全屏模式。我最后添加了

init_instance_callback : 'resizeEditorBox' 

初始化。

resizeEditorBox = function (editor) {
            setTimeout("tinyMCE.activeEditor.execCommand('mceFullScreen')", 100);
}

在初始化并注释掉之前:

//                    a.win.setTimeout(function() {
//                        tinymce.dom.Event.remove(a.win, "resize", e.resizeFunc);
//                        tinyMCE.get(c.getParam("fullscreen_editor_id")).setContent(c.getContent({format:"raw"}), {format:"raw"});
//                        tinyMCE.remove(c);
//                        a.remove("mce_fullscreen_container");
//                        i.style.overflow = c.getParam("fullscreen_html_overflow");
//                        a.setStyle(a.doc.body, "overflow", c.getParam("fullscreen_overflow"));
//                        a.win.scrollTo(c.getParam("fullscreen_scrollx"), c.getParam("fullscreen_scrolly"));
//                        tinyMCE.settings = tinyMCE.oldSettings
//                    }, 10)

在 editor_plugin.js 中 - 因为我不需要任何其他模式,只需要全屏模式。希望有帮助。


7
投票

我知道这是一个老问题,但我在tinyMCE 4上也遇到了同样的问题,并且无法使上述任何解决方案发挥作用。这对我有用:

tinyMCE.activeEditor.buttons.charmap.onclick();

例如,我试图在插件中创建一个自定义菜单,其中包含一些内置命令:

tinymce.PluginManager.add('myplugin', function(editor) {

    editor.addButton('mymenu', {
        type: 'menubutton',
        text: 'Insert',
        icon: false,
        menu: [
            {
                text: 'Special Character',
                icon: 'charmap',
                onclick: function() { editor.buttons.charmap.onclick(); }
            }
        ]
    });

});

5
投票

tinyMCE.activeEditor.execCommand('commandName');

其中

commandName
是注册到预览按钮的命令。


2
投票

我想补充一点,我还需要将按钮设置为活动状态(突出显示)。我使用这段代码来完成此任务:

tinyMCE.activeEditor.controlManager.setActive('spellchecker', true);

将拼写检查器替换为您在 TinyMCE init 上的 theme_advanced_buttons 中设置的工具栏选项名称。

干杯!

顺便说一句,如果您想默认打开某个按钮,这里有一个论坛帖子可能会帮助您: http://www.tinymce.com/forum/viewtopic.php?pid=95893#p95893


1
投票

您还可以执行以下操作:

editor.ui.registry.getAll().buttons['my-button'].onAction()

我的案例的完整示例:(我尝试在不导入任何内容的情况下触发)

const parentTinyMCEId = 'mce_0' // When you know the target id window.tinymce.editors.forEach(editor => { // Loop to find correct editor if (editor.id === tinyMCEId) { editor.execCommand('mceFocus', false, editor.id) // Set focus to the editor setTimeout(() => { editor.ui.registry.getAll().buttons['my-button'].onAction() // Trigger it! }, 100) return false // Stop looping } })
    

0
投票

Button creation example

要从您下一步想要执行的位置触发单击 TinyMCE 按钮:

jQuery(".js_inpost_gallery_insert_shortcode").click(function() { tinyMCE.get(tinyMCE.activeEditor.editorId).controlManager.get(tinyMCE.activeEditor.editorId + "_" + 'pn_tinymce_button').settings.onclick(); return false; });
    

0
投票
无论谁需要,对于Rails with capybara,点击工具栏上的任何插件图标:

page.execute_script("$(tinymce.activeEditor.execCommand('mceEmoticons'))")
代替 

'mceEmoticons'

 可以使用任何其他插件,只需添加 
'mce'
 前缀和插件名称的第一个大写字母即可。

© www.soinside.com 2019 - 2024. All rights reserved.