TinyMCE外包裹所选元素

发布于 2024-12-04 02:11:08 字数 3183 浏览 0 评论 0原文

我在让 TinyMCE 包装所选内容时遇到问题。

第一个 style_format 简单地将类添加到所选元素,这工作正常。

问题在于第二个 style_format 我希望它将所选元素包装在

Eg 内 使用下面的 jQuery 版本之前

<p>test text</p>
<p>test text</p>
<p>test text</p>
<p>test text</p>

<p class="accordion_top">test text</p>
<div class="accordion_middle">
<div class="accordion_middle-wrapper">
    <p>test text</p>
    <p>test text</p>
    <p>test text</p>
</div>
</div>

,有问题的代码是底部样式格式

$("#tinymce").tinymce({
    script_url : HOME+"/webapp/shared/javascript/tiny_mce/tiny_mce.js",
    mode : "textareas",
    theme : "advanced",
    skin : "cirkuit",
    width: "726",
    plugins : "advlist,insertdatetime,paste,print,searchreplace,spellchecker,table,wordcount,visualchars,xhtmlxtras,template,codemagic",
    theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,selectall,undo,redo,|,hr,acronym,charmap,blockquote,replace,|,insertdate,inserttime,|,cleanup,removeformat,codemagic,",
    theme_advanced_buttons2 : "wrap_div,styleselect,formatselect,|,bold,italic,underline,bullist,numlist,|,table,|,link,unlink,insertimage,spellchecker|mybutton",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",
    //theme_advanced_buttons1 : "|,,table,pasteword",
    theme_advanced_blockformats : "p,h2,h3,h4,h5,h6",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    forced_root_block : "p",
    force_br_newlines : false,
    force_p_newlines : true,
    valid_elements : "span[class|id],br[class|id],a[href|target|title],img[src|id|width|height|class|alt],i,"+
    "li[class|id],ul[class|id],ol[class|id],p[class|id],"+
    "table[class|id],th[class|id],tr[class|id],td[class|id],thead,tbody,"+
    "h1[class|id],h2[class|id],h3[class|id],h4[class|id],h5[class|id],h6[class|id],strong[class|id],"+
    "div[class|id]",
    content_css : TEMPLATE_HOME+"/css/tinymce.css?" + new Date().getTime(),
    plugin_insertdate_dateFormat : "%d/%m/%Y",
    plugin_insertdate_timeFormat : "%H:%M",
    paste_auto_cleanup_on_paste : true,
    convert_urls: false,
    relative_urls: false,
    // Style formats
    style_formats : [
         {title : 'Accorion Top', selector : 'p,h2,h3,h4,h5,h6', classes : 'accordion_top'},
         {title : 'Accorion Middle', block : 'div', classes : 'accordion_middle'}
    ],
    setup: function (ed) {
        // Create an wrap DIV button
        ed.addButton ('wrap_div', {
            'title' : 'Wrap Accordion',
            'image' : HOME+'/webapp/shared/javascript/tiny_mce/themes/advanced/img/createlink.gif',
            'onclick' : function () {
                var text = ed.selection.getContent({ 'format' : 'text' });
                if (text) {
                    tinyInsert('<div class="accordion_middle"><div class="accordion_middle-wrapper">' + text + '</div></div>');
                }
            }
        });
    }
});

I am having a problem getting TinyMCE to wrap the contents of the selection.

The first style_format simple adds the class to the selected element, this works fine.

The problem is with the second style_format I want it to wrap the selected elements inside of the

E.g.
BEFORE

<p>test text</p>
<p>test text</p>
<p>test text</p>
<p>test text</p>

AFTER

<p class="accordion_top">test text</p>
<div class="accordion_middle">
<div class="accordion_middle-wrapper">
    <p>test text</p>
    <p>test text</p>
    <p>test text</p>
</div>
</div>

Using the jQuery version I have below, the code in question is the bottom style formats

