我需要在我的summernote编辑器中复制粘贴一些内容。但是当我粘贴时,它会采用我复制它的页面样式。我需要将其粘贴为纯文本。有没有解决方案?
答案 0 :(得分:65)
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之后,每个回调都应该被回调对象包装。 (source)
这意味着原始代码应写为
$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指出这一点(所以如果这一点有帮助,请提出他的回答!)
TL; DR:这是一个功能强大的 demo
答案 1 :(得分:3)
onPaste-callback工作得很好,但我遇到了不同浏览器中不同处理换行符的问题。所以我提出了以下解决方案,它使用了html-linebreaks:
$(".htmleditor").summernote({
callbacks: {
// callback for pasting text only (no formatting)
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
bufferText = bufferText.replace(/\r?\n/g, '<br>');
document.execCommand('insertHtml', false, bufferText);
}
}
});
答案 2 :(得分:2)
管理为IE11做一个可怕的黑客工作。遗憾的是,这也要求用户提供粘贴权限。如果有人提出更好的建议,我会全力以赴。
var trap = false;
$(document).ready(function () {
$('#summernote').summernote({
callbacks: {
onPaste: function (e) {
if (document.queryCommandSupported("insertText")) {
var text = $(e.currentTarget).html();
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
setTimeout(function () {
document.execCommand('insertText', false, bufferText);
}, 10);
e.preventDefault();
} else { //IE
var text = window.clipboardData.getData("text")
if (trap) {
trap = false;
} else {
trap = true;
setTimeout(function () {
document.execCommand('paste', false, text);
}, 10);
e.preventDefault();
}
}
}
}
})
})
答案 3 :(得分:1)
ctrl + shift + V是最简单的解决方案:D