wangEditor icon indicating copy to clipboard operation
wangEditor copied to clipboard

自定义粘贴逻辑中直接返回false或preventDefault,无法阻止粘贴txt文件中复制的纯文体内容

Open Jonson-sy opened this issue 2 years ago • 5 comments
trafficstars

bug 描述

自定义粘贴逻辑中直接返回false或preventDefault,无法阻止粘贴txt文件中复制的纯文体内容

你预期的样子是?

能阻止粘贴

系统和浏览器及版本号

  • 操作系统 : macos 12.4
  • 浏览器和版本: chrome 114.0.5735.198

wangEditor 版本

wangeditor5

demo 能否复现该 bug ?

不知道

  • 中文 demo https://www.wangeditor.com/demo/
  • English demo https://www.wangeditor.com/demo/?lang=en

在线 demo

请尽量提供在线 demo (推荐以下网站),帮助我们最低成本复现 bug

  • https://codesandbox.io/
  • https://codepen.io/
  • https://stackblitz.com/

最小成本的复现步骤

  • 步骤一 在一个txt文件中复制一段内容
  • 步骤二 在自定义粘贴事件对应处理函数中直接返回false
  • 步骤三 粘贴到富文本编辑器中

Jonson-sy avatar Jul 04 '23 05:07 Jonson-sy

确实存在这个问题,你可以根据官网推荐的自定义粘贴 customPaste 事件单独处理。 const customPaste = (editor: IDomEditor, event: Event, callback: Function), 可以根据第三个参数,回调函数传入false阻止默认事件。

下面是我针对txt文件做的单独处理

const customPaste = (editor: IDomEditor, event: any, callback: Function) => {
  const html = event.clipboardData.getData('text/html');
  const text = event.clipboardData.getData('text/plain');
  // 阻止默认的粘贴行为 txt没有html样式
  if (!html && text) {
    const currentText = editor.getText();
    const MAXLENGTH = props.maxLength ?? 5000;
    if (text.length + currentText.length > MAXLENGTH) {
      console.log('阻止继续粘贴');
      callback(false);
      event.preventDefault();
      return false;
    }
    return true;
  } else {
    // 继续执行默认的粘贴行为
    return true;
  }
};

希望可以帮到你。

yook-w avatar Dec 25 '23 08:12 yook-w