The VS Code of video editing, built for agents.
TypeScript
Enhance scene initialization and asset handling in SceneInitOverlay and Timeline components
konstantin-paulusac630bcbf152
diff --git a/apps/web/src/components/canvas/scene-init-overlay.tsx b/apps/web/src/components/canvas/scene-init-overlay.tsxindex 9cef2e83..d35e263f 100644--- a/apps/web/src/components/canvas/scene-init-overlay.tsx+++ b/apps/web/src/components/canvas/scene-init-overlay.tsx@@ -4,8 +4,7 @@import { createMemo, createSignal, For, Show } from "solid-js";import { useQuery, useTrait, useWorld } from "@diffusionstudio/koota-solid";-import { Scene as SceneElement } from "@diffusionstudio/reconciler";-import { ChildOf, getCameraMatrix, getNextName, Root, setCamera, Source } from "@diffusionstudio/runtime";+import { ChildOf, Library, Root, setCamera, Source } from "@diffusionstudio/runtime";import { Icon } from "@/components/ui/icon";import {DropdownMenu,@@ -22,7 +21,11 @@ import {PRESET_CATEGORIES,} from "@/lib/layout-presets";import { useEditor } from "@/engine";+import { droppedFiles, importFiles } from "@/engine/asset-actions";+import { createScene, insertAssetsInNewScene } from "@/engine/new-scene";+import { ASSET_DRAG_TYPE } from "@/components/sidebar-left/folder-item";+import type { Rect } from "@diffusionstudio/runtime";import type { LayoutPreset } from "@/lib/layout-presets";@@ -44,6 +47,7 @@ export function SceneInitOverlay() {const [editing, setEditing] = createSignal(false);const [selectedPreset, setSelectedPreset] = createSignal<LayoutPreset>(DEFAULT_PRESET);const [sceneName, setSceneName] = createSignal(DEFAULT_NAME);+ const [dropping, setDropping] = createSignal(false);let overlayRef: HTMLDivElement | undefined;let buttonRef: HTMLButtonElement | undefined;@@ -86,34 +90,70 @@ export function SceneInitOverlay() {}};- const handleInitializeScene = () => {+ /**+ * Puts the new scene exactly where the placeholder frame is, rather than+ * framing it the way the camera would on its own. A scene made from a+ * dropped asset need not have the placeholder's aspect ratio, so it is+ * fitted inside the frame rather than stretched over it.+ */+ const focusPlaceholder = (rect: Rect) => {const buttonRect = buttonRef?.getBoundingClientRect();+ // The overlay covers the canvas, so its rect is the canvas rect.const canvasRect = overlayRef?.getBoundingClientRect();if (!buttonRect || !canvasRect) return;- const template = selectedPreset();- const worldX = Math.round(-template.width / 2);- const worldY = Math.round(-template.height / 2);-- const name = sceneName() != DEFAULT_NAME- ? sceneName()- : getNextName(world, 'Scene');-- // Fit the camera so the scene appears where the placeholder is: the- // overlay covers the canvas, so its rect is the canvas rect.- const screenX = buttonRect.left - canvasRect.left;- const screenY = buttonRect.top - canvasRect.top;- const scale = buttonRect.width / template.width;- setCamera(world, { a: scale, b: 0, c: 0, d: scale, e: screenX - worldX * scale, f: screenY - worldY * scale });- editor.reportEdit(root, 'camera', getCameraMatrix(world));-- const [scene] = editor.insertElement(root, () => (- <SceneElement name={name} x={worldX} y={worldY} width={template.width} height={template.height} fill="#000000" />- ));- if (!scene) return;-- editor.activate(scene);- editor.select(scene);+ const scale = Math.min(buttonRect.width / rect.width, buttonRect.height / rect.height);+ const screenX = buttonRect.left - canvasRect.left + (buttonRect.width - rect.width * scale) / 2;+ const screenY = buttonRect.top - canvasRect.top + (buttonRect.height - rect.height * scale) / 2;+ setCamera(world, { a: scale, b: 0, c: 0, d: scale, e: screenX - rect.x * scale, f: screenY - rect.y * scale });+ };++ const sceneOptions = () => ({+ ...(sceneName() != DEFAULT_NAME ? { name: sceneName() } : {}),+ format: selectedPreset(),+ focus: focusPlaceholder,+ });++ const handleInitializeScene = () => {+ const scene = createScene(world, selectedPreset(), sceneOptions());+ if (scene) editor.select(scene);+ };++ /**+ * Assets dropped on the placeholder all go into the one scene it becomes,+ * which takes its format from the last of them that has one.+ */+ const handleDrop = async (event: DragEvent) => {+ event.preventDefault();+ event.stopPropagation();+ setDropping(false);++ const library = world.get(Library);+ if (!library) return;++ // Read the transfer before the first await: it is gone by the time an+ // import resolves.+ const assetIds = event.dataTransfer?.getData(ASSET_DRAG_TYPE)?.split(',').filter(Boolean) ?? [];+ const files = droppedFiles(event);++ const assets = assetIds.map((id) => library.get(id)).filter((asset) => asset != null);+ if (files.length) assets.push(...await importFiles(library, files, ''));+ if (!assets.length) return;++ insertAssetsInNewScene(world, assets, sceneOptions());+ };++ const handleDragOver = (event: DragEvent) => {+ event.preventDefault();+ event.stopPropagation();+ setDropping(true);+ };++ const handleDragLeave = (event: DragEvent) => {+ // Moving onto the frame or the name is still a drag over the overlay.+ const to = event.relatedTarget;+ if (to instanceof Node && overlayRef?.contains(to)) return;+ setDropping(false);};return (@@ -121,6 +161,9 @@ export function SceneInitOverlay() {<divref={overlayRef}class="absolute inset-0 z-2 flex items-center justify-center"+ on:drop={handleDrop}+ on:dragover={handleDragOver}+ on:dragleave={handleDragLeave}><divclass="relative"@@ -182,6 +225,7 @@ export function SceneInitOverlay() {<buttontype="button"class="w-full h-full bg-accent/50 border border-border overflow-hidden flex items-center justify-center hover:bg-accent hover:border-input active:bg-muted active:border-input"+ classList={{ 'bg-accent! border-input!': dropping() }}onClick={handleInitializeScene}ref={buttonRef}>diff --git a/apps/web/src/components/timeline/timeline.tsx b/apps/web/src/components/timeline/timeline.tsxindex a94d28f6..77064060 100644--- a/apps/web/src/components/timeline/timeline.tsx+++ b/apps/web/src/components/timeline/timeline.tsx@@ -5,15 +5,14 @@import { onCleanup, onMount } from 'solid-js';import { toast } from 'somoto';import { useWorld } from '@diffusionstudio/koota-solid';-import { FrameRate, framesToSeconds } from '@diffusionstudio/runtime';+import { FrameRate, framesToSeconds, getActiveEntity } from '@diffusionstudio/runtime';import { droppedFiles, importFiles } from '@/engine/asset-actions';import { insertAsset } from '@/engine/insert-asset';+import { insertAssetsInNewScene } from '@/engine/new-scene';import { useLibrary } from '@/engine/library';import { useTimeline } from '@/context/timeline';import { ASSET_DRAG_TYPE } from '@/components/sidebar-left/folder-item';-import type { Asset } from '@diffusionstudio/assets';-/*** The timeline's canvas. What is drawn on it is the timeline system's* business (see `@/engine/timeline`); this is the element it draws on and@@ -31,6 +30,10 @@ export function Timeline() {* Assets dropped on the timeline start where they were dropped, unlike* ones dropped on the canvas, which start at the playhead: the whole point* of aiming at a place on the timeline is to say when.+ *+ * With no scene to drop into, they get one of their own rather than+ * landing loose at the root, sized to the last of them that has a size+ * (see `insertAssetsInNewScene`).*/const handleDrop = async (event: DragEvent) => {event.preventDefault();@@ -42,21 +45,26 @@ export function Timeline() {const fps = world.get(FrameRate)?.value ?? 30;const start = framesToSeconds(Math.max(0, timeline.clientToFrame(event.clientX)), fps);- const place = (asset: Asset) => {- if (!insertAsset(world, asset, { start })) {- toast("Nothing to insert into", { description: "Open a scene first." });- }- };-+ // Read the transfer before the first await: it is gone by the time an+ // import resolves.const ids = event.dataTransfer?.getData(ASSET_DRAG_TYPE)?.split(',').filter(Boolean) ?? [];- for (const id of ids) {- const asset = lib.get(id);- if (asset) place(asset);+ const files = droppedFiles(event);++ const assets = ids.map((id) => lib.get(id)).filter((asset) => asset != null);+ if (files.length) assets.push(...await importFiles(lib, files, ''));+ if (!assets.length) return;++ if (!getActiveEntity(world)) {+ if (!insertAssetsInNewScene(world, assets, { start })) {+ toast("Nothing to insert into", { description: "Open a project first." });+ }+ return;}- const files = droppedFiles(event);- if (files.length) {- for (const asset of await importFiles(lib, files, '')) place(asset);+ for (const asset of assets) {+ if (!insertAsset(world, asset, { start })) {+ toast("Nothing to insert into", { description: "Open a scene first." });+ }}};diff --git a/apps/web/src/engine/new-scene.tsx b/apps/web/src/engine/new-scene.tsxnew file mode 100644index 00000000..e5668dd2--- /dev/null+++ b/apps/web/src/engine/new-scene.tsx@@ -0,0 +1,112 @@+/* 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/. */++// A project's first scene, and the assets that started it. Dropping media+// into a project that has no scene to put it in makes one around it rather+// than leaving the clips loose at the root, which is what `insertAsset`+// would otherwise do. The canvas and the timeline share this; they differ+// only in where the new scene should end up on screen.++import { Scene as SceneElement } from '@diffusionstudio/reconciler';+import { focusRect, getCameraMatrix, getNextName, Root, Source } from '@diffusionstudio/runtime';++import { getDocumentEditor } from './editor';+import { AUDIO_SIZE, insertAsset } from './insert-asset';++import type { Asset } from '@diffusionstudio/assets';+import type { Rect } from '@diffusionstudio/runtime';+import type { Entity, World } from 'koota';++export interface Size {+ width: number;+ height: number;+}++/** The format a new scene gets when nothing going into it brings one. */+export const DEFAULT_SCENE_FORMAT: Size = { width: 1920, height: 1080 };++export interface NewSceneOptions {+ /** The scene's name; the next free "Scene N" by default. */+ name?: string;+ /** The format to fall back to when no asset has one of its own. */+ format?: Size;+ /**+ * Brings the new scene into view, given the rect it occupies in world+ * space. The camera is framed on it by default; the canvas placeholder+ * puts it exactly where the placeholder was instead.+ */+ focus?: (rect: Rect) => void;+ /** Where on the timeline the assets start, in seconds; the playhead by default. */+ start?: number;+}++/**+ * Adds the project's first scene, of `format`, centered on the world origin,+ * makes it the active one, and brings it into view. Returns the scene, or+ * null when there is no project for it to be written under.+ */+export function createScene(world: World, format: Size, options: NewSceneOptions = {}): Entity | null {+ const root = world.get(Root)!;+ if (!root.get(Source)?.value) return null;++ const editor = getDocumentEditor(world);+ const rect: Rect = { x: Math.round(-format.width / 2), y: Math.round(-format.height / 2), ...format };+ const name = options.name ?? getNextName(world, 'Scene');++ // The camera moves first, so the scene is in view the moment it exists.+ (options.focus ?? ((target: Rect) => focusRect(world, target)))(rect);+ editor.reportEdit(root, 'camera', getCameraMatrix(world));++ const [scene] = editor.insertElement(root, () => (+ <SceneElement name={name} x={rect.x} y={rect.y} width={rect.width} height={rect.height} fill="#000000" />+ ));+ if (!scene) return null;++ editor.activate(scene);+ return scene;+}++/**+ * Puts `assets` in a scene of their own and selects them. The scene's format+ * is the last asset that has a size of its own — that one fills the scene+ * exactly, and the rest are centered in it; a drop of nothing but audio or a+ * transcript falls back to `options.format`. Returns the scene, or null when+ * there is nothing to insert into.+ */+export function insertAssetsInNewScene(+ world: World,+ assets: ReadonlyArray<Asset>,+ options: NewSceneOptions = {},+): Entity | null {+ const sized = assets.findLast(hasSize);+ const format = sized ? sizeOf(sized) : options.format ?? DEFAULT_SCENE_FORMAT;++ const scene = createScene(world, format, options);+ if (!scene) return null;++ const inserted: Entity[] = [];+ for (const asset of assets) {+ const size = hasSize(asset) ? sizeOf(asset) : AUDIO_SIZE;+ const entity = insertAsset(world, asset, {+ parent: scene,+ // The scene is new, so its computed bounds are not there to center+ // against yet: the format is what it will measure.+ x: (format.width - size.width) / 2,+ y: (format.height - size.height) / 2,+ start: options.start,+ });+ if (entity) inserted.push(entity);+ }++ getDocumentEditor(world).select(inserted.length ? inserted : scene);+ return scene;+}++/** Assets that bring a format with them; the rest are laid out inside one. */+export const hasSize = (asset: Asset): asset is Extract<Asset, Size> => 'width' in asset && 'height' in asset;++export const sizeOf = (asset: Extract<Asset, Size>): Size => ({+ width: Math.round(asset.width),+ height: Math.round(asset.height),+});