feat(studio): add drag-and-drop from Blocks panel to Timeline

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Ángel
2026-05-10 20:53:47 +00:00
co-authored by Claude Opus 4.6
parent 4fff683d2f
commit b726afcda8
5 changed files with 166 additions and 30 deletions
+62
View File
@@ -3616,6 +3616,67 @@ export function StudioApp() {
[activeCompPath, handleTimelineAssetDrop, timelineElements, uploadProjectFiles],
);
const handleTimelineBlockDrop = useCallback(
async (
block: { name: string; file: string; title: string; duration: number; category: string },
placement: Pick<TimelineElement, "start" | "track">,
) => {
const pid = projectIdRef.current;
if (!pid) return;
// Fetch block content from registry
const res = await fetch(
`/api/registry/blocks/${block.name}/files/${encodeURIComponent(block.file)}`,
);
if (!res.ok) {
showToast(`Failed to fetch block: ${block.title}`);
return;
}
const data = (await res.json()) as { content?: string };
if (typeof data.content !== "string") {
showToast(`Invalid block content: ${block.title}`);
return;
}
// Write block to compositions/
const targetPath = `compositions/${block.file}`;
await writeProjectFile(targetPath, data.content);
// Now add it to the active composition's timeline as a sub-composition
const compPath = activeCompPath || "index.html";
const compRes = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(compPath)}`);
if (!compRes.ok) return;
const compData = (await compRes.json()) as { content?: string };
if (typeof compData.content !== "string") return;
const blockId = block.name.replace(/[^a-zA-Z0-9_-]/g, "-");
const normalizedStart = Number(placement.start.toFixed(2));
const normalizedDuration = block.duration || 5;
// Build the sub-composition element
const subCompHtml = `<div data-composition-id="${blockId}" data-composition-src="${targetPath}" data-start="${normalizedStart}" data-duration="${normalizedDuration}" style="position:absolute;inset:0;width:1920px;height:1080px;"></div>`;
// Insert before closing </body> or at end of root composition
let patchedContent = compData.content;
const bodyCloseIdx = patchedContent.lastIndexOf("</body>");
if (bodyCloseIdx >= 0) {
patchedContent =
patchedContent.slice(0, bodyCloseIdx) +
subCompHtml +
"\n" +
patchedContent.slice(bodyCloseIdx);
} else {
patchedContent += "\n" + subCompHtml;
}
await writeProjectFile(compPath, patchedContent);
await refreshFileTree();
setRefreshKey((k) => k + 1);
showToast(`Added ${block.title} to timeline at ${normalizedStart.toFixed(1)}s`, "info");
},
[activeCompPath, refreshFileTree, showToast, writeProjectFile],
);
// ── File Management Handlers ──
const handleCreateFile = useCallback(
@@ -4119,6 +4180,7 @@ export function StudioApp() {
renderClipContent={renderClipContent}
onDeleteElement={handleTimelineElementDelete}
onAssetDrop={handleTimelineAssetDrop}
onBlockDrop={handleTimelineBlockDrop}
onFileDrop={handleTimelineFileDrop}
onMoveElement={handleTimelineElementMove}
onResizeElement={handleTimelineElementResize}
@@ -8,6 +8,7 @@ import {
type BlockCategory,
type BlockEntry,
} from "./blockCatalog";
import { TIMELINE_BLOCK_MIME } from "../../utils/timelineAssetDrop";
interface BlocksPanelProps {
onAddBlock: (block: BlockEntry) => void;
@@ -44,7 +45,30 @@ function BlockCard({
const icon = CATEGORY_ICONS[block.category];
return (
<div className="group relative flex flex-col rounded-2xl border border-neutral-800 bg-neutral-950 transition-all hover:border-neutral-700 hover:bg-neutral-900/80">
<div
draggable
onDragStart={(e) => {
e.dataTransfer.effectAllowed = "copy";
e.dataTransfer.setData(
TIMELINE_BLOCK_MIME,
JSON.stringify({
name: block.name,
file: block.file,
title: block.title,
duration: block.duration,
category: block.category,
}),
);
const ghost = document.createElement("div");
ghost.textContent = block.title;
ghost.style.cssText =
"position:fixed;top:-100px;left:-100px;padding:6px 12px;background:#18181b;color:#fff;border-radius:8px;font-size:11px;font-weight:600;white-space:nowrap;pointer-events:none;z-index:9999;";
document.body.appendChild(ghost);
e.dataTransfer.setDragImage(ghost, 0, 0);
requestAnimationFrame(() => ghost.remove());
}}
className="group relative flex flex-col rounded-2xl border border-neutral-800 bg-neutral-950 transition-all hover:border-neutral-700 hover:bg-neutral-900/80"
>
{/* Visual preview area */}
<div
className="relative flex h-[72px] items-center justify-center overflow-hidden rounded-t-2xl"
@@ -41,6 +41,10 @@ interface NLELayoutProps {
assetPath: string,
placement: Pick<TimelineElement, "start" | "track">,
) => Promise<void> | void;
onBlockDrop?: (
block: { name: string; file: string; title: string; duration: number; category: string },
placement: Pick<TimelineElement, "start" | "track">,
) => Promise<void> | void;
/** Persist timeline move actions back into source HTML */
onMoveElement?: (
element: TimelineElement,
@@ -87,6 +91,7 @@ export const NLELayout = memo(function NLELayout({
onFileDrop,
onDeleteElement,
onAssetDrop,
onBlockDrop,
onMoveElement,
onResizeElement,
onBlockedEditAttempt,
@@ -449,6 +454,7 @@ export const NLELayout = memo(function NLELayout({
onFileDrop={onFileDrop}
onDeleteElement={onDeleteElement}
onAssetDrop={onAssetDrop}
onBlockDrop={onBlockDrop}
onMoveElement={onMoveElement}
onResizeElement={onResizeElement}
onBlockedEditAttempt={onBlockedEditAttempt}
@@ -27,7 +27,7 @@ import {
type TimelineTheme,
} from "./timelineTheme";
import { getPinchTimelineZoomPercent, getTimelinePixelsPerSecond } from "./timelineZoom";
import { TIMELINE_ASSET_MIME } from "../../utils/timelineAssetDrop";
import { TIMELINE_ASSET_MIME, TIMELINE_BLOCK_MIME } from "../../utils/timelineAssetDrop";
import {
canInspectTimelineElement,
getTimelineElementKey,
@@ -315,6 +315,11 @@ interface TimelineProps {
assetPath: string,
placement: { start: number; track: number },
) => Promise<void> | void;
/** Called when a block is dropped from the Blocks panel */
onBlockDrop?: (
block: { name: string; file: string; title: string; duration: number; category: string },
placement: Pick<TimelineElement, "start" | "track">,
) => Promise<void> | void;
/** Persist a clip move back into source HTML */
onDeleteElement?: (
element: import("../store/playerStore").TimelineElement,
@@ -384,6 +389,7 @@ export const Timeline = memo(function Timeline({
renderClipOverlay,
onFileDrop,
onAssetDrop,
onBlockDrop,
onDeleteElement,
onMoveElement,
onResizeElement,
@@ -1109,10 +1115,12 @@ export const Timeline = memo(function Timeline({
const handleAssetDragOver = useCallback((e: React.DragEvent) => {
if (disabledRef.current) return;
const hasFiles = e.dataTransfer.files.length > 0;
const hasAsset = Array.from(e.dataTransfer.types).includes(TIMELINE_ASSET_MIME);
if (!hasFiles && !hasAsset) return;
const types = Array.from(e.dataTransfer.types);
const hasAsset = types.includes(TIMELINE_ASSET_MIME);
const hasBlock = types.includes(TIMELINE_BLOCK_MIME);
if (!hasFiles && !hasAsset && !hasBlock) return;
e.preventDefault();
if (hasAsset) {
if (hasAsset || hasBlock) {
e.dataTransfer.dropEffect = "copy";
}
setIsDragOver(true);
@@ -1148,33 +1156,68 @@ export const Timeline = memo(function Timeline({
}
const assetPayload = e.dataTransfer.getData(TIMELINE_ASSET_MIME);
if (!assetPayload || !onAssetDrop) return;
try {
const parsed = JSON.parse(assetPayload) as { path?: string };
if (!parsed.path) return;
const scroll = scrollRef.current;
const rect = scroll?.getBoundingClientRect();
if (!scroll || !rect) return;
const placement = resolveTimelineAssetDrop(
{
rectLeft: rect.left,
rectTop: rect.top,
scrollLeft: scroll.scrollLeft,
scrollTop: scroll.scrollTop,
pixelsPerSecond: ppsRef.current,
duration: durationRef.current,
trackHeight: TRACK_H,
trackOrder: trackOrderRef.current,
},
e.clientX,
e.clientY,
);
void onAssetDrop(parsed.path, placement);
} catch {
// ignore malformed drag payloads
if (assetPayload && onAssetDrop) {
try {
const parsed = JSON.parse(assetPayload) as { path?: string };
if (!parsed.path) return;
const scroll = scrollRef.current;
const rect = scroll?.getBoundingClientRect();
if (!scroll || !rect) return;
const placement = resolveTimelineAssetDrop(
{
rectLeft: rect.left,
rectTop: rect.top,
scrollLeft: scroll.scrollLeft,
scrollTop: scroll.scrollTop,
pixelsPerSecond: ppsRef.current,
duration: durationRef.current,
trackHeight: TRACK_H,
trackOrder: trackOrderRef.current,
},
e.clientX,
e.clientY,
);
void onAssetDrop(parsed.path, placement);
} catch {
// ignore malformed drag payloads
}
return;
}
const blockPayload = e.dataTransfer.getData(TIMELINE_BLOCK_MIME);
if (blockPayload && onBlockDrop) {
try {
const parsed = JSON.parse(blockPayload) as {
name: string;
file: string;
title: string;
duration: number;
category: string;
};
const scroll = scrollRef.current;
const rect = scroll?.getBoundingClientRect();
if (!scroll || !rect) return;
const placement = resolveTimelineAssetDrop(
{
rectLeft: rect.left,
rectTop: rect.top,
scrollLeft: scroll.scrollLeft,
scrollTop: scroll.scrollTop,
pixelsPerSecond: ppsRef.current,
duration: durationRef.current,
trackHeight: TRACK_H,
trackOrder: trackOrderRef.current,
},
e.clientX,
e.clientY,
);
void onBlockDrop(parsed, placement);
} catch {
// ignore malformed drag payloads
}
}
},
[onAssetDrop, onFileDrop],
[onAssetDrop, onBlockDrop, onFileDrop],
);
const handlePinchWheel = useCallback(
@@ -1,6 +1,7 @@
import { AUDIO_EXT, IMAGE_EXT, VIDEO_EXT } from "./mediaTypes";
export const TIMELINE_ASSET_MIME = "application/x-hyperframes-asset";
export const TIMELINE_BLOCK_MIME = "application/x-hyperframes-block";
const FALLBACK_TIMELINE_FILE_DROP_DURATION = 5;
export type TimelineAssetKind = "image" | "video" | "audio";