diff --git a/src/components/product/previsualization/card/index.tsx b/src/components/product/previsualization/card/index.tsx index 041836f..24b3ee1 100644 --- a/src/components/product/previsualization/card/index.tsx +++ b/src/components/product/previsualization/card/index.tsx @@ -8,175 +8,176 @@ import { AnimatePresence, motion } from "framer-motion"; import { useMeasure } from "@/hooks/use-measure"; const roboto = Roboto({ - subsets: ["latin"], - weight: ["400", "500", "700"], - variable: "--font-roboto", + subsets: ["latin"], + weight: ["400", "500", "700"], + variable: "--font-roboto", }); type PrevisualizationCardProps = { - video: Video; - options: { - theme: "light" | "dark"; - activeWidgets: Widget[]; - display: Display; - textSize: number; - cornerRadius: number; - spacing: number; - }; + video: Video; + options: { + theme: "light" | "dark"; + activeWidgets: Widget[]; + display: Display; + textSize: number; + cornerRadius: number; + spacing: number; + }; }; const PrevisualizationCard = React.forwardRef< - HTMLDivElement, - PrevisualizationCardProps + HTMLDivElement, + PrevisualizationCardProps >(({ video, options }: PrevisualizationCardProps) => { - const buildFooter = () => { - const viewsCount = - isActiveWidgets(options.activeWidgets, "videoViews") && video.viewsCount; - const publishedAt = - isActiveWidgets(options.activeWidgets, "videoPublishedAt") && - video.publishedAt; - const hasSeparator = viewsCount && publishedAt; + const buildFooter = () => { + const viewsCount = + isActiveWidgets(options.activeWidgets, "videoViews") && video.viewsCount; + const publishedAt = + isActiveWidgets(options.activeWidgets, "videoPublishedAt") && + video.publishedAt; + const hasSeparator = viewsCount && publishedAt; - return `${viewsCount ? `${viewsCount} vues` : ""}${ - hasSeparator ? " • " : "" - }${publishedAt ? "Il y a 16 heures" : ""}`; - }; + return `${viewsCount ? `${viewsCount} vues` : ""}${ + hasSeparator ? " • " : "" + }${publishedAt ? "Il y a 16 heures" : ""}`; + }; - return ( - -
- {"Miniature + return ( + +
+ {"Miniature - {isActiveWidgets(options.activeWidgets, "videoDuration") && ( -
- ??:?? -
- )} + {isActiveWidgets(options.activeWidgets, "videoDuration") && ( +
+ ??:?? +
+ )} - {isActiveWidgets(options.activeWidgets, "videoProgressBar") && ( - - )} -
+ {isActiveWidgets(options.activeWidgets, "videoProgressBar") && ( + + )} +
-
- {isActiveWidgets(options.activeWidgets, "channelLogo") && ( - {`Logo - )} +
+ {isActiveWidgets(options.activeWidgets, "channelLogo") && ( + {`Logo + )} -
-
- {video.title} -
+
+
+ {video.title} +
- {isActiveWidgets(options.activeWidgets, "channelName") && ( -
{video.channelName}
- )} + {isActiveWidgets(options.activeWidgets, "channelName") && ( +
{video.channelName}
+ )} -
{buildFooter()}
-
-
- - ); +
{buildFooter()}
+
+
+
+ ); }); export default PrevisualizationCard; diff --git a/src/components/product/previsualization/index.tsx b/src/components/product/previsualization/index.tsx index 4df3777..f34cd65 100644 --- a/src/components/product/previsualization/index.tsx +++ b/src/components/product/previsualization/index.tsx @@ -1,174 +1,178 @@ +import React from "react"; + import Button from "@/components/ui/button"; import Icon from "@/components/ui/icon"; +import Select from "@/components/ui/select"; +import { defaultOptions, defaultVideo } from "@/constants/default-video"; import { css, cx } from "@styled-system/css"; +import type { ImageExtension } from "@/types/image"; +import { convertHtmlToImage } from "@/plugins/html-to-image"; + import PrevisualizationCard from "./card"; -import { defaultOptions, defaultVideo } from "@/constants/default-video"; import type { Widget } from ".."; -import Select from "@/components/ui/select"; import { useForm } from "react-hook-form"; -import React from "react"; + +const ELEMENT_ID = "image-to-download"; type ProductPrevisualizationProps = { - className?: string; - activeWidgets: Widget[]; + className?: string; + activeWidgets: Widget[]; }; type FormData = { - format: "png" | "jpg" | "webp"; - size: "0.5" | "0.75" | "1" | "1.5" | "2" | "3"; + format: ImageExtension; + size: "0.5" | "0.75" | "1" | "1.5" | "2" | "3"; }; const ProductPrevisualization: React.FC = ({ - className, - activeWidgets, + className, + activeWidgets, }: ProductPrevisualizationProps) => { - const ref = React.useRef(null); - - const { register, handleSubmit } = useForm({ - defaultValues: { - format: "png", - size: "1", - }, - }); - - // Un truc dans l'idée tu capte jsp j'pense bref azy - const onSubmit = handleSubmit(({ format, size }: FormData) => { - console.log("Data", format, size); - - //TODO: html to image and return image - - return "caca"; - }); - - const handleDownload = () => { - const image = onSubmit(); - console.log("Download image", image); - if (ref.current) { - } - }; - - const handleCopy = () => { - if (ref.current) { - const image = onSubmit(); - console.log("Copy image", image); - } - }; - - return ( -
-
-

- Prévisualisation -

- -
- -
- -
- -
- - - - - - - -
-
- ); + const ref = React.useRef(null); + + const { register, handleSubmit } = useForm({ + defaultValues: { + format: "png", + size: "1", + }, + }); + + const handleDownload = handleSubmit(async ({ format, size }: FormData) => { + const imageBase64 = await convertHtmlToImage(ELEMENT_ID, format); + + const link = document.createElement("a"); + link.download = `my-image-name.${format}`; + link.href = imageBase64; + link.click(); + }); + + const handleCopy = handleSubmit(async ({ format, size }: FormData) => { + const imageBase64 = await convertHtmlToImage(ELEMENT_ID, format); + + const blob = await fetch(imageBase64).then((res) => res.blob()); + + const clipboardItemInput = new ClipboardItem({ + [blob.type]: blob, + }); + + await navigator.clipboard.write([clipboardItemInput]); + }); + + return ( +
+
+

+ Prévisualisation +

+ +
+ +
+ +
+ +
+ + + + + + + +
+
+ ); }; export default ProductPrevisualization;