@@ -10,11 +10,12 @@ import {
1010import { useRef , useState } from "react" ;
1111import { Button } from "@/components/ui/button" ;
1212import type { QuoteFormData } from "@/model/quote.model" ;
13- import { ArrowLeftIcon , Save , Share } from "lucide-react" ;
13+ import { ArrowLeftIcon , CircleArrowDown , LoaderCircle , Save , Share } from "lucide-react" ;
1414import useBackground , { type Pri_set , type TextureKey } from "@/hook/use-background.hook" ;
1515import { sanitizeHTML } from "@/helper/sanitize-html" ;
1616import { cn } from "@/lib/utils" ;
1717import { ListTags } from "@/feature/quote/list.tags" ;
18+ import { toPng } from "html-to-image"
1819
1920interface Props {
2021 quoteFormData : QuoteFormData
@@ -24,8 +25,28 @@ export default function ShareBackground(props: Props) {
2425 const { quoteFormData } = props
2526 const { buildStyle } = useBackground ( )
2627 const noteRef = useRef < HTMLDivElement > ( null )
28+ const [ downloading , setDownloading ] = useState ( false )
2729 const cardStyle = buildStyle ( quoteFormData . texture ! , quoteFormData . pri_set ! )
2830
31+ const exportAsImage = async ( ) => {
32+ if ( ! noteRef . current ) return
33+ setDownloading ( true )
34+ const dataUrl = await toPng ( noteRef . current , {
35+ pixelRatio : 2 , // crisp image
36+ //backgroundColor: "#fff"
37+ cacheBust : true ,
38+ backgroundColor : cardStyle . backgroundColor
39+ } )
40+
41+ const link = document . createElement ( "a" )
42+ link . download = "note-by-pri-" + new Date ( ) . getTime ( ) + "-.png"
43+ link . href = dataUrl
44+ link . click ( )
45+ setTimeout ( ( ) => {
46+ setDownloading ( false )
47+ } , 500 ) ;
48+ }
49+
2950 return (
3051 < Drawer
3152 >
@@ -42,25 +63,44 @@ export default function ShareBackground(props: Props) {
4263 Share it with your close one.
4364 </ DrawerDescription >
4465 </ DrawerHeader >
45- { /* Top bar */ }
46- < div className = "flex items-center justify-between " >
66+ < div className = "flex flex-col gap-4" >
4767
48- </ div >
68+ < div className = "flex items-center justify-between gap-4 " >
4969
50- { /* Live preview */ }
51- < div className = "w-full border rounded-lg overflow-hidden shadow" >
52- < div
53- ref = { noteRef }
54- style = { cardStyle }
55- className = {
56- cn (
57- "border p-2 bg-card rounded-xl " ,
58- "flex flex-col justify-between items-start gap-2" ,
59- "" ,
60- )
61-
62- } >
63- < div className = "
70+ < div className = "flex flex-col gap-4" >
71+ < Button
72+ className = ""
73+ variant = { "outline" }
74+ onClick = { ( e ) => {
75+ e . preventDefault ( )
76+ exportAsImage ( )
77+ } }
78+ aria-label = "Download quote"
79+ size = { "lg" }
80+ >
81+ { downloading ? < LoaderCircle className = "animate-spin" /> : < CircleArrowDown /> }
82+ </ Button >
83+ < p className = "text-xs" >
84+ { downloading ? "Downloading" : "Download" }
85+ </ p >
86+ </ div >
87+
88+ </ div >
89+
90+ { /* Live preview */ }
91+ < div className = "w-full border rounded-lg overflow-hidden shadow" >
92+ < div
93+ ref = { noteRef }
94+ style = { cardStyle }
95+ className = {
96+ cn (
97+ "border p-2 bg-card rounded-xl " ,
98+ "flex flex-col justify-between items-start gap-2" ,
99+ "" ,
100+ )
101+
102+ } >
103+ < div className = "
64104 tiptap
65105 prose-sm
66106 removed-prose
@@ -69,13 +109,16 @@ export default function ShareBackground(props: Props) {
69109 removed-xl:prose-2xl
70110 removed-prose-foreground
71111 " > { /* IMPORTANT */ }
72- < div dangerouslySetInnerHTML = { { __html : sanitizeHTML ( quoteFormData . text ! ) } } > </ div >
73- </ div >
74- < div className = "flex w-full items-end justify-between gap-2" >
75- < ListTags tags = { quoteFormData ?. tags ?. map ( ( tag , i ) => { return { id : i , name : tag } } ) || [ ] } />
112+ < div dangerouslySetInnerHTML = { { __html : sanitizeHTML ( quoteFormData . text ! ) } } > </ div >
113+ </ div >
114+ < div className = "flex w-full items-end justify-between gap-2" >
115+ < ListTags tags = { quoteFormData ?. tags ?. map ( ( tag , i ) => { return { id : i , name : tag } } ) || [ ] } />
76116
117+ </ div >
77118 </ div >
78119 </ div >
120+
121+
79122 </ div >
80123
81124
0 commit comments