mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 23:47:33 +00:00
feat(studio): preserve drop position when dropping blocks onto preview
Blocks dragged from the Blocks panel and dropped onto the composition preview now land at the position where they were dropped, instead of always being placed at (0, 0). The preview viewport converts screen coordinates to composition space using the stage element's bounding rect, accounting for zoom and pan. A visual drop indicator (dashed border overlay) appears while dragging over the preview.
This commit is contained in:
@@ -255,6 +255,36 @@ export function StudioApp() {
|
||||
],
|
||||
);
|
||||
|
||||
const handlePreviewBlockDrop = useCallback(
|
||||
(blockName: string, position: { left: number; top: number }) => {
|
||||
if (!projectId) return;
|
||||
void addBlockToProject({
|
||||
projectId,
|
||||
blockName,
|
||||
activeCompPath,
|
||||
visualPosition: position,
|
||||
timelineElements,
|
||||
readProjectFile: fileManager.readProjectFile,
|
||||
writeProjectFile: fileManager.writeProjectFile,
|
||||
recordEdit: editHistory.recordEdit,
|
||||
refreshFileTree: fileManager.refreshFileTree,
|
||||
reloadPreview,
|
||||
showToast,
|
||||
});
|
||||
},
|
||||
[
|
||||
projectId,
|
||||
activeCompPath,
|
||||
timelineElements,
|
||||
fileManager.readProjectFile,
|
||||
fileManager.writeProjectFile,
|
||||
fileManager.refreshFileTree,
|
||||
editHistory.recordEdit,
|
||||
reloadPreview,
|
||||
showToast,
|
||||
],
|
||||
);
|
||||
|
||||
const clearDomSelectionRef = useRef<() => void>(() => {});
|
||||
const domEditSelectionBridgeRef = useRef<DomEditSelection | null>(null);
|
||||
const handleDomEditElementDeleteRef = useRef<(s: DomEditSelection) => Promise<void>>(
|
||||
@@ -541,6 +571,7 @@ export function StudioApp() {
|
||||
handleTimelineElementDelete={timelineEditing.handleTimelineElementDelete}
|
||||
handleTimelineAssetDrop={timelineEditing.handleTimelineAssetDrop}
|
||||
handleTimelineBlockDrop={handleTimelineBlockDrop}
|
||||
handlePreviewBlockDrop={handlePreviewBlockDrop}
|
||||
handleTimelineFileDrop={timelineEditing.handleTimelineFileDrop}
|
||||
handleTimelineElementMove={timelineEditing.handleTimelineElementMove}
|
||||
handleTimelineElementResize={timelineEditing.handleTimelineElementResize}
|
||||
|
||||
@@ -29,6 +29,10 @@ export interface StudioPreviewAreaProps {
|
||||
blockName: string,
|
||||
placement: Pick<TimelineElement, "start" | "track">,
|
||||
) => Promise<void> | void;
|
||||
handlePreviewBlockDrop?: (
|
||||
blockName: string,
|
||||
position: { left: number; top: number },
|
||||
) => Promise<void> | void;
|
||||
handleTimelineFileDrop: (
|
||||
files: File[],
|
||||
placement?: Pick<TimelineElement, "start" | "track">,
|
||||
@@ -53,6 +57,7 @@ export function StudioPreviewArea({
|
||||
handleTimelineElementDelete,
|
||||
handleTimelineAssetDrop,
|
||||
handleTimelineBlockDrop,
|
||||
handlePreviewBlockDrop,
|
||||
handleTimelineFileDrop,
|
||||
handleTimelineElementMove,
|
||||
handleTimelineElementResize,
|
||||
@@ -104,6 +109,7 @@ export function StudioPreviewArea({
|
||||
onDeleteElement={handleTimelineElementDelete}
|
||||
onAssetDrop={handleTimelineAssetDrop}
|
||||
onBlockDrop={handleTimelineBlockDrop}
|
||||
onPreviewBlockDrop={handlePreviewBlockDrop}
|
||||
onFileDrop={handleTimelineFileDrop}
|
||||
onMoveElement={handleTimelineElementMove}
|
||||
onResizeElement={handleTimelineElementResize}
|
||||
|
||||
@@ -54,6 +54,10 @@ interface NLELayoutProps {
|
||||
blockName: string,
|
||||
placement: Pick<TimelineElement, "start" | "track">,
|
||||
) => Promise<void> | void;
|
||||
onPreviewBlockDrop?: (
|
||||
blockName: string,
|
||||
position: { left: number; top: number },
|
||||
) => Promise<void> | void;
|
||||
/** Persist timeline move actions back into source HTML */
|
||||
onMoveElement?: (
|
||||
element: TimelineElement,
|
||||
@@ -107,6 +111,7 @@ export const NLELayout = memo(function NLELayout({
|
||||
onDeleteElement,
|
||||
onAssetDrop,
|
||||
onBlockDrop,
|
||||
onPreviewBlockDrop,
|
||||
onMoveElement,
|
||||
onResizeElement,
|
||||
onBlockedEditAttempt,
|
||||
@@ -353,6 +358,7 @@ export const NLELayout = memo(function NLELayout({
|
||||
portrait={portrait}
|
||||
directUrl={directUrl}
|
||||
suppressLoadingOverlay={hasLoadedOnceRef.current}
|
||||
onBlockDrop={onPreviewBlockDrop}
|
||||
/>
|
||||
{!isFullscreen && previewOverlay}
|
||||
</div>
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
type PreviewZoomState,
|
||||
} from "./previewZoom";
|
||||
import { readStudioUiPreferences, writeStudioUiPreferences } from "../../utils/studioUiPreferences";
|
||||
import { usePreviewBlockDrop } from "./usePreviewBlockDrop";
|
||||
|
||||
interface NLEPreviewProps {
|
||||
projectId: string;
|
||||
@@ -21,6 +22,7 @@ interface NLEPreviewProps {
|
||||
portrait?: boolean;
|
||||
directUrl?: string;
|
||||
suppressLoadingOverlay?: boolean;
|
||||
onBlockDrop?: (blockName: string, position: { left: number; top: number }) => void;
|
||||
}
|
||||
|
||||
export function getPreviewPlayerKey({
|
||||
@@ -90,6 +92,7 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
portrait,
|
||||
directUrl,
|
||||
suppressLoadingOverlay,
|
||||
onBlockDrop,
|
||||
}: NLEPreviewProps) {
|
||||
const activeKey = getPreviewPlayerKey({ projectId, directUrl });
|
||||
const viewportRef = useRef<HTMLDivElement>(null);
|
||||
@@ -364,6 +367,13 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
};
|
||||
}, [applyPan]);
|
||||
|
||||
const {
|
||||
isDragOver: previewDragOver,
|
||||
handleDragOver: handlePreviewDragOver,
|
||||
handleDragLeave: handlePreviewDragLeave,
|
||||
handleDrop: handlePreviewDrop,
|
||||
} = usePreviewBlockDrop({ portrait, stageRef, onBlockDrop });
|
||||
|
||||
const initial = zoomRef.current;
|
||||
|
||||
return (
|
||||
@@ -373,6 +383,9 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
className="relative flex-1 flex items-center justify-center p-2 overflow-hidden min-h-0 outline-none focus:ring-1 focus:ring-studio-accent/40 bg-neutral-700"
|
||||
tabIndex={0}
|
||||
aria-label="Composition preview"
|
||||
onDragOver={handlePreviewDragOver}
|
||||
onDragLeave={handlePreviewDragLeave}
|
||||
onDrop={handlePreviewDrop}
|
||||
>
|
||||
<div className="absolute inset-2 flex items-center justify-center pointer-events-none">
|
||||
<div
|
||||
@@ -402,6 +415,9 @@ export const NLEPreview = memo(function NLEPreview({
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{previewDragOver && (
|
||||
<div className="absolute inset-2 z-40 rounded-lg border-2 border-dashed border-studio-accent/50 bg-studio-accent/[0.04] pointer-events-none" />
|
||||
)}
|
||||
<div
|
||||
ref={hudRef}
|
||||
className="pointer-events-none absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 z-50 rounded-lg px-4 py-2 text-sm font-mono tabular-nums text-white/90 bg-black/60 backdrop-blur-sm shadow-lg"
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { useCallback, useState, type RefObject } from "react";
|
||||
import { TIMELINE_BLOCK_MIME } from "../../utils/timelineAssetDrop";
|
||||
|
||||
interface UsePreviewBlockDropOptions {
|
||||
portrait?: boolean;
|
||||
stageRef: RefObject<HTMLDivElement | null>;
|
||||
onBlockDrop?: (blockName: string, position: { left: number; top: number }) => void;
|
||||
}
|
||||
|
||||
interface BlockDropPayload {
|
||||
name: string;
|
||||
dimensions?: { width: number; height: number };
|
||||
}
|
||||
|
||||
function parseBlockPayload(raw: string): BlockDropPayload | null {
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as {
|
||||
name?: string;
|
||||
dimensions?: { width: number; height: number };
|
||||
};
|
||||
return parsed.name ? (parsed as BlockDropPayload) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCompositionPosition(
|
||||
clientX: number,
|
||||
clientY: number,
|
||||
stageRect: DOMRect,
|
||||
portrait: boolean | undefined,
|
||||
): { left: number; top: number } | null {
|
||||
if (stageRect.width === 0 || stageRect.height === 0) return null;
|
||||
|
||||
const normalizedX = (clientX - stageRect.left) / stageRect.width;
|
||||
const normalizedY = (clientY - stageRect.top) / stageRect.height;
|
||||
|
||||
const compWidth = portrait ? 1080 : 1920;
|
||||
const compHeight = portrait ? 1920 : 1080;
|
||||
|
||||
return {
|
||||
left: Math.max(0, Math.min(normalizedX * compWidth, compWidth)),
|
||||
top: Math.max(0, Math.min(normalizedY * compHeight, compHeight)),
|
||||
};
|
||||
}
|
||||
|
||||
function centerBlockAtPosition(
|
||||
pos: { left: number; top: number },
|
||||
block: BlockDropPayload,
|
||||
): { left: number; top: number } {
|
||||
const blockW = block.dimensions?.width ?? 0;
|
||||
const blockH = block.dimensions?.height ?? 0;
|
||||
return {
|
||||
left: Math.max(0, pos.left - blockW / 2),
|
||||
top: Math.max(0, pos.top - blockH / 2),
|
||||
};
|
||||
}
|
||||
|
||||
export function usePreviewBlockDrop({
|
||||
portrait,
|
||||
stageRef,
|
||||
onBlockDrop,
|
||||
}: UsePreviewBlockDropOptions) {
|
||||
const [isDragOver, setIsDragOver] = useState(false);
|
||||
|
||||
const handleDragOver = useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
if (!onBlockDrop) return;
|
||||
if (!e.dataTransfer.types.includes(TIMELINE_BLOCK_MIME)) return;
|
||||
e.preventDefault();
|
||||
e.dataTransfer.dropEffect = "copy";
|
||||
setIsDragOver(true);
|
||||
},
|
||||
[onBlockDrop],
|
||||
);
|
||||
|
||||
const handleDragLeave = useCallback(() => {
|
||||
setIsDragOver(false);
|
||||
}, []);
|
||||
|
||||
// fallow-ignore-next-line complexity
|
||||
const handleDrop = useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
setIsDragOver(false);
|
||||
if (!onBlockDrop) return;
|
||||
|
||||
const payload = e.dataTransfer.getData(TIMELINE_BLOCK_MIME);
|
||||
if (!payload) return;
|
||||
e.preventDefault();
|
||||
|
||||
const block = parseBlockPayload(payload);
|
||||
const stage = stageRef.current;
|
||||
if (!block || !stage) return;
|
||||
|
||||
const pos = resolveCompositionPosition(
|
||||
e.clientX,
|
||||
e.clientY,
|
||||
stage.getBoundingClientRect(),
|
||||
portrait,
|
||||
);
|
||||
if (!pos) return;
|
||||
|
||||
onBlockDrop(block.name, centerBlockAtPosition(pos, block));
|
||||
},
|
||||
[onBlockDrop, stageRef, portrait],
|
||||
);
|
||||
|
||||
return { isDragOver, handleDragOver, handleDragLeave, handleDrop };
|
||||
}
|
||||
@@ -14,6 +14,7 @@ interface AddBlockOptions {
|
||||
blockName: string;
|
||||
activeCompPath: string | null;
|
||||
placement?: { start: number; track: number };
|
||||
visualPosition?: { left: number; top: number };
|
||||
timelineElements: TimelineElement[];
|
||||
readProjectFile: (path: string) => Promise<string>;
|
||||
writeProjectFile: (path: string, content: string) => Promise<void>;
|
||||
@@ -44,6 +45,7 @@ export async function addBlockToProject(
|
||||
blockName,
|
||||
activeCompPath,
|
||||
placement,
|
||||
visualPosition,
|
||||
timelineElements,
|
||||
readProjectFile,
|
||||
writeProjectFile,
|
||||
@@ -133,6 +135,9 @@ export async function addBlockToProject(
|
||||
? (block as { dimensions: { height: number } }).dimensions.height
|
||||
: hostDims.height;
|
||||
|
||||
const left = visualPosition ? Math.round(visualPosition.left) : 0;
|
||||
const top = visualPosition ? Math.round(visualPosition.top) : 0;
|
||||
|
||||
const subCompHtml =
|
||||
`<div data-composition-id="${compId}" ` +
|
||||
`data-composition-src="${compositionFile}" ` +
|
||||
@@ -140,7 +145,7 @@ export async function addBlockToProject(
|
||||
`data-duration="${formatTimelineAttributeNumber(duration)}" ` +
|
||||
`data-track-index="${track}" ` +
|
||||
`data-width="${width}" data-height="${height}" ` +
|
||||
`style="position: absolute; left: 0px; top: 0px; width: ${width}px; height: ${height}px; z-index: ${zIndex}">` +
|
||||
`style="position: absolute; left: ${left}px; top: ${top}px; width: ${width}px; height: ${height}px; z-index: ${zIndex}">` +
|
||||
`</div>`;
|
||||
|
||||
const patchedContent = insertTimelineAssetIntoSource(originalContent, subCompHtml);
|
||||
|
||||
Reference in New Issue
Block a user