Skip to content
Open
Show file tree
Hide file tree
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
244 changes: 134 additions & 110 deletions src/components/svgs/copySvg.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -147,45 +147,51 @@

// Convert SVG as React component:
const convertSvgReactComponent = async (tsx: boolean) => {
const svgUrlToCopy = getSvgUrl();
optionsOpen = false;
try {
const svgUrlToCopy = getSvgUrl();
optionsOpen = false;

isLoading = true;
isLoading = true;

const title = svgInfo.title.split(" ").join("");
let content = await getSource({
url: svgUrlToCopy,
optimize,
});
const title = svgInfo.title.split(" ").join("");
let content = await getSource({
url: svgUrlToCopy,
optimize,
});

if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}
if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}

const dataComponent = {
code: content,
typescript: tsx,
name: title,
optimize,
};
const { data, error } = await getReactCode(dataComponent);
const dataComponent = {
code: content,
typescript: tsx,
name: title,
optimize,
};
const { data, error } = await getReactCode(dataComponent);

if (error || !data) {
toast.error("Failed to fetch React component", {
description: `${error ?? ""}`,
duration: 5000,
});
isLoading = false;
return;
}

if (error || !data) {
toast.error("Failed to fetch React component", {
description: `${error ?? ""}`,
duration: 5000,
await clipboard(data);

toast.success(`Copied as React ${tsx ? "TSX" : "JSX"} component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});

isLoading = false;
} catch (err) {
console.error("Error converting to React component:", err);
toast.error("Failed to convert React component");
isLoading = false;
return;
}

await clipboard(data);

toast.success(`Copied as React ${tsx ? "TSX" : "JSX"} component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});

isLoading = false;
};

// Copy SVG as Vue Component:
Expand Down Expand Up @@ -266,114 +272,132 @@

// Copy SVG as Standalone Angular component:
const convertSvgAngularComponent = async () => {
isLoading = true;
optionsOpen = false;
try {
isLoading = true;
optionsOpen = false;

const title = svgInfo.title.split(" ").join("");
const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});
const title = svgInfo.title.split(" ").join("");
const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});

if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}
if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}

if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}
if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}

const angularComponent = getAngularCode({
componentName: title,
svgContent: content,
});
const angularComponent = getAngularCode({
componentName: title,
svgContent: content,
});

await clipboard(angularComponent);
await clipboard(angularComponent);

toast.success(`Copied as Standalone Angular component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});
toast.success(`Copied as Standalone Angular component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});

isLoading = false;
isLoading = false;
} catch (err) {
console.error("Error converting to Angular component:", err);
toast.error("Failed to convert Angular component");
isLoading = false;
}
};

// Copy SVG as Web Component:
const convertSvgWebComponent = async () => {
isLoading = true;
optionsOpen = false;
try {
isLoading = true;
optionsOpen = false;

const title = svgInfo.title.split(" ").join("");
const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});
const title = svgInfo.title.split(" ").join("");
const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});

if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}
if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}

if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}
if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}

const webComponentCode = getWebComponent({
name: title,
content: content,
});
const webComponentCode = getWebComponent({
name: title,
content: content,
});

await clipboard(webComponentCode);
await clipboard(webComponentCode);

toast.success(`Copied as Web Component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});
toast.success(`Copied as Web Component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});

isLoading = false;
isLoading = false;
} catch (err) {
console.error("Error converting to Web Component:", err);
toast.error("Failed to convert Web Component");
isLoading = false;
}
};

// Copy SVG as Astro component:
const convertSvgAstroComponent = async () => {
isLoading = true;
optionsOpen = false;
try {
isLoading = true;
optionsOpen = false;

const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});
const svgUrlToCopy = getSvgUrl();
let content = await getSource({
url: svgUrlToCopy,
optimize,
});

if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}
if (svgUrlToCopy) {
content = prefixSvgIds(content, getPrefixFromSvgUrl(svgUrlToCopy));
}

if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}
if (!content) {
toast.error("Failed to fetch the SVG content", {
duration: 5000,
});
isLoading = false;
return;
}

const astroComponentCode = getAstroCode({
svgContent: content,
});
const astroComponentCode = getAstroCode({
svgContent: content,
});

await clipboard(astroComponentCode);
await clipboard(astroComponentCode);

toast.success(`Copied as Astro Component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});
toast.success(`Copied as Astro Component`, {
description: `${svgInfo.title} - ${svgInfo.category}`,
});

isLoading = false;
isLoading = false;
} catch (err) {
console.error("Error converting to Astro Component:", err);
toast.error("Failed to convert Astro Component");
isLoading = false;
}
};
</script>

Expand Down
12 changes: 2 additions & 10 deletions src/components/svgs/downloadSvg.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@
<div class={cn("flex flex-col gap-4 md:flex-row")}>
{#if typeof svgInfo.route === "string"}
<div class={cardDownloadStyles}>
<img
src={isDarkTheme() ? svgInfo.route : svgInfo.route}
alt={svgInfo.title}
class={imgStyles}
/>
<img src={svgInfo.route} alt={svgInfo.title} class={imgStyles} />
<Button
class="justify-between"
title="Download logo"
Expand Down Expand Up @@ -212,11 +208,7 @@

{#if typeof svgInfo.wordmark === "string" && svgInfo.wordmark !== undefined}
<div class={cardDownloadStyles}>
<img
src={isDarkTheme() ? svgInfo.wordmark : svgInfo.wordmark}
alt={svgInfo.title}
class={imgStyles}
/>
<img src={svgInfo.wordmark} alt={svgInfo.title} class={imgStyles} />
<Button
class="justify-between"
title="Download Wordmark logo"
Expand Down
7 changes: 7 additions & 0 deletions src/routes/api/svgs/svgr/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export const POST = async ({ request }: RequestEvent) => {
try {
const body = await request.json();

if (!body.code || !body.name) {
return json(
{ error: "Missing required fields: code and name" },
{ status: 400 },
);
}

let svgCode = body.code;
const typescript = body.typescript;
const name = body.name.replace(/[^a-zA-Z0-9]/g, "");
Expand Down
8 changes: 7 additions & 1 deletion src/templates/getSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ interface SourceParams {
}

export const getSource = async (params: SourceParams) => {
const response = await fetch(params.url || "");
if (!params.url) return "";
const response = await fetch(params.url);
if (!response.ok) {
throw new Error(
`Failed to fetch SVG: ${response.status} ${response.statusText}`,
);
}
const content = await response.text();
if (!params.optimize) return content;
const optimizedContent = optimizeSvg({ svgCode: content });
Expand Down