mirror of
https://github.com/pheralb/svgl.git
synced 2024-11-10 14:46:54 +08:00
Merge pull request #290 from xavimondev/main
⚙️ Fix clipboard on safari + separate services
This commit is contained in:
commit
78f69b56d9
@ -6,7 +6,9 @@
|
|||||||
import * as Popover from '@/ui/popover';
|
import * as Popover from '@/ui/popover';
|
||||||
|
|
||||||
// Utils:
|
// Utils:
|
||||||
import { MIMETYPE, getSvgContent } from '@/utils/getSvgContent';
|
import { getSvgContent } from '@/utils/getSvgContent';
|
||||||
|
import { getReactComponentCode } from '@/utils/getReactComponentCode';
|
||||||
|
import { clipboard } from '@/utils/clipboard';
|
||||||
import { copyToClipboard as figmaCopyToClipboard } from '@/figma/copy-to-clipboard';
|
import { copyToClipboard as figmaCopyToClipboard } from '@/figma/copy-to-clipboard';
|
||||||
import { buttonStyles } from '@/ui/styles';
|
import { buttonStyles } from '@/ui/styles';
|
||||||
import { cn } from '@/utils/cn';
|
import { cn } from '@/utils/cn';
|
||||||
@ -64,22 +66,12 @@
|
|||||||
const svgUrlToCopy = getSvgUrl();
|
const svgUrlToCopy = getSvgUrl();
|
||||||
optionsOpen = false;
|
optionsOpen = false;
|
||||||
|
|
||||||
const data = {
|
const content = await getSvgContent(svgUrlToCopy);
|
||||||
[MIMETYPE]: getSvgContent(svgUrlToCopy, true)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isInFigma) {
|
if (isInFigma) {
|
||||||
const content = (await getSvgContent(svgUrlToCopy, false)) as string;
|
|
||||||
figmaCopyToClipboard(content);
|
figmaCopyToClipboard(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
await clipboard(content);
|
||||||
const clipboardItem = new ClipboardItem(data);
|
|
||||||
await navigator.clipboard.write([clipboardItem]);
|
|
||||||
} catch (error) {
|
|
||||||
const content = (await getSvgContent(svgUrlToCopy, false)) as string;
|
|
||||||
await navigator.clipboard.writeText(content);
|
|
||||||
}
|
|
||||||
|
|
||||||
const category = Array.isArray(svgInfo.category)
|
const category = Array.isArray(svgInfo.category)
|
||||||
? svgInfo.category.sort().join(' - ')
|
? svgInfo.category.sort().join(' - ')
|
||||||
@ -111,32 +103,26 @@
|
|||||||
|
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
|
|
||||||
try {
|
|
||||||
const title = svgInfo.title.split(' ').join('');
|
const title = svgInfo.title.split(' ').join('');
|
||||||
const content = (await getSvgContent(svgUrlToCopy, false)) as string;
|
const content = await getSvgContent(svgUrlToCopy);
|
||||||
const getCode = await fetch('/api/svgs/svgr', {
|
const dataComponent = { code: content, typescript: tsx, name: title };
|
||||||
method: 'POST',
|
const { data, error } = await getReactComponentCode(dataComponent);
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
if (error || !data) {
|
||||||
},
|
toast.error('Failed to fetch React component', {
|
||||||
body: JSON.stringify({ code: content, typescript: tsx, name: title })
|
description: `${error ?? ''}`,
|
||||||
|
duration: 5000
|
||||||
});
|
});
|
||||||
const data = await getCode.json();
|
return;
|
||||||
const clipboardItem = new ClipboardItem({
|
}
|
||||||
'text/plain': new Blob([data], { type: 'text/plain' })
|
|
||||||
});
|
await clipboard(data);
|
||||||
await navigator.clipboard.write([clipboardItem]);
|
|
||||||
toast.success(`Copied as React ${tsx ? 'TSX' : 'JSX'} component`, {
|
toast.success(`Copied as React ${tsx ? 'TSX' : 'JSX'} component`, {
|
||||||
description: `${svgInfo.title} - ${svgInfo.category}`
|
description: `${svgInfo.title} - ${svgInfo.category}`
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
toast.error('Failed to copy as React component', {
|
|
||||||
description: `${error}`,
|
|
||||||
duration: 5000
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ export const POST = async ({ request }: RequestEvent) => {
|
|||||||
{ componentName: name }
|
{ componentName: name }
|
||||||
);
|
);
|
||||||
|
|
||||||
return json(jsCode, { status: 200 });
|
return json({ data: jsCode }, { status: 200 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return json(
|
return json(
|
||||||
{ error: `Error al transformar el SVG a componente React: ${error}` },
|
{ error: `Error al transformar el SVG a componente React: ${error}` },
|
||||||
|
15
src/utils/clipboard.ts
Normal file
15
src/utils/clipboard.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
const MIMETYPE = 'text/plain';
|
||||||
|
|
||||||
|
export const clipboard = async (content: string) => {
|
||||||
|
try {
|
||||||
|
const clipboardItem = new ClipboardItem({
|
||||||
|
[MIMETYPE]: new Blob([content], { type: MIMETYPE })
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(async () => {
|
||||||
|
await navigator.clipboard.write([clipboardItem]);
|
||||||
|
}, 200);
|
||||||
|
} catch (error) {
|
||||||
|
await navigator.clipboard.writeText(content);
|
||||||
|
}
|
||||||
|
};
|
23
src/utils/getReactComponentCode.ts
Normal file
23
src/utils/getReactComponentCode.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
interface iComponentCode {
|
||||||
|
code: string;
|
||||||
|
name: string;
|
||||||
|
typescript: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getReactComponentCode = async (
|
||||||
|
params: iComponentCode
|
||||||
|
): Promise<{ data?: string; error?: string }> => {
|
||||||
|
try {
|
||||||
|
const getCode = await fetch('/api/svgs/svgr', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(params)
|
||||||
|
});
|
||||||
|
const data = await getCode.json();
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
return { error: 'An error has ocurred. Try again.' };
|
||||||
|
}
|
||||||
|
};
|
@ -1,7 +1,5 @@
|
|||||||
export const MIMETYPE = 'text/plain';
|
export const getSvgContent = async (url: string | undefined) => {
|
||||||
export const getSvgContent = async (url: string | undefined, isSupported: boolean) => {
|
|
||||||
const response = await fetch(url || '');
|
const response = await fetch(url || '');
|
||||||
const content = await response.text();
|
const content = await response.text();
|
||||||
const blob = new Blob([content], { type: MIMETYPE });
|
return content;
|
||||||
return isSupported ? blob : content;
|
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user