简体   繁体   English

从剪贴板复制并粘贴富文本到 mailto 正文

[英]Copy and Paste rich text to mailto body in react from clipboard

I have a table in which I want the user to be able to send this table via email by copying and paste to mailto subject on click.我有一个表,我希望用户能够通过 email 通过单击复制并粘贴到邮件主题来发送此表。

Here is a live demo on code sandbox: copy and paste rich text这是代码沙箱的现场演示: 复制和粘贴富文本

A function to copy and paste rich text to mailto body from clipboard A function 从剪贴板复制并粘贴富文本到邮件正文

  const copyToClip = () => {
    let range = document.createRange();
    range.selectNodeContents(tableRef.current);
    let sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    document.execCommand("Copy");
    sel.removeAllRanges();
    //paste the copied data to mailto body
    document.addEventListener("paste", function (event) {
      var clipText = event.clipboardData.getData("Text");
      window.location = `mailto:?subject=I wanted you to see this site&body=${clipText}`;
    });
  };

The expected result when the user clicks the copy button, it opens the client default email and past the table to the body like this below.用户单击复制按钮时的预期结果,它打开客户端默认 email 并将表格传递到如下所示的正文。

在此处输入图像描述

Note: I am able to send the copied data as a string but not a rich-text.注意:我可以将复制的数据作为字符串发送,但不能以富文本格式发送。

What do I need to to do solve the problem?我需要做什么来解决问题?

Change:改变:

var clipText = event.clipboardData.getData("Text");

to

var clipText = encodeURIComponent(event.clipboardData.getData("Text"));

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

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