在summernote编辑器中将内容粘贴为纯文本 [英] Paste content as plain text in summernote editor
问题描述
我需要在我的summernote编辑器中复制粘贴一些内容。但是当我粘贴时,它会从我复制它的页面中获取页面样式。我需要将其粘贴为纯文本。是否有解决方案?
使用 onPaste
回拨 使用 onPaste
选项定义一个回调,该回调将剥离标签并手动插入文本。
$ editor.summernote({
onPaste:function(e){
var bufferText =((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
document.execCommand('insertText',false ,bufferText);
}
});
贷: https://github.com/summernote/summernote/issues/303
解决Firefox问题:
您可能仍然遇到Firefox问题。如果是这样,在一个定时器函数中包装 document.execCommand
,以稍微延迟它的执行:
setTimeout(function(){
document.execCommand('insertText',false,bufferText);
},10);
更新为v0.7.0 +
选项中的回调位置在v0.7.0之后发生变化
在v0.7.0之后,每个回调都应该被回调对象包装。 (source)
blockquote>
这意味着原始代码应该写为
$ editor。 summernote({
callbacks:{
onPaste:function(e){
var bufferText =((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text ');
e.preventDefault();
document.execCommand('insertText',false,bufferText);
}
}
});
感谢Mathieu Castets指出了这一点(所以如果这一点有帮助,upvote他的答案!)
TL; DR:以下是一个功能 demo
I need to copy paste some content in my summernote editor .But when I paste, it takes the style of the page from where I have copied it.I need to paste it as plain text. Is there any solution?
解决方案Use the
onPaste
CallbackUse the
onPaste
option to define a callback that will strip the tags and manually insert the text.$editor.summernote({ onPaste: function (e) { var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text'); e.preventDefault(); document.execCommand('insertText', false, bufferText); } });
Credit: https://github.com/summernote/summernote/issues/303
Solve Firefox Problems:
You may still have problems with Firefox. If so, wrap
document.execCommand
in a timer function to delay its execution a tiny bit:setTimeout(function(){ document.execCommand( 'insertText', false, bufferText ); }, 10);
Update for v0.7.0+
Position of callbacks in options is changed after v0.7.0
After v0.7.0, every callbacks should be wrapped by callbacks object. (source)This means that the original code should be written as
$editor.summernote({ callbacks: { onPaste: function (e) { var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text'); e.preventDefault(); document.execCommand('insertText', false, bufferText); } } });
Credit to Mathieu Castets for pointing this out (so if this bit helped, upvote his answer!)
TL;DR: Here's a functional demo
这篇关于在summernote编辑器中将内容粘贴为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!