Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor project menu components to utilize new engine structure and improve functionality

konstantin-paulus428217f089de
diff --git a/apps/web/src/components/sidebar-left/project-menu/edit-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/edit-menu.tsx
index 3e28b5ed..67fc9164 100644
--- a/apps/web/src/components/sidebar-left/project-menu/edit-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/edit-menu.tsx
@@ -8,42 +8,34 @@ import {
DropdownMenuShortcut,
DropdownMenuGroup,
} from "@/components/ui/dropdown-menu";
-import { useEngine } from "@/context/engine";
-import { copySelection, pasteClipboard, duplicateSelection, selectStageEntities, selectParentEntities, selectChildEntities, addComponent, removeComponent, clearComponent } from "@/components/engine";
-import { useECS } from "@/context/ecs";
-import { hasComponent, Or, query } from 'bitecs';
+import { useWorld } from "@diffusionstudio/koota-solid";
+import {
+ copySelection,
+ deleteSelection,
+ duplicateSelection,
+ pasteSelection,
+ selectAll,
+ selectChildren,
+ selectParents,
+ toggleSelectionHidden,
+ useEditor,
+ useSelection,
+} from "@/engine";
export function EditMenu() {
- const { world } = useEngine();
- const { selectedNodes } = useECS();
- const hasSelection = () => selectedNodes().size > 0;
- const c = world.components;
-
- const handleToggleHidden = () => {
- const eids = [...query(world, [c.Selected, Or(c.Geometry, c.Group)])];
- const anyVisible = Array.from(eids).some((eid) => !hasComponent(world, eid, c.Hidden));
-
- if (anyVisible) {
- eids.forEach(eid => addComponent(world, eid, c.Hidden));
- } else {
- eids.forEach(eid => removeComponent(world, eid, c.Hidden));
- }
- };
+ const world = useWorld();
+ const editor = useEditor();
+ const { nodes } = useSelection();
+ const hasSelection = () => nodes().length > 0;
return (
<>
<DropdownMenuGroup>
- <DropdownMenuItem
- disabled={!world.history.canUndo}
- onSelect={() => world.history.undo()}
- >
+ <DropdownMenuItem disabled>
Undo
<DropdownMenuShortcut>⌘Z</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem
- disabled={!world.history.canRedo}
- onSelect={() => world.history.redo()}
- >
+ <DropdownMenuItem disabled>
Redo
<DropdownMenuShortcut>⇧⌘Z</DropdownMenuShortcut>
</DropdownMenuItem>
@@ -59,7 +51,7 @@ export function EditMenu() {
Copy
<DropdownMenuShortcut>⌘C</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => pasteClipboard(world)}>
+ <DropdownMenuItem onSelect={() => pasteSelection(world)}>
Paste
<DropdownMenuShortcut>⌘V</DropdownMenuShortcut>
</DropdownMenuItem>
@@ -75,7 +67,10 @@ export function EditMenu() {
<DropdownMenuSeparator />
<DropdownMenuGroup>
- <DropdownMenuItem>
+ <DropdownMenuItem
+ disabled={!hasSelection()}
+ onSelect={() => deleteSelection(world)}
+ >
Delete
<DropdownMenuShortcut>⌫</DropdownMenuShortcut>
</DropdownMenuItem>
@@ -86,7 +81,7 @@ export function EditMenu() {
<DropdownMenuGroup>
<DropdownMenuItem
disabled={!hasSelection()}
- onSelect={handleToggleHidden}
+ onSelect={() => toggleSelectionHidden(world)}
>
Hide
<DropdownMenuShortcut>⇧⌘H</DropdownMenuShortcut>
@@ -96,27 +91,27 @@ export function EditMenu() {
<DropdownMenuSeparator />
<DropdownMenuGroup>
- <DropdownMenuItem onSelect={() => selectStageEntities(world)}>
+ <DropdownMenuItem onSelect={() => selectAll(world)}>
Select all
<DropdownMenuShortcut>⌘A</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem
disabled={!hasSelection()}
- onSelect={() => selectParentEntities(world)}
+ onSelect={() => selectParents(world)}
>
Select parent
<DropdownMenuShortcut>{"\\"}</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem
disabled={!hasSelection()}
- onSelect={() => selectChildEntities(world)}
+ onSelect={() => selectChildren(world)}
>
Select children
<DropdownMenuShortcut>↩︎</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem
disabled={!hasSelection()}
- onSelect={() => clearComponent(world, c.Selected, false)}
+ onSelect={() => editor.clearSelection()}
>
Deselect
<DropdownMenuShortcut>Esc</DropdownMenuShortcut>
diff --git a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
index 57e8aefc..735cf2b6 100644
--- a/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/file-menu.tsx
@@ -237,6 +237,12 @@ export function FileAssetMenu() {
);
}
+/**
+ * The last of this menu still on the bitecs engine: the render pipeline
+ * (`@/context/render` → `@/components/engine/encode`) takes an old-world
+ * scene, so both the scene it exports and the list below have to come from
+ * there. Moves to koota with the encoder (see the TODO in inspector/export).
+ */
export function FileExportMenu() {
const { world } = useEngine();
const { exportScene, exportCurrentFrame } = useExport();
diff --git a/apps/web/src/components/sidebar-left/project-menu/index.tsx b/apps/web/src/components/sidebar-left/project-menu/index.tsx
index 7b98649d..2d2735a5 100644
--- a/apps/web/src/components/sidebar-left/project-menu/index.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/index.tsx
@@ -9,6 +9,7 @@ import {
DropdownMenuItem,
DropdownMenuPortal,
DropdownMenuSeparator,
+ DropdownMenuShortcut,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
@@ -16,7 +17,8 @@ import {
DropdownMenuGroup,
} from "@/components/ui/dropdown-menu";
import { useNavigate } from "@solidjs/router";
-import { Show } from "solid-js";
+import { Show, onCleanup, onMount } from "solid-js";
+import { isInputTarget } from "@/utils";
import { useEditorApi } from "@/context/dapi";
import { downloadDesktopApp } from "@/lib/desktop-app";
import { FileMenu } from "./file-menu";
@@ -40,6 +42,23 @@ export function ProjectMenu() {
navigate("/?dashboard=account");
};
+ /**
+ * Leaving the editor is the app's command rather than the runtime's, so its
+ * key is bound here, with the item that offers it.
+ */
+ const handleShortcut = (event: KeyboardEvent) => {
+ if (!(event.metaKey || event.ctrlKey) || !event.shiftKey) return;
+ if (event.key.toLowerCase() !== "d" || isInputTarget(event)) return;
+
+ event.preventDefault();
+ handleOpenDashboard();
+ };
+
+ onMount(() => {
+ window.addEventListener("keydown", handleShortcut);
+ onCleanup(() => window.removeEventListener("keydown", handleShortcut));
+ });
+
return (
<>
<DropdownMenu placement="bottom-start">
@@ -58,6 +77,7 @@ export function ProjectMenu() {
<DropdownMenuGroup>
<DropdownMenuItem onSelect={handleOpenDashboard}>
Go to dashboard
+ <DropdownMenuShortcut>⇧⌘D</DropdownMenuShortcut>
</DropdownMenuItem>
</DropdownMenuGroup>
diff --git a/apps/web/src/components/sidebar-left/project-menu/view-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/view-menu.tsx
index cce815af..f13ef7c2 100644
--- a/apps/web/src/components/sidebar-left/project-menu/view-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/view-menu.tsx
@@ -8,39 +8,39 @@ import {
DropdownMenuShortcut,
DropdownMenuGroup,
} from "@/components/ui/dropdown-menu";
-import { useEngine } from "@/context/engine";
+import { useWorld } from "@diffusionstudio/koota-solid";
import { useLayout } from "@/context/layout";
-import { useECS } from "@/context/ecs";
+import { useSelection, zoomBy, zoomTo, zoomToFit, zoomToSelection } from "@/engine";
export function ViewMenu() {
- const engine = useEngine();
+ const world = useWorld();
const layout = useLayout();
- const { selectedNodes } = useECS();
+ const { nodes } = useSelection();
- const hasSelection = () => selectedNodes().size > 0;
+ const hasSelection = () => nodes().length > 0;
return (
<>
<DropdownMenuGroup>
- <DropdownMenuItem onSelect={() => engine.camera.zoomBy(1.25)}>
+ <DropdownMenuItem onSelect={() => zoomBy(world, 1.25)}>
Zoom in
<DropdownMenuShortcut>⌘+</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => engine.camera.zoomBy(0.8)}>
+ <DropdownMenuItem onSelect={() => zoomBy(world, 0.8)}>
Zoom out
<DropdownMenuShortcut>⌘-</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => engine.camera.resetZoom()}>
+ <DropdownMenuItem onSelect={() => zoomTo(world, 1)}>
Zoom to 100%
<DropdownMenuShortcut>⌘0</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => engine.camera.fitToActiveScene()}>
+ <DropdownMenuItem onSelect={() => zoomToFit(world)}>
Zoom to fit
<DropdownMenuShortcut>⌘1</DropdownMenuShortcut>
</DropdownMenuItem>
<DropdownMenuItem
disabled={!hasSelection()}
- onSelect={() => engine.camera.fitToSelection()}
+ onSelect={() => zoomToSelection(world)}
>
Zoom to selection
<DropdownMenuShortcut>⌘2</DropdownMenuShortcut>
diff --git a/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx b/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
index a54f7c50..f775a305 100644
--- a/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/inspector-header.tsx
@@ -13,32 +13,16 @@ import {
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { focusContent, getCameraMatrix, Root, setCameraZoom, zoomCameraBy } from "@diffusionstudio/runtime";
-import { useCameraScale } from "@/engine";
-import { useEditor } from "@/engine/hooks/use-editor";
+import { useCameraScale, zoomBy, zoomTo, zoomToFit } from "@/engine";
export function InspectorHeader() {
const world = useWorld();
- const editor = useEditor();
// Every operation here is anchored on the stage itself — its center, or its
// content — so the menu needs no canvas geometry of its own.
const scale = useCameraScale();
const zoomLabel = () => `${Math.round(scale() * 100)}%`;
- const zoomBy = (factor: number) => {
- zoomCameraBy(world, factor);
- editor.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
- };
- const zoomTo = (percent: number) => {
- setCameraZoom(world, percent / 100);
- editor.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
- };
- const zoomToFit = () => {
- focusContent(world);
- editor.reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
- };
-
return (
<div class="h-12 shrink-0 flex items-center px-4">
<span class="text-[12px] font-450 text-foreground">
@@ -60,26 +44,26 @@ export function InspectorHeader() {
/>
<DropdownMenuPortal>
<DropdownMenuContent class="w-40">
- <DropdownMenuItem onSelect={() => zoomBy(1.25)}>
+ <DropdownMenuItem onSelect={() => zoomBy(world, 1.25)}>
Zoom in
<DropdownMenuShortcut>⌘+</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => zoomBy(0.8)}>
+ <DropdownMenuItem onSelect={() => zoomBy(world, 0.8)}>
Zoom out
<DropdownMenuShortcut>⌘-</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={zoomToFit}>
+ <DropdownMenuItem onSelect={() => zoomToFit(world)}>
Zoom to fit
<DropdownMenuShortcut>⌘1</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => zoomTo(50)}>
+ <DropdownMenuItem onSelect={() => zoomTo(world, 0.5)}>
Zoom to 50%
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => zoomTo(100)}>
+ <DropdownMenuItem onSelect={() => zoomTo(world, 1)}>
Zoom to 100%
<DropdownMenuShortcut>⌘0</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => zoomTo(200)}>
+ <DropdownMenuItem onSelect={() => zoomTo(world, 2)}>
Zoom to 200%
</DropdownMenuItem>
</DropdownMenuContent>
diff --git a/apps/web/src/engine/camera.ts b/apps/web/src/engine/camera.ts
new file mode 100644
index 00000000..75e23415
--- /dev/null
+++ b/apps/web/src/engine/camera.ts
@@ -0,0 +1,54 @@
+/* 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/. */
+
+/**
+ * What a menu item, a key or the zoom control does to the view. The camera is
+ * the project's — the file says where it stands — so every move of it is
+ * reported to the document, the same way a pan is (see `CameraController`).
+ */
+
+import {
+ Root,
+ focusContent,
+ focusEntities,
+ getCameraMatrix,
+ getSelection,
+ setCameraZoom,
+ zoomCameraBy,
+} from '@diffusionstudio/runtime';
+
+import { getDocumentEditor } from './editor';
+
+import type { World } from 'koota';
+
+function reportCamera(world: World): void {
+ getDocumentEditor(world).reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
+}
+
+/** Zooms around the center of the viewport: above 1 in, below 1 out. */
+export function zoomBy(world: World, factor: number): void {
+ zoomCameraBy(world, factor);
+ reportCamera(world);
+}
+
+/** Zooms to an absolute scale, 1 being 100%. */
+export function zoomTo(world: World, scale: number): void {
+ setCameraZoom(world, scale);
+ reportCamera(world);
+}
+
+/** Frames everything on the stage. */
+export function zoomToFit(world: World): void {
+ focusContent(world);
+ reportCamera(world);
+}
+
+/** Frames the selection, wherever on the stage it sits. */
+export function zoomToSelection(world: World): void {
+ const selected = getSelection(world);
+ if (!selected.length) return;
+
+ focusEntities(world, selected);
+ reportCamera(world);
+}
diff --git a/apps/web/src/engine/index.ts b/apps/web/src/engine/index.ts
index 1dc8a819..085cf59e 100644
--- a/apps/web/src/engine/index.ts
+++ b/apps/web/src/engine/index.ts
@@ -5,6 +5,7 @@
export * from './create-engine';
export * from './context';
export * from './canvas';
+export * from './camera';
export * from './camera-controller';
export * from './hooks';
export * from './editor';
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index 9bfe191f..efcc8223 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -16,8 +16,6 @@ import {
Time,
Tool,
ToolType,
- focusContent,
- focusEntities,
getActiveEntity,
getCameraMatrix,
getEntityChildren,
@@ -26,14 +24,13 @@ import {
getSelection,
isGroupLike,
isSequence,
- resetCameraZoom,
setPlayhead,
store,
togglePlayback,
- zoomCameraBy,
} from '@diffusionstudio/runtime';
import { Not, Or } from 'koota';
+import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';
import { getDocumentEditor } from '../editor';
import { splitAtPlayhead } from '../split';
import { Keys, Pointer } from '../traits';
@@ -253,39 +250,9 @@ const seekSeconds = (seconds: number) => (world: World): void =>
/** How much of the zoom a step takes, in or out. */
const ZOOM_STEP = 1.25;
-/**
- * The camera is the document's, so a zoom travels like a pan does (see
- * `CameraController`): the world already holds the new matrix, the file is
- * told what it now says.
- */
-function reportCamera(world: World): void {
- getDocumentEditor(world).reportEdit(world.get(Root)!, 'camera', getCameraMatrix(world));
-}
+const zoom = (factor: number) => (world: World): void => zoomBy(world, factor);
-const zoomBy = (factor: number) => (world: World): void => {
- zoomCameraBy(world, factor);
- reportCamera(world);
-};
-
-function resetZoom(world: World): void {
- resetCameraZoom(world);
- reportCamera(world);
-}
-
-/** Frames everything on the stage — the zoom menu's "Zoom to fit". */
-function focusAll(world: World): void {
- focusContent(world);
- reportCamera(world);
-}
-
-/** Frames what is selected, wherever on the stage it sits. */
-function focusSelection(world: World): void {
- const selected = getSelection(world);
- if (!selected.length) return;
-
- focusEntities(world, selected);
- reportCamera(world);
-}
+const zoomActualSize = (world: World): void => zoomTo(world, 1);
/**
* Hides the selection, or brings it back once all of it is hidden — the same
@@ -339,6 +306,18 @@ export function restackSelection(world: World, target: 'front' | 'back'): void {
const restack = (target: 'front' | 'back') => (world: World): void => restackSelection(world, target);
+/**
+ * Selects everything the stage holds directly — the scenes and whatever else
+ * sits loose on it, rather than what is inside them.
+ */
+export function selectAll(world: World): void {
+ const root = world.get(Root);
+ if (!root) return;
+
+ const entities = [...world.query(NODES, ChildOf(root), Not(Hidden), Not(Culled))];
+ if (entities.length) getDocumentEditor(world).select(entities);
+}
+
/**
* Selects what holds the selection. A sequence is a container the timeline
* draws rather than a node the canvas selects, so the walk goes on through
@@ -395,18 +374,19 @@ function deselect(world: World): void {
const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['backspace'], action: deleteSelection },
{ keys: ['delete'], action: deleteSelection },
- { keys: ['d', 'mod'], action: duplicateSelection },
+ { keys: ['d', 'mod', '!shift'], action: duplicateSelection },
{ keys: ['b', 'mod'], action: splitAtPlayhead },
{ keys: ['c', 'mod'], action: copySelection },
{ keys: ['v', 'mod'], action: pasteSelection },
{ keys: ['x', 'mod'], action: cutSelection },
{ keys: ['h', 'mod', 'shift'], action: toggleSelectionHidden },
- { keys: ['=', 'mod'], action: zoomBy(ZOOM_STEP) },
- { keys: ['+', 'mod'], action: zoomBy(ZOOM_STEP) },
- { keys: ['-', 'mod'], action: zoomBy(1 / ZOOM_STEP) },
- { keys: ['0', 'mod'], action: resetZoom },
- { keys: ['1', 'mod'], action: focusAll },
- { keys: ['2', 'mod'], action: focusSelection },
+ { keys: ['a', 'mod'], action: selectAll },
+ { keys: ['=', 'mod'], action: zoom(ZOOM_STEP) },
+ { keys: ['+', 'mod'], action: zoom(ZOOM_STEP) },
+ { keys: ['-', 'mod'], action: zoom(1 / ZOOM_STEP) },
+ { keys: ['0', 'mod'], action: zoomActualSize },
+ { keys: ['1', 'mod'], action: zoomToFit },
+ { keys: ['2', 'mod'], action: zoomToSelection },
{ keys: ['v', '!mod'], action: selectTool(ToolType.MOVE) },
{ keys: ['h', '!mod'], action: selectTool(ToolType.HAND) },
{ keys: ['f', '!mod'], action: selectTool(ToolType.SCENE) },