mirror of
https://github.com/pheralb/svgl.git
synced 2025-12-29 08:01:36 +08:00
🚀 Add new /templates utility + add support for web components
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
`;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
interface ReactComponentParams {
|
||||
code: string;
|
||||
name: string;
|
||||
typescript: boolean;
|
||||
}
|
||||
|
||||
export const getReactCode = async (
|
||||
params: ReactComponentParams
|
||||
): 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: `⚠️ getReactCode: An error has ocurred - ${error}` };
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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}
|
||||
`;
|
||||
};
|
||||
@@ -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}
|
||||
`;
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
interface WebComponentParams {
|
||||
name: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export const getWebComponent = (params: WebComponentParams) => {
|
||||
return `
|
||||
class Icon${params.name} extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: "open" });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = /* html */ \`
|
||||
<style>
|
||||
svg {
|
||||
width: var(--size, 128px);
|
||||
color: var(--color, currentColor);
|
||||
}
|
||||
</style>
|
||||
${params.content}
|
||||
\`;
|
||||
}
|
||||
}
|
||||
customElements.define("icon-${params.name.toLowerCase()}", Icon${params.name});
|
||||
`;
|
||||
};
|
||||
Reference in New Issue
Block a user