Skip to content

Commit

Permalink
chore: add react rough
Browse files Browse the repository at this point in the history
  • Loading branch information
zouhangwithsweet committed Aug 16, 2024
1 parent 63304aa commit f2095bf
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 15 deletions.
69 changes: 54 additions & 15 deletions entrypoints/injected/components/Download.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { useAtomValue } from 'jotai'
import { memo, useEffect, useState } from 'react'
import { Clipboard } from 'react-feather'
import SVG from 'react-inlinesvg'
import { RoughSVG } from 'react-rough-fiber'
import { useCopyToClipboard } from 'usehooks-ts'

import { arrayBufferToBase64, arrayBufferToImageFile } from '@/entrypoints/utils/file'

import { currentSelection, exportExt, exportScale } from '../store'

function isSVG(node: SceneNode, depth: number = 0): boolean {
if (depth >= 4) return true // Max depth reached, assume it's SVG

if ('children' in node && node.children.length > 0) {
return node.children.every((child) => isSVG(child, depth + 1))
} else {
return ['VECTOR', 'ELLIPSE', 'RECTANGLE', 'POLYGON', 'POLYLINE', 'STAR', 'LINE'].includes(node.type)
}
}

export const Download = memo((props: { minimized?: boolean }) => {
const node = useAtomValue(currentSelection)
const [imageBase64, setImageBase64] = useState('')
Expand All @@ -32,30 +44,30 @@ export const Download = memo((props: { minimized?: boolean }) => {

useEffect(() => {
;(async () => {
if (node && show) {
const data = await node.exportAsync({
format: 'PNG',
constraint: {
type: 'SCALE',
value: 1,
},
})
setImageBase64('data:image/png;base64,' + arrayBufferToBase64(data))
setSvgString('')

if (node.type === 'VECTOR') {
if (node) {
if (isSVG(node)) {
const svgData = await node.exportAsync({
format: 'SVG_STRING',
})
const svgBase64 = 'data:image/svg+xml;base64,' + btoa(svgData)
setImageBase64(svgBase64)
setSvgString(svgData)
} else {
const data = await node.exportAsync({
format: 'PNG',
constraint: {
type: 'SCALE',
value: 1,
},
})
setImageBase64('data:image/png;base64,' + arrayBufferToBase64(data))
setSvgString('')
}
} else {
setImageBase64('')
}
})()
}, [ext, node, scale, show])
}, [ext, node, scale])

return (
<div
Expand Down Expand Up @@ -103,10 +115,37 @@ export const Download = memo((props: { minimized?: boolean }) => {
'conic-gradient(#fff .25turn,#f7f7f7 .25turn .5turn,#fff .5turn .75turn,#f7f7f7 .75turn) top left/20px 20px repeat',
}}
>
<img src={imageBase64} alt="" className="w-full max-w-60 max-h-210px h-auto object-contain" />
{svgString ? (
// todo
<RoughSVG
className="[&>svg]:min-w-20 p-5"
options={{
roughness: 1,
bowing: 1,
seed: 0,
fillStyle: 'hachure',
fillWeight: 1,
hachureAngle: -41,
hachureGap: 4,
curveStepCount: 9,
curveFitting: 0.95,
disableMultiStroke: false,
disableMultiStrokeFill: false,
simplification: 0,
dashOffset: 4,
dashGap: 4,
zigzagOffset: 4,
preserveVertices: false,
}}
>
<SVG src={svgString} height="auto" />
</RoughSVG>
) : (
<img src={imageBase64} alt="" className="w-full max-w-60 max-h-210px h-auto object-contain" />
)}
</div>
)}
{node?.type === 'VECTOR' && svgString && (
{svgString && (
<div className="flex flex-col items-stretch bg-#f5f5f5 rounded-sm overflow-hidden">
<div className="px-4 h-8 flex-center justify-between border-b border-$color-border border-solid">
<span className="text-$color-text-secondary text-xs">svg</span>
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-feather": "^2.0.10",
"react-inlinesvg": "^4.1.3",
"react-reconciler": "^0.29.2",
"react-rough-fiber": "^0.0.5",
"shiki": "^1.1.7",
"tailwind-merge": "^2.2.1",
"transform-to-tailwindcss-core": "^0.0.19",
Expand Down
60 changes: 60 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2095bf

Please sign in to comment.