Skip to content

Commit 714b533

Browse files
feat: export to jpg (#183)
1 parent 97f420e commit 714b533

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

frontend/package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"date-fns": "^4.1.0",
4747
"fetch-cookie": "^3.0.1",
4848
"framer-motion": "^11.3.28",
49+
"html-to-image": "^1.11.11",
4950
"jotai": "^2.9.3",
5051
"lru-cache": "^11.0.1",
5152
"lucide-react": "^0.426.0",

frontend/src/app/plans/preview/[id]/page.client.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use client";
22

3+
import { toPng } from "html-to-image";
34
import { useAtom } from "jotai";
45
import { useRouter } from "next/navigation";
56
import * as React from "react";
6-
import { useMemo } from "react";
7-
import { LuDownloadCloud } from "react-icons/lu";
7+
import { useCallback, useMemo, useRef } from "react";
88

99
import { planFamily } from "@/atoms/plan-family";
1010
import { plansIds } from "@/atoms/plans-ids";
@@ -20,7 +20,9 @@ export function SharePlanPage({ planId }: { planId: string }) {
2020
const [planToCopy, setPlanToCopy] = useAtom(planFamily({ id: uuid }));
2121

2222
const router = useRouter();
23+
const captureRef = useRef<HTMLDivElement>(null);
2324

25+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2426
const copyPlan = () => {
2527
const newPlan = {
2628
id: uuid,
@@ -41,18 +43,38 @@ export function SharePlanPage({ planId }: { planId: string }) {
4143
router.push(`/plans/edit/${newPlan.id}`);
4244
}, 200);
4345
};
46+
47+
const downloadPlan = useCallback(() => {
48+
if (captureRef.current === null) {
49+
return;
50+
}
51+
52+
toPng(captureRef.current, { cacheBust: true })
53+
.then((dataUrl) => {
54+
const link = document.createElement("a");
55+
link.download = `${plan.name}.png`;
56+
link.href = dataUrl;
57+
link.click();
58+
})
59+
.catch((error: unknown) => {
60+
console.error(error);
61+
});
62+
}, [captureRef, plan.name]);
4463
return (
4564
<div className="flex grow flex-col">
4665
<div className="flex items-center justify-center gap-4 p-2">
4766
<Button
48-
onClick={copyPlan}
67+
onClick={downloadPlan}
4968
className="flex items-center justify-center gap-4 text-nowrap rounded-md text-lg"
5069
>
51-
Skopiuj
52-
<LuDownloadCloud />
70+
Pobierz .jpg
5371
</Button>
5472
</div>
55-
<div className="flex flex-col gap-2 overflow-auto p-1 scrollbar-thin scrollbar-track-sky-300 scrollbar-thumb-sky-900">
73+
74+
<div
75+
ref={captureRef}
76+
className="flex flex-col gap-2 overflow-auto bg-white p-1 scrollbar-thin scrollbar-track-sky-300 scrollbar-thumb-sky-900"
77+
>
5678
{[
5779
{ day: Day.MONDAY, label: "Poniedziałek" },
5880
{ day: Day.TUESDAY, label: "Wtorek" },

0 commit comments

Comments
 (0)