How to make tinymce paste in plain text by default

后端 未结 11 913
忘掉有多难
忘掉有多难 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:21

    Without plug-in: Listen to paste event, get clipboard data

    If you cannot use or do not want to use a plug-in for whatever reason, you can create your own "paste as plain text" callback function like so:

    tinyMCE.init({
    
        // ...,
    
        setup: function (editor) {
    
            // Listen for paste event, add "Paste as plain text" callback
            editor.onPaste.add(function (editor, e) {
    
                // Prevent default paste behavior
                e.preventDefault();
    
                // Check for clipboard data in various places for cross-browser compatibility.
                // Get that data as text.
                var content = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
    
                // Let TinyMCE do the heavy lifting for inserting that content into the editor.
                editor.execCommand('mceInsertContent', false, content);
            });
        }
    });
    

    Note: This was created for TinyMCE 3.5.x. Compatibility may vary by version.

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

    I did as follows:

    var pastePlainText = function() {
        // No need to pass in an ID, instead fetch the first tinyMCE instance
        var ed = tinyMCE.get(0); 
        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;
            });
        }
    };
    

    And then:

    tinyMCE.init({
        // ...
        plugins: "paste",
        oninit: pastePlainText // Note, without "
        // ...
    })
    
    0 讨论(0)
  • 2020-12-02 05:28

    I have solved this problem with this code

    tinyMCE.init({
    ...
    plugins : "paste",
    paste_text_sticky : true,
    setup : function(ed) {
        ed.onInit.add(function(ed) {
          ed.pasteAsPlainText = true;
        });
      }
    ....
    })
    
    0 讨论(0)
  • 2020-12-02 05:29

    Isn't it better to use:

    var ed = tinyMCE.activeEditor;
    

    instead of:

    var ed = tinyMCE.get('elm1');
    
    0 讨论(0)
  • 2020-12-02 05:30

    I think the easiest way would be this:

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