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

fix: clipboard doesn't work on safari #14

Merged
merged 1 commit into from
Aug 7, 2022

Conversation

xavimondev
Copy link
Contributor

@xavimondev xavimondev commented Aug 7, 2022

I've solved the issue about clipboard on safari. Basically, we can't use the writeText and write functions in the following contexts:

  1. Trying to copy a string as a result of a promise.
  2. If we use await and then the functions mentioned above, it will throw an exception because Apple doesn't allow this. Check this answer on stackoverflow.

So, for that reason it was necessary to create a method getSvgContent to fetch data. I based on this documentation: Webkit clipboard to create it.
After that, it was able to copy svg's content on safari. However, there were troubles on Chrome because the function write not allow to copy strings. I ended up following this tutorial, as you can see

async function copy() {
  const image = await fetch('kitten.png').then(response => response.blob());
  const text = new Blob(['Cute sleeping kitten'], {type: 'text/plain'});
  const item = new ClipboardItem({
    'text/plain': text,
    'image/png': image
  });
  await navigator.clipboard.write([item]);
}

It is necessary to use Blob if we want to copy a text. Finally, another issue was that ClipboardItem is not supported on Firefox, hence inside of catch block, there is a logic when a user try to copy svg's content on Firefox. You can see it like a fallback. See the compatibility.

Before PR

before.mov

After PR

after.mov

Tested on Safari(15.6) , Chrome(103.0), Microsoft Edge(104.0), Firefox(103.0.1) and Opera(89.0)

BTW, I didn't test on iOS and iPadOS

@vercel
Copy link

vercel bot commented Aug 7, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
svgl ✅ Ready (Inspect) Visit Preview Aug 7, 2022 at 5:20AM (UTC)

Copy link
Owner

@pheralb pheralb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

@pheralb
Copy link
Owner

pheralb commented Aug 7, 2022

🚀 Muchas gracias 🚀

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

Successfully merging this pull request may close these issues.

None yet

2 participants