Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE outer wrap selected elements

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>');
                }
            }
        });
    }
});
like image 913
John Magnolia Avatar asked Sep 09 '11 14:09

John Magnolia


1 Answers

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).

like image 51
Thariama Avatar answered Nov 10 '22 18:11

Thariama