我对夏季笔记中的内容不满意。
Codepen:http://codepen.io/anon/pen/LkLYvV
当前行为: Safari/Firefox -当粘贴图像时,它只是显示粘贴而不是ALLowed,但是在Chrome上,它显示粘贴不允许与图像粘贴在一起。
预期行为:完全禁用图像粘贴。
这似乎指向了Chrome中的一个问题,即启动粘贴事件。
我想防止粘贴图像,所以我已经添加了e.preventDefault,但铬仍然粘贴图像。下面是我的代码和一些用于调试行为的console.log。还有其他防止铬糊的方法吗?
$('#summernote').summernote({
height: 300,
minHeight: 300,
toolbar: [
// [groupName, [list of button]]
['style', ['bold', 'italic', 'underline', 'clear']],
['fontsize', ['fontsize']],
['para', ['ul', 'ol', 'paragraph']],
['insert',['link','picture']]
],
callbacks : {
onPaste: function (e) {
e.preventDefault();
console.log('pasting'); // Chrome shows This
var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text'); //This actually removes the image because image is not text.
console.log(bufferText); //Chrome Shows Empty
console.log(typeof bufferText); //Chrome Shows String
console.log(bufferText.length); //Chrome Shows 0
if (bufferText.length == 0) {
console.log ('in if loop'); //Chrome Shows this in Console
document.execCommand('insertText', false, 'Paste Not Allowed'); //Chrome wrote "Paste Not Allowed" in contenteditable but the image still get pasted.
return false;
}
if (!bufferText)
return true;
document.execCommand('insertText', false, bufferText);
}
}
});更新:向preventDefault.抛出新错误
目前这是我暂时做的事。如果有人能提出一个比抛出一个错误来停止默认操作更好的方法的话。
看起来这个安全设置/ chrome浏览器,我必须添加一些东西来获取bufferText
var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('text/plain') || null;
if (bufferText == null) {
throw new Error("Cannot Paste Image");
}通过抛出新的错误,我可以停止张贴图像,但我可以允许所有其他形式的文本。
发布于 2016-07-03 09:52:52
在回调onPaste的末尾,放置一个return false; --它应该可以工作。这就是我用而不是e.preventDefault()的方式
https://stackoverflow.com/questions/38168158
复制相似问题