在tinymce中添加自定义下拉列表

时间:2013-05-15 09:47:20

标签: drop-down-menu tinymce customization

我必须在tinymce中显示一个下拉列表。我用谷歌搜索任何教程或任何好的例子,但我发现代码:

// Adds a menu to the currently active editor instance
var dm = tinyMCE.activeEditor.controlManager.createDropMenu('somemenu');

// Add some menu items
dm.add({title : 'Menu 1', onclick : function() {
    alert('Item 1 was clicked.');
}});

dm.add({title : 'Menu 2', onclick : function() {
    alert('Item 2 was clicked.');
}});

// Adds a submenu
var sub1 = dm.addMenu({title : 'Menu 3'});
sub1.add({title : 'Menu 1.1', onclick : function() {
    alert('Item 1.1 was clicked.');
}});

// Adds a horizontal separator
sub1.addSeparator();

sub1.add({title : 'Menu 1.2', onclick : function() {
    alert('Item 1.2 was clicked.');
}});

// Adds a submenu to the submenu
var sub2 = sub1.addMenu({title : 'Menu 1.3'});

// Adds items to the sub sub menu
sub2.add({title : 'Menu 1.3.1', onclick : function() {
    alert('Item 1.3.1 was clicked.');
}});

sub2.add({title : 'Menu 1.3.2', onclick : function() {
    alert('Item 1.3.2 was clicked.');
}});

dm.add({title : 'Menu 4', onclick : function() {
    alert('Item 3 was clicked.');
}});

// Display the menu at position 100, 100
dm.showMenu(100, 100);

此代码似乎创建了一个下拉列表,但我不知道在何处放置此代码或如何使用它来显示自定义下拉列表。 请有人帮我在tinyMCE中添加自定义下拉列表。

3 个答案:

答案 0 :(得分:10)

var myListItems = ['Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7',
        'Item8', 'Item9', 'Item10', 'Item11'];

tinymce.PluginManager.add('mypluginname', function (editor) {
    var menuItems = [];
    tinymce.each(myListItems, function (myListItemName) {
        menuItems.push({
            text: myListItemName,
            onclick: function () {
                editor.insertContent(myListItemName);
            }
        });
    });

    editor.addButton('mypluginname', {
        type: 'menubutton',
        text: 'My Plugin Name',
        icon: 'code',
        menu: menuItems
    });

    editor.addMenuItem('mypluginnameDropDownMenu', {
        icon: 'code',
        text: 'My Plugin Name',
        menu: menuItems,
        context: 'insert',
        prependToContext: true
    });
});

然后在初始化编辑器时添加到插件列表中:

$('#myTesxtArea').tinymce({
theme: "modern",
convert_urls: false,
height: 100,
plugins: [
    "advlist autolink lists link image charmap print preview hr anchor pagebreak",
    "searchreplace wordcount visualblocks visualchars code fullscreen",
    "insertdatetime nonbreaking save table contextmenu directionality",
    "paste textcolor","mypluginname"
],
toolbar1: "undo redo | forecolor backcolor | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
image_advtab: true
});

下面是带有下拉列表的面板按钮:

dropdown demo

答案 1 :(得分:5)

首先,注册插件:

var myListItems = ['Item1','Item2'];
tinymce.PluginManager.add('myNewPluginName', function(editor) {
    var menuItems = [];
    tinymce.each(myListItems, function(myListItemName) {
        menuItems.push({
            text: myListItemName,
            onclick: function() {
                editor.insertContent(myListItemName);
            }
        });
    });

    editor.addMenuItem('insertValueOfMyNewDropdown', {
        icon: 'date',
        text: 'Do something with this new dropdown',
        menu: menuItems,
        context: 'insert'
    });
});

然后在初始化编辑器时添加到插件列表中:

$('#myTesxtArea').tinymce({
    theme: "modern",
    convert_urls: false,
    height: 100,
    plugins: [
        "advlist autolink lists link image charmap print preview hr anchor pagebreak",
        "searchreplace wordcount visualblocks visualchars code fullscreen",
        "insertdatetime nonbreaking save table contextmenu directionality",
        "myNewPluginName paste textcolor"
    ],
    toolbar1: "undo redo | forecolor backcolor | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
    image_advtab: true
});

答案 2 :(得分:0)

以下是TinyMCE创建插件http://www.tinymce.com/wiki.php/TinyMCE3x:Creating_a_plugin的文档。我会从这开始,只是为了了解插件的工作原理。然后,为了更好地创建下拉菜单,请查看contextmenu插件。复制并修改它以满足您的需求。