From 72507987c7b4d6248ce288a9ec1e314e0a093ad1 Mon Sep 17 00:00:00 2001 From: selemondev Date: Sun, 27 Oct 2024 21:51:22 +0300 Subject: [PATCH] chore: revert unintended linter formatting changes --- src/components/copySvg.svelte | 78 +++++++++++++++++++++++++- src/components/icons/svelteIcon.svelte | 17 ++++++ src/components/icons/vueIcon.svelte | 14 +++++ src/utils/componentTemplate.ts | 21 +++++++ src/utils/parseSvgContent.ts | 26 +++++++++ 5 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 src/components/icons/svelteIcon.svelte create mode 100644 src/components/icons/vueIcon.svelte create mode 100644 src/utils/componentTemplate.ts create mode 100644 src/utils/parseSvgContent.ts diff --git a/src/components/copySvg.svelte b/src/components/copySvg.svelte index f93e34f..79eabec 100644 --- a/src/components/copySvg.svelte +++ b/src/components/copySvg.svelte @@ -12,7 +12,12 @@ import { copyToClipboard as figmaCopyToClipboard } from '@/figma/copy-to-clipboard'; import { buttonStyles } from '@/ui/styles'; import { cn } from '@/utils/cn'; + import { componentTemplate } from '@/utils/componentTemplate'; + + //Icons: import ReactIcon from './icons/reactIcon.svelte'; + import VueIcon from './icons/vueIcon.svelte'; + import SvelteIcon from './icons/svelteIcon.svelte'; // Props: export let iconSize = 24; @@ -67,6 +72,7 @@ optionsOpen = false; const content = await getSvgContent(svgUrlToCopy); + if (isInFigma) { figmaCopyToClipboard(content); } @@ -125,6 +131,34 @@ isLoading = false; }; + + // Copy as either Vue or Svelte component: + const copySvgComponent = async (ts: boolean, framework: 'Vue' | 'Svelte') => { + try { + const svgUrlToCopy = getSvgUrl(); + + optionsOpen = false; + + const content = await getSvgContent(svgUrlToCopy); + + const copyCode = componentTemplate(ts ? 'ts' : '', content, framework); + + if (copyCode) { + await clipboard(copyCode); + } + + const category = Array.isArray(svgInfo?.category) + ? svgInfo.category.sort().join(' - ') + : svgInfo.category; + + toast.success(`Copied as ${framework} ${ts ? 'TS' : 'JS'} component`, { + description: `${svgInfo?.title} - ${category}` + }); + } catch (err) { + console.error(`Error copying ${framework} component:`, err); + toast.error(`Failed to copy ${framework} component`); + } + }; (optionsOpen = isOpen)}> @@ -142,7 +176,7 @@ + + + + + + diff --git a/src/components/icons/svelteIcon.svelte b/src/components/icons/svelteIcon.svelte new file mode 100644 index 0000000..35ed793 --- /dev/null +++ b/src/components/icons/svelteIcon.svelte @@ -0,0 +1,17 @@ + + + diff --git a/src/components/icons/vueIcon.svelte b/src/components/icons/vueIcon.svelte new file mode 100644 index 0000000..8a0ce10 --- /dev/null +++ b/src/components/icons/vueIcon.svelte @@ -0,0 +1,14 @@ + + + diff --git a/src/utils/componentTemplate.ts b/src/utils/componentTemplate.ts new file mode 100644 index 0000000..8dbb5bb --- /dev/null +++ b/src/utils/componentTemplate.ts @@ -0,0 +1,21 @@ +import { parseSvgContent } from './parseSvgContent'; + +export const componentTemplate = (lang: string, content: string, framework: 'Vue' | 'Svelte') => { + const { templateContent, componentStyle } = parseSvgContent(content, framework); + + if (framework === 'Vue') { + return ` + + + ${componentStyle} + `; + } else { + return ` + ${templateContent} + +${componentStyle} + `; + } +}; \ No newline at end of file diff --git a/src/utils/parseSvgContent.ts b/src/utils/parseSvgContent.ts new file mode 100644 index 0000000..6bfaba4 --- /dev/null +++ b/src/utils/parseSvgContent.ts @@ -0,0 +1,26 @@ +export const parseSvgContent = (content: string, framework: 'Vue' | 'Svelte') => { + if (content.includes(']*\?>/i, ''); + } + // Regular expression to match ` + : ''; + + return { + componentStyle, + templateContent + }; +};