feat: add Import Image functionality to add images as new layers
Implements image import feature that adds images as new layers without clearing existing work: **New Import Image Function** (`hooks/use-file-operations.ts`): - `importImage()` - Creates new layer with imported image - Preserves all existing layers and canvas state - Automatically names layer from filename (removes extension) - Loading indicator during import process - Success/error toast notifications - Supports all common image formats (PNG, JPG, WEBP, etc.) **File Menu Integration** (`components/editor/file-menu.tsx`): - Added "Import Image..." menu item with ImagePlus icon - Positioned after "Open..." for logical flow - Separate file input for import (only accepts images) - Import handler with file input reset - Keyboard accessible with proper ARIA roles **Key Differences from Open:** - **Open** - Replaces entire canvas (clears all layers, resets dimensions) - **Import** - Adds image as new layer (preserves existing work) **User Experience:** - File > Import Image... opens native file picker - Only image files accepted (image/*) - Import creates layer with image dimensions - Layer positioned at origin (0, 0) - Can import multiple images as separate layers - Works alongside existing drag & drop functionality **Use Cases:** - Adding reference images to canvas - Building collages from multiple images - Importing assets without losing current work - Layer-based compositing workflows All functionality tested and working with no build errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
|||||||
Download,
|
Download,
|
||||||
Save,
|
Save,
|
||||||
ChevronDown,
|
ChevronDown,
|
||||||
|
ImagePlus,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ export function FileMenu() {
|
|||||||
const {
|
const {
|
||||||
createNewImage,
|
createNewImage,
|
||||||
handleFileInput,
|
handleFileInput,
|
||||||
|
importImage,
|
||||||
exportImage,
|
exportImage,
|
||||||
saveProject,
|
saveProject,
|
||||||
} = useFileOperations();
|
} = useFileOperations();
|
||||||
@@ -25,6 +27,7 @@ export function FileMenu() {
|
|||||||
const [isNewDialogOpen, setIsNewDialogOpen] = useState(false);
|
const [isNewDialogOpen, setIsNewDialogOpen] = useState(false);
|
||||||
const [isExportDialogOpen, setIsExportDialogOpen] = useState(false);
|
const [isExportDialogOpen, setIsExportDialogOpen] = useState(false);
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const importInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const handleOpenFile = () => {
|
const handleOpenFile = () => {
|
||||||
fileInputRef.current?.click();
|
fileInputRef.current?.click();
|
||||||
@@ -42,6 +45,22 @@ export function FileMenu() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleImportImage = () => {
|
||||||
|
importInputRef.current?.click();
|
||||||
|
setIsMenuOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleImportChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const file = e.target.files?.[0];
|
||||||
|
if (file) {
|
||||||
|
await importImage(file);
|
||||||
|
}
|
||||||
|
// Reset input
|
||||||
|
if (importInputRef.current) {
|
||||||
|
importInputRef.current.value = '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleSaveProject = () => {
|
const handleSaveProject = () => {
|
||||||
saveProject('project');
|
saveProject('project');
|
||||||
setIsMenuOpen(false);
|
setIsMenuOpen(false);
|
||||||
@@ -94,6 +113,15 @@ export function FileMenu() {
|
|||||||
Open...
|
Open...
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={handleImportImage}
|
||||||
|
className="flex items-center gap-3 w-full px-4 py-2 text-sm hover:bg-accent transition-colors focus:outline-none focus:bg-accent text-left"
|
||||||
|
role="menuitem"
|
||||||
|
>
|
||||||
|
<ImagePlus className="h-4 w-4" aria-hidden="true" />
|
||||||
|
Import Image...
|
||||||
|
</button>
|
||||||
|
|
||||||
<div className="h-px bg-border my-1" role="separator" />
|
<div className="h-px bg-border my-1" role="separator" />
|
||||||
|
|
||||||
<button
|
<button
|
||||||
@@ -121,7 +149,7 @@ export function FileMenu() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Hidden file input */}
|
{/* Hidden file inputs */}
|
||||||
<input
|
<input
|
||||||
ref={fileInputRef}
|
ref={fileInputRef}
|
||||||
type="file"
|
type="file"
|
||||||
@@ -129,6 +157,13 @@ export function FileMenu() {
|
|||||||
onChange={handleFileChange}
|
onChange={handleFileChange}
|
||||||
className="hidden"
|
className="hidden"
|
||||||
/>
|
/>
|
||||||
|
<input
|
||||||
|
ref={importInputRef}
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
onChange={handleImportChange}
|
||||||
|
className="hidden"
|
||||||
|
/>
|
||||||
|
|
||||||
{/* Dialogs */}
|
{/* Dialogs */}
|
||||||
<NewImageDialog
|
<NewImageDialog
|
||||||
|
|||||||
@@ -127,6 +127,40 @@ export function useFileOperations() {
|
|||||||
[clearLayers, clearHistory, setDimensions, createLayer, setLoading]
|
[clearLayers, clearHistory, setDimensions, createLayer, setLoading]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import image as new layer (without clearing existing layers)
|
||||||
|
*/
|
||||||
|
const importImage = useCallback(
|
||||||
|
async (file: File) => {
|
||||||
|
setLoading(true, `Importing ${file.name}...`);
|
||||||
|
try {
|
||||||
|
const img = await openImageFile(file);
|
||||||
|
|
||||||
|
// Create new layer with imported image
|
||||||
|
const layer = createLayer({
|
||||||
|
name: file.name.replace(/\.[^/.]+$/, ''), // Remove extension
|
||||||
|
width: img.width,
|
||||||
|
height: img.height,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (layer.canvas) {
|
||||||
|
const ctx = layer.canvas.getContext('2d');
|
||||||
|
if (ctx) {
|
||||||
|
ctx.drawImage(img, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success(`Imported ${file.name} as new layer`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to import image:', error);
|
||||||
|
toast.error('Failed to import image file');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[createLayer, setLoading]
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export current view as image
|
* Export current view as image
|
||||||
*/
|
*/
|
||||||
@@ -223,6 +257,7 @@ export function useFileOperations() {
|
|||||||
createNewImage,
|
createNewImage,
|
||||||
openImage,
|
openImage,
|
||||||
openProject,
|
openProject,
|
||||||
|
importImage,
|
||||||
exportImage,
|
exportImage,
|
||||||
saveProject,
|
saveProject,
|
||||||
handleFileInput,
|
handleFileInput,
|
||||||
|
|||||||
Reference in New Issue
Block a user