Tiny MCE添加自定义HTML标记

Jon*_*rft 15 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>
Run Code Online (Sandbox Code Playgroud)

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

我试过这个http://alexzag.blogspot.co.uk/2009/12/custom-tags-in-tinymce.html,但不知怎的,这不起作用.

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

pas*_*sty 46

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

我正在为工作中的测试项目扩展TinyMCE,我们的解决方案需要自定义标签 - 其中一些用户应该只能输入一行,而在其他行中(作为你的em)可以输入大量文本.

要完成的步骤,以实现所需的解决方案:

  • 告诉TinyMCE编辑器,使用两个配置关键字extended_valid_elementscustom_elements,你的元素很好:

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

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

  • 为自定义元素创建自定义CSS样式,并将它们放在自定义CSS文件中(在我的案例editor.css中,在TinyMCE配置中指定的那个):

    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目录中.我打电话给我的客户:

插件代码:

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>');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

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

    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",
     });
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

和你的例子中的来源:

在此输入图像描述


Tha*_*ama 5

首先,你需要修改tinymce设置valid_elementsvalid_children你的需要(添加em到所需的标签valid_elementsem子标签(可能p)valid_children).

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