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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/components/svgInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,33 @@ const downloadSvg = (url?: string) => {
download(url || "");
};


const MIMETYPE = 'text/plain';

// Return content of svg as blob =>
const getSvgContent = async (url: string | undefined, isSupported: boolean) => {
const response = await fetch(url || "");
const content = await response.text();

// It was necessary to use blob because in chrome there were issues with the copy to clipboard
const blob = new Blob([content], { type: MIMETYPE });
return isSupported ? blob : content;
}

// Copy to clipboard =>
const copyToClipboard = (url?: string) => {
fetch(url || "").then((response) => {
response.text().then((content) => {
navigator.clipboard.writeText(content);
toast("Copied to clipboard", ToastTheme);
});
});
const copyToClipboard = async (url?: string) => {
const data = {
[MIMETYPE]: getSvgContent(url, true)
};
try {
const clipboardItem = new ClipboardItem(data);
await navigator.clipboard.write([clipboardItem]);
} catch (error) {
// This section works as a fallback on Firefox
const content = await getSvgContent(url, false) as string;
await navigator.clipboard.writeText(content);
}
toast("Copied to clipboard", ToastTheme);
};

const SVGInfo = (props: SVGCardProps) => {
Expand Down