【发布时间】:2015-09-08 17:22:51
【问题描述】:
我需要在我的summernote编辑器中复制粘贴一些内容。但是当我粘贴时,它会采用我复制它的页面的样式。我需要将其粘贴为纯文本。有什么解决办法吗?
【问题讨论】:
标签: javascript html summernote
我需要在我的summernote编辑器中复制粘贴一些内容。但是当我粘贴时,它会采用我复制它的页面的样式。我需要将其粘贴为纯文本。有什么解决办法吗?
【问题讨论】:
标签: javascript html 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 之后,每个回调都应该被回调对象包裹。 (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
【讨论】:
<span> 标签。之后一切正常。 @jcuenod
onpaste 而不是 onPaste。 6.5 版修复了这个问题。
在 v0.7.0 之后,每个回调都应该被回调对象包裹。 http://summernote.org/deep-dive/#callbacks
因此,如果您使用的是 v0.7.0 或更高版本的 Summernote,jcuenod 的答案现在可以重写为:
$('.summernote').summernote({
callbacks: {
onPaste: function (e) {
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
e.preventDefault();
// Firefox fix
setTimeout(function () {
document.execCommand('insertText', false, bufferText);
}, 10);
}
}
});
【讨论】:
设法为 IE11 制作了一个可怕的 hack。遗憾的是,这也会要求用户提供粘贴许可。如果有人提出更好的建议,我会全力以赴。
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();
}
}
}
}
})
})
【讨论】:
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);
}
}
});
【讨论】:
ctrl+shift+V 是最简单的解决方案:D
【讨论】: