sudodoki / copy-to-clipboard

Copy stuff into clipboard from your browser using JS
http://sudodoki.github.io/copy-to-clipboard/example/
MIT License
1.28k stars 132 forks source link

fix: clearData with format to fix only can set one format clipboard data bug #142

Closed HellowVirgil closed 1 month ago

HellowVirgil commented 1 month ago

In this case, only text/html format can be set to clipboard, and when we paste to a plain textarea it will be null:

copyToClipboard(url, {
  format: 'text/plain',
});
copyToClipboard(`<a href="${url}">aaa</a>`, {
  format: 'text/html',
});

because the clipboardData has been cleaned when called each time.

So we should only clear the current format when set data.

HellowVirgil commented 1 month ago

can use like this:

copyToClipboard(url, {
  format: 'text/plain',
  onCopy: clipboardData => {
    (clipboardData as DataTransfer | null)?.setData(
      'text/html',
      `<a href="${url}">aaa</a>`,
    );
  },
});