-
Notifications
You must be signed in to change notification settings - Fork 19
01 VACE feat: add image control #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { useState } from "react"; | ||
| import { Plus, X } from "lucide-react"; | ||
| import { LabelWithTooltip } from "./ui/label-with-tooltip"; | ||
| import { getAssetUrl } from "../lib/api"; | ||
| import { MediaPicker } from "./MediaPicker"; | ||
|
|
||
| interface ImageManagerProps { | ||
| images: string[]; | ||
| onImagesChange: (images: string[]) => void; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export function ImageManager({ | ||
| images, | ||
| onImagesChange, | ||
| disabled, | ||
| }: ImageManagerProps) { | ||
| const [isMediaPickerOpen, setIsMediaPickerOpen] = useState(false); | ||
|
|
||
| const handleAddImage = (imagePath: string) => { | ||
| onImagesChange([...images, imagePath]); | ||
| }; | ||
|
|
||
| const handleRemoveImage = (index: number) => { | ||
| onImagesChange(images.filter((_, i) => i !== index)); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <LabelWithTooltip | ||
| label="Reference Images" | ||
| tooltip="Select reference images for VACE conditioning. Images will guide the video generation style and content." | ||
| className="text-sm font-medium mb-2" | ||
| /> | ||
|
|
||
| <div className="grid grid-cols-2 gap-2"> | ||
| <button | ||
| onClick={() => setIsMediaPickerOpen(true)} | ||
| disabled={disabled} | ||
| className="aspect-square border-2 border-dashed rounded-lg flex flex-col items-center justify-center hover:bg-accent hover:border-accent-foreground disabled:opacity-50 disabled:cursor-not-allowed transition-colors" | ||
| > | ||
| <Plus className="h-6 w-6 mb-1 text-muted-foreground" /> | ||
| <span className="text-xs text-muted-foreground">Add Image</span> | ||
| </button> | ||
|
|
||
| {images.map((imagePath, index) => ( | ||
| <div | ||
| key={index} | ||
| className="aspect-square border rounded-lg overflow-hidden relative group" | ||
| > | ||
| <img | ||
| src={getAssetUrl(imagePath)} | ||
| alt={`Reference ${index + 1}`} | ||
| className="w-full h-full object-cover" | ||
| /> | ||
| <button | ||
| onClick={() => handleRemoveImage(index)} | ||
| disabled={disabled} | ||
| className="absolute top-1 right-1 bg-black/70 hover:bg-black text-white rounded p-1 opacity-0 group-hover:opacity-100 transition-opacity disabled:opacity-50" | ||
| title="Remove image" | ||
| > | ||
| <X className="h-3 w-3" /> | ||
| </button> | ||
| </div> | ||
| ))} | ||
| </div> | ||
|
|
||
| <MediaPicker | ||
| isOpen={isMediaPickerOpen} | ||
| onClose={() => setIsMediaPickerOpen(false)} | ||
| onSelectImage={handleAddImage} | ||
| disabled={disabled} | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { useState, useEffect, useRef } from "react"; | ||
| import { X, Upload } from "lucide-react"; | ||
| import { Button } from "./ui/button"; | ||
| import { | ||
| listAssets, | ||
| uploadAsset, | ||
| getAssetUrl, | ||
| type AssetFileInfo, | ||
| } from "../lib/api"; | ||
|
|
||
| interface MediaPickerProps { | ||
| isOpen: boolean; | ||
| onClose: () => void; | ||
| onSelectImage: (imagePath: string) => void; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| export function MediaPicker({ | ||
| isOpen, | ||
| onClose, | ||
| onSelectImage, | ||
| disabled, | ||
| }: MediaPickerProps) { | ||
| const [images, setImages] = useState<AssetFileInfo[]>([]); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const [isUploading, setIsUploading] = useState(false); | ||
| const fileInputRef = useRef<HTMLInputElement>(null); | ||
| const modalRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const loadImages = async () => { | ||
| setIsLoading(true); | ||
| try { | ||
| const response = await listAssets("image"); | ||
| setImages(response.assets); | ||
| } catch (error) { | ||
| console.error("loadImages: Failed to load images:", error); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (isOpen) { | ||
| loadImages(); | ||
| } | ||
| }, [isOpen]); | ||
|
|
||
| useEffect(() => { | ||
| const handleClickOutside = (event: MouseEvent) => { | ||
| if ( | ||
| modalRef.current && | ||
| !modalRef.current.contains(event.target as Node) | ||
| ) { | ||
| onClose(); | ||
| } | ||
| }; | ||
|
|
||
| if (isOpen) { | ||
| document.addEventListener("mousedown", handleClickOutside); | ||
| return () => | ||
| document.removeEventListener("mousedown", handleClickOutside); | ||
| } | ||
| }, [isOpen, onClose]); | ||
|
|
||
| const handleUploadClick = () => { | ||
| fileInputRef.current?.click(); | ||
| }; | ||
|
|
||
| const handleFileUpload = async ( | ||
| event: React.ChangeEvent<HTMLInputElement> | ||
| ) => { | ||
| const file = event.target.files?.[0]; | ||
| if (!file) return; | ||
|
|
||
| const allowedTypes = [ | ||
| "image/png", | ||
| "image/jpeg", | ||
| "image/jpg", | ||
| "image/webp", | ||
| "image/bmp", | ||
| ]; | ||
| if (!allowedTypes.includes(file.type)) { | ||
| console.error( | ||
| "handleFileUpload: Invalid file type. Allowed types: PNG, JPEG, JPG, WEBP, BMP" | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const maxSize = 50 * 1024 * 1024; | ||
| if (file.size > maxSize) { | ||
| console.error( | ||
| `handleFileUpload: File size exceeds maximum of ${maxSize / (1024 * 1024)}MB` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| setIsUploading(true); | ||
| try { | ||
| const uploadedFile = await uploadAsset(file); | ||
| await loadImages(); | ||
| onSelectImage(uploadedFile.path); | ||
| } catch (error) { | ||
| console.error("handleFileUpload: Failed to upload image:", error); | ||
| } finally { | ||
| setIsUploading(false); | ||
| if (fileInputRef.current) { | ||
| fileInputRef.current.value = ""; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const handleSelectImage = (imagePath: string) => { | ||
| onSelectImage(imagePath); | ||
| }; | ||
|
|
||
| if (!isOpen) return null; | ||
|
|
||
| return ( | ||
| <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50"> | ||
| <div | ||
| ref={modalRef} | ||
| className="bg-card border rounded-lg shadow-lg p-6 max-w-2xl w-full mx-4" | ||
| > | ||
| <div className="flex items-center justify-between mb-4"> | ||
| <h2 className="text-lg font-semibold">Media Picker</h2> | ||
| <Button | ||
| size="sm" | ||
| variant="ghost" | ||
| onClick={onClose} | ||
| className="h-6 w-6 p-0" | ||
| > | ||
| <X className="h-4 w-4" /> | ||
| </Button> | ||
| </div> | ||
|
|
||
| <input | ||
| type="file" | ||
| accept="image/png,image/jpeg,image/jpg,image/webp,image/bmp" | ||
| onChange={handleFileUpload} | ||
| className="hidden" | ||
| ref={fileInputRef} | ||
| disabled={disabled || isUploading} | ||
| /> | ||
|
|
||
| {isLoading ? ( | ||
| <div className="text-center py-12 text-muted-foreground"> | ||
| Loading images... | ||
| </div> | ||
| ) : ( | ||
| <div className="max-h-96 overflow-y-auto"> | ||
| <div className="grid grid-cols-3 gap-4"> | ||
| <button | ||
| onClick={handleUploadClick} | ||
| disabled={disabled || isUploading} | ||
| className="aspect-square border-2 border-dashed rounded-lg flex flex-col items-center justify-center hover:bg-accent hover:border-accent-foreground disabled:opacity-50 disabled:cursor-not-allowed transition-colors" | ||
| > | ||
| <Upload className="h-8 w-8 mb-2 text-muted-foreground" /> | ||
| <span className="text-sm text-muted-foreground"> | ||
| {isUploading ? "Uploading..." : "Upload"} | ||
| </span> | ||
| </button> | ||
|
|
||
| {images.map((image, index) => ( | ||
| <button | ||
| key={image.path} | ||
| onClick={() => handleSelectImage(image.path)} | ||
| disabled={disabled} | ||
| className="aspect-square border rounded-lg overflow-hidden hover:ring-2 hover:ring-primary disabled:opacity-50 disabled:cursor-not-allowed transition-all relative" | ||
| title={image.name} | ||
| > | ||
| <img | ||
| src={getAssetUrl(image.path)} | ||
| alt={image.name} | ||
| className="w-full h-full object-cover" | ||
| loading="lazy" | ||
| /> | ||
| <div className="absolute top-1 left-1 bg-black/50 text-white text-xs px-1 rounded"> | ||
| {index + 1} | ||
| </div> | ||
| </button> | ||
| ))} | ||
|
|
||
| {images.length === 0 && ( | ||
| <div className="col-span-2 text-center py-8 text-muted-foreground text-sm"> | ||
| No images found. Upload an image to get started. | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| <p className="text-xs text-muted-foreground mt-4"> | ||
| {images.length > 0 | ||
| ? `${images.length} images available, sorted by most recent` | ||
| : "No images available"} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.