Javascript 默认情况下如何将tinymce粘贴为纯文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2695731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:29:09  来源:igfitidea点击:

How to make tinymce paste in plain text by default

javascriptjquerytinymce

提问by Ryan

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 "paste as text" button.

在谷歌上搜索了数千次,没有人给出一个完整的解决方案,说明如何在默认情况下以纯文本形式粘贴 Tinymce,并在不单击“粘贴为文本”按钮的情况下去除任何格式。

Any Ideas of how to implement that? or how to enable the "paste as text" button automatically?

关于如何实施的任何想法?或者如何自动启用“粘贴为文本”按钮?

Thank you

谢谢

采纳答案by er-v

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

编辑:此解决方案适用于 3.x 版,对于 4.x 版,请阅读@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.

问题是 Paste 插件会在每次粘贴时自动重置纯文本粘贴。所以我们需要做的就是——把它放回去。以下代码应该会有所帮助。

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

....
});

The definition of setPlainText

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.

所以现在它总是很​​简单。

回答by Paulo Almeida

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

对于 tinyMCE 3X 或 4X,情况略有变化。现在你可以做到这一点,它工作正常。

tinymce.init({
    plugins: "paste",
    paste_as_text: true
});

回答by Dariusz Lyson

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

回答by stovroz

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

我自己刚遇到这个,发现从 TinyMCE 3.4.2 开始,你可以简单地:

paste_text_sticky: true,
paste_text_sticky_default: true

...which was nice.

......这很好。

回答by brothers28

I think the easiest way would be this:

我认为最简单的方法是这样的:

tinymce.init({
   ...
   paste_as_text: true,
   plugins: "paste",
   ...
});

回答by gfullam

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.

注意:这是为 TinyMCE 3.5.x 创建的。兼容性可能因版本而异。

回答by Asen Mitov

Isn't it better to use:

使用不是更好:

var ed = tinyMCE.activeEditor;

instead of:

代替:

var ed = tinyMCE.get('elm1');

回答by laarsk

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

仅供参考,TinyMCE 通过将其作为粘贴插件中的默认选项实施来改进了这一点。更多信息: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:

然而,它仍然不完美。所以这里有一个脚本,它也会跳出所有的 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

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

回答by LifterCoder

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

如果您使用 .yml 文件,请添加插件pastepaste_as_text: true

default:
  plugins:
    - paste
  paste_as_text: true

回答by Mitch Dempsey

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 onChangeor something), but they you might end up fixing code that had already been pasted and thus, 'double fixing' it.

我不确定这是否可行,因为“粘贴为纯文本”实际上是在将文本添加到窗口之前对其进行清理。如果只是将数据粘贴到窗口中,则无法进行任何操作。(除非你连接到onChange或其他东西),但他们最终可能会修复已经粘贴的代码,因此,“双重修复”它。