How to make tinymce paste in plain text by default

后端 未结 11 912
忘掉有多难
忘掉有多难 2020-12-02 04:24

Googled it thousands of times, No one gives a complete solution of how to make Tinymce paste in plain text by default and strip out any formatting without clicking the \"pas

相关标签:
11条回答
  • 2020-12-02 05:04

    Just ran into this one myself and discovered that as of TinyMCE 3.4.2 you can simply:

    paste_text_sticky: true,
    paste_text_sticky_default: true
    

    ...which was nice.

    0 讨论(0)
  • 2020-12-02 05:07

    EDIT: this solution is for version 3.x, for 4.x version read the answer from @Paulo Neves

    The problem is that Paste plugin automatically resets plain text paste on every paste. So all we need to do - set it back. The following code should help.

    tinyMCE.init({
    ...
    oninit : "setPlainText",
    plugins : "paste"
    
    ....
    });
    

    The definition of setPlainText

     function setPlainText() {
            var ed = tinyMCE.get('elm1');
    
            ed.pasteAsPlainText = true;  
    
            //adding handlers crossbrowser
            if (tinymce.isOpera || /Firefox\/2/.test(navigator.userAgent)) {
                ed.onKeyDown.add(function (ed, e) {
                    if (((tinymce.isMac ? e.metaKey : e.ctrlKey) && e.keyCode == 86) || (e.shiftKey && e.keyCode == 45))
                        ed.pasteAsPlainText = true;
                });
            } else {            
                ed.onPaste.addToTop(function (ed, e) {
                    ed.pasteAsPlainText = true;
                });
            }
        }
    

    So now it always will be plain.

    0 讨论(0)
  • 2020-12-02 05:08

    I'm not sure this is possible, since "paste as plaintext" actually performs cleanup on the text before it adds it to the window. If you just paste data into the window, no operations can be done. (Unless you hooked into the onChange or something), but they you might end up fixing code that had already been pasted and thus, 'double fixing' it.

    0 讨论(0)
  • 2020-12-02 05:11

    FYI, TinyMCE has improved this by implementing it as a default option in the paste plugin. More info: http://www.tinymce.com/wiki.php/Plugin:paste

    However, it's still not perfect. So here is a script that also trips off all HTML:

    // Paste
            paste_auto_cleanup_on_paste : true,
            paste_remove_spans: true,
            paste_remove_styles: true,
            paste_retain_style_properties: false,
    
            paste_preprocess : function(pl, o) 
            {    // Replace <div> with <p>
                o.content = o.content.replace(/<div>/gi, "<p>");    
                o.content = o.content.replace(/<\/div>/gi, "</p>");
                o.content = o.content.replace(/<\r\n/gi, "\n");
                o.content = o.content.replace(/<\n\n/gi, "\n");
                o.content = o.content.replace(/<\n\n/gi, "\n");
    
                // Replace empty styles
                o.content = o.content.replace(/<style><\/style>/gi, "");    
    
                o.wordContent = true;            
            },
    
            paste_postprocess : function(pl, o) 
            {    //console.log(o.node.innerHTML);
                var ed = pl.editor, dom = ed.dom;
    
                // Remove all tags which are not <p> or <br>
                tinymce.each(dom.select('*', o.node), function(el) 
                {    if (el.tagName.toLowerCase() != "p" && el.tagName.toLowerCase() != "br") 
                    {    dom.remove(el, 1); // 1 = KeepChildren
                        console.log(el.tagName);
                    }
                    dom.setAttrib(el, 'style', '');
                });
    
            },
    

    Source: http://www.tinymce.com/forum/viewtopic.php?pid=60121#p60121

    0 讨论(0)
  • 2020-12-02 05:16

    if you use a .yml file, add the plugin paste and paste_as_text: true

    default:
      plugins:
        - paste
      paste_as_text: true
    
    0 讨论(0)
  • 2020-12-02 05:17

    For the tinyMCE 3X or 4X things have change a little. now you can do this and it works fine.

    tinymce.init({
        plugins: "paste",
        paste_as_text: true
    });
    
    0 讨论(0)
提交回复
热议问题