Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop TinyMCE Stripping Indents When Pasting In Code

I am using the latest TinyMCE which is great, but the only issue I have is when people post code samples. Doesn't matter if they post it in a PRE format or just normally.

TinyMCE strips out all the indents (Tabs) and makes it hard to read, has anyone on here managed to get around this? Code examples/samples would be appreciated.

like image 453
YodasMyDad Avatar asked Oct 31 '22 20:10

YodasMyDad


1 Answers

Is this what your after. It seems to work correctly on this JSFiddle

HTML

<form>
    <textarea id='editor_instance_1'></textarea>
</form>
<button id='post-code-btn'>Post Code</button>
<div id='post-area'></div>

JS

//create instance of an mce editor
var ed = new tinymce.Editor('editor_instance_1', {
    //add custom formats
    style_formats: [
      {title: 'Code', block: 'pre', styles: {'color': '#000', 'font-size':  '11px'}},
      {title: 'Text', block: 'p', styles: {'color': '#ff0000', 'font-size': '16px'}}
    ],
    //force the starting block to pre
    forced_root_block : 'pre'    
}, tinymce.EditorManager);

//render the editor on screen
ed.render();

var postButton = document.getElementById('post-code-btn');
var postArea = document.getElementById('post-area');

postButton.onclick = function(){
    //get html structure from the editor instance
    var code = ed.getBody().innerHTML;
    //simulate posting of html
    new Request.HTML({
        url: '/echo/html/',
        data: {
           html: code,
           delay: 0
         },
         type: 'post',
         update: 'post-area',    
    }).send();
}

I pasted in code from multiple sources including eclipse, SO and JSFiddle.

like image 196
PalinDrome555 Avatar answered Nov 18 '22 19:11

PalinDrome555