【问题标题】:How to disable image pasting in ngx-editor?如何在 ngx-editor 中禁用图像粘贴?
【发布时间】:2021-10-11 22:03:18
【问题描述】:

我试图将 ngx-editor 用于富文本编辑器,但我想禁用在编辑器中粘贴图像的属性。尽管我已从工具栏中删除了插入图像按钮,但它仍在编辑器中粘贴图像。

ngx-editor 文档链接:https://sibiraj-s.github.io/ngx-editor/#/

【问题讨论】:

  • 添加示例代码

标签: angular rich-text-editor ngx-editor


【解决方案1】:
@HostListener('paste', ['$event'])
private pasteFromClipboard(event: KeyboardEvent): void {
  event.preventDefault();
    
  if (this.insertClipboardImage(event)) {
    return;
  }
}

private insertClipboardImage(event): File | null{
  const pastedImage = getPastedImage(event);

  if (!pastedImage)
    return null;

  return pastedImage;
}

private getPastedImage(event): File | null {
  if (event.clipboardData) {
    if (event.clipboardData.files && event.clipboardData.files.length && isImageFile(event.clipboardData.files[0])) {
      return event.clipboardData.files[0];
    }
  }

  return null;
}

private isImageFile(file: File): boolean {
  return file.type.search(/^image\//i) === 0;
}

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 2010-10-24
  • 1970-01-01
相关资源
最近更新 更多