Create professional demos and 3D mockups in seconds, directly in your browser
feat: implement CanvasContextMenu and Viewer3DControlsBridge components; refactor VideoCanvas to utilize new context menu and viewer controls
CristianOlivera14beebe0b3847
diff --git a/app/components/ui/editor/Mockup3dMenu.tsx b/app/components/ui/editor/Mockup3dMenu.tsxindex 72f726c..3b578a0 100644--- a/app/components/ui/editor/Mockup3dMenu.tsx+++ b/app/components/ui/editor/Mockup3dMenu.tsx@@ -37,40 +37,39 @@ function PositionPad({const fromEvent = useCallback((e: React.PointerEvent) => {if (!rectCache.current) return;-+const rect = rectCache.current;const currentWidth = rect.width;const rx = Math.max(0, Math.min(currentWidth, e.clientX - rect.left));const ry = Math.max(0, Math.min(PAD_H, e.clientY - rect.top));-+onChangeX(Math.round((rx / currentWidth) * X_HALF * 2 - X_HALF));onChangeY(Math.round((ry / PAD_H) * Y_HALF * 2 - Y_HALF));}, [onChangeX, onChangeY]);const bgLayerStyle: React.CSSProperties = backgroundUrl? {- backgroundImage: `url('${backgroundUrl}')`,- backgroundSize: "cover",- backgroundPosition: "center",- backgroundRepeat: "no-repeat",- }+ backgroundImage: `url('${backgroundUrl}')`,+ backgroundSize: "cover",+ backgroundPosition: "center",+ backgroundRepeat: "no-repeat",+ }: backgroundColorCss- ? backgroundColorCss.startsWith("#") || backgroundColorCss.startsWith("rgb")- ? { backgroundColor: backgroundColorCss }- : {- backgroundImage: backgroundColorCss,- backgroundSize: "cover",- backgroundPosition: "center",- }- : {};+ ? backgroundColorCss.startsWith("#") || backgroundColorCss.startsWith("rgb")+ ? { backgroundColor: backgroundColorCss }+ : {+ backgroundImage: backgroundColorCss,+ backgroundSize: "cover",+ backgroundPosition: "center",+ }+ : {};return (<div className="relative group w-full cursor-default"><divref={padRef}- className={`relative w-full rounded-[14px] overflow-hidden select-none border shadow-[inset_0_2px_8px_rgba(0,0,0,0.8)] transition-all duration-200 ${- isDraggingState ? "border-cyan-500/40 ring-1 ring-cyan-500/20" : "border-zinc-800/50"- }`}+ className={`relative w-full rounded-[14px] overflow-hidden select-none border shadow-[inset_0_2px_8px_rgba(0,0,0,0.8)] transition-all duration-200 ${isDraggingState ? "border-cyan-500/40 ring-1 ring-cyan-500/20" : "border-zinc-800/50"+ }`}style={{ height: PAD_H }}onPointerDown={(e) => {dragging.current = true;@@ -140,7 +139,7 @@ function ActiveDevicePreview({ tpl }: { tpl: ActiveDeviceTpl }) {const video = videoRef.current;if (!video) return;- let isMounted = true;+ let isMounted = true;if (isHovering) {const playVideo = async () => {@@ -179,9 +178,8 @@ function ActiveDevicePreview({ tpl }: { tpl: ActiveDeviceTpl }) {src={tpl.posterUrl}alt={tpl.title}draggable={false}- className={`absolute inset-0 h-full w-full object-cover transition-all duration-500 ${- isHovering ? "scale-105 opacity-0" : "scale-100 opacity-100"- }`}+ className={`absolute inset-0 h-full w-full object-cover transition-all duration-500 ${isHovering ? "scale-105 opacity-0" : "scale-100 opacity-100"+ }`}/>) : (<div className="absolute inset-0 flex items-center justify-center">@@ -198,15 +196,13 @@ function ActiveDevicePreview({ tpl }: { tpl: ActiveDeviceTpl }) {playsInlinepreload="metadata"onLoadedData={() => setVideoReady(true)}- className={`absolute inset-0 h-full w-full object-cover transition-all duration-500 ${- isHovering && videoReady ? "scale-105 opacity-100" : "scale-100 opacity-0"- }`}+ className={`absolute inset-0 h-full w-full object-cover transition-all duration-500 ${isHovering && videoReady ? "scale-105 opacity-100" : "scale-100 opacity-0"+ }`}/>)}<div- className={`absolute inset-0 z-20 bg-black/20 transition-opacity duration-300 ${- isHovering ? "opacity-100" : "opacity-0"- }`}+ className={`absolute inset-0 z-20 bg-black/20 transition-opacity duration-300 ${isHovering ? "opacity-100" : "opacity-0"+ }`}/><div className=" flex items-center gap-2 absolute bottom-0 left-0 right-0 px-3 py-2 bg-gradient-to-t from-black/80 to-transparent z-30"><Icon icon={tpl.icon} width={14} />@@ -308,7 +304,7 @@ export function Mockup3dMenu({setImagePhoneRotY,setImagePhoneOpening,setImagePhoneShadow,- setImagePhoneShadowColor,+ setImagePhoneShadowColor]);return (@@ -335,6 +331,7 @@ export function Mockup3dMenu({</div><SliderControl+ icon="solar:scale-linear"label={t("scale")}value={Math.round(imagePhoneScale * 100)}min={30}diff --git a/app/components/ui/editor/VideoCanvas.tsx b/app/components/ui/editor/VideoCanvas.tsxindex 815264e..298d11a 100644--- a/app/components/ui/editor/VideoCanvas.tsx+++ b/app/components/ui/editor/VideoCanvas.tsx@@ -23,11 +23,12 @@ import { CanvasElementsLayer } from "./CanvasElementsLayer";import { EditorHoverTooltip } from "./EditorHoverTooltip";import DropImage from "@/components/ui/DropImage";import { LayersPanel } from "./LayersPanel";-import { Icon } from "@iconify/react";import { useMockup3dContext } from "@/app/contexts/Mockup3dContext";import { PHONE_H, PHONE_W, DEVICE_3D_DIMENSIONS, DEVICE_VIEWER_DEFAULTS } from "@/lib/phone3d.utils";-import { ViewerControls3D } from "@/lib/viewer-controls3d";+import { Viewer3DControls } from "@/lib/viewer-controls3d";import { ControlsPopup } from "@/components/ui/ControlsPopup";+import { CanvasContextMenu } from "@/components/ui/CanvasContextMenu";+import { Viewer3DControlsBridge } from "@/components/ui/Viewer3DControlsBridge";export type { VideoCanvasHandle, VideoCanvasProps };@@ -366,9 +367,12 @@ const VideoCanvasInner = forwardRef<VideoCanvasHandle, VideoCanvasProps>(functio}, [aspectRatio, customAspectRatio]);const deviceDefaults = DEVICE_VIEWER_DEFAULTS[imagePhoneDevice] ?? { environment: "studio", glow: 1.0 };- const viewer3D = ViewerControls3D({- defaultEnvironment: deviceDefaults.environment,- defaultGlow: deviceDefaults.glow,++ const [viewer3D, setViewer3D] = useState<Viewer3DControls>({+ autoRotate: false,+ rotationSpeed: 3.5,+ glow: deviceDefaults.glow,+ environment: deviceDefaults.environment,});useEffect(() => {@@ -1691,95 +1695,19 @@ const VideoCanvasInner = forwardRef<VideoCanvasHandle, VideoCanvasProps>(functio<DropImage />)}- {canvasCtxMenu && (() => {- const ids = canvasSelectedIds.length > 0 ? canvasSelectedIds : (selectedElementId ? [selectedElementId] : []);- const isMulti = ids.length > 1;- const singleId = ids[0] ?? null;- return (- <div- data-canvas-ctx-menu- className="fixed z-[9999] bg-black border border-white/15 rounded-xl shadow-2xl py-1 min-w-45 overflow-hidden"- style={{ left: Math.min(canvasCtxMenu.x, window.innerWidth - 196), top: Math.min(canvasCtxMenu.y, window.innerHeight - 160) }}- onPointerDown={(e) => e.stopPropagation()}- >- {!isMulti && singleId && (- <>- <button- className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"- onClick={() => {- const maxZ = Math.max(...canvasElements.map(e => e.zIndex), VIDEO_Z_INDEX);- if (onElementUpdate) onElementUpdate(singleId, { zIndex: maxZ + 1 });- setCanvasCtxMenu(null);- }}- >- <Icon icon="qlementine-icons:bring-to-front-16" className="size-3.5 shrink-0 opacity-70" />- Traer al frente- </button>- <button- className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"- onClick={() => {- const el = canvasElements.find(e => e.id === singleId);- if (!el || !onElementUpdate) return;- if (el.zIndex >= VIDEO_Z_INDEX) {- onElementUpdate(singleId, { zIndex: VIDEO_Z_INDEX - 1 });- } else {- const minZ = Math.min(...canvasElements.map(e => e.zIndex));- onElementUpdate(singleId, { zIndex: Math.max(1, minZ - 1) });- }- setCanvasCtxMenu(null);- }}- >- <Icon icon="qlementine-icons:bring-to-back-16" className="size-3.5 shrink-0 opacity-70" />- Enviar atrás- </button>- <div className="my-1 h-px bg-white/6" />- </>- )}- {isMulti && (- <>- <button- className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"- onClick={() => {- const newGroupId = crypto.randomUUID();- ids.forEach(id => { if (onElementUpdate) onElementUpdate(id, { groupId: newGroupId }); });- setCanvasCtxMenu(null);- }}- >- <Icon icon="solar:layers-minimalistic-bold" className="size-3.5 shrink-0 opacity-70" />- Agrupar ({ids.length})- </button>- <button- className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"- onClick={() => {- const groupIds = new Set(- ids.map(id => canvasElements.find(e => e.id === id)?.groupId).filter(Boolean)- );- canvasElements- .filter(e => e.groupId && groupIds.has(e.groupId))- .forEach(e => { if (onElementUpdate) onElementUpdate(e.id, { groupId: undefined }); });- setCanvasCtxMenu(null);- }}- >- <Icon icon="solar:layers-bold" className="size-3.5 shrink-0 opacity-70" />- Desagrupar- </button>- <div className="my-1 h-px bg-white/6" />- </>- )}- <button- className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-red-400 hover:bg-red-500/10 transition-colors text-left"- onClick={() => {- if (onElementDelete) onElementDelete(ids.length === 1 ? ids[0] : [...ids]);- setCanvasSelectedIds([]);- setCanvasCtxMenu(null);- }}- >- <Icon icon="solar:trash-bin-trash-bold" className="size-3.5 shrink-0 opacity-70" />- {isMulti ? `Eliminar ${ids.length} elementos` : "Eliminar"}- </button>- </div>- );- })()}+ {canvasCtxMenu && (+ <CanvasContextMenu+ canvasCtxMenu={canvasCtxMenu}+ canvasSelectedIds={canvasSelectedIds}+ selectedElementId={selectedElementId}+ canvasElements={canvasElements}+ VIDEO_Z_INDEX={VIDEO_Z_INDEX}+ onElementUpdate={onElementUpdate}+ onElementDelete={onElementDelete}+ setCanvasCtxMenu={setCanvasCtxMenu}+ setCanvasSelectedIds={setCanvasSelectedIds}+ />+ )}<div className="absolute inset-0 pointer-events-none z-0"style={{ backgroundImage: 'radial-gradient(rgb(39, 39, 42) 1px, transparent 1px)', backgroundSize: '24px 24px' }}>@@ -2181,10 +2109,12 @@ const VideoCanvasInner = forwardRef<VideoCanvasHandle, VideoCanvasProps>(functio{/* ── 3D phone overlay (video & image mode) ── */}{imagePhoneActive && (- <div- className="absolute inset-0 pointer-events-none"- style={{ zIndex: 155, overflow: "visible" }}- >+ <div className="absolute inset-0 pointer-events-none" style={{ zIndex: 155, overflow: "visible" }}>+ <Viewer3DControlsBridge+ environment={deviceDefaults.environment}+ glow={deviceDefaults.glow}+ onChange={setViewer3D}+ /><ControlsPopup /><divclassName="absolute animate-in fade-in zoom-in-95 duration-300"diff --git a/components/ui/CanvasContextMenu.tsx b/components/ui/CanvasContextMenu.tsxnew file mode 100644index 0000000..a11deb0--- /dev/null+++ b/components/ui/CanvasContextMenu.tsx@@ -0,0 +1,138 @@+import { Icon } from "@iconify/react";+import { useTranslations } from "next-intl";++interface CanvasElement {+ id: string;+ zIndex: number;+ groupId?: string;+}++interface CanvasContextMenuProps {+ canvasCtxMenu: { x: number; y: number } | null;+ canvasSelectedIds: string[];+ selectedElementId: string | null;+ canvasElements: CanvasElement[];+ VIDEO_Z_INDEX: number;+ onElementUpdate?: (id: string, updates: Partial<CanvasElement>) => void;+ onElementDelete?: (ids: string | string[]) => void;+ setCanvasCtxMenu: (menu: { x: number; y: number } | null) => void;+ setCanvasSelectedIds: (ids: string[]) => void;+}++export function CanvasContextMenu({+ canvasCtxMenu,+ canvasSelectedIds,+ selectedElementId,+ canvasElements,+ VIDEO_Z_INDEX,+ onElementUpdate,+ onElementDelete,+ setCanvasCtxMenu,+ setCanvasSelectedIds,+}: CanvasContextMenuProps) {+ const t = useTranslations("canvasMenu");++ if (!canvasCtxMenu) return null;++ const ids = canvasSelectedIds.length > 0 ? canvasSelectedIds : selectedElementId ? [selectedElementId] : [];+ const isMulti = ids.length > 1;+ const singleId = ids[0] ?? null;++ return (+ <div+ data-canvas-ctx-menu+ className="fixed z-[9999] bg-black border border-white/15 rounded-xl shadow-2xl py-1 min-w-45 overflow-hidden"+ style={{+ left: Math.min(canvasCtxMenu.x, window.innerWidth - 196),+ top: Math.min(canvasCtxMenu.y, window.innerHeight - 160),+ }}+ onPointerDown={(e) => e.stopPropagation()}+ >+ {!isMulti && singleId && (+ <>+ <button+ className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"+ onClick={() => {+ const maxZ = Math.max(...canvasElements.map((e) => e.zIndex), VIDEO_Z_INDEX);+ if (onElementUpdate) onElementUpdate(singleId, { zIndex: maxZ + 1 });+ setCanvasCtxMenu(null);+ }}+ >+ <Icon icon="qlementine-icons:bring-to-front-16" className="size-3.5 shrink-0 opacity-70" />+ {t("bringToFront")}+ </button>++ <button+ className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"+ onClick={() => {+ const el = canvasElements.find((e) => e.id === singleId);+ if (!el || !onElementUpdate) return;+ if (el.zIndex >= VIDEO_Z_INDEX) {+ onElementUpdate(singleId, { zIndex: VIDEO_Z_INDEX - 1 });+ } else {+ const minZ = Math.min(...canvasElements.map((e) => e.zIndex));+ onElementUpdate(singleId, { zIndex: Math.max(1, minZ - 1) });+ }+ setCanvasCtxMenu(null);+ }}+ >+ <Icon icon="qlementine-icons:bring-to-back-16" className="size-3.5 shrink-0 opacity-70" />+ {t("sendToBack")}+ </button>++ <div className="my-1 h-px bg-white/6" />+ </>+ )}++ {isMulti && (+ <>+ <button+ className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"+ onClick={() => {+ const newGroupId = crypto.randomUUID();+ ids.forEach((id) => {+ if (onElementUpdate) onElementUpdate(id, { groupId: newGroupId });+ });+ setCanvasCtxMenu(null);+ }}+ >+ <Icon icon="solar:layers-minimalistic-bold" className="size-3.5 shrink-0 opacity-70" />+ {t("group", { count: ids.length })}+ </button>++ <button+ className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-neutral-300 hover:bg-white/6 transition-colors text-left"+ onClick={() => {+ const groupIds = new Set(+ ids.map((id) => canvasElements.find((e) => e.id === id)?.groupId).filter(Boolean)+ );+ canvasElements+ .filter((e) => e.groupId && groupIds.has(e.groupId))+ .forEach((e) => {+ if (onElementUpdate) onElementUpdate(e.id, { groupId: undefined });+ });+ setCanvasCtxMenu(null);+ }}+ >+ <Icon icon="solar:layers-bold" className="size-3.5 shrink-0 opacity-70" />+ {t("ungroup")}+ </button>++ <div className="my-1 h-px bg-white/6" />+ </>+ )}++ <button+ className="w-full flex items-center gap-2.5 px-3 py-1.5 text-[11.5px] text-red-400 hover:bg-red-500/10 transition-colors text-left"+ onClick={() => {+ if (onElementDelete) onElementDelete(ids.length === 1 ? ids[0] : [...ids]);+ setCanvasSelectedIds([]);+ setCanvasCtxMenu(null);+ }}+ >+ <Icon icon="solar:trash-bin-trash-bold" className="size-3.5 shrink-0 opacity-70" />+ {isMulti ? t("deleteMultiple", { count: ids.length }) : t("deleteSingle")}+ </button>+ </div>+ );+}\ No newline at end of filediff --git a/components/ui/Viewer3DControlsBridge.tsx b/components/ui/Viewer3DControlsBridge.tsxnew file mode 100644index 0000000..8b1c7cd--- /dev/null+++ b/components/ui/Viewer3DControlsBridge.tsx@@ -0,0 +1,21 @@+import { EnvironmentPreset, Viewer3DControls, ViewerControls3D } from "@/lib/viewer-controls3d";+import { useEffect } from "react";++export function Viewer3DControlsBridge({+ environment,+ glow,+ onChange,+}: {+ environment: EnvironmentPreset;+ glow: number;+ onChange: (controls: Viewer3DControls) => void;+}) {+ const controls = ViewerControls3D({ defaultEnvironment: environment, defaultGlow: glow });++ useEffect(() => {+ onChange(controls);+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [controls.autoRotate, controls.rotationSpeed, controls.glow, controls.environment]);++ return null;+}\ No newline at end of filediff --git a/lib/viewer-controls3d.ts b/lib/viewer-controls3d.tsindex 6469fc5..185e1e1 100644--- a/lib/viewer-controls3d.ts+++ b/lib/viewer-controls3d.ts@@ -26,7 +26,7 @@ const ENVIRONMENT_OPTIONS = ['apartment', 'studio', 'city', 'park', 'lobby',] as const;-interface Viewer3DControls {+export interface Viewer3DControls {autoRotate: boolean;rotationSpeed: number;glow: number;