简体   繁体   English

TinyMCE 4 向菜单栏添加下拉菜单

[英]TinyMCE 4 add drop down menu to menu bar

I need to add another drop down menu next to "Tools" item in TinyMCE 4:我需要在 TinyMCE 4 中的“工具”项旁边添加另一个下拉菜单:

新物品

The closest solution I found was this:我找到的最接近的解决方案是这样的:

// Adds a custom menu item to the editor that inserts contents when clicked
// The context option allows you to add the menu item to an existing default menu
tinymce.init({
   ...

   setup: function(ed) {
      ed.addMenuItem('example', {
         text: 'My menu item',
         context: 'tools',
         onclick: function() {
            ed.insertContent('Hello world!!');
         }
      });
   }
});

But it only adds an item to the already existing "Tools" menu.但它只会向现有的“工具”菜单添加一个项目。

You can try to specify both 'menu' and 'menubar' option when you call tinymce.init() to add a new menubar item on the modern theme.当您调用 tinymce.init() 以在现代主题上添加新的菜单栏项时,您可以尝试同时指定 'menu' 和 'menubar' 选项。

I tried it and it works.我试过了,它有效。

You can check the live demo on http://fiddle.tinymce.com/39eaab/1 with TinyMCE 4.1.7.您可以使用 TinyMCE 4.1.7 在http://fiddle.tinymce.com/39eaab/1上查看实时演示。

<script type="text/javascript">
tinymce.init({
    selector: "textarea",
    menu : {
        file   : {title : 'File'  , items : 'newdocument'},
        edit   : {title : 'Edit'  , items : 'undo redo | cut copy paste pastetext | selectall'},
        insert : {title : 'Insert', items : 'link media | template hr'},
        view   : {title : 'View'  , items : 'visualaid'},
        format : {title : 'Format', items : 'bold italic underline strikethrough superscript subscript | formats | removeformat'},
        table  : {title : 'Table' , items : 'inserttable tableprops deletetable | cell row column'},
        tools  : {title : 'Tools' , items : 'spellchecker code'},
        newmenu: {title : 'New Menu', items : 'newmenuitem'}
    },
    menubar: 'file edit newmenu',
    setup: function(editor) {
        editor.addMenuItem('newmenuitem', {
            text: 'New Menu Item',
            context: 'newmenu',
            onclick: function () { alert('yey!'); }
        });
    }
});
</script>

<form method="post" action="dump.php">
    <textarea name="content"></textarea>
</form>

Not sure it's what you need, but what if you try this:不确定这是你需要的,但如果你试试这个怎么办:

<script type="text/javascript">
tinymce.init({
    selector: "textarea",
    toolbar: "mybutton",
    setup: function(editor) {
        editor.addButton('mybutton', {
            type: 'menubutton',
            text: 'My button',
            icon: false,
            menu: [
                {text: 'Menu item 1', onclick: function() {editor.insertContent('Menu item 1');}},
                {text: 'Menu item 2', onclick: function() {editor.insertContent('Menu item 2');}}
            ]
        });
    }
});
</script>

You can view the result of the code here你可以在这里查看代码的结果

Since you generate the menu dynamically, that is, the menu data comes in a JSON object, I try to loop through a FOR and it does not work for me.由于您动态生成菜单,即菜单数据来自一个 JSON 对象,我尝试通过 FOR 循环,但它对我不起作用。

editor.ui.registry.addMenuButton('mybutton', {
                text: 'Insertar un campo del formulario',
                fetch: function (callback) {
                    var menuItems = [];
                    for (let i = 0; i < Object.keys(json).length; i++){
                        //console.log(json[i]);
                    //for (var clave in json) {
                        var items = [
                            {
                                type: 'menuitem',
                                text: json[i].text,
                                onAction: function () {
                                    editor.insertContent(json[i].value);
                                }
                            }
                        ];
                        menuItems.push(items);
                    }
                    callback(menuItems);
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM