Tiny MCE添加自定义HTML标记

时间:2013-03-14 10:52:22

标签: tinymce modx

我正在使用Tiny 4.3.3 for MODx 我需要添加一个

<p class="classname">
 <em class="openImg"></em>
  Some randome Input text by the user
 <em class="closeImg"></em>
</p>

我不介意是否是一个额外的菜单项或在Paragraph下拉菜单中。我只想减少可能的工作时间。

我试过这个http://alexzag.blogspot.co.uk/2009/12/custom-tags-in-tinymce.html,但不管怎么说这都不行。

有人能指点我一个好的教程或者告诉我如何在下拉菜单中添加一个图标或名称,自动创建带有正确类的p和em标签吗? 感谢

2 个答案:

答案 0 :(得分:44)

问题问题已经有一段时间了,但由于我目前正在做同样的事情,我想我会分享我在这件事上的发现和解决方案。 :)

我正在为工作中的测试项目扩展TinyMCE,我们的解决方案需要自定义标签 - 其中一些用户应该只能输入一行,而在其他人中(作为 em )很多文字。

要完成的步骤,以达到理想的解决方案:

  • 使用两个配置关键字 extended_valid_elements custom_elements 告诉TinyMCE编辑器,您的元素良好

    tinymce.init({ selector: "textarea#editor", // ... extended_valid_elements : "emstart,emend", custom_elements: "emstart,emend", content_css: "editor.css" });

  • 为开始和结束标记创建两个图像。我将我的名字命名为 emstart.png emend.png

  • 为自定义元素创建自定义CSS样式,并将它们放在自定义CSS文件中(TinyMCE配置中指定的那个,在我的情况下是 editor.css ):< / p>

    emstart { background: url(emstart.png) no-repeat; background-position: left -3px top -3px; padding: 10px 10px 5px 10px; background-color:#aabbcc; border:1px dotted #CCCCCC; height:50px; width:100px; }

    emend { background: url(emend.png) no-repeat; background-position: left -3px bottom -3px; padding: 5px 10px 10px 10px; background-color:#aabbcc; border:1px dotted #CCCCCC; height:50px; width:100px; }

  • 编写一个自定义插件,输入新标签并将其放入plugins目录。我打电话给我的 customem

插件代码:

tinymce.PluginManager.add('customem', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('customEmElementButton', {
        text: 'Custom EM',
        icon: false,
        onclick: function() {
            // Open window
            editor.windowManager.open({
                title: 'Please input text',
                body: [
                    {type: 'textbox', name: 'description', label: 'Text'}
                ],
                onsubmit: function(e) {
                    // Insert content when the window form is submitted
                    editor.insertContent('<emstart>EM Start</emstart><p>' + e.data.description + '</p><emend>EM End</emend>');
                }
            });
        }
    });

    // Adds a menu item to the tools menu
    editor.addMenuItem('customEmElementMenuItem', {
        text: 'Custom EM Element',
        context: 'tools',
        onclick: function() {
            editor.insertContent('<emstart>EM Start</emstart><p>Example text!</p><emend>EM End</emend>');
        }
    });
});

最后一步是将自定义插件加载到编辑器中(使用插件工具栏配置选项)并享受结果:

    tinymce.init({
        selector: "textarea#editor",
        height: "500px",
        plugins: [
                 "code, preview, contextmenu, image, link, searchreplace, customem"
           ],
        toolbar: "bold italic | example | code | preview | link | searchreplace | customEmElementButton",
        contextmenu: "bold italic",
        extended_valid_elements : "emstart,emend",
        custom_elements: "emstart,emend",
        content_css: "editor.css",
     });

编辑器现在看起来像这样:

enter image description here

和你的例子中的来源一样:

enter image description here

答案 1 :(得分:5)

首先,您需要根据需要修改tinymce设置valid_elementsvalid_children(将em添加到valid_elementsem作为孩子到期望的标签(可能是p)到valid_children)。

其次,您需要一个带有自己的下拉菜单的插件或按钮来插入此代码。