$("#tinymce").tinymce({
    script_url : HOME+"/webapp/shared/javascript/tiny_mce/tiny_mce.js",
    mode : "textareas",
    theme : "advanced",
    skin : "cirkuit",
    width: "726",
    plugins : "advlist,insertdatetime,paste,print,searchreplace,spellchecker,table,wordcount,visualchars,xhtmlxtras,template,codemagic",
    theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,selectall,undo,redo,|,hr,acronym,charmap,blockquote,replace,|,insertdate,inserttime,|,cleanup,removeformat,codemagic,",
    theme_advanced_buttons2 : "wrap_div,styleselect,formatselect,|,bold,italic,underline,bullist,numlist,|,table,|,link,unlink,insertimage,spellchecker|mybutton",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",
    //theme_advanced_buttons1 : "|,,table,pasteword",
    theme_advanced_blockformats : "p,h2,h3,h4,h5,h6",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    forced_root_block : "p",
    force_br_newlines : false,
    force_p_newlines : true,
    valid_elements : "span[class|id],br[class|id],a[href|target|title],img[src|id|width|height|class|alt],i,"+
    "li[class|id],ul[class|id],ol[class|id],p[class|id],"+
    "table[class|id],th[class|id],tr[class|id],td[class|id],thead,tbody,"+
    "h1[class|id],h2[class|id],h3[class|id],h4[class|id],h5[class|id],h6[class|id],strong[class|id],"+
    "div[class|id]",
    content_css : TEMPLATE_HOME+"/css/tinymce.css?" + new Date().getTime(),
    plugin_insertdate_dateFormat : "%d/%m/%Y",
    plugin_insertdate_timeFormat : "%H:%M",
    paste_auto_cleanup_on_paste : true,
    convert_urls: false,
    relative_urls: false,
    // Style formats
    style_formats : [
         {title : 'Accorion Top', selector : 'p,h2,h3,h4,h5,h6', classes : 'accordion_top'},
         {title : 'Accorion Middle', block : 'div', classes : 'accordion_middle'}
    ],
    setup: function (ed) {
        // Create an wrap DIV button
        ed.addButton ('wrap_div', {
            'title' : 'Wrap Accordion',
            'image' : HOME+'/webapp/shared/javascript/tiny_mce/themes/advanced/img/createlink.gif',
            'onclick' : function () {
                var text = ed.selection.getContent({ 'format' : 'text' });
                if (text) {
                    tinyInsert('<div class="accordion_middle"><div class="accordion_middle-wrapper">' + text + '</div></div>');
                }
            }
        });
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2024-12-11 02:11:08

它需要对tinymce核心(Formatter.js)进行重大更改才能使用样式插件获得您想要的内容。
我会编写一个自己的函数,放置在自己的tinymce 插件中。
为了实现您的目标,您不需要那么多代码。

编辑:你几乎是正确的。首先使用 span 尝试此操作,以确保它在您的编辑器中有效。

setup: function (ed) {
    // Create an wrap DIV button
    ed.addButton ('wrap_div', {
        'title' : 'Wrap Accordion',
        'image' : HOME+'/webapp/shared/javascript/tiny_mce/themes/advanced/img/createlink.gif',
        'onclick' : function () {
            var text = ed.selection.getContent({ 'format' : 'text' });
            if (text && text.length > 0) {
                ed.execCommand('mceInsertContent', false, '<span class="accordion_middle"><span class="accordion_middle-wrapper">' + text + '</span></span>');
            }
        }
    });
}

下一步是将 span 替换为 div 并确保您的 tinymce 设置允许嵌套 div 标签(请参阅 valid_children 设置)。

It requires big changes in the tinymce core (Formatter.js) to get what you want using the style plugin.
I would write an own function placed in an own tinymce plugin.
To achieve your goal you wouldn'tneed that much code.

EDIT: You got it almost correct. Try this first using spans to make sure this works in your editor.

setup: function (ed) {
    // Create an wrap DIV button
    ed.addButton ('wrap_div', {
        'title' : 'Wrap Accordion',
        'image' : HOME+'/webapp/shared/javascript/tiny_mce/themes/advanced/img/createlink.gif',
        'onclick' : function () {
            var text = ed.selection.getContent({ 'format' : 'text' });
            if (text && text.length > 0) {
                ed.execCommand('mceInsertContent', false, '<span class="accordion_middle"><span class="accordion_middle-wrapper">' + text + '</span></span>');
            }
        }
    });
}

Next step will be to replace the spans withs divs and make sure your tinymce settings will allow nested div tags (see the valid_children setting).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文