diff --git a/components/shared/Editor.v2.tsx b/components/shared/Editor.v2.tsx index f008956..e05917c 100644 --- a/components/shared/Editor.v2.tsx +++ b/components/shared/Editor.v2.tsx @@ -1,7 +1,14 @@ "use client"; import React, { useState, useRef, useEffect, useCallback } from "react"; -import { Upload, X, RotateCcw, RotateCcwIcon, LinkIcon } from "lucide-react"; +import { + Upload, + X, + RotateCcw, + RotateCcwIcon, + LinkIcon, + Sparkles, +} from "lucide-react"; import { useDropzone } from "react-dropzone"; import { saveAs } from "file-saver"; import jsPDF from "jspdf"; @@ -30,17 +37,22 @@ import { Separator } from "@radix-ui/react-select"; import ExportButton from "./buttons/ExportButton"; import { truncateFileName } from "@/lib/utils"; import { backgroundUrls, screenSizes } from "@/lib/constants"; +import { getGradientFromImage } from "@/lib/extractColors"; const validationError = { customHeight: "", customWidth: "", } satisfies ValidationError; +type BackgroundTab = "color" | "gradient" | "image"; + const defaultSettings = { image: null, background: backgroundUrls[0], + backgroundTab: "image" as BackgroundTab, customColor1: "#ffffff", customColor2: "#000000", + customColor3: "#000000", gradientAngle: 0, screenSize: screenSizes[2], zoom: 50, @@ -76,6 +88,10 @@ const defaultSettings = { export default function MockupEditor() { const [image, setImage] = useState(defaultSettings.image); + + const [backgroundTab, setBackgroundTab] = useState( + defaultSettings.backgroundTab + ); const [background, setBackground] = useState(defaultSettings.background); const [isCustomBackground, setIsCustomBackground] = useState(false); const [isUrlFormat, setIsUrlFormat] = useState(true); @@ -90,6 +106,9 @@ export default function MockupEditor() { const [customColor2, setCustomColor2] = useState( defaultSettings.customColor2 ); + const [customColor3, setCustomColor3] = useState( + defaultSettings.customColor3 + ); const [gradientAngle, setGradientAngle] = useState( defaultSettings.gradientAngle ); @@ -133,7 +152,6 @@ export default function MockupEditor() { const [browsedFile, setIsBrowsedFile] = useState(false); const [displayFileName, setDisplayFileName] = useState(""); - // Refs for canvas and container elements const canvasRef = useRef(null); @@ -220,6 +238,7 @@ export default function MockupEditor() { setBackground(defaultSettings.background); setCustomColor1(defaultSettings.customColor1); setCustomColor2(defaultSettings.customColor2); + setCustomColor3(defaultSettings.customColor3); setGradientAngle(defaultSettings.gradientAngle); setScreenSize(defaultSettings.screenSize); setPresetScreenSize(defaultSettings.screenSize); @@ -237,10 +256,27 @@ export default function MockupEditor() { setDownloadFormat(defaultSettings.format); setLoadedImage(null); setIsCustomBackground(false); + setBackgroundTab(defaultSettings.backgroundTab); setCustomImg(""); setDisplayFileName(""); }; + const autoPickColor = async () => { + if (!image) { + return; + } + getGradientFromImage(image).then((colors) => { + if (colors) { + console.log(colors); + setCustomColor1(colors[0]); + setCustomColor2(colors[1]); + setCustomColor3(colors[2]); + setBackground("gradient"); + setBackgroundTab("gradient"); + } + }); + }; + const updateCanvasScale = useCallback(() => { if (containerRef.current && canvasRef.current) { const containerWidth = containerRef.current.clientWidth; @@ -330,7 +366,8 @@ export default function MockupEditor() { Math.sin((gradientAngle * Math.PI) / 180) * screenSize.height ); gradient.addColorStop(0, customColor1); - gradient.addColorStop(1, customColor2); + gradient.addColorStop(0.33, customColor2); + gradient.addColorStop(1, customColor3); ctx.fillStyle = gradient; ctx.fillRect(0, 0, screenSize.width, screenSize.height); } else { @@ -639,8 +676,6 @@ export default function MockupEditor() { } }, [displayFileName]); - - return (
@@ -648,7 +683,7 @@ export default function MockupEditor() {
-