The VS Code of video editing, built for agents.
TypeScript
Implement drag-and-drop functionality for timeline layers
konstantin-paulus329938d95767
diff --git a/apps/web/src/components/timeline/layers/context.tsx b/apps/web/src/components/timeline/layers/context.tsxindex 33b6de10..894f84e2 100644--- a/apps/web/src/components/timeline/layers/context.tsx+++ b/apps/web/src/components/timeline/layers/context.tsx@@ -2,24 +2,241 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-import { createContext, createSignal, useContext, type JSX, type Signal } from "solid-js";+import { createContext, createSignal, onCleanup, useContext, type JSX, type Signal } from "solid-js";+import { useWorld } from "@diffusionstudio/koota-solid";+import { Expanded, getEntityTree, isMask, isSequence } from "@diffusionstudio/runtime";-import { assert } from "@/utils";+import { useEditor, useTimelineIndex } from "@/engine/hooks";+import { useTimeline } from "@/context/timeline";+import { resolveSequentialOverlaps } from "@/engine/overlap";+import { assert, clamp } from "@/utils";+import { flattenRows, getListBottom, hitRows, resolveGap } from "./drag";import type { Entity } from "koota";+import type { DropTarget, FlatRow, GapContext, LayerDrag } from "./drag";++/** How far the pointer travels before a press means a drag, not a click. */+const DRAG_THRESHOLD_PX = 4;+/** Holding the pointer this close to the list's edge scrolls it. */+const AUTO_SCROLL_ZONE_PX = 28;+const AUTO_SCROLL_MAX_STEP_PX = 8;+/** Hovering a collapsed container this long opens it, as dropping into it would. */+const EXPAND_DWELL_MS = 600;type LayerContextValue = {/** The row whose height is being dragged, if any. */resized: Signal<Entity | null>;+ /** The row being dragged to a new place in the tree, and where it would drop. */+ drag: LayerDrag;}const LayerContext = createContext<LayerContextValue>();+/**+ * Besides sharing gesture state with the rows, this is where dragging them+ * to restack and reparent lives, Figma-style. The rows never move while the+ * pointer is down; an indicator says where the layer would land — a line+ * with a dot for a spot between siblings, a border around a container for a+ * drop inside it — and the document is only written on release, through+ * `editor.reparent`, so the drop is one undo step.+ */export function LayerContextProvider(props: { children: JSX.Element }) {+ const world = useWorld();+ const editor = useEditor();+ const timeline = useTimeline();+ const index = useTimelineIndex();+const resized = createSignal<Entity | null>(null);+ const [dragging, setDragging] = createSignal<Entity | null>(null);+ const [target, setTarget] = createSignal<DropTarget | null>(null);++ let pending: { entity: Entity; startX: number; startY: number } | null = null;+ let active: { entity: Entity; forbidden: Set<Entity>; isMask: boolean } | null = null;+ let dwell: { entity: Entity; since: number } | null = null;+ let pointerX = 0;+ let pointerY = 0;+ let dirty = false;+ let raf = 0;++ const begin = (event: PointerEvent, entity: Entity): void => {+ if (pending !== null || active !== null) return;++ pending = { entity, startX: event.clientX, startY: event.clientY };+ pointerX = event.clientX;+ pointerY = event.clientY;++ document.addEventListener('pointermove', handleMove);+ document.addEventListener('pointerup', handleUp);+ document.addEventListener('keydown', handleKey, true);+ };++ const activate = (entity: Entity): void => {+ active = {+ entity,+ forbidden: new Set(getEntityTree(world, entity)),+ isMask: isMask(entity),+ };+ dirty = true;+ setDragging(entity);+ raf = requestAnimationFrame(tick);+ };++ const handleMove = (event: PointerEvent): void => {+ pointerX = event.clientX;+ pointerY = event.clientY;+ dirty = true;++ if (pending !== null && active === null) {+ const distance = Math.hypot(pointerX - pending.startX, pointerY - pending.startY);+ if (distance >= DRAG_THRESHOLD_PX) activate(pending.entity);+ }+ };++ const handleUp = (): void => {+ const held = active;+ const drop = target();+ cleanup();+ if (held !== null && drop !== null) performDrop(held.entity, drop);+ };++ /** Escape lets go of the layer without dropping it anywhere. */+ const handleKey = (event: KeyboardEvent): void => {+ if (event.key !== 'Escape' || active === null) return;+ event.stopPropagation();+ cleanup();+ };++ const cleanup = (): void => {+ document.removeEventListener('pointermove', handleMove);+ document.removeEventListener('pointerup', handleUp);+ document.removeEventListener('keydown', handleKey, true);+ cancelAnimationFrame(raf);++ pending = null;+ active = null;+ dwell = null;+ dirty = false;+ setDragging(null);+ setTarget(null);+ };++ const tick = (): void => {+ raf = requestAnimationFrame(tick);+ if (active === null) return;++ autoScroll();++ // The dwell fires on quiet frames too — holding still is the gesture.+ if (dwell !== null && performance.now() - dwell.since >= EXPAND_DWELL_MS) {+ const entity = dwell.entity;+ dwell = null;+ dirty = true;+ editor.editProperty(entity, 'expanded', true);+ }++ if (!dirty) return;+ dirty = false;+ updateTarget();+ };++ /** Nudges the shared scroll while the pointer holds near either edge. */+ const autoScroll = (): void => {+ const viewport = document.querySelector<HTMLElement>('[data-timeline-layers-viewport]');+ if (!viewport) return;++ const rect = viewport.getBoundingClientRect();+ let step = 0;++ if (pointerY < rect.top + AUTO_SCROLL_ZONE_PX) {+ step = -Math.min(AUTO_SCROLL_MAX_STEP_PX, (rect.top + AUTO_SCROLL_ZONE_PX - pointerY) * 0.2);+ } else if (pointerY > rect.bottom - AUTO_SCROLL_ZONE_PX) {+ step = Math.min(AUTO_SCROLL_MAX_STEP_PX, (pointerY - (rect.bottom - AUTO_SCROLL_ZONE_PX)) * 0.2);+ }++ if (step !== 0) {+ timeline.scrollBy(step);+ dirty = true;+ }+ };++ const updateTarget = (): void => {+ const layersEl = document.querySelector<HTMLElement>('[data-timeline-layers]');+ const viewportEl = document.querySelector<HTMLElement>('[data-timeline-layers-viewport]');+ const { root, layers } = index();++ if (!layersEl || !viewportEl || root === null || active === null) {+ dwell = null;+ setTarget(null);+ return;+ }++ const rowEls = layersEl.querySelectorAll<HTMLElement>('[data-layer-row]');+ const flat = flattenRows(layers, root);++ // The index rebuilding mid-frame can briefly disagree with the DOM; skip+ // the frame rather than pair rows with the wrong rects.+ if (rowEls.length !== flat.length || flat.length === 0) {+ dwell = null;+ setTarget(null);+ return;+ }++ const rows: FlatRow[] = flat.map((row, i) => ({ ...row, rect: rowEls[i]!.getBoundingClientRect() }));+ const containerRect = layersEl.getBoundingClientRect();+ const viewportRect = viewportEl.getBoundingClientRect();+ const y = clamp(pointerY, viewportRect.top, viewportRect.bottom - 1);++ const context: GapContext = {+ root,+ dragged: active.entity,+ forbidden: active.forbidden,+ draggedIsMask: active.isMask,+ containerRect,+ listBottom: getListBottom(layersEl, containerRect),+ };++ const hit = hitRows(rows, y, context.forbidden);++ if (hit.kind === 'inside') {+ const { node, rect } = hit.row;++ // Dropping into a collapsed container is blind; hovering long enough+ // opens it so the drop can aim at a spot inside instead.+ if (node.expandable && !node.expanded) {+ if (dwell?.entity !== node.entity) dwell = { entity: node.entity, since: performance.now() };+ } else {+ dwell = null;+ }++ setTarget({+ kind: 'inside',+ parent: node.entity,+ top: rect.top - containerRect.top,+ height: rect.height,+ });+ return;+ }++ dwell = null;+ setTarget(resolveGap(hit.index, rows, pointerX, context));+ };++ const performDrop = (entity: Entity, drop: DropTarget): void => {+ if (drop.kind === 'line') {+ editor.reparent(entity, drop.parent, drop.anchor);+ return;+ }++ if (!editor.reparent(entity, drop.parent)) return;+ // A sequence keeps its clips on one line; the drop wins the overlaps.+ if (isSequence(drop.parent)) resolveSequentialOverlaps(world, [entity]);+ if (!drop.parent.has(Expanded)) editor.editProperty(drop.parent, 'expanded', true);+ };++ onCleanup(cleanup);return (- <LayerContext.Provider value={{ resized }}>+ <LayerContext.Provider value={{ resized, drag: { dragging, target, begin } }}>{props.children}</LayerContext.Provider>);diff --git a/apps/web/src/components/timeline/layers/drag.ts b/apps/web/src/components/timeline/layers/drag.tsnew file mode 100644index 00000000..1814a341--- /dev/null+++ b/apps/web/src/components/timeline/layers/drag.ts@@ -0,0 +1,241 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++/**+ * The geometry of dragging rows to restack and reparent them: where the+ * pointer is in the tree, and what drop that means. The gesture itself+ * lives in the layer context; everything here is pure.+ *+ * It is all read back from the DOM: the rows render flat under+ * `[data-timeline-layers]` in the same depth-first order the timeline index+ * lists them, so pairing the two by position gives every visible row its+ * rect, depth and ancestor chain without the rows registering anything.+ */++import { isGroupLike, isMask, isSequence } from '@diffusionstudio/runtime';+import { NESTED_INDENT_PX } from './config';++import type { Accessor } from 'solid-js';+import type { Entity } from 'koota';+import type { TimelineNode } from '@diffusionstudio/runtime';++/** Client X of the insertion dot for a depth-0 drop; each depth steps inwards.+ * Sits where a row's icon starts, past the chevron column. */+const DOT_CENTER_PX = 20;+/** A container row's edge strips mean "next to", its middle means "into". */+const EDGE_ZONE_MAX_PX = 8;++export type DropTarget =+ | {+ kind: 'line';+ parent: Entity;+ /** The document sibling the layer is inserted before; none appends last. */+ anchor: Entity | undefined;+ /** List-local Y of the insertion line. */+ y: number;+ /** List-local X of the dot's center, one indent step per depth. */+ inset: number;+ }+ | {+ kind: 'inside';+ parent: Entity;+ /** List-local extent of the highlighted row. */+ top: number;+ height: number;+ };++export type LayerDrag = {+ /** The row being dragged, once the pointer has moved far enough to mean it. */+ dragging: Accessor<Entity | null>;+ /** Where the drag would drop right now. */+ target: Accessor<DropTarget | null>;+ /** Arm a drag from a row's pointerdown; it activates past the threshold. */+ begin: (event: PointerEvent, entity: Entity) => void;+};++/** A visible clip row, its place in the tree paired with its place on screen. */+export type FlatRow = {+ node: TimelineNode;+ depth: number;+ /** The row's parent in the document: another row's entity, or the scene. */+ parent: Entity;+ /** Row entities on the path above this one, indexed by their depth. */+ ancestors: Entity[];+ rect: DOMRect;+};++export type GapContext = {+ root: Entity;+ dragged: Entity;+ /** The dragged subtree: nothing in it may become the parent of the drop. */+ forbidden: Set<Entity>;+ /** Masks stack apart from plain layers; anchors must stay in their bucket. */+ draggedIsMask: boolean;+ containerRect: DOMRect;+ /** Client Y just below the last row of the whole list. */+ listBottom: number;+};++/**+ * The visible clip rows in render order, each with its depth and the row+ * entities above it. Sub-item and keyframe rows indent the tree but never+ * hold layers, so they contribute depth without appearing in the list.+ */+export function flattenRows(nodes: TimelineNode[], root: Entity): Omit<FlatRow, 'rect'>[] {+ const out: Omit<FlatRow, 'rect'>[] = [];+ const ancestors: Entity[] = [];++ const walk = (nodes: TimelineNode[], depth: number, parent: Entity): void => {+ for (const node of nodes) {+ if (node.kind === 'geometry') {+ out.push({ node, depth, parent, ancestors: ancestors.slice() });+ ancestors.push(node.entity);+ walk(node.children, depth + 1, node.entity);+ ancestors.pop();+ } else {+ walk(node.children, depth + 1, parent);+ }+ }+ };++ walk(nodes, 0, root);+ return out;+}++/** Client Y just below the last row, sub-item rows included. */+export function getListBottom(layersEl: HTMLElement, containerRect: DOMRect): number {+ for (let el = layersEl.lastElementChild; el !== null; el = el.previousElementSibling) {+ if ((el as HTMLElement).dataset.dropIndicator !== undefined) continue;+ return el.getBoundingClientRect().bottom;+ }+ return containerRect.top;+}++export type RowHit =+ /** The boundary between rows `index - 1` and `index`. */+ | { kind: 'gap'; index: number }+ | { kind: 'inside'; row: FlatRow };++/**+ * What the pointer is over. A container row splits into edge strips that+ * mean "next to it" and a middle that means "into it"; a plain row splits+ * at its midline. The run of sub-item rows under a clip belongs to the gap+ * after it — there is no spot for a layer between a clip and its fills.+ */+export function hitRows(rows: FlatRow[], y: number, forbidden: Set<Entity>): RowHit {+ let index = -1;+ for (let i = 0; i < rows.length; i++) {+ if (rows[i]!.rect.top <= y) index = i;+ else break;+ }++ if (index === -1) return { kind: 'gap', index: 0 };++ const row = rows[index]!;+ const { rect } = row;+ if (y > rect.bottom) return { kind: 'gap', index: index + 1 };++ const entity = row.node.entity;+ const container = (isGroupLike(entity) || isSequence(entity)) && !forbidden.has(entity);++ if (container) {+ const edge = Math.min(EDGE_ZONE_MAX_PX, rect.height * 0.25);+ if (y < rect.top + edge) return { kind: 'gap', index };+ if (y > rect.bottom - edge) return { kind: 'gap', index: index + 1 };+ return { kind: 'inside', row };+ }++ if (y < rect.top + rect.height / 2) return { kind: 'gap', index };+ return { kind: 'gap', index: index + 1 };+}++/**+ * Turn a gap into a drop. One boundary can close several nested groups, so+ * it offers an insertion at every depth between the row below and one level+ * into the row above; the pointer's X picks among them, the way the dot+ * moves inwards as the drop nests deeper.+ *+ * The column reads top-down while the file reads bottom-up, so the anchor —+ * the document sibling the layer is inserted before — is the nearest row+ * *above* the line at the chosen depth, and a line at the top of a parent's+ * children appends last, which renders frontmost.+ */+export function resolveGap(gapIndex: number, rows: FlatRow[], x: number, context: GapContext): DropTarget | null {+ const above = rows[gapIndex - 1];+ const below = rows[gapIndex];+ const lineY = Math.max((below ? below.rect.top : context.listBottom) - context.containerRect.top, 1);++ if (above === undefined) {+ return { kind: 'line', parent: context.root, anchor: undefined, y: lineY, inset: DOT_CENTER_PX };+ }++ // The gap right under an open container doubles as its topmost slot —+ // for group-likes always, and for clips that already stack layers (masks).+ const canEnterAbove =+ above.node.expanded &&+ !context.forbidden.has(above.node.entity) &&+ (isGroupLike(above.node.entity) || above.node.children.some((child) => child.kind === 'geometry'));++ const maxDepth = above.depth + (canEnterAbove ? 1 : 0);+ const minDepth = Math.min(below?.depth ?? 0, maxDepth);+ const desired = Math.round((x - context.containerRect.left - DOT_CENTER_PX) / NESTED_INDENT_PX);++ let best: { parent: Entity; anchor: Entity | undefined; depth: number } | null = null;++ for (let depth = maxDepth; depth >= minDepth; depth--) {+ let parent: Entity;+ let anchor: Entity | undefined;++ if (depth === above.depth + 1) {+ parent = above.node.entity;+ } else {+ const candidate = depth === 0 ? context.root : above.ancestors[depth - 1];+ if (candidate === undefined || context.forbidden.has(candidate)) continue;+ parent = candidate;+ anchor = scanAnchor(rows, gapIndex, depth, parent, context);+ }++ // Deeper wins a tie: iterating downwards, only a strictly closer depth+ // replaces the one found first.+ if (best === null || Math.abs(depth - desired) < Math.abs(best.depth - desired)) {+ best = { parent, anchor, depth };+ }+ }++ if (best === null) return null;++ return {+ kind: 'line',+ parent: best.parent,+ anchor: best.anchor,+ y: lineY,+ inset: DOT_CENTER_PX + best.depth * NESTED_INDENT_PX,+ };+}++/**+ * The document sibling to insert before: the nearest row above the gap at+ * the chosen depth. Restricted to the dragged layer's stacking bucket —+ * masks and plain layers render as separate runs, so an anchor from the+ * other bucket would land the row somewhere the line never was.+ */+function scanAnchor(+ rows: FlatRow[],+ gapIndex: number,+ depth: number,+ parent: Entity,+ context: GapContext,+): Entity | undefined {+ for (let i = gapIndex - 1; i >= 0; i--) {+ const row = rows[i]!;+ if (row.depth < depth) break;+ if (row.depth > depth) continue;+ if (row.parent !== parent) break;+ // The dragged row is about to leave its spot; it cannot be the anchor.+ if (row.node.entity === context.dragged) continue;+ if (isMask(row.node.entity) === context.draggedIsMask) return row.node.entity;+ }++ return undefined;+}diff --git a/apps/web/src/components/timeline/layers/drop-indicator.tsx b/apps/web/src/components/timeline/layers/drop-indicator.tsxnew file mode 100644index 00000000..47a920ed--- /dev/null+++ b/apps/web/src/components/timeline/layers/drop-indicator.tsx@@ -0,0 +1,55 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++import { Show } from 'solid-js';+import { useLayerContext } from './context';++import type { DropTarget } from './drag';++/** Narrow the drop target to one kind, for keyed `<Show>`s. */+function pick<K extends DropTarget['kind']>(target: DropTarget | null, kind: K) {+ return target?.kind === kind ? (target as Extract<DropTarget, { kind: K }>) : null;+}++/**+ * Where a dragged row would drop: a line with a dot between rows, the dot+ * stepping inwards with the depth of the spot; a border around a container+ * the row would drop into. Positioned in the list's own space, so it rides+ * the scroll transform with the rows.+ */+export function DropIndicator() {+ const { drag } = useLayerContext();++ return (+ <>+ <Show when={pick(drag.target(), 'line')}>+ {(line) => (+ <div+ data-drop-indicator+ class="absolute inset-x-0 top-0 z-30 pointer-events-none"+ style={{ transform: `translateY(${line().y}px)` }}+ >+ <div+ class="absolute right-0 -top-px h-0.5 rounded-full bg-primary"+ style={{ left: `${line().inset + 3}px` }}+ />+ <div+ class="absolute -top-1 size-2 rounded-full border-2 border-primary"+ style={{ left: `${line().inset - 4}px` }}+ />+ </div>+ )}+ </Show>+ <Show when={pick(drag.target(), 'inside')}>+ {(box) => (+ <div+ data-drop-indicator+ class="absolute inset-x-0 z-30 pointer-events-none ring-2 ring-inset ring-primary"+ style={{ top: `${box().top}px`, height: `${box().height}px` }}+ />+ )}+ </Show>+ </>+ );+}diff --git a/apps/web/src/components/timeline/layers/layers.tsx b/apps/web/src/components/timeline/layers/layers.tsxindex 41e5b163..b90e660d 100644--- a/apps/web/src/components/timeline/layers/layers.tsx+++ b/apps/web/src/components/timeline/layers/layers.tsx@@ -41,6 +41,7 @@ import { store } from '@/init';import { createStoredSignal } from '@/lib/store';import { Layer } from './layer';import { LayerContextProvider } from './context';+import { DropIndicator } from './drop-indicator';import { formatFrames, TIME_FORMAT_OPTIONS, type TimeFormat } from '../time-format';import type { TimelineNode } from '@diffusionstudio/runtime';@@ -276,6 +277,7 @@ export function Layers() {<Index each={layers()}>{(layer) => <Layer layer={layer()} />}</Index>+ <DropIndicator /></LayerContextProvider></div></div>diff --git a/apps/web/src/components/timeline/layers/node.tsx b/apps/web/src/components/timeline/layers/node.tsxindex c568bcc5..e0c0f1d3 100644--- a/apps/web/src/components/timeline/layers/node.tsx+++ b/apps/web/src/components/timeline/layers/node.tsx@@ -62,7 +62,8 @@ export function NodeLayer(props: LayerRowProps) {const selected = useTag(entity, Selected);const controlsVisible = createMemo(() => hovering() || muted() || soloed() || hidden());- const [resized, setResized] = useLayerContext().resized;+ const { resized: resizedSignal, drag } = useLayerContext();+ const [resized, setResized] = resizedSignal;const [editing, setEditing] = createSignal(false);let originalName = '';@@ -99,12 +100,17 @@ export function NodeLayer(props: LayerRowProps) {if (!wasSoloed) entity().add(Soloed);};- /** A press on a row selects it, shift-clicking to extend the selection. */+ /**+ * A press on a row selects it, shift-clicking to extend the selection. A+ * plain press also arms a drag: moving far enough turns the press into+ * dragging the layer to a new place in the tree.+ */const handleRowPointerDown = (e: PointerEvent) => {if (e.button !== 0 || resized() !== null) return;if ((e.target as HTMLElement | null)?.closest('button')) return;editor.select(entity(), { extend: e.shiftKey });+ if (!e.shiftKey) drag.begin(e, entity());};let resizeStartY = 0;@@ -187,14 +193,16 @@ export function NodeLayer(props: LayerRowProps) {<ContextMenu><ContextMenuTriggeras="div"+ data-layer-rowclass="w-full text-muted-foreground group relative select-none"onPointerEnter={() => setRowHover(world, entity())}onPointerLeave={() => setRowHover(world, null)}onPointerDown={handleRowPointerDown}classList={{'bg-accent': selected(),- 'bg-accent/70': !selected() && hovering() && resized() === null,+ 'bg-accent/70': !selected() && hovering() && resized() === null && drag.dragging() === null,'bg-accent/40': !selected() && !(hovering() && resized() === null) && props.ancestorSelected,+ 'opacity-60': drag.dragging() === entity(),}}style={{ height: height() + 'px' }}>diff --git a/apps/web/src/engine/timeline/controller.ts b/apps/web/src/engine/timeline/controller.tsindex 6c1a4937..c6e855e6 100644--- a/apps/web/src/engine/timeline/controller.ts+++ b/apps/web/src/engine/timeline/controller.ts@@ -173,6 +173,15 @@ export function createTimelineController(world: World) {});};+ /** Programmatic vertical scroll, for gestures that hold near an edge. */+ const scrollBy = (deltaY: number): void => {+ withScene((scene) => {+ setScrollY(world, scene, getScrollY(world, scene) + deltaY);+ applyScroll();+ updateTimelineTransform(world, scene);+ });+ };+const clientToFrame = (clientX: number): number => {const scene = getTimelineScene(world);if (scene === null) return 0;@@ -256,6 +265,7 @@ export function createTimelineController(world: World) {attachCanvas,detachCanvas,scroll,+ scrollBy,clientToFrame,clientToTime,setMinimized,