'use client'; import { useState, useCallback } from 'react'; import { X, Link, Unlink } from 'lucide-react'; import { useLayerStore, useCanvasStore } from '@/store'; import { toast } from '@/lib/toast-utils'; interface CanvasResizeDialogProps { isOpen: boolean; onClose: () => void; } export function CanvasResizeDialog({ isOpen, onClose }: CanvasResizeDialogProps) { const { layers } = useLayerStore(); // Get current canvas dimensions from first layer const currentWidth = layers[0]?.width || 800; const currentHeight = layers[0]?.height || 600; const [width, setWidth] = useState(currentWidth); const [height, setHeight] = useState(currentHeight); const [maintainAspectRatio, setMaintainAspectRatio] = useState(true); const [resizeMethod, setResizeMethod] = useState<'scale' | 'crop' | 'expand'>('scale'); const aspectRatio = currentWidth / currentHeight; const handleWidthChange = useCallback( (newWidth: number) => { setWidth(newWidth); if (maintainAspectRatio) { setHeight(Math.round(newWidth / aspectRatio)); } }, [maintainAspectRatio, aspectRatio] ); const handleHeightChange = useCallback( (newHeight: number) => { setHeight(newHeight); if (maintainAspectRatio) { setWidth(Math.round(newHeight * aspectRatio)); } }, [maintainAspectRatio, aspectRatio] ); const handleApply = useCallback(() => { if (width < 1 || height < 1 || width > 10000 || height > 10000) { toast.error('Width and height must be between 1 and 10000 pixels'); return; } // Apply resize based on method const { updateLayer } = useLayerStore.getState(); layers.forEach((layer) => { if (!layer.canvas) return; const newCanvas = document.createElement('canvas'); newCanvas.width = width; newCanvas.height = height; const ctx = newCanvas.getContext('2d'); if (!ctx) return; switch (resizeMethod) { case 'scale': // Scale the content to fit new size ctx.drawImage(layer.canvas, 0, 0, width, height); break; case 'crop': // Crop/center the content const offsetX = Math.max(0, (width - layer.width) / 2); const offsetY = Math.max(0, (height - layer.height) / 2); ctx.drawImage(layer.canvas, offsetX, offsetY); break; case 'expand': // Expand canvas, center existing content const centerX = (width - layer.width) / 2; const centerY = (height - layer.height) / 2; ctx.drawImage(layer.canvas, centerX, centerY); break; } updateLayer(layer.id, { canvas: newCanvas, width, height, }); }); toast.success(`Canvas resized to ${width} × ${height}px`); onClose(); }, [width, height, resizeMethod, layers, onClose]); if (!isOpen) return null; return (
{/* Header */}

Canvas Size

{/* Content */}
{/* Current Size */}

Current size: {currentWidth} × {currentHeight}px

{/* New Dimensions */}
handleWidthChange(Number(e.target.value))} className="w-full mt-1 px-3 py-2 border border-border rounded-md bg-background text-foreground" />
handleHeightChange(Number(e.target.value))} className="w-full mt-1 px-3 py-2 border border-border rounded-md bg-background text-foreground" />
{/* Resize Method */}
{/* Footer */}
); }