Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copied text/html content not getting pasted in code editor, slack, etc. #112

Open
jasminMerchant opened this issue Apr 21, 2021 · 4 comments

Comments

@jasminMerchant
Copy link

jasminMerchant commented Apr 21, 2021

This is a great library, but we are facing some issue.

We have used this library in a project to copy an HTML table when clicked on a button. It seems to copy the table content, but while pasting, it is not pasting anything in Code Editor (VS Code in my case), Slack, Skype, and such areas. But when pasted in Notes, Docs, or Pages software, it is pasting the content properly in tabular format.

Has this issue happened before or is there any fix for this?

Any help will be very appreciated!

@imdmitrykravchenko
Copy link

I faced the same issue and solve it using that code snippet

const copyToClipboard = (value) => {
  const input = document.createElement('textarea');

  document.body.appendChild(input);
  input.value = value;
  input.select();
  document.execCommand('copy');
  document.body.removeChild(input);
};

execCommand browsers support is good enough to make that reliable

Hope it helps!

@sudodoki
Copy link
Owner

sudodoki commented Aug 2, 2022

@jasminMerchant can you provide jsbin or similar sandbox example to reproduce? Maybe, you needed to pass text/html as options.format?

@klamping
Copy link

klamping commented Jan 26, 2023

Here's an alternative version that allows you to paste either the rich text, or the plain text, depending on the text box it's being pasted into:

async function copyText (value) {
    const typeRich = 'text/html';
    const blobRich = new Blob([value], { type: typeRich });

    const typePlain = 'text/plain';
    // optionally, replace/remove html tags with plain text equivalents (e.g., newlines instead of <br>)
    // const plainText = replaceHtmlTags(value);
    const blobPlain = new Blob([value], { type: typePlain });

    // Copy both types so it can be pasted into either a Rich Text editor with formatting,
    // or a plain text editor with basic formatting (e.g., new lines but not HTML tags)
    const data = [new ClipboardItem({ [typeRich]: blobRich, [typePlain]: blobPlain })];

    await navigator.clipboard.write(data);
}

@tuffkidd
Copy link

tuffkidd commented Feb 1, 2023

@klamping Your code works wonderfully. The text copy function in Apple Newsroom works differently. When pasting into the visual studio code(Untitled-1), your code is attached in "html" and the Apple Newsroom code is attached in "plain". What would be different from your code? I think you're right, but I'm curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants