Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

fix: Undo/redo

CristianOlivera1c39f5c3250b6
diff --git a/app/[locale]/(editor)/editor/page.tsx b/app/[locale]/(editor)/editor/page.tsx
index f105030..1450040 100644
--- a/app/[locale]/(editor)/editor/page.tsx
+++ b/app/[locale]/(editor)/editor/page.tsx
@@ -62,8 +62,6 @@ export default function Editor() {
// Auth — needed for building production-ready Recipe JSON
const { user } = useAuth();
const {
- undoMotion, redoMotion, pushHistory,
- canUndoMotion, canRedoMotion,
imagePhoneActive, setImagePhoneActive,
imagePhoneX, setImagePhoneX,
imagePhoneY, setImagePhoneY,
@@ -73,6 +71,7 @@ export default function Editor() {
imagePhoneRotZ, setImagePhoneRotZ,
imagePhonePerspective, setImagePhonePerspective,
imagePhoneDevice, setImagePhoneDevice,
+ imagePhonePresetId, setImagePhonePresetId,
imagePhoneOpening, setImagePhoneOpening,
imagePhoneShadow, setImagePhoneShadow,
imagePhoneShadowColor, setImagePhoneShadowColor,
@@ -1054,6 +1053,19 @@ export default function Editor() {
apply3DToBackground,
imageMaskConfig,
videoMaskConfig,
+ imagePhoneActive,
+ imagePhoneX,
+ imagePhoneY,
+ imagePhoneScale,
+ imagePhoneRotX,
+ imagePhoneRotY,
+ imagePhoneRotZ,
+ imagePhonePerspective,
+ imagePhoneDevice,
+ imagePhonePresetId,
+ imagePhoneOpening,
+ imagePhoneShadow,
+ imagePhoneShadowColor,
});
}, 300);
return () => {
@@ -1068,6 +1080,9 @@ export default function Editor() {
zoomFragments, mockupId, mockupConfig, canvasElements,
audioTracks, muteOriginalAudio, masterVolume, cameraConfig,
videoTransform, imageTransform, apply3DToBackground, imageMaskConfig, videoMaskConfig,
+ imagePhoneActive, imagePhoneX, imagePhoneY, imagePhoneScale, imagePhoneRotX,
+ imagePhoneRotY, imagePhoneRotZ, imagePhonePerspective, imagePhoneDevice,
+ imagePhonePresetId, imagePhoneOpening, imagePhoneShadow, imagePhoneShadowColor,
setEditorState
]);
@@ -1098,6 +1113,19 @@ export default function Editor() {
setApply3DToBackground(editorState.apply3DToBackground);
setImageMaskConfig(editorState.imageMaskConfig);
setVideoMaskConfig(editorState.videoMaskConfig);
+ setImagePhoneActive(editorState.imagePhoneActive);
+ setImagePhoneX(editorState.imagePhoneX);
+ setImagePhoneY(editorState.imagePhoneY);
+ setImagePhoneScale(editorState.imagePhoneScale);
+ setImagePhoneRotX(editorState.imagePhoneRotX);
+ setImagePhoneRotY(editorState.imagePhoneRotY);
+ setImagePhoneRotZ(editorState.imagePhoneRotZ);
+ setImagePhonePerspective(editorState.imagePhonePerspective);
+ setImagePhoneDevice(editorState.imagePhoneDevice);
+ setImagePhonePresetId(editorState.imagePhonePresetId);
+ setImagePhoneOpening(editorState.imagePhoneOpening);
+ setImagePhoneShadow(editorState.imagePhoneShadow);
+ setImagePhoneShadowColor(editorState.imagePhoneShadowColor);
}, [editorState]);
// Handler para cambiar el mockup
@@ -2132,21 +2160,6 @@ export default function Editor() {
if (isInputFocused) return;
- // Motion-specific undo/redo when the motion tool is active
- if (activeTool === "motion") {
- if ((e.ctrlKey || e.metaKey) && e.key === 'z' && !e.shiftKey) {
- e.preventDefault();
- undoMotion();
- return;
- }
- if (((e.ctrlKey || e.metaKey) && e.key === 'y') ||
- ((e.ctrlKey || e.metaKey) && e.shiftKey && e.key === 'z')) {
- e.preventDefault();
- redoMotion();
- return;
- }
- }
-
if ((e.ctrlKey || e.metaKey) && e.key === 'z' && !e.shiftKey) {
e.preventDefault();
if (canUndo) {
@@ -2165,7 +2178,7 @@ export default function Editor() {
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
- }, [undo, redo, canUndo, canRedo, activeTool, undoMotion, redoMotion]);
+ }, [undo, redo, canUndo, canRedo]);
// Keyboard listener for Ctrl+V image paste (photo mode only)
useEffect(() => {
@@ -3093,11 +3106,6 @@ export default function Editor() {
imageExportProgress={imageExportProgress}
canvasWidth={customAspectRatio?.width || 1920}
canvasHeight={customAspectRatio?.height || 1080}
- activeTool={activeTool}
- onUndoMotion={undoMotion}
- onRedoMotion={redoMotion}
- canUndoMotion={canUndoMotion}
- canRedoMotion={canRedoMotion}
/>
}
ref={canvasRef}
diff --git a/app/components/ui/editor/EditorTopBar.tsx b/app/components/ui/editor/EditorTopBar.tsx
index 8f0c3db..d97b117 100644
--- a/app/components/ui/editor/EditorTopBar.tsx
+++ b/app/components/ui/editor/EditorTopBar.tsx
@@ -5,7 +5,6 @@ import { ExportDropdown } from "../ExportDropdown";
import { ExportImageDropdown } from "../ExportImageDropdown";
import type { ExportQuality, ExportProgress } from "@/types";
import type { EditorMode } from "@/types/editor-mode.types";
-import type { Tool } from "@/types/editor.types";
import type { ImageExportFormat } from "@/types/image-project.types";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { useEffect, useState } from "react";
@@ -36,12 +35,6 @@ interface EditorTopBarProps {
imageExportProgress?: ImageExportProgress;
canvasWidth?: number;
canvasHeight?: number;
- // Motion-aware undo/redo: when activeTool is "motion", these take over.
- activeTool?: Tool;
- onUndoMotion?: () => boolean;
- onRedoMotion?: () => boolean;
- canUndoMotion?: boolean;
- canRedoMotion?: boolean;
}
export function EditorTopBar({
@@ -57,19 +50,8 @@ export function EditorTopBar({
imageExportProgress,
canvasWidth = 1920,
canvasHeight = 1080,
- activeTool,
- onUndoMotion,
- onRedoMotion,
- canUndoMotion = false,
- canRedoMotion = false,
}: EditorTopBarProps) {
const isPhotoMode = editorMode === "photo";
- const isMotionActive = activeTool === "motion";
- // Motion tool is the active one: route undo/redo through motion.
- const effectiveOnUndo = isMotionActive ? onUndoMotion : onUndo;
- const effectiveOnRedo = isMotionActive ? onRedoMotion : onRedo;
- const effectiveCanUndo = isMotionActive ? canUndoMotion : canUndo;
- const effectiveCanRedo = isMotionActive ? canRedoMotion : canRedo;
const t = useTranslations("editor.topBar");
const [showAlert, setShowAlert] = useState(false);
const [prevStatus, setPrevStatus] = useState<string>(exportProgress.status);
@@ -152,33 +134,21 @@ export function EditorTopBar({
<div className="flex items-center ml-auto">
<div className="flex items-center gap-2 border-r border-white/10 pr-3">
- <TooltipAction label={effectiveCanUndo ? t("history.undo") : t("history.noUndo")}>
+ <TooltipAction label={canUndo ? t("history.undo") : t("history.noUndo")}>
<button
- onClick={() => {
- if (isMotionActive) {
- onUndoMotion?.();
- } else {
- onUndo?.();
- }
- }}
- disabled={!effectiveCanUndo}
- className={`transition-colors ${effectiveCanUndo ? "hover:text-white text-white/70" : "opacity-30 cursor-not-allowed text-white/30"
+ onClick={() => onUndo?.()}
+ disabled={!canUndo}
+ className={`transition-colors ${canUndo ? "hover:text-white text-white/70" : "opacity-30 cursor-not-allowed text-white/30"
}`}
>
<Icon icon="mdi:undo" width="20" />
</button>
</TooltipAction>
- <TooltipAction label={effectiveCanRedo ? t("history.redo") : t("history.noRedo")}>
+ <TooltipAction label={canRedo ? t("history.redo") : t("history.noRedo")}>
<button
- onClick={() => {
- if (isMotionActive) {
- onRedoMotion?.();
- } else {
- onRedo?.();
- }
- }}
- disabled={!effectiveCanRedo}
- className={`transition-colors ${effectiveCanRedo ? "hover:text-white text-white/70" : "opacity-30 cursor-not-allowed text-white/60"
+ onClick={() => onRedo?.()}
+ disabled={!canRedo}
+ className={`transition-colors ${canRedo ? "hover:text-white text-white/70" : "opacity-30 cursor-not-allowed text-white/60"
}`}
>
<Icon icon="mdi:redo" width="20" />
diff --git a/app/components/ui/editor/Mockup3dMenu.tsx b/app/components/ui/editor/Mockup3dMenu.tsx
index 4871e55..3620816 100644
--- a/app/components/ui/editor/Mockup3dMenu.tsx
+++ b/app/components/ui/editor/Mockup3dMenu.tsx
@@ -135,8 +135,6 @@ export interface Mockup3dMenuProps {
setImagePhoneRotX: (v: number) => void;
setImagePhoneRotY: (v: number) => void;
- pushHistory: () => void;
-
backgroundUrl?: string | null;
backgroundColorCss?: string | null;
@@ -163,8 +161,6 @@ export function Mockup3dMenu({
setImagePhoneRotX,
setImagePhoneRotY,
- pushHistory,
-
backgroundUrl,
backgroundColorCss,
@@ -253,7 +249,7 @@ export function Mockup3dMenu({
min={30}
max={300}
step={1}
- onChange={(v) => { pushHistory(); setImagePhoneScale(v / 100); }}
+ onChange={(v) => { setImagePhoneScale(v / 100); }}
suffix="%"
/>
@@ -265,7 +261,7 @@ export function Mockup3dMenu({
min={0}
max={100}
step={1}
- onChange={(v) => { pushHistory(); setImagePhoneOpening(v / 100); }}
+ onChange={(v) => { setImagePhoneOpening(v / 100); }}
suffix="%"
/>
)}
@@ -277,7 +273,7 @@ export function Mockup3dMenu({
min={0}
max={100}
step={1}
- onChange={(v) => { pushHistory(); setImagePhoneShadow(v / 100); }}
+ onChange={(v) => { setImagePhoneShadow(v / 100); }}
suffix="%"
/>
@@ -288,7 +284,6 @@ export function Mockup3dMenu({
y={imagePhoneY}
onChangeX={setImagePhoneX}
onChangeY={setImagePhoneY}
- onDragStart={pushHistory}
backgroundUrl={backgroundUrl}
backgroundColorCss={backgroundColorCss}
/>
diff --git a/app/components/ui/editor/MockupMenu.tsx b/app/components/ui/editor/MockupMenu.tsx
index fa9a350..903e542 100644
--- a/app/components/ui/editor/MockupMenu.tsx
+++ b/app/components/ui/editor/MockupMenu.tsx
@@ -234,7 +234,6 @@ export function MockupMenu({
imagePhoneShadow,
setImagePhoneShadow,
setImagePhoneShadowColor,
- pushHistory,
} = useMockup3dContext();
// ── Derived ──
@@ -312,7 +311,6 @@ export function MockupMenu({
setImagePhoneOpening(1);
setImagePhoneScale(1);
} else {
- // phone, iphone, samsung — all rendered through Phone3DViewer.tsx
setImagePhoneRotX(-58.23);
setImagePhoneRotY(-29.82);
}
@@ -364,7 +362,6 @@ export function MockupMenu({
setImagePhoneY={setImagePhoneY}
setImagePhoneRotX={setImagePhoneRotX}
setImagePhoneRotY={setImagePhoneRotY}
- pushHistory={pushHistory}
backgroundUrl={resolvedBackgroundUrl}
backgroundColorCss={backgroundColorCss}
onBack={() => setPage("home")}
diff --git a/app/contexts/Mockup3dContext.tsx b/app/contexts/Mockup3dContext.tsx
index 95c643e..9147d2b 100644
--- a/app/contexts/Mockup3dContext.tsx
+++ b/app/contexts/Mockup3dContext.tsx
@@ -1,6 +1,6 @@
"use client";
-import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from "react";
+import { createContext, useContext, useState, type ReactNode } from "react";
interface Mockup3dState {
selectedTemplateId: string | null;
@@ -48,16 +48,6 @@ interface Mockup3dState {
/** Drop-shadow color (CSS color string) */
imagePhoneShadowColor: string;
setImagePhoneShadowColor: (v: string) => void;
- /** Undo the last motion transform change. Returns true if there was something to undo. */
- undoMotion: () => boolean;
- /** Redo a previously-undone motion transform change. Returns true if there was something to redo. */
- redoMotion: () => boolean;
- /** Push the current motion transform state to the undo history. */
- pushHistory: () => void;
- /** True when there is at least one entry in the motion undo stack. */
- canUndoMotion: boolean;
- /** True when there is at least one entry in the motion redo stack. */
- canRedoMotion: boolean;
}
const Mockup3dContext = createContext<Mockup3dState | null>(null);
@@ -66,7 +56,6 @@ export function Mockup3dProvider({ children }: { children: ReactNode }) {
const [selectedTemplateId, setSelectedTemplateId] = useState<string | null>(null);
const [motionImageUrl, setMotionImageUrl] = useState<string | null>(null);
const [motionIntensity, setMotionIntensity] = useState(70);
- // Add this with your other useState hooks inside Mockup3dProvider
// Image mode phone mockup
const [imagePhoneActive, setImagePhoneActive] = useState(false);
@@ -83,71 +72,6 @@ export function Mockup3dProvider({ children }: { children: ReactNode }) {
const [imagePhoneShadow, setImagePhoneShadow] = useState(0.6);
const [imagePhoneShadowColor, setImagePhoneShadowColor] = useState("#000000");
- // ── Undo/redo stack for motion transforms (max 50 entries) ────────────────
- const historyRef = useRef<{
- past: Array<{
- x: number; y: number; scale: number;
- rx: number; ry: number; rz: number;
- opening: number; shadow: number; shadowColor: string;
- }>;
- future: Array<{
- x: number; y: number; scale: number;
- rx: number; ry: number; rz: number;
- opening: number; shadow: number; shadowColor: string;
- }>;
- }>({ past: [], future: [] });
- const MAX_HISTORY = 50;
- const [canUndoMotion, setCanUndoMotion] = useState(false);
- const [canRedoMotion, setCanRedoMotion] = useState(false);
-
- const refreshCanFlags = () => {
- setCanUndoMotion(historyRef.current.past.length > 0);
- setCanRedoMotion(historyRef.current.future.length > 0);
- };
-
- const pushHistory = () => {
- historyRef.current.past.push({
- x: imagePhoneX, y: imagePhoneY, scale: imagePhoneScale,
- rx: imagePhoneRotX, ry: imagePhoneRotY, rz: imagePhoneRotZ,
- opening: imagePhoneOpening, shadow: imagePhoneShadow, shadowColor: imagePhoneShadowColor,
- });
- if (historyRef.current.past.length > MAX_HISTORY) historyRef.current.past.shift();
- historyRef.current.future = [];
- refreshCanFlags();
- };
-
- const undoMotion = useCallback(() => {
- const past = historyRef.current.past;
- if (past.length === 0) return false;
- historyRef.current.future.push({
- x: imagePhoneX, y: imagePhoneY, scale: imagePhoneScale,
- rx: imagePhoneRotX, ry: imagePhoneRotY, rz: imagePhoneRotZ,
- opening: imagePhoneOpening, shadow: imagePhoneShadow, shadowColor: imagePhoneShadowColor,
- });
- const snap = past.pop()!;
- setImagePhoneX(snap.x); setImagePhoneY(snap.y); setImagePhoneScale(snap.scale);
- setImagePhoneRotX(snap.rx); setImagePhoneRotY(snap.ry); setImagePhoneRotZ(snap.rz);
- setImagePhoneOpening(snap.opening); setImagePhoneShadow(snap.shadow); setImagePhoneShadowColor(snap.shadowColor);
- refreshCanFlags();
- return true;
- }, [imagePhoneX, imagePhoneY, imagePhoneScale, imagePhoneRotX, imagePhoneRotY, imagePhoneRotZ, imagePhoneOpening, imagePhoneShadow, imagePhoneShadowColor]);
-
- const redoMotion = useCallback(() => {
- const future = historyRef.current.future;
- if (future.length === 0) return false;
- historyRef.current.past.push({
- x: imagePhoneX, y: imagePhoneY, scale: imagePhoneScale,
- rx: imagePhoneRotX, ry: imagePhoneRotY, rz: imagePhoneRotZ,
- opening: imagePhoneOpening, shadow: imagePhoneShadow, shadowColor: imagePhoneShadowColor,
- });
- const snap = future.pop()!;
- setImagePhoneX(snap.x); setImagePhoneY(snap.y); setImagePhoneScale(snap.scale);
- setImagePhoneRotX(snap.rx); setImagePhoneRotY(snap.ry); setImagePhoneRotZ(snap.rz);
- setImagePhoneOpening(snap.opening); setImagePhoneShadow(snap.shadow); setImagePhoneShadowColor(snap.shadowColor);
- refreshCanFlags();
- return true;
- }, [imagePhoneX, imagePhoneY, imagePhoneScale, imagePhoneRotX, imagePhoneRotY, imagePhoneRotZ, imagePhoneOpening, imagePhoneShadow, imagePhoneShadowColor]);
-
return (
<Mockup3dContext.Provider value={{
selectedTemplateId, setSelectedTemplateId,
@@ -166,9 +90,6 @@ export function Mockup3dProvider({ children }: { children: ReactNode }) {
imagePhoneOpening, setImagePhoneOpening,
imagePhoneShadow, setImagePhoneShadow,
imagePhoneShadowColor, setImagePhoneShadowColor,
- undoMotion, redoMotion, pushHistory,
- canUndoMotion,
- canRedoMotion,
}}>
{children}
</Mockup3dContext.Provider>
diff --git a/types/editor-state.types.ts b/types/editor-state.types.ts
index 6b24ff2..5a58628 100644
--- a/types/editor-state.types.ts
+++ b/types/editor-state.types.ts
@@ -37,6 +37,19 @@ export interface EditorState {
apply3DToBackground: boolean;
imageMaskConfig: ImageMaskConfig;
videoMaskConfig: ImageMaskConfig;
+ imagePhoneActive: boolean;
+ imagePhoneX: number;
+ imagePhoneY: number;
+ imagePhoneScale: number;
+ imagePhoneRotX: number;
+ imagePhoneRotY: number;
+ imagePhoneRotZ: number;
+ imagePhonePerspective: number;
+ imagePhoneDevice: 'phone' | 'iphone' | 'iphone-13-pro-max' | 'samsung' | 'laptop';
+ imagePhonePresetId: string;
+ imagePhoneOpening: number;
+ imagePhoneShadow: number;
+ imagePhoneShadowColor: string;
}
export function createInitialEditorState(overrides?: Partial<EditorState>): EditorState {
@@ -90,6 +103,19 @@ export function createInitialEditorState(overrides?: Partial<EditorState>): Edit
videoMaskConfig: {
enabled: false,
},
+ imagePhoneActive: false,
+ imagePhoneX: 0,
+ imagePhoneY: 0,
+ imagePhoneScale: 1,
+ imagePhoneRotX: 0,
+ imagePhoneRotY: 0,
+ imagePhoneRotZ: 0,
+ imagePhonePerspective: 600,
+ imagePhoneDevice: 'phone',
+ imagePhonePresetId: 'front',
+ imagePhoneOpening: 1,
+ imagePhoneShadow: 0.6,
+ imagePhoneShadowColor: '#000000',
...overrides,
};
}