feat: enhance canvas functionality with new node types and improved data handling

- Added support for a new "compare" node type to facilitate side-by-side image comparisons.
- Updated AI image and prompt nodes to include aspect ratio handling for better image generation.
- Enhanced canvas toolbar to include export functionality for canvas data.
- Improved data resolution for compare nodes by resolving incoming edges and updating node data accordingly.
- Refactored frame node to support dynamic resizing and exporting capabilities.
- Introduced debounced saving for prompt node to optimize performance during user input.
This commit is contained in:
Matthias
2026-03-25 21:33:22 +01:00
parent fffdae3a9c
commit da6529f263
19 changed files with 1801 additions and 122 deletions

View File

@@ -7,6 +7,7 @@ import { api } from "@/convex/_generated/api";
import type { Id } from "@/convex/_generated/dataModel";
import BaseNodeWrapper from "./base-node-wrapper";
import { DEFAULT_MODEL_ID, getModel } from "@/lib/ai-models";
import { DEFAULT_ASPECT_RATIO } from "@/lib/image-formats";
import { cn, formatEurFromCents } from "@/lib/utils";
import {
Loader2,
@@ -25,6 +26,10 @@ type AiImageNodeData = {
/** Gebuchte Credits in Euro-Cent (PRD: nach Commit) */
creditCost?: number;
canvasId?: string;
/** OpenRouter image_config.aspect_ratio */
aspectRatio?: string;
outputWidth?: number;
outputHeight?: number;
_status?: string;
_statusMessage?: string;
};
@@ -93,6 +98,7 @@ export default function AiImageNode({
prompt,
referenceStorageId,
model: nodeData.model ?? DEFAULT_MODEL_ID,
aspectRatio: nodeData.aspectRatio ?? DEFAULT_ASPECT_RATIO,
});
} catch (err) {
setLocalError(err instanceof Error ? err.message : "Generation failed");
@@ -105,7 +111,10 @@ export default function AiImageNode({
getModel(nodeData.model ?? DEFAULT_MODEL_ID)?.name ?? "AI";
return (
<BaseNodeWrapper selected={selected} className="w-[320px] overflow-hidden">
<BaseNodeWrapper
selected={selected}
className="flex h-full w-full min-h-0 min-w-0 flex-col overflow-hidden"
>
<Handle
type="target"
position={Position.Left}
@@ -113,13 +122,13 @@ export default function AiImageNode({
className="!h-3 !w-3 !bg-violet-500 !border-2 !border-background"
/>
<div className="border-b border-border px-3 py-2">
<div className="shrink-0 border-b border-border px-3 py-2">
<div className="text-xs font-medium text-emerald-600 dark:text-emerald-400">
🖼 AI Image
</div>
</div>
<div className="group relative h-[320px] overflow-hidden bg-muted">
<div className="group relative min-h-0 flex-1 overflow-hidden bg-muted">
{status === "idle" && !nodeData.url && (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-3 text-muted-foreground">
<ImageIcon className="h-10 w-10 opacity-30" />
@@ -209,12 +218,12 @@ export default function AiImageNode({
</div>
{nodeData.prompt && (
<div className="border-t border-border px-3 py-2">
<div className="shrink-0 border-t border-border px-3 py-2">
<p className="line-clamp-2 text-[10px] text-muted-foreground">
{nodeData.prompt}
</p>
<p className="mt-0.5 text-[10px] text-muted-foreground/60">
{modelName}
{modelName} · {nodeData.aspectRatio ?? DEFAULT_ASPECT_RATIO}
</p>
</div>
)}

View File

@@ -1,74 +1,168 @@
"use client";
import { Handle, Position, type NodeProps, type Node } from "@xyflow/react";
import Image from "next/image";
import { useCallback, useRef, useState } from "react";
import { Handle, Position, type NodeProps } from "@xyflow/react";
import { ImageIcon } from "lucide-react";
import BaseNodeWrapper from "./base-node-wrapper";
type CompareNodeData = {
interface CompareNodeData {
leftUrl?: string;
rightUrl?: string;
_status?: string;
};
leftLabel?: string;
rightLabel?: string;
}
export type CompareNode = Node<CompareNodeData, "compare">;
export default function CompareNode({ data, selected }: NodeProps) {
const nodeData = data as CompareNodeData;
const [sliderX, setSliderX] = useState(50);
const containerRef = useRef<HTMLDivElement>(null);
const hasLeft = !!nodeData.leftUrl;
const hasRight = !!nodeData.rightUrl;
const handleMouseDown = useCallback((event: React.MouseEvent) => {
event.stopPropagation();
const move = (moveEvent: MouseEvent) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const x = Math.max(
0,
Math.min(1, (moveEvent.clientX - rect.left) / rect.width),
);
setSliderX(x * 100);
};
const up = () => {
window.removeEventListener("mousemove", move);
window.removeEventListener("mouseup", up);
};
window.addEventListener("mousemove", move);
window.addEventListener("mouseup", up);
}, []);
const handleTouchStart = useCallback((event: React.TouchEvent) => {
event.stopPropagation();
const move = (moveEvent: TouchEvent) => {
if (!containerRef.current || moveEvent.touches.length === 0) return;
const rect = containerRef.current.getBoundingClientRect();
const touch = moveEvent.touches[0];
const x = Math.max(0, Math.min(1, (touch.clientX - rect.left) / rect.width));
setSliderX(x * 100);
};
const end = () => {
window.removeEventListener("touchmove", move);
window.removeEventListener("touchend", end);
};
window.addEventListener("touchmove", move);
window.addEventListener("touchend", end);
}, []);
export default function CompareNode({
data,
selected,
}: NodeProps<CompareNode>) {
return (
<BaseNodeWrapper selected={selected} className="w-[500px] p-2">
<div className="text-xs font-medium text-muted-foreground mb-1">
🔀 Vergleich
</div>
<div className="flex h-40 gap-2">
<div className="relative flex min-h-0 flex-1 overflow-hidden rounded bg-muted">
{data.leftUrl ? (
<Image
src={data.leftUrl}
alt="Vergleich Bild A"
fill
className="object-cover"
sizes="250px"
draggable={false}
/>
) : (
<div className="flex h-full w-full items-center justify-center text-xs text-muted-foreground">
Bild A
</div>
)}
</div>
<div className="relative flex min-h-0 flex-1 overflow-hidden rounded bg-muted">
{data.rightUrl ? (
<Image
src={data.rightUrl}
alt="Vergleich Bild B"
fill
className="object-cover"
sizes="250px"
draggable={false}
/>
) : (
<div className="flex h-full w-full items-center justify-center text-xs text-muted-foreground">
Bild B
</div>
)}
</div>
</div>
<BaseNodeWrapper selected={selected} className="w-[500px] p-0">
<div className="px-3 py-2 text-xs font-medium text-muted-foreground"> Compare</div>
<Handle
type="target"
position={Position.Left}
id="left"
className="h-3! w-3! bg-primary! border-2! border-background!"
style={{ top: "40%" }}
className="!h-3 !w-3 !border-2 !border-background !bg-blue-500"
/>
<Handle
type="target"
position={Position.Left}
position={Position.Right}
id="right"
className="h-3! w-3! bg-primary! border-2! border-background!"
style={{ top: "60%" }}
style={{ top: "40%" }}
className="!h-3 !w-3 !border-2 !border-background !bg-emerald-500"
/>
<div
ref={containerRef}
className="nodrag relative h-[320px] w-[500px] select-none overflow-hidden rounded-b-xl bg-muted"
onMouseDown={handleMouseDown}
onTouchStart={handleTouchStart}
>
{!hasLeft && !hasRight && (
<div className="pointer-events-none absolute inset-0 flex flex-col items-center justify-center gap-3 text-muted-foreground">
<ImageIcon className="h-10 w-10 opacity-30" />
<p className="px-8 text-center text-xs opacity-60">
Connect two image nodes - left handle (blue) and right handle (green)
</p>
</div>
)}
{hasRight && (
// eslint-disable-next-line @next/next/no-img-element
<img
src={nodeData.rightUrl}
alt={nodeData.rightLabel ?? "Right"}
className="pointer-events-none absolute inset-0 h-full w-full object-contain"
draggable={false}
/>
)}
{hasLeft && (
<div
className="pointer-events-none absolute inset-0 overflow-hidden"
style={{ width: `${sliderX}%` }}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={nodeData.leftUrl}
alt={nodeData.leftLabel ?? "Left"}
className="absolute inset-0 h-full w-full object-contain"
style={{ width: "500px", maxWidth: "none" }}
draggable={false}
/>
</div>
)}
{hasLeft && hasRight && (
<>
<div
className="pointer-events-none absolute bottom-0 top-0 z-10 w-0.5 bg-white shadow-md"
style={{ left: `${sliderX}%` }}
/>
<div
className="pointer-events-none absolute top-1/2 z-20 -translate-x-1/2 -translate-y-1/2"
style={{ left: `${sliderX}%` }}
>
<div className="flex h-8 w-8 items-center justify-center rounded-full border border-border bg-white shadow-lg">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none">
<path
d="M5 8H1M11 8H15M5 5L2 8L5 11M11 5L14 8L11 11"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</div>
</div>
</>
)}
{hasLeft && (
<div className="pointer-events-none absolute left-2 top-2 z-10">
<span className="rounded bg-blue-500/80 px-1.5 py-0.5 text-[10px] font-medium text-white backdrop-blur-sm">
{nodeData.leftLabel ?? "Before"}
</span>
</div>
)}
{hasRight && (
<div className="pointer-events-none absolute right-2 top-2 z-10">
<span className="rounded bg-emerald-500/80 px-1.5 py-0.5 text-[10px] font-medium text-white backdrop-blur-sm">
{nodeData.rightLabel ?? "After"}
</span>
</div>
)}
</div>
</BaseNodeWrapper>
);
}

View File

@@ -1,72 +1,119 @@
"use client";
import { useState, useCallback } from "react";
import { type NodeProps, type Node } from "@xyflow/react";
import { useMutation } from "convex/react";
import { useCallback, useState } from "react";
import { Handle, NodeResizer, Position, type NodeProps } from "@xyflow/react";
import { useAction, useMutation } from "convex/react";
import { Download, Loader2 } from "lucide-react";
import { api } from "@/convex/_generated/api";
import type { Id } from "@/convex/_generated/dataModel";
import { useDebouncedCallback } from "@/hooks/use-debounced-callback";
import BaseNodeWrapper from "./base-node-wrapper";
type FrameNodeData = {
interface FrameNodeData {
label?: string;
resolution?: string;
_status?: string;
_statusMessage?: string;
};
width?: number;
height?: number;
}
export type FrameNode = Node<FrameNodeData, "frame">;
export default function FrameNode({ id, data, selected }: NodeProps<FrameNode>) {
export default function FrameNode({ id, data, selected, width, height }: NodeProps) {
const nodeData = data as FrameNodeData;
const updateData = useMutation(api.nodes.updateData);
const [editingLabel, setEditingLabel] = useState<string | null>(null);
const exportFrame = useAction(api.export.exportFrame);
const displayLabel = data.label ?? "Frame";
const isEditing = editingLabel !== null;
const [label, setLabel] = useState(nodeData.label ?? "Frame");
const [isExporting, setIsExporting] = useState(false);
const [exportError, setExportError] = useState<string | null>(null);
const handleDoubleClick = useCallback(() => {
setEditingLabel(displayLabel);
}, [displayLabel]);
const debouncedSave = useDebouncedCallback((value: string) => {
void updateData({ nodeId: id as Id<"nodes">, data: { ...nodeData, label: value } });
}, 500);
const handleBlur = useCallback(() => {
if (editingLabel !== null && editingLabel !== data.label) {
updateData({
nodeId: id as Id<"nodes">,
data: {
...data,
label: editingLabel,
_status: undefined,
_statusMessage: undefined,
},
});
const handleLabelChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setLabel(event.target.value);
debouncedSave(event.target.value);
},
[debouncedSave],
);
const handleExport = useCallback(async () => {
if (isExporting) return;
setIsExporting(true);
setExportError(null);
try {
const result = await exportFrame({ frameNodeId: id as Id<"nodes"> });
const a = document.createElement("a");
a.href = result.url;
a.download = result.filename;
a.click();
} catch (error) {
setExportError(error instanceof Error ? error.message : "Export failed");
} finally {
setIsExporting(false);
}
setEditingLabel(null);
}, [editingLabel, data, id, updateData]);
}, [exportFrame, id, isExporting]);
const frameW = Math.round(width ?? 400);
const frameH = Math.round(height ?? 300);
return (
<BaseNodeWrapper
selected={selected}
className="min-w-[300px] min-h-[200px] p-3 border-blue-500/30"
className="relative h-full w-full border-2 border-dashed border-muted-foreground/40 !bg-transparent p-0 shadow-none"
>
{isEditing ? (
<NodeResizer isVisible={selected} minWidth={200} minHeight={150} />
<div className="absolute -top-8 left-0 flex items-center gap-2">
<input
value={editingLabel}
onChange={(e) => setEditingLabel(e.target.value)}
onBlur={handleBlur}
onKeyDown={(e) => e.key === "Enter" && handleBlur()}
autoFocus
className="nodrag text-xs font-medium text-blue-500 bg-transparent border-0 outline-none w-full"
value={label}
onChange={handleLabelChange}
onKeyDown={(event) => {
if (event.key === "Enter") {
(event.target as HTMLInputElement).blur();
}
}}
className="nodrag nowheel w-40 border-none bg-transparent text-sm font-medium text-muted-foreground outline-none focus:text-foreground"
/>
) : (
<div
onDoubleClick={handleDoubleClick}
className="text-xs font-medium text-blue-500 cursor-text"
<span className="text-xs text-muted-foreground/60">
{frameW}x{frameH}
</span>
<button
onClick={() => void handleExport()}
disabled={isExporting}
title="Export as PNG"
className="nodrag flex items-center gap-1 rounded-md border border-border bg-background px-2 py-1 text-xs font-medium transition-colors hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
type="button"
>
🖥 {displayLabel}{" "}
{data.resolution && (
<span className="text-muted-foreground">({data.resolution})</span>
{isExporting ? (
<Loader2 className="h-3 w-3 animate-spin" />
) : (
<Download className="h-3 w-3" />
)}
</div>
{isExporting ? "Exporting..." : "Export PNG"}
</button>
</div>
{exportError && (
<div className="absolute -bottom-6 left-0 text-xs text-destructive">{exportError}</div>
)}
<div className="nodrag h-full w-full" />
<Handle
type="target"
position={Position.Left}
id="frame-in"
className="!h-3 !w-3 !border-2 !border-background !bg-orange-500"
/>
<Handle
type="source"
position={Position.Right}
id="frame-out"
className="!h-3 !w-3 !border-2 !border-background !bg-orange-500"
/>
</BaseNodeWrapper>
);
}

View File

@@ -8,10 +8,28 @@ import type { Id } from "@/convex/_generated/dataModel";
import BaseNodeWrapper from "./base-node-wrapper";
import { useDebouncedCallback } from "@/hooks/use-debounced-callback";
import { DEFAULT_MODEL_ID } from "@/lib/ai-models";
import {
DEFAULT_ASPECT_RATIO,
getAiImageNodeOuterSize,
getImageViewportSize,
IMAGE_FORMAT_GROUP_LABELS,
IMAGE_FORMAT_PRESETS,
} from "@/lib/image-formats";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Sparkles, Loader2 } from "lucide-react";
type PromptNodeData = {
prompt?: string;
aspectRatio?: string;
model?: string;
canvasId?: string;
_status?: string;
@@ -29,13 +47,25 @@ export default function PromptNode({
const { getEdges, getNode } = useReactFlow();
const [prompt, setPrompt] = useState(nodeData.prompt ?? "");
const [aspectRatio, setAspectRatio] = useState(
nodeData.aspectRatio ?? DEFAULT_ASPECT_RATIO
);
const [isGenerating, setIsGenerating] = useState(false);
const [error, setError] = useState<string | null>(null);
const promptRef = useRef(prompt);
const aspectRatioRef = useRef(aspectRatio);
promptRef.current = prompt;
aspectRatioRef.current = aspectRatio;
useEffect(() => {
setPrompt(nodeData.prompt ?? "");
}, [nodeData.prompt]);
useEffect(() => {
setAspectRatio(nodeData.aspectRatio ?? DEFAULT_ASPECT_RATIO);
}, [nodeData.aspectRatio]);
const dataRef = useRef(data);
dataRef.current = data;
@@ -44,14 +74,18 @@ export default function PromptNode({
const createEdge = useMutation(api.edges.create);
const generateImage = useAction(api.ai.generateImage);
const debouncedSave = useDebouncedCallback((value: string) => {
const debouncedSave = useDebouncedCallback(() => {
const raw = dataRef.current as Record<string, unknown>;
const { _status, _statusMessage, ...rest } = raw;
void _status;
void _statusMessage;
updateData({
nodeId: id as Id<"nodes">,
data: { ...rest, prompt: value },
data: {
...rest,
prompt: promptRef.current,
aspectRatio: aspectRatioRef.current,
},
});
}, 500);
@@ -59,7 +93,15 @@ export default function PromptNode({
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = e.target.value;
setPrompt(value);
debouncedSave(value);
debouncedSave();
},
[debouncedSave]
);
const handleAspectRatioChange = useCallback(
(value: string) => {
setAspectRatio(value);
debouncedSave();
},
[debouncedSave]
);
@@ -93,18 +135,24 @@ export default function PromptNode({
const posX = (currentNode?.position?.x ?? 0) + offsetX;
const posY = currentNode?.position?.y ?? 0;
const viewport = getImageViewportSize(aspectRatio);
const outer = getAiImageNodeOuterSize(viewport);
const aiNodeId = await createNode({
canvasId,
type: "ai-image",
positionX: posX,
positionY: posY,
width: 320,
height: 320,
width: outer.width,
height: outer.height,
data: {
prompt,
model: DEFAULT_MODEL_ID,
modelTier: "standard",
canvasId,
aspectRatio,
outputWidth: viewport.width,
outputHeight: viewport.height,
},
});
@@ -122,6 +170,7 @@ export default function PromptNode({
prompt,
referenceStorageId,
model: DEFAULT_MODEL_ID,
aspectRatio,
});
} catch (err) {
setError(err instanceof Error ? err.message : "Generation failed");
@@ -130,6 +179,7 @@ export default function PromptNode({
}
}, [
prompt,
aspectRatio,
isGenerating,
nodeData.canvasId,
id,
@@ -166,6 +216,45 @@ export default function PromptNode({
className="nodrag nowheel w-full resize-none rounded-md border border-border bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-violet-500"
/>
<div className="flex flex-col gap-1.5">
<Label
htmlFor={`prompt-format-${id}`}
className="text-[11px] font-medium text-muted-foreground"
>
Format
</Label>
<Select
value={aspectRatio}
onValueChange={handleAspectRatioChange}
>
<SelectTrigger
id={`prompt-format-${id}`}
className="nodrag nowheel w-full"
size="sm"
>
<SelectValue placeholder="Seitenverhältnis" />
</SelectTrigger>
<SelectContent className="nodrag">
{(["square", "landscape", "portrait"] as const).map((group) => {
const presets = IMAGE_FORMAT_PRESETS.filter(
(p) => p.group === group
);
if (presets.length === 0) return null;
return (
<SelectGroup key={group}>
<SelectLabel>{IMAGE_FORMAT_GROUP_LABELS[group]}</SelectLabel>
{presets.map((p) => (
<SelectItem key={p.aspectRatio} value={p.aspectRatio}>
{p.label}
</SelectItem>
))}
</SelectGroup>
);
})}
</SelectContent>
</Select>
</div>
{error && (
<p className="text-xs text-destructive">{error}</p>
)}