Skip to content

Commit 2623635

Browse files
committed
added download to share drawer
1 parent df8129e commit 2623635

5 files changed

Lines changed: 117 additions & 67 deletions

File tree

‎frontend/src/components/common/tiptap-customized.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Tiptap = (props: Props) => {
2525
editorProps: {
2626
attributes: {
2727
style: styleString,
28-
class: 'p-2 border bg-card max-h-64 overflow-auto rounded-xl '
28+
class: 'p-2 border bg-card min-h-48 max-h-64 overflow-auto rounded-xl '
2929
}
3030
},
3131
onUpdate: (updates) => {

‎frontend/src/feature/quote/card/quote.card.tsx‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type { Quote, QuoteDetails } from "@/model/quote.model";
77
import { Link } from "@tanstack/react-router";
88
import { Check, Copy, Maximize2, PenIcon, Trash, Save, CircleArrowDown, LoaderCircle } from "lucide-react";
99
import { useState, useRef } from "react";
10-
import { toPng } from "html-to-image"
1110
import { ListTags } from "@/feature/quote/list.tags";
1211

1312
interface Props {
@@ -19,31 +18,12 @@ interface Props {
1918
const QuoteCard = (props: Props) => {
2019
const { quote, onEdit, onDelete, } = props
2120
const [copying, setCopying] = useState(false)
22-
const [downloading, setDownloading] = useState(false)
2321
const { buildStyle } = useBackground()
2422
const noteRef = useRef<HTMLDivElement>(null)
2523

2624
const cardStyle = buildStyle(quote.texture!, quote.pri_set!)
2725

28-
const exportAsImage = async () => {
29-
if (!noteRef.current) return
30-
setDownloading(true)
31-
const dataUrl = await toPng(noteRef.current, {
32-
pixelRatio: 2, // crisp image
33-
//backgroundColor: "#fff"
34-
cacheBust: true,
35-
backgroundColor: cardStyle.backgroundColor
36-
})
37-
38-
const link = document.createElement("a")
39-
link.download = new Date().getTime() + "-note.png"
40-
link.href = dataUrl
41-
link.click()
42-
setTimeout(() => {
43-
setDownloading(false)
44-
}, 500);
45-
}
46-
26+
4727
const onCopy = async (text: string) => {
4828
setCopying(true)
4929
await window.navigator.clipboard.writeText(text)
@@ -103,20 +83,6 @@ const QuoteCard = (props: Props) => {
10383
{copying ? <Check className="text-green-500" /> : <Copy />}
10484
</Button>
10585

106-
<Button
107-
className="hover:text-green-600 "
108-
variant={"outline"}
109-
onClick={(e) => {
110-
e.preventDefault()
111-
exportAsImage()
112-
}}
113-
aria-label="Copy quote"
114-
size={"sm"}
115-
>
116-
117-
{downloading ? <LoaderCircle className="animate-spin" /> : <CircleArrowDown />}
118-
119-
</Button>
12086
{/*
12187
<Button
12288
className="hover:text-yellow-600 "

‎frontend/src/feature/quote/drawer/choose-background.drawer.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ interface Props {
2020
export default function ChooseBackground(props: Props) {
2121
const { onValueUpdate} = props
2222
const { textures, colorPresets, buildStyle } = useBackground()
23-
const [selectedTexture, setSelectedTexture] = useState<TextureKey> ( "cardboard");
24-
const [selectedPreset, setSelectedPreset] = useState<Pri_set>('pri_set_1');
23+
const [selectedTexture, setSelectedTexture] = useState<TextureKey> ();
24+
const [selectedPreset, setSelectedPreset] = useState<Pri_set>('pri_set_0');
2525

2626

2727
const handleTextureClick = (key: TextureKey)=>{
@@ -73,7 +73,7 @@ export default function ChooseBackground(props: Props) {
7373
<div className="w-full h-48 rounded-lg mb-4 overflow-hidden shadow">
7474
<div
7575
className="w-full h-full"
76-
style={buildStyle(selectedTexture, selectedPreset)}
76+
style={buildStyle(selectedTexture!, selectedPreset)}
7777
/>
7878
</div>
7979

‎frontend/src/feature/quote/drawer/share.drawer.tsx‎

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
import { useRef, useState } from "react";
1111
import { Button } from "@/components/ui/button";
1212
import type { QuoteFormData } from "@/model/quote.model";
13-
import { ArrowLeftIcon, Save, Share } from "lucide-react";
13+
import { ArrowLeftIcon, CircleArrowDown, LoaderCircle, Save, Share } from "lucide-react";
1414
import useBackground, { type Pri_set, type TextureKey } from "@/hook/use-background.hook";
1515
import { sanitizeHTML } from "@/helper/sanitize-html";
1616
import { cn } from "@/lib/utils";
1717
import { ListTags } from "@/feature/quote/list.tags";
18+
import { toPng } from "html-to-image"
1819

1920
interface 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

‎frontend/src/hook/use-background.hook.ts‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,53 @@ interface ColorPresets {
1111
}
1212

1313
const colorPresets = {
14-
pri_set_1: { id: 1, name: "red", color: "red" } as ColorPresets,
15-
pri_set_2: { id: 2, name: "sun", overlay: "linear-gradient(to right, #ff512f, #dd2476)" } as ColorPresets, // gradient overlay
16-
pri_set_3: { id: 3, name: "yellow", color: "yellow" } as ColorPresets,
17-
pri_set_4: { id: 4, name: "blue", color: "blue" } as ColorPresets,
18-
pri_set_5: { id: 5, name: "dark-fade", overlay: "linear-gradient(to bottom, rgba(0,0,0,0.2), rgba(0,0,0,0.6))" } as ColorPresets,
19-
}
14+
pri_set_0: { id: 0, name: "default" } as ColorPresets,
15+
16+
pri_set_1: { id: 1, name: "red", color: "red" } as ColorPresets,
17+
18+
pri_set_2: {
19+
id: 2,
20+
name: "sun",
21+
overlay: "linear-gradient(to right, #ff512f, #dd2476)",
22+
} as ColorPresets, // gradient overlay
23+
24+
pri_set_3: { id: 3, name: "yellow", color: "yellow" } as ColorPresets,
25+
26+
pri_set_4: { id: 4, name: "blue", color: "blue" } as ColorPresets,
27+
28+
pri_set_5: {
29+
id: 5,
30+
name: "dark-fade",
31+
overlay:
32+
"linear-gradient(to bottom, rgba(0,0,0,0.2), rgba(0,0,0,0.6))",
33+
} as ColorPresets,
34+
35+
// 🔽 NEW SOLID COLORS
36+
pri_set_6: { id: 6, name: "green", color: "#22c55e" } as ColorPresets,
37+
pri_set_7: { id: 7, name: "purple", color: "#a855f7" } as ColorPresets,
38+
pri_set_8: { id: 8, name: "slate", color: "#334155" } as ColorPresets,
39+
40+
// 🔽 NEW GRADIENT OVERLAYS
41+
pri_set_9: {
42+
id: 9,
43+
name: "ocean",
44+
overlay: "linear-gradient(to right, #2193b0, #6dd5ed)",
45+
} as ColorPresets,
46+
47+
pri_set_10: {
48+
id: 10,
49+
name: "mint",
50+
overlay: "linear-gradient(to right, #00b09b, #96c93d)",
51+
} as ColorPresets,
52+
53+
pri_set_11: {
54+
id: 11,
55+
name: "violet-fade",
56+
overlay:
57+
"linear-gradient(to bottom, rgba(139,92,246,0.2), rgba(139,92,246,0.7))",
58+
} as ColorPresets,
59+
};
60+
2061

2162
export type Pri_set = keyof typeof colorPresets
2263

0 commit comments

Comments
 (0)