简体   繁体   English

Angular contenteditable div 限制粘贴内容

[英]Angular contenteditable div restrict pasted content

Is it possible to restrict the input of a contenteditable div?是否可以限制内容可编辑 div 的输入? I try to write my own WYSIWYG Editor and it should not be possible to paste content from another website and to adopt the style.我尝试编写自己的 WYSIWYG 编辑器,应该无法从其他网站粘贴内容并采用这种风格。

So at least the same effect as I would paste it to a textarea.所以至少与我将其粘贴到文本区域的效果相同。

Is there any inbuild solution or do I need to implement it by an Event?是否有任何内置解决方案或者我需要通过事件来实现它? If yes how is that possible?如果是,那怎么可能?

I figured it out.我想到了。 I caught the value and inserted it after I manipulating it:我在操作后捕获了该值并插入了它:

html: html:

<div contenteditable (paste)="onPaste($event)" (input)="onInput($event)"></div>

typescript: typescript:

onPaste(e) {
  // Stop data actually being pasted into div
  e.preventDefault();

  // Get pasted data via clipboard API
  const clipboardData = e.clipboardData;
  const pastedText = clipboardData.getData('text');

  // Insert the value on cursor position
  window.document.execCommand('insertText', false, pastedText);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM