mirror of
https://github.com/pheralb/svgl.git
synced 2025-02-06 15:17:58 +08:00
✨ Add new templates for Angular, React, Svelte, and Vue components; refactor existing React code
This commit is contained in:
parent
6db07df810
commit
c2c876a0c2
26
src/templates/getAngularCode.ts
Normal file
26
src/templates/getAngularCode.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
interface AngularComponentParams {
|
||||||
|
svgContent: string;
|
||||||
|
componentName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAngularCode(params: AngularComponentParams): string {
|
||||||
|
const updatedSvgContent = params.svgContent.replace(
|
||||||
|
/<svg([^>]*)>/,
|
||||||
|
`<svg$1 [attr.width]="size.width" [attr.height]="size.height">`
|
||||||
|
);
|
||||||
|
|
||||||
|
return `
|
||||||
|
import { Component, Input } from '@angular/core';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'svg-${params.componentName}',
|
||||||
|
standalone: true,
|
||||||
|
template: \`
|
||||||
|
${updatedSvgContent.trim()}
|
||||||
|
\`,
|
||||||
|
})
|
||||||
|
export class ${params.componentName}Component {
|
||||||
|
@Input({ required: true }) size: { width: number; height: number };
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
interface iComponentCode {
|
interface ReactComponentParams {
|
||||||
code: string;
|
code: string;
|
||||||
name: string;
|
name: string;
|
||||||
typescript: boolean;
|
typescript: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getReactComponentCode = async (
|
export const getReactCode = async (
|
||||||
params: iComponentCode
|
params: ReactComponentParams
|
||||||
): Promise<{ data?: string; error?: string }> => {
|
): Promise<{ data?: string; error?: string }> => {
|
||||||
try {
|
try {
|
||||||
const getCode = await fetch('/api/svgs/svgr', {
|
const getCode = await fetch('/api/svgs/svgr', {
|
||||||
@ -18,6 +18,6 @@ export const getReactComponentCode = async (
|
|||||||
const data = await getCode.json();
|
const data = await getCode.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { error: 'An error has ocurred. Try again.' };
|
return { error: `⚠️ getReactCode: An error has ocurred - ${error}` };
|
||||||
}
|
}
|
||||||
};
|
};
|
9
src/templates/getSource.ts
Normal file
9
src/templates/getSource.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
interface SourceParams {
|
||||||
|
url: string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getSource = async (params: SourceParams) => {
|
||||||
|
const response = await fetch(params.url || '');
|
||||||
|
const content = await response.text();
|
||||||
|
return content;
|
||||||
|
};
|
15
src/templates/getSvelteCode.ts
Normal file
15
src/templates/getSvelteCode.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { parseSvgContent } from '@/utils/parseSvgContent';
|
||||||
|
|
||||||
|
interface SvelteComponentParams {
|
||||||
|
lang: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getSvelteCode = (params: SvelteComponentParams) => {
|
||||||
|
const { templateContent, componentStyle } = parseSvgContent(params.content, 'Svelte');
|
||||||
|
return `<script${params.lang ? ` lang="${params.lang}"` : ''}></script>
|
||||||
|
${templateContent}
|
||||||
|
|
||||||
|
${componentStyle}
|
||||||
|
`;
|
||||||
|
};
|
17
src/templates/getVueCode.ts
Normal file
17
src/templates/getVueCode.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { parseSvgContent } from '@/utils/parseSvgContent';
|
||||||
|
|
||||||
|
interface VueComponentParams {
|
||||||
|
lang: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getVueCode = (params: VueComponentParams) => {
|
||||||
|
const { templateContent, componentStyle } = parseSvgContent(params.content, 'Vue');
|
||||||
|
return `<script setup${params.lang ? ` lang="${params.lang}"` : ''}></script>
|
||||||
|
<template>
|
||||||
|
${templateContent}
|
||||||
|
</template>
|
||||||
|
|
||||||
|
${componentStyle}
|
||||||
|
`;
|
||||||
|
};
|
@ -1,21 +0,0 @@
|
|||||||
import { parseSvgContent } from './parseSvgContent';
|
|
||||||
|
|
||||||
export const componentTemplate = (lang: string, content: string, framework: 'Vue' | 'Svelte') => {
|
|
||||||
const { templateContent, componentStyle } = parseSvgContent(content, framework);
|
|
||||||
|
|
||||||
if (framework === 'Vue') {
|
|
||||||
return `<script setup${lang ? ` lang="${lang}"` : ''}></script>
|
|
||||||
<template>
|
|
||||||
${templateContent}
|
|
||||||
</template>
|
|
||||||
|
|
||||||
${componentStyle}
|
|
||||||
`;
|
|
||||||
} else {
|
|
||||||
return `<script${lang ? ` lang="${lang}"` : ''}></script>
|
|
||||||
${templateContent}
|
|
||||||
|
|
||||||
${componentStyle}
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,28 +0,0 @@
|
|||||||
export function generateAngularComponent(svgContent: string, componentName: string): string {
|
|
||||||
const updatedSvgContent = svgContent.replace(
|
|
||||||
/<svg([^>]*)>/,
|
|
||||||
`<svg$1 [attr.width]="size.width" [attr.height]="size.height">`
|
|
||||||
);
|
|
||||||
|
|
||||||
return `
|
|
||||||
/**
|
|
||||||
* -------------------------------------------------------------------------
|
|
||||||
* This Angular standalone component was generated by svgl.app
|
|
||||||
* 🧩 A beautiful library with SVG logos
|
|
||||||
* -------------------------------------------------------------------------
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { Component, Input } from '@angular/core';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'svg-${componentName}',
|
|
||||||
standalone: true,
|
|
||||||
template: \`
|
|
||||||
${updatedSvgContent.trim()}
|
|
||||||
\`,
|
|
||||||
})
|
|
||||||
export class ${componentName}Component {
|
|
||||||
@Input({ required: true }) size: { width: number; height: number };
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
export const getSvgContent = async (url: string | undefined) => {
|
|
||||||
const response = await fetch(url || '');
|
|
||||||
const content = await response.text();
|
|
||||||
return content;
|
|
||||||
};
|
|
Loading…
x
Reference in New Issue
Block a user