mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-18 23:55:16 +00:00
More edit page changes + fix eraser
This commit is contained in:
1 parent
4b8e8c126b
commit
23cdf65fd7
8 files changed
+191
-536
No files matched your search
@@ -19,6 +19,7 @@ export type LineNode = {
|
||||
offsetY?: number;
|
||||
zIndex: number;
|
||||
locked?: boolean; // Add locked property
|
||||
globalCompositeOperation?: string; // Add globalCompositeOperation property
|
||||
};
|
||||
|
||||
// Add this enum at the top of the file with other types
|
||||
|
||||
@@ -13,8 +13,7 @@ import Konva from "konva"; // Import Konva namespace for types
|
||||
|
||||
// https://github.com/SaladTechnologies/comfyui-api
|
||||
|
||||
import { useEditStore } from "./stores/EditState";
|
||||
import { MiraiProps } from "../PageDraw/PaintSurface";
|
||||
import { ActiveEditTool, useEditStore } from "./stores/EditState";
|
||||
import SplitPane from "../PageDraw/components/ui/SplitPane";
|
||||
import { useStageCentering } from "../PageDraw/hooks/useCenteredStage";
|
||||
import { useGlobalMouseUp } from "../PageDraw/hooks/useGlobalMouseUp";
|
||||
@@ -22,6 +21,19 @@ import { useRightPanelLayoutManagement } from "../PageDraw/hooks/useRightPanelLa
|
||||
import { LineNode } from "../PageDraw/stores/SceneState";
|
||||
import { Node } from "../PageDraw/Node";
|
||||
|
||||
export type EditPaintSurfaceProps = {
|
||||
nodes: Node[];
|
||||
selectedNodeIds: string[];
|
||||
onCanvasSizeChange?: (width: number, height: number) => void;
|
||||
fillColor?: string;
|
||||
activeTool?: ActiveEditTool;
|
||||
brushColor?: string;
|
||||
brushSize?: number;
|
||||
onSelectionChange?: (isSelecting: boolean) => void;
|
||||
stageRef: React.RefObject<Konva.Stage>;
|
||||
transformerRefs: React.RefObject<{ [key: string]: Konva.Transformer }>;
|
||||
};
|
||||
|
||||
export const EditPaintSurface = ({
|
||||
nodes,
|
||||
selectedNodeIds,
|
||||
@@ -33,7 +45,7 @@ export const EditPaintSurface = ({
|
||||
onSelectionChange,
|
||||
stageRef,
|
||||
transformerRefs,
|
||||
}: MiraiProps) => {
|
||||
}: EditPaintSurfaceProps) => {
|
||||
// switch off to be preview panel mode.
|
||||
const singlePaneMode = true
|
||||
|
||||
@@ -138,7 +150,7 @@ export const EditPaintSurface = ({
|
||||
activeTool: string,
|
||||
nodeDraggable: boolean,
|
||||
): boolean => {
|
||||
return activeTool !== "draw" && activeTool !== "eraser" && nodeDraggable;
|
||||
return activeTool !== "edit" && nodeDraggable;
|
||||
};
|
||||
|
||||
// Combined mouse handlers
|
||||
@@ -169,21 +181,25 @@ export const EditPaintSurface = ({
|
||||
|
||||
// Handle drawing tools - only start if within bounds
|
||||
if (
|
||||
(activeTool === "draw" || activeTool === "eraser") &&
|
||||
(activeTool === "edit") &&
|
||||
isWithinLeftPanel(stagePoint)
|
||||
) {
|
||||
const lineId = `line-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const opacity = activeTool === "draw" ? store.brushOpacity : 1;
|
||||
const opacity = activeTool === "edit" ? store.brushOpacity : 1;
|
||||
const composite = store.editOperation === "add" ? "source-over" : "destination-out";
|
||||
const strokeColor = store.editOperation === "add" ? brushColor : "#FFFFFF"; // Eraser uses transparent stroke
|
||||
|
||||
const newLineNode: LineNode = {
|
||||
id: lineId,
|
||||
type: "line",
|
||||
points: [stagePoint.x, stagePoint.y],
|
||||
stroke: activeTool === "draw" ? brushColor : fillColor || "#ffffff",
|
||||
stroke: strokeColor,
|
||||
strokeWidth: brushSize / stage.scaleX(),
|
||||
draggable: true,
|
||||
opacity: opacity,
|
||||
locked: false,
|
||||
zIndex: 0, // Default zIndex, can be adjusted later
|
||||
globalCompositeOperation: composite,
|
||||
};
|
||||
store.selectNode(null);
|
||||
store.addLineNode(newLineNode, false); // Don't save state when starting line
|
||||
@@ -227,7 +243,7 @@ export const EditPaintSurface = ({
|
||||
if (!stage) return;
|
||||
|
||||
// Update cursor position for draw/eraser tools
|
||||
if (activeTool === "draw" || activeTool === "eraser") {
|
||||
if (activeTool === "edit") {
|
||||
const pointer = stage.getPointerPosition();
|
||||
if (pointer) {
|
||||
store.setCursorPosition(pointer);
|
||||
@@ -253,7 +269,7 @@ export const EditPaintSurface = ({
|
||||
if (
|
||||
isDrawing &&
|
||||
currentLineId &&
|
||||
(activeTool === "draw" || activeTool === "eraser")
|
||||
(activeTool === "edit")
|
||||
) {
|
||||
const point = stage.getPointerPosition();
|
||||
if (!point) return;
|
||||
@@ -435,7 +451,7 @@ export const EditPaintSurface = ({
|
||||
const container = e.target.getStage()?.container();
|
||||
if (!container) return;
|
||||
const defaultCursor =
|
||||
activeTool === "draw" || activeTool === "eraser"
|
||||
activeTool === "edit"
|
||||
? "none"
|
||||
: activeTool === "select"
|
||||
? "grab"
|
||||
@@ -451,7 +467,7 @@ export const EditPaintSurface = ({
|
||||
return;
|
||||
}
|
||||
|
||||
if (activeTool === "draw" || activeTool === "eraser") {
|
||||
if (activeTool === "edit") {
|
||||
stage.container().style.cursor = "none";
|
||||
const pointer = stage.getPointerPosition();
|
||||
if (pointer) {
|
||||
@@ -483,7 +499,7 @@ export const EditPaintSurface = ({
|
||||
if (
|
||||
store.cursorVisible &&
|
||||
store.cursorPosition &&
|
||||
(activeTool === "draw" || activeTool === "eraser")
|
||||
(activeTool === "edit")
|
||||
) {
|
||||
// Counteract stage transforms to position cursor in screen space
|
||||
const stageX = stage.x();
|
||||
@@ -500,7 +516,7 @@ export const EditPaintSurface = ({
|
||||
cursorNode.position(store.cursorPosition);
|
||||
cursorNode.radius(brushSize / 2);
|
||||
|
||||
if (activeTool === "draw") {
|
||||
if (store.editOperation === "add") {
|
||||
cursorNode.fill(brushColor);
|
||||
cursorNode.stroke("rgba(255, 255, 255, 0.7)");
|
||||
cursorNode.strokeWidth(1);
|
||||
@@ -513,13 +529,7 @@ export const EditPaintSurface = ({
|
||||
cursorNode.visible(false);
|
||||
}
|
||||
cursorLayer.batchDraw();
|
||||
}, [
|
||||
store.cursorVisible,
|
||||
store.cursorPosition,
|
||||
activeTool,
|
||||
brushColor,
|
||||
brushSize,
|
||||
]);
|
||||
}, [store.cursorVisible, store.cursorPosition, activeTool, brushColor, brushSize, stageRef, store.editOperation]);
|
||||
|
||||
const renderNode = (node: Node | LineNode) => {
|
||||
// Node Callbacks
|
||||
@@ -528,7 +538,7 @@ export const EditPaintSurface = ({
|
||||
nodeId: string,
|
||||
) => {
|
||||
// Don't select nodes when draw or erase tools are active
|
||||
if (activeTool === "draw" || activeTool === "eraser") {
|
||||
if (activeTool === "edit") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -781,6 +791,8 @@ export const EditPaintSurface = ({
|
||||
offsetX={lineNode.offsetX || 0}
|
||||
offsetY={lineNode.offsetY || 0}
|
||||
zIndex={lineNode.zIndex}
|
||||
// @ts-expect-error: We don't have globalCompositeOperation in the type definition
|
||||
globalCompositeOperation={lineNode.globalCompositeOperation || "source-over"}
|
||||
/>
|
||||
{renderTransformer()}
|
||||
</React.Fragment>
|
||||
@@ -993,9 +1005,7 @@ export const EditPaintSurface = ({
|
||||
onMouseEnter={handleStageMouseEnter}
|
||||
onMouseLeave={handleStageMouseLeave}
|
||||
>
|
||||
{/* Left Panel */}
|
||||
<Layer
|
||||
ref={leftPanelRef}
|
||||
clipFunc={(ctx) => {
|
||||
ctx.rect(0, 0, store.getAspectRatioDimensions().width, store.getAspectRatioDimensions().height); // leftPanelWidth, leftPanelHeight);
|
||||
}}
|
||||
@@ -1009,7 +1019,14 @@ export const EditPaintSurface = ({
|
||||
listening={false}
|
||||
zIndex={-1}
|
||||
/>
|
||||
|
||||
</Layer>
|
||||
{/* Left Panel */}
|
||||
<Layer
|
||||
ref={leftPanelRef}
|
||||
clipFunc={(ctx) => {
|
||||
ctx.rect(0, 0, store.getAspectRatioDimensions().width, store.getAspectRatioDimensions().height); // leftPanelWidth, leftPanelHeight);
|
||||
}}
|
||||
>
|
||||
{/* Render all nodes including line nodes */}
|
||||
{[...nodes, ...store.lineNodes]
|
||||
.sort((a, b) => (a.zIndex || 0) - (b.zIndex || 0))
|
||||
|
||||
@@ -16,9 +16,9 @@ import { useCopyPasteHotkeys } from "../PageDraw/hooks/useCopyPasteHotkeys";
|
||||
import { useDeleteHotkeys } from "../PageDraw/hooks/useDeleteHotkeys";
|
||||
import { useUndoRedoHotkeys } from "../PageDraw/hooks/useUndoRedoHotkeys";
|
||||
import { captureStageImageBitmap } from "../PageDraw/hooks/useUpdateSnapshot";
|
||||
import PromptEditor from "../PageDraw/PromptEditor/PromptEditor";
|
||||
import PromptEditor from "./PromptEditor/PromptEditor";
|
||||
import { AspectRatioType } from "../PageDraw/stores/SceneState";
|
||||
import { useEditStore } from "./stores/EditState";
|
||||
import { ActiveEditTool, useEditStore } from "./stores/EditState";
|
||||
import { EditPaintSurface } from "./EditPaintSurface";
|
||||
|
||||
const PageEdit = () => {
|
||||
@@ -202,26 +202,8 @@ const PageEdit = () => {
|
||||
console.log("Vary clicked");
|
||||
// Handle vary action here
|
||||
}}
|
||||
onAspectRatioChange={(ratio: string) => {
|
||||
console.log("Aspect ratio:", ratio);
|
||||
// Convert ratio string to AspectRatioType enum
|
||||
const ratioToType = (ratio: string): AspectRatioType => {
|
||||
switch (ratio) {
|
||||
case "2:3":
|
||||
return AspectRatioType.PORTRAIT;
|
||||
case "3:2":
|
||||
return AspectRatioType.LANDSCAPE;
|
||||
case "1:1":
|
||||
return AspectRatioType.SQUARE;
|
||||
default:
|
||||
return AspectRatioType.NONE;
|
||||
}
|
||||
};
|
||||
|
||||
const aspectRatioType = ratioToType(ratio);
|
||||
store.setAspectRatioType(aspectRatioType);
|
||||
onFitPressed()
|
||||
}}
|
||||
onModeChange={(mode: string) => { store.setActiveTool(mode as ActiveEditTool) }}
|
||||
selectedMode={store.activeTool}
|
||||
onEnqueuePressed={onEnqueuedPressed}
|
||||
/>
|
||||
</div>
|
||||
@@ -231,26 +213,16 @@ const PageEdit = () => {
|
||||
store.setActiveTool("select");
|
||||
}}
|
||||
onAddShape={(shape: "rectangle" | "circle" | "triangle"): void => {
|
||||
// Calculate center position based on canvas dimensions
|
||||
const centerX = canvasWidth.current / 3;
|
||||
const centerY = canvasHeight.current / 3;
|
||||
if (shape === "rectangle") {
|
||||
store.createRectangle(centerX, centerY);
|
||||
} else if (shape === "circle") {
|
||||
store.createCircle(centerX, centerY);
|
||||
} else if (shape === "triangle") {
|
||||
store.createTriangle(centerX, centerY);
|
||||
}
|
||||
}}
|
||||
onPaintBrush={(hex: string, size: number, opacity: number): void => {
|
||||
store.setActiveTool("draw");
|
||||
store.setBrushColor(hex);
|
||||
store.setBrushSize(size);
|
||||
store.setBrushOpacity(opacity);
|
||||
// store.setActiveTool("draw");
|
||||
// store.setBrushColor(hex);
|
||||
// store.setBrushSize(size);
|
||||
// store.setBrushOpacity(opacity);
|
||||
}}
|
||||
onEraser={(size: number): void => {
|
||||
store.setActiveTool("eraser");
|
||||
store.setBrushSize(size);
|
||||
// store.setActiveTool("eraser");
|
||||
// store.setBrushSize(size);
|
||||
}}
|
||||
onCanvasBackground={(hex: string): void => {
|
||||
console.log("Canvas background activated", { color: hex });
|
||||
@@ -327,48 +299,6 @@ const PageEdit = () => {
|
||||
}}
|
||||
onMenuAction={async (action) => {
|
||||
switch (action) {
|
||||
case 'LOCK':
|
||||
store.toggleLock(store.selectedNodeIds);
|
||||
break;
|
||||
case 'REMOVE_BACKGROUND':
|
||||
// returns a success if we have selected images only
|
||||
await store.removeBackground(store.selectedNodeIds,
|
||||
async (success: boolean, image_base64: string, message: string) => {
|
||||
if (!success) {
|
||||
console.error(message);
|
||||
return { success: false };
|
||||
}
|
||||
try {
|
||||
const response = await FalBackgroundRemoval({ base64_image: image_base64 });
|
||||
if (response.status !== "success" || !("payload" in response)) {
|
||||
console.error("Failed to remove background", response);
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
const base64String = response.payload?.base64_bytes as string;
|
||||
const binaryString = atob(base64String);
|
||||
const bytes = Uint8Array.from(binaryString, c => c.charCodeAt(0));
|
||||
const blob = new Blob([bytes], { type: "image/png" });
|
||||
const file = new File([blob], "generated_image.png", { type: blob.type });
|
||||
return { success: true, file };
|
||||
} catch (error) {
|
||||
console.error("Failed to remove background", error);
|
||||
return { success: false };
|
||||
}
|
||||
});
|
||||
break;
|
||||
case 'BRING_TO_FRONT':
|
||||
store.bringToFront(store.selectedNodeIds);
|
||||
break;
|
||||
case 'BRING_FORWARD':
|
||||
store.bringForward(store.selectedNodeIds);
|
||||
break;
|
||||
case 'SEND_BACKWARD':
|
||||
store.sendBackward(store.selectedNodeIds);
|
||||
break;
|
||||
case 'SEND_TO_BACK':
|
||||
store.sendToBack(store.selectedNodeIds);
|
||||
break;
|
||||
case 'DUPLICATE':
|
||||
store.copySelectedItems()
|
||||
store.pasteItems()
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import React from "react";
|
||||
import { PromptEditorProps } from "./types";
|
||||
import { PromptBoxEdit } from "@storyteller/ui-promptbox";
|
||||
import { uploadImage } from "../../../components/reusable/UploadModalMedia/uploadImage";
|
||||
|
||||
// Set this value on when enqueue is pressed nasty global variable.
|
||||
import { getCanvasRenderBitmap } from "../../../signals/canvasRenderBitmap"
|
||||
import { JobProvider, useJobContext } from "~/pages/PageDraw/JobContext";
|
||||
import { EncodeImageBitmapToBase64 } from "~/pages/PageDraw/utilities/EncodeImageBitmapToBase64";
|
||||
const PromptEditor: React.FC<PromptEditorProps> = ({
|
||||
onEnqueuePressed,
|
||||
onModeChange,
|
||||
selectedMode,
|
||||
}) => {
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-3xl flex-col space-y-2">
|
||||
<div className="flex w-full justify-center">
|
||||
</div>
|
||||
|
||||
<JobProvider>
|
||||
<PromptBoxEdit
|
||||
uploadImage={uploadImage}
|
||||
getCanvasRenderBitmap={getCanvasRenderBitmap}
|
||||
EncodeImageBitmapToBase64={EncodeImageBitmapToBase64}
|
||||
useJobContext={useJobContext}
|
||||
onEnqueuePressed={onEnqueuePressed}
|
||||
onModeSelectionChange={onModeChange}
|
||||
selectedMode={selectedMode}
|
||||
/>
|
||||
</JobProvider>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default PromptEditor;
|
||||
@@ -0,0 +1,55 @@
|
||||
export interface PromptEditorProps {
|
||||
initialPrompt?: string;
|
||||
onPromptChange?: (prompt: string) => void;
|
||||
onRandomize?: () => void;
|
||||
onVary?: () => void;
|
||||
onAIStrengthChange?: (strength: number) => void;
|
||||
onImageStyleChange?: (images: ImageStyle[]) => void;
|
||||
onEnqueuePressed?: () => void;
|
||||
onModeChange?: (mode: string) => void;
|
||||
selectedMode?: string;
|
||||
}
|
||||
|
||||
export type AspectRatio = '1:1' | '3:2' | '2:3';
|
||||
|
||||
export interface ImageStyle {
|
||||
id: string;
|
||||
url: string;
|
||||
weight: number;
|
||||
}
|
||||
|
||||
export interface PromptTextAreaProps {
|
||||
value: string;
|
||||
onChange: (value: string) => void;
|
||||
images: ImageStyle[];
|
||||
onImageUpdate: (id: string, updates: Partial<ImageStyle>) => void;
|
||||
onImageRemove: (id: string) => void;
|
||||
}
|
||||
|
||||
export interface AIStrengthSliderProps {
|
||||
value: number;
|
||||
onChange: (value: number) => void;
|
||||
width?: string | number;
|
||||
height?: string | number;
|
||||
inset?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export interface ControlButtonsProps {
|
||||
aspectRatio: AspectRatio;
|
||||
onAspectRatioChange: (ratio: AspectRatio) => void;
|
||||
onStyleClick: () => void;
|
||||
onImageStyleClick: () => void;
|
||||
onRandomizeClick: () => void;
|
||||
onVaryClick: () => void;
|
||||
}
|
||||
|
||||
export interface ImageStyleSelectorProps {
|
||||
onImageSelect: (image: File) => void;
|
||||
}
|
||||
|
||||
export interface ImagePreviewProps {
|
||||
image: ImageStyle;
|
||||
onUpdate: (id: string, updates: Partial<ImageStyle>) => void;
|
||||
onRemove: (id: string) => void;
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import { create } from 'zustand';
|
||||
import { LineNode, ActiveTool, AspectRatioType, convertFileToBase64, generateId } from '~/pages/PageDraw/stores/SceneState';
|
||||
import { SceneState } from '~/pages/PageEnigma/Editor/Commands';
|
||||
import { LineNode, AspectRatioType, generateId } from '~/pages/PageDraw/stores/SceneState';
|
||||
import { Node } from "~/pages/PageDraw/Node";
|
||||
|
||||
export type ActiveEditTool = 'select' | 'edit' | 'expand';
|
||||
export type EditOperation = 'add' | 'minus';
|
||||
|
||||
interface EditState {
|
||||
// Nodes
|
||||
nodes: Node[];
|
||||
@@ -13,7 +15,8 @@ interface EditState {
|
||||
clipboard: (Node | LineNode)[]; // To store copied items
|
||||
|
||||
// Toolbar state
|
||||
activeTool: ActiveTool;
|
||||
activeTool: ActiveEditTool;
|
||||
editOperation: EditOperation;
|
||||
brushColor: string;
|
||||
brushSize: number;
|
||||
brushOpacity: number;
|
||||
@@ -33,12 +36,6 @@ interface EditState {
|
||||
// Batch operations
|
||||
setNodes: (nodes: Node[]) => void;
|
||||
|
||||
// Node creation helpers
|
||||
createRectangle: (x: number, y: number, width?: number, height?: number, fill?: string) => void;
|
||||
createCircle: (x: number, y: number, radius?: number, fill?: string) => void;
|
||||
createTriangle: (x: number, y: number, width?: number, height?: number, fill?: string) => void;
|
||||
createImage: (x: number, y: number, source: string | File, width?: number, height?: number) => void;
|
||||
|
||||
// History management
|
||||
history: { nodes: Node[]; lineNodes: LineNode[] }[];
|
||||
historyIndex: number;
|
||||
@@ -66,7 +63,7 @@ interface EditState {
|
||||
pasteItems: () => void;
|
||||
|
||||
// Toolbar actions
|
||||
setActiveTool: (tool: ActiveTool) => void;
|
||||
setActiveTool: (tool: ActiveEditTool) => void;
|
||||
setBrushColor: (color: string) => void;
|
||||
setBrushOpacity: (opacity: number) => void;
|
||||
setBrushSize: (size: number) => void;
|
||||
@@ -86,16 +83,6 @@ interface EditState {
|
||||
serializeSceneToString: () => Promise<string>;
|
||||
loadSceneFromString: (jsonString: string) => boolean;
|
||||
|
||||
// New functions
|
||||
bringToFront: (nodeIds: string[]) => void;
|
||||
sendToBack: (nodeIds: string[]) => void;
|
||||
bringForward: (nodeIds: string[]) => void;
|
||||
sendBackward: (nodeIds: string[]) => void;
|
||||
removeBackground: (nodeIds: string[], operation: (success: boolean, base64_image: string, message: string) => Promise<{ success: boolean; file?: File }>) => Promise<void>;
|
||||
|
||||
// Add new lock action
|
||||
toggleLock: (nodeIds: string[]) => void;
|
||||
|
||||
// Add minimal aspect ratio property
|
||||
aspectRatioType: AspectRatioType;
|
||||
|
||||
@@ -133,11 +120,12 @@ export const useEditStore = create<EditState>((set, get) => ({
|
||||
historyIndex: -1,
|
||||
|
||||
// Toolbar initial state
|
||||
activeTool: 'select',
|
||||
activeTool: 'select', // Default to 'select' tool
|
||||
editOperation: 'add', // Default to 'add' operation
|
||||
brushColor: '#000000',
|
||||
brushSize: 5,
|
||||
brushSize: 30,
|
||||
brushOpacity: 1,
|
||||
fillColor: 'white',
|
||||
fillColor: 'rgba(0, 0, 255, 0.5)',
|
||||
|
||||
// Cursor initial state
|
||||
cursorPosition: null,
|
||||
@@ -263,58 +251,6 @@ export const useEditStore = create<EditState>((set, get) => ({
|
||||
get().saveState();
|
||||
},
|
||||
|
||||
// Node creation helpers
|
||||
createRectangle: (x: number, y: number, width = 100, height = 100, fill = '#e0e0e0') => {
|
||||
const node = new Node({
|
||||
id: generateId(),
|
||||
type: 'rectangle',
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
fill,
|
||||
stroke: '#444',
|
||||
strokeWidth: 2,
|
||||
draggable: true
|
||||
});
|
||||
get().addNode(node);
|
||||
get().setActiveTool('select'); // Switch to select tool after creating
|
||||
},
|
||||
|
||||
createCircle: (x: number, y: number, radius = 50, fill = '#f0f0f0') => {
|
||||
const node = new Node({
|
||||
id: generateId(),
|
||||
type: 'circle',
|
||||
x,
|
||||
y,
|
||||
width: radius * 2,
|
||||
height: radius * 2,
|
||||
fill,
|
||||
stroke: '#333',
|
||||
strokeWidth: 2,
|
||||
draggable: true
|
||||
});
|
||||
get().addNode(node);
|
||||
get().setActiveTool('select'); // Switch to select tool after creating
|
||||
},
|
||||
|
||||
createTriangle: (x: number, y: number, width = 100, height = 100, fill = '#d0d0d0') => {
|
||||
const node = new Node({
|
||||
id: generateId(),
|
||||
type: 'triangle',
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
fill,
|
||||
stroke: '#555',
|
||||
strokeWidth: 2,
|
||||
draggable: true
|
||||
});
|
||||
get().addNode(node);
|
||||
get().setActiveTool('select'); // Switch to select tool after creating
|
||||
},
|
||||
|
||||
// History management
|
||||
saveState: () => {
|
||||
// Don't save state if we're in the middle of restoring
|
||||
@@ -351,7 +287,8 @@ export const useEditStore = create<EditState>((set, get) => ({
|
||||
|
||||
return {
|
||||
history: newHistory,
|
||||
historyIndex: newHistory.length - 1
|
||||
historyIndex: newHistory.length - 1,
|
||||
editOperation: state.editOperation === "add" ? "minus" : "add",
|
||||
};
|
||||
});
|
||||
},
|
||||
@@ -750,7 +687,7 @@ export const useEditStore = create<EditState>((set, get) => ({
|
||||
},
|
||||
|
||||
// Toolbar actions
|
||||
setActiveTool: (tool: ActiveTool) => set({ activeTool: tool }),
|
||||
setActiveTool: (tool: ActiveEditTool) => set({ activeTool: tool }),
|
||||
setBrushColor: (color: string) => set({ brushColor: color }),
|
||||
setBrushSize: (size: number) => set({ brushSize: size }),
|
||||
setFillColor: (color: string) => set({ fillColor: color }),
|
||||
@@ -923,273 +860,6 @@ export const useEditStore = create<EditState>((set, get) => ({
|
||||
return get().importSceneFromJson(jsonString);
|
||||
},
|
||||
|
||||
// New functions
|
||||
bringToFront: (nodeIds: string[]) => {
|
||||
set(state => {
|
||||
// Get highest zIndex from all nodes and line nodes
|
||||
const allZIndices = [
|
||||
...state.nodes.map(n => n.zIndex || 0),
|
||||
...state.lineNodes.map(n => n.zIndex || 0)
|
||||
];
|
||||
const maxZ = Math.max(...allZIndices, 0) + 1;
|
||||
|
||||
return {
|
||||
nodes: state.nodes.map(node =>
|
||||
nodeIds.includes(node.id)
|
||||
? new Node({ ...node, zIndex: maxZ })
|
||||
: node
|
||||
),
|
||||
lineNodes: state.lineNodes.map(node =>
|
||||
nodeIds.includes(node.id)
|
||||
? { ...node, zIndex: maxZ }
|
||||
: node
|
||||
)
|
||||
};
|
||||
});
|
||||
get().saveState();
|
||||
},
|
||||
|
||||
sendToBack: (nodeIds: string[]) => {
|
||||
set(state => {
|
||||
// Get lowest zIndex from all nodes and line nodes
|
||||
const allZIndices = [
|
||||
...state.nodes.map(n => n.zIndex || 0),
|
||||
...state.lineNodes.map(n => n.zIndex || 0)
|
||||
];
|
||||
const minZ = Math.min(...allZIndices, 0) - 1;
|
||||
|
||||
return {
|
||||
nodes: state.nodes.map(node =>
|
||||
nodeIds.includes(node.id)
|
||||
? new Node({ ...node, zIndex: minZ })
|
||||
: node
|
||||
),
|
||||
lineNodes: state.lineNodes.map(node =>
|
||||
nodeIds.includes(node.id)
|
||||
? { ...node, zIndex: minZ }
|
||||
: node
|
||||
)
|
||||
};
|
||||
});
|
||||
get().saveState();
|
||||
},
|
||||
|
||||
bringForward: (nodeIds: string[]) => {
|
||||
set(state => {
|
||||
// Combine all items with their current zIndex
|
||||
const allItems = [
|
||||
...state.nodes.map(n => ({ id: n.id, z: Math.max(0, n.zIndex || 0), selected: nodeIds.includes(n.id) })),
|
||||
...state.lineNodes.map(n => ({ id: n.id, z: Math.max(0, n.zIndex || 0), selected: nodeIds.includes(n.id) }))
|
||||
].sort((a, b) => a.z - b.z);
|
||||
|
||||
// Find the highest z-index among selected items
|
||||
const selectedItems = allItems.filter(item => item.selected);
|
||||
if (selectedItems.length === 0) return state;
|
||||
|
||||
const highestSelectedZ = Math.max(...selectedItems.map(item => item.z));
|
||||
|
||||
// Find the closest item above our selection
|
||||
const nextItem = allItems.find(item => !item.selected && item.z > highestSelectedZ);
|
||||
|
||||
// If no item above, swap with the highest non-selected item
|
||||
if (!nextItem) {
|
||||
const highestNonSelected = allItems.filter(item => !item.selected)
|
||||
.sort((a, b) => b.z - a.z)[0];
|
||||
if (!highestNonSelected) return state;
|
||||
|
||||
// Swap positions
|
||||
return {
|
||||
nodes: state.nodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return new Node({ ...node, zIndex: highestNonSelected.z });
|
||||
}
|
||||
if (node.id === highestNonSelected.id) {
|
||||
return new Node({ ...node, zIndex: highestSelectedZ });
|
||||
}
|
||||
return node;
|
||||
}),
|
||||
lineNodes: state.lineNodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return { ...node, zIndex: highestNonSelected.z };
|
||||
}
|
||||
if (node.id === highestNonSelected.id) {
|
||||
return { ...node, zIndex: highestSelectedZ };
|
||||
}
|
||||
return node;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
const newZ = nextItem.z;
|
||||
return {
|
||||
nodes: state.nodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return new Node({ ...node, zIndex: newZ });
|
||||
}
|
||||
if (node.id === nextItem.id) {
|
||||
return new Node({ ...node, zIndex: highestSelectedZ });
|
||||
}
|
||||
return node;
|
||||
}),
|
||||
lineNodes: state.lineNodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return { ...node, zIndex: newZ };
|
||||
}
|
||||
if (node.id === nextItem.id) {
|
||||
return { ...node, zIndex: highestSelectedZ };
|
||||
}
|
||||
return node;
|
||||
})
|
||||
};
|
||||
});
|
||||
get().saveState();
|
||||
},
|
||||
|
||||
sendBackward: (nodeIds: string[]) => {
|
||||
set(state => {
|
||||
// Combine all items with their current zIndex
|
||||
const allItems = [
|
||||
...state.nodes.map(n => ({ id: n.id, z: Math.max(0, n.zIndex || 0), selected: nodeIds.includes(n.id) })),
|
||||
...state.lineNodes.map(n => ({ id: n.id, z: Math.max(0, n.zIndex || 0), selected: nodeIds.includes(n.id) }))
|
||||
].sort((a, b) => b.z - a.z);
|
||||
|
||||
// Find the lowest z-index among selected items
|
||||
const selectedItems = allItems.filter(item => item.selected);
|
||||
if (selectedItems.length === 0) return state;
|
||||
|
||||
const lowestSelectedZ = Math.min(...selectedItems.map(item => item.z));
|
||||
|
||||
// Find the closest item below our selection
|
||||
const nextItem = allItems.find(item => !item.selected && item.z < lowestSelectedZ);
|
||||
|
||||
// If no item below, swap with the lowest non-selected item
|
||||
if (!nextItem) {
|
||||
const lowestNonSelected = allItems.filter(item => !item.selected)
|
||||
.sort((a, b) => a.z - b.z)[0];
|
||||
if (!lowestNonSelected) return state;
|
||||
|
||||
// Swap positions
|
||||
return {
|
||||
nodes: state.nodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return new Node({ ...node, zIndex: lowestNonSelected.z });
|
||||
}
|
||||
if (node.id === lowestNonSelected.id) {
|
||||
return new Node({ ...node, zIndex: lowestSelectedZ });
|
||||
}
|
||||
return node;
|
||||
}),
|
||||
lineNodes: state.lineNodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return { ...node, zIndex: lowestNonSelected.z };
|
||||
}
|
||||
if (node.id === lowestNonSelected.id) {
|
||||
return { ...node, zIndex: lowestSelectedZ };
|
||||
}
|
||||
return node;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
const newZ = nextItem.z;
|
||||
return {
|
||||
nodes: state.nodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return new Node({ ...node, zIndex: newZ });
|
||||
}
|
||||
if (node.id === nextItem.id) {
|
||||
return new Node({ ...node, zIndex: lowestSelectedZ });
|
||||
}
|
||||
return node;
|
||||
}),
|
||||
lineNodes: state.lineNodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return { ...node, zIndex: newZ };
|
||||
}
|
||||
if (node.id === nextItem.id) {
|
||||
return { ...node, zIndex: lowestSelectedZ };
|
||||
}
|
||||
return node;
|
||||
})
|
||||
};
|
||||
});
|
||||
get().saveState();
|
||||
},
|
||||
removeBackground: async (nodeIds: string[], operation: (success: boolean, base64_image: string, message: string) => Promise<{ success: boolean; file?: File }>) => {
|
||||
const hasImageNodes = nodeIds.some(id => {
|
||||
const node = get().nodes.find(n => n.id === id);
|
||||
return node ? node.type === 'image' : false;
|
||||
});
|
||||
|
||||
if (hasImageNodes) {
|
||||
const firstNode = get().nodes.find(node => nodeIds.includes(node.id) && node.type === 'image');
|
||||
if (firstNode && firstNode.imageFile) {
|
||||
try {
|
||||
const base64Image = await convertFileToBase64(firstNode.imageFile);
|
||||
const { success, file } = await operation(true, base64Image, "Removing Background.");
|
||||
|
||||
if (success && file) {
|
||||
// Create a new node instance
|
||||
const updatedNode = new Node({
|
||||
...firstNode,
|
||||
imageFile: file
|
||||
});
|
||||
|
||||
// Load the new image
|
||||
await updatedNode.setImageFromFile(file);
|
||||
|
||||
// Update the store with the fully loaded node
|
||||
set((state: SceneState) => ({
|
||||
nodes: state.nodes.map((node: Node) => {
|
||||
if (node.id === firstNode.id) {
|
||||
return updatedNode;
|
||||
}
|
||||
return node;
|
||||
}),
|
||||
}));
|
||||
|
||||
get().saveState();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error updating image:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// Add toggleLock action
|
||||
toggleLock: (nodeIds: string[]) => {
|
||||
set((state) => {
|
||||
// Update regular nodes
|
||||
const updatedNodes = state.nodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return new Node({
|
||||
...node,
|
||||
locked: !node.locked
|
||||
});
|
||||
}
|
||||
return node;
|
||||
});
|
||||
|
||||
// Update line nodes
|
||||
const updatedLineNodes = state.lineNodes.map(node => {
|
||||
if (nodeIds.includes(node.id)) {
|
||||
return {
|
||||
...node,
|
||||
locked: !node.locked
|
||||
};
|
||||
}
|
||||
return node;
|
||||
});
|
||||
|
||||
return {
|
||||
nodes: updatedNodes,
|
||||
lineNodes: updatedLineNodes
|
||||
};
|
||||
});
|
||||
get().saveState();
|
||||
},
|
||||
|
||||
// Add aspect ratio actions
|
||||
setAspectRatioType: (type: AspectRatioType) => {
|
||||
set({ aspectRatioType: type });
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./lib/PromptBox2D";
|
||||
export * from "./lib/PromptBox3D";
|
||||
export * from "./lib/PromptBoxEdit";
|
||||
export * from "./lib/PromptBoxVideo";
|
||||
export * from "./lib/PromptBoxImage";
|
||||
@@ -21,6 +21,9 @@ import {
|
||||
faPlus,
|
||||
faUpload,
|
||||
faImages,
|
||||
faMousePointer,
|
||||
faEdit,
|
||||
faExpand,
|
||||
} from "@fortawesome/pro-solid-svg-icons";
|
||||
import {
|
||||
faRectangleVertical,
|
||||
@@ -41,6 +44,7 @@ import {
|
||||
|
||||
import { showActionReminder } from "@storyteller/ui-action-reminder-modal";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { ButtonIconSelect } from "@storyteller/ui-button-icon-select";
|
||||
|
||||
interface ReferenceImage {
|
||||
id: string;
|
||||
@@ -63,7 +67,8 @@ interface PromptBoxEditProps {
|
||||
EncodeImageBitmapToBase64: (imageBitmap: ImageBitmap) => Promise<string>;
|
||||
useJobContext: () => JobContextType;
|
||||
onEnqueuePressed?: () => void | Promise<void>;
|
||||
onAspectRatioChange?: (ratio: AspectRatio) => void;
|
||||
onModeSelectionChange?: (mode: string) => void;
|
||||
selectedMode?: string;
|
||||
}
|
||||
|
||||
export const PromptBoxEdit = ({
|
||||
@@ -72,7 +77,8 @@ export const PromptBoxEdit = ({
|
||||
EncodeImageBitmapToBase64,
|
||||
useJobContext,
|
||||
onEnqueuePressed,
|
||||
onAspectRatioChange,
|
||||
onModeSelectionChange,
|
||||
selectedMode,
|
||||
}: PromptBoxEditProps) => {
|
||||
useSignals();
|
||||
|
||||
@@ -91,23 +97,6 @@ export const PromptBoxEdit = ({
|
||||
const [uploadingImages, setUploadingImages] = useState<
|
||||
{ id: string; file: File }[]
|
||||
>([]);
|
||||
const [aspectRatioList, setAspectRatioList] = useState<PopoverItem[]>([
|
||||
{
|
||||
label: "3:2",
|
||||
selected: true,
|
||||
icon: <FontAwesomeIcon icon={faRectangle} className="h-4 w-4" />,
|
||||
},
|
||||
{
|
||||
label: "2:3",
|
||||
selected: false,
|
||||
icon: <FontAwesomeIcon icon={faRectangleVertical} className="h-4 w-4" />,
|
||||
},
|
||||
{
|
||||
label: "1:1",
|
||||
selected: false,
|
||||
icon: <FontAwesomeIcon icon={faSquare} className="h-4 w-4" />,
|
||||
},
|
||||
]);
|
||||
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
@@ -121,16 +110,6 @@ export const PromptBoxEdit = ({
|
||||
}
|
||||
});
|
||||
|
||||
const handleAspectRatioSelect = (selectedItem: PopoverItem) => {
|
||||
onAspectRatioChange?.(selectedItem.label);
|
||||
setAspectRatioList((prev) =>
|
||||
prev.map((item) => ({
|
||||
...item,
|
||||
selected: item.label === selectedItem.label,
|
||||
}))
|
||||
);
|
||||
};
|
||||
|
||||
const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const files = event.target.files;
|
||||
if (files) {
|
||||
@@ -428,12 +407,6 @@ export const PromptBoxEdit = ({
|
||||
}
|
||||
};
|
||||
|
||||
const getCurrentAspectRatioIcon = () => {
|
||||
const selected = aspectRatioList.find((item) => item.selected);
|
||||
if (!selected || !selected.icon) return faRectangle;
|
||||
const iconElement = selected.icon as React.ReactElement<any, any>;
|
||||
return iconElement.props.icon;
|
||||
};
|
||||
|
||||
const getCurrentSoraRemixAspectRatio = (): SoraImageRemixAspectRatio => {
|
||||
const selected = aspectRatioList.find((item) => item.selected);
|
||||
@@ -458,6 +431,27 @@ export const PromptBoxEdit = ({
|
||||
}
|
||||
};
|
||||
|
||||
const modes = [
|
||||
{
|
||||
value: "select",
|
||||
icon: faMousePointer,
|
||||
text: "Select",
|
||||
tooltip: "Selection mode",
|
||||
},
|
||||
{
|
||||
value: "edit",
|
||||
icon: faEdit,
|
||||
text: "Edit Region",
|
||||
tooltip: "Edit area for inpainting",
|
||||
},
|
||||
{
|
||||
value: "expand",
|
||||
icon: faExpand,
|
||||
text: "Expand",
|
||||
tooltip: "Expand area for outpainting",
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<>
|
||||
<Modal
|
||||
@@ -516,40 +510,7 @@ export const PromptBoxEdit = ({
|
||||
</div>
|
||||
)}
|
||||
<div className="glass w-[730px] rounded-xl p-4">
|
||||
<input
|
||||
type="file"
|
||||
ref={fileInputRef}
|
||||
className="hidden"
|
||||
accept="image/*"
|
||||
onChange={handleFileUpload}
|
||||
multiple
|
||||
/>
|
||||
<div className="flex justify-center gap-2">
|
||||
<PopoverMenu
|
||||
mode="button"
|
||||
panelTitle="Add Image"
|
||||
items={[
|
||||
{
|
||||
label: "Upload from device",
|
||||
selected: false,
|
||||
icon: <FontAwesomeIcon icon={faUpload} className="h-4 w-4" />,
|
||||
action: "upload",
|
||||
},
|
||||
{
|
||||
label: "Choose from library",
|
||||
selected: false,
|
||||
icon: <FontAwesomeIcon icon={faImages} className="h-4 w-4" />,
|
||||
action: "gallery",
|
||||
},
|
||||
]}
|
||||
onPanelAction={handleAction}
|
||||
showIconsInList
|
||||
buttonClassName="bg-transparent hover:bg-transparent py-1.5 px-0 pr-1 m-0 hover:opacity-50 transition-opacity duration-100 ring-0 border-none focus:ring-0 outline-none"
|
||||
triggerIcon={
|
||||
<FontAwesomeIcon icon={faPlus} className="text-xl" />
|
||||
}
|
||||
/>
|
||||
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
rows={1}
|
||||
@@ -565,26 +526,11 @@ export const PromptBoxEdit = ({
|
||||
</div>
|
||||
<div className="mt-2 flex items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Tooltip
|
||||
content="Aspect ratio"
|
||||
position="top"
|
||||
className="z-50"
|
||||
closeOnClick={true}
|
||||
>
|
||||
<PopoverMenu
|
||||
items={aspectRatioList}
|
||||
onSelect={handleAspectRatioSelect}
|
||||
mode="toggle"
|
||||
panelTitle="Aspect Ratio"
|
||||
showIconsInList
|
||||
triggerIcon={
|
||||
<FontAwesomeIcon
|
||||
icon={getCurrentAspectRatioIcon()}
|
||||
className="h-4 w-4"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</Tooltip>
|
||||
<ButtonIconSelect
|
||||
options={modes}
|
||||
onOptionChange={onModeSelectionChange}
|
||||
selectedOption={selectedMode}
|
||||
/>
|
||||
<Tooltip
|
||||
content={
|
||||
useSystemPrompt
|
||||
|
||||
Reference in new issue
Block a user