Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: implement node context menu for right-click actions on canvas nodes

konstantin-paulusd3d2a0b46821
diff --git a/apps/web/src/components/canvas/canvas.tsx b/apps/web/src/components/canvas/canvas.tsx
index 92d7ee43..0c713142 100644
--- a/apps/web/src/components/canvas/canvas.tsx
+++ b/apps/web/src/components/canvas/canvas.tsx
@@ -4,11 +4,11 @@
import { useWorld } from "@diffusionstudio/koota-solid";
import { findSceneAt, screenToWorld, worldToLocal, Library, Root } from "@diffusionstudio/runtime";
-import { CameraController, EngineCanvas } from "@/engine";
+import { CameraController, EngineCanvas, nodesAt } from "@/engine";
import { insertAsset } from "@/engine/insert-asset";
import { droppedFiles, importFiles } from "@/engine/asset-actions";
import { Toolbar } from "./toolbar";
-import { CanvasCommands } from "@/components/commands";
+import { CanvasCommands, NodeContextMenu } from "@/components/commands";
import { DesktopAppBanner } from "./desktop-app-banner";
import { toast } from "somoto"
import { SceneInitOverlay } from "./scene-init-overlay";
@@ -79,7 +79,9 @@ export function Canvas() {
<CanvasCommands />
<DesktopAppBanner />
<SceneInitOverlay />
- <EngineCanvas />
+ <NodeContextMenu class="size-full" pick={(event) => nodesAt(world, event)}>
+ <EngineCanvas />
+ </NodeContextMenu>
<CameraController />
</div>
</div>
diff --git a/apps/web/src/components/commands/index.tsx b/apps/web/src/components/commands/index.tsx
index 125ee217..6a302340 100644
--- a/apps/web/src/components/commands/index.tsx
+++ b/apps/web/src/components/commands/index.tsx
@@ -20,6 +20,7 @@ import { UpscaleCommand } from "./upscale";
export { CommandBar } from "./command-bar";
export { CommandPalette } from "./palette";
+export { NodeContextMenu } from "./node-menu";
export { ReloadWindowCommand } from "./reload-window";
/**
diff --git a/apps/web/src/components/commands/node-menu.tsx b/apps/web/src/components/commands/node-menu.tsx
new file mode 100644
index 00000000..ca303397
--- /dev/null
+++ b/apps/web/src/components/commands/node-menu.tsx
@@ -0,0 +1,137 @@
+/* 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 menu a right-click on nodes puts up, the way Figma's does: the click
+// selects what it lands on first (unless that is selected already, so a
+// selection of several survives it), and the menu lists what the palette
+// offers for that selection. Every surface that shows nodes wraps itself in
+// one, and says which nodes are under the pointer.
+
+import { createSignal, For, Show } from "solid-js";
+import { useWorld } from "@diffusionstudio/koota-solid";
+import {
+ ContextMenu,
+ ContextMenuContent,
+ ContextMenuGroup,
+ ContextMenuItem,
+ ContextMenuPortal,
+ ContextMenuSeparator,
+ ContextMenuShortcut,
+ ContextMenuTrigger,
+} from "@/components/ui/context-menu";
+import { useCommands } from "@/context/commands";
+import { getDocumentEditor, useSelection } from "@/engine";
+
+import type { Entity } from "koota";
+import type { JSX } from "solid-js";
+import type { RegisteredCommand } from "@/context/commands";
+
+/**
+ * The commands listed over a selection, by id, a rule between sections. One
+ * that has nothing in the selection to work on is left out, and a section
+ * left with none goes with it.
+ */
+const NODE_SECTIONS = [
+ ["shortcut:copy", "shortcut:cut", "shortcut:paste", "shortcut:duplicate", "shortcut:delete"],
+ [
+ "shortcut:group",
+ "shortcut:ungroup",
+ "shortcut:wrap-in-scene",
+ "shortcut:wrap-in-sequence",
+ "shortcut:unwrap-sequence",
+ ],
+ ["shortcut:bring-to-front", "shortcut:send-to-back", "shortcut:toggle-hidden"],
+ ["upscale", "remove-background", "remove-silence", "auto-captions"],
+ ["edit-with-prompt", "make-video", "rerun", "reuse-settings"],
+];
+
+/** The commands listed over nothing. */
+const STAGE_SECTIONS = [["shortcut:paste", "shortcut:select-all"]];
+
+/** Whether `command` would run over the selection rather than ask for a target. */
+const offered = (command: RegisteredCommand) => (command.available?.() ?? true) && (command.ready?.() ?? true);
+
+export function NodeContextMenu(props: {
+ /** The nodes a right-click at `event` is on; empty over nothing. */
+ pick(event: MouseEvent): Entity[];
+ class?: string;
+ children: JSX.Element;
+}) {
+ const world = useWorld();
+ const { commands, execute } = useCommands();
+ const { nodes } = useSelection();
+ const [frozen, setFrozen] = createSignal<RegisteredCommand[][] | null>(null);
+
+ const select = (event: MouseEvent) => {
+ const editor = getDocumentEditor(world);
+ const picked = props.pick(event);
+ const selected = nodes();
+
+ if (!picked.length) editor.clearSelection();
+ else if (!picked.every((node) => selected.includes(node))) editor.select(picked);
+ };
+
+ /**
+ * What is on offer for the selection. Followed while the menu is open:
+ * what the selected media is (which `upscale` and the other media commands
+ * look at) is read off the engine once per tick, so a selection the
+ * right-click just made shows up in it a frame after the menu opens.
+ */
+ const live = () => {
+ const byId = new Map(commands().map((command) => [command.id, command]));
+ const layout = nodes().length ? NODE_SECTIONS : STAGE_SECTIONS;
+ return layout
+ .map((ids) => ids.map((id) => byId.get(id)).filter((command) => command !== undefined).filter(offered))
+ .filter((section) => section.length > 0);
+ };
+
+ /**
+ * Holds the list as it stands until the menu is open again: a command run
+ * from it changes the selection, and the menu should not change under the
+ * pointer while it fades out.
+ */
+ const freeze = () => {
+ if (frozen() === null) setFrozen(live());
+ };
+
+ const sections = () => frozen() ?? live();
+
+ return (
+ <ContextMenu onOpenChange={(open) => (open ? setFrozen(null) : freeze())}>
+ <ContextMenuTrigger class={props.class} onContextMenu={select}>
+ {props.children}
+ </ContextMenuTrigger>
+ <ContextMenuPortal>
+ <ContextMenuContent class="w-[220px]">
+ <For each={sections()}>
+ {(section, index) => (
+ <>
+ <Show when={index() > 0}>
+ <ContextMenuSeparator />
+ </Show>
+ <ContextMenuGroup>
+ <For each={section}>
+ {(command) => (
+ <ContextMenuItem
+ onSelect={() => {
+ freeze();
+ execute(command);
+ }}
+ >
+ {command.title}
+ <Show when={command.shortcut}>
+ {(keys) => <ContextMenuShortcut>{keys().join("")}</ContextMenuShortcut>}
+ </Show>
+ </ContextMenuItem>
+ )}
+ </For>
+ </ContextMenuGroup>
+ </>
+ )}
+ </For>
+ </ContextMenuContent>
+ </ContextMenuPortal>
+ </ContextMenu>
+ );
+}
diff --git a/apps/web/src/components/commands/rerun.tsx b/apps/web/src/components/commands/rerun.tsx
index 0ad8cf5f..5850d57b 100644
--- a/apps/web/src/components/commands/rerun.tsx
+++ b/apps/web/src/components/commands/rerun.tsx
@@ -17,12 +17,15 @@ import {
} from "@/components/canvas/action-bar";
export function RerunCommand() {
+ const { media } = useMediaSelection();
+
useCommand({
id: "rerun",
title: "Rerun generation",
group: "Generate",
icon: "rerun",
keywords: ["regenerate", "again", "retry"],
+ available: () => media().some((entry) => entry.asset?.job !== undefined),
bar: RerunBar,
});
diff --git a/apps/web/src/components/commands/shortcuts.tsx b/apps/web/src/components/commands/shortcuts.tsx
index 215c68f2..7c09c558 100644
--- a/apps/web/src/components/commands/shortcuts.tsx
+++ b/apps/web/src/components/commands/shortcuts.tsx
@@ -19,13 +19,20 @@ import {
} from "@diffusionstudio/runtime";
import { useCommand } from "@/context/commands";
import {
+ copySelection,
+ cutSelection,
+ deleteSelection,
+ duplicateSelection,
+ getDocumentEditor,
getEditHistory,
groupSelection,
+ pasteSelection,
redoEdit,
restackSelection,
seekBy,
seekToSelectionEdge,
seekToTimelineEdge,
+ selectAll,
splitAtPlayhead,
toggleSelectionHidden,
undoEdit,
@@ -39,13 +46,14 @@ import {
import type { Entity, World } from "koota";
import type { Section } from "@/context/commands";
-import type { EditHistory } from "@/engine";
+import type { DocumentEditor, EditHistory } from "@/engine";
/** What a shortcut needs to have something to work on. */
type ShortcutState = {
/** The selected nodes. */
nodes: Entity[];
history: EditHistory;
+ editor: DocumentEditor;
scene: Entity | null;
};
@@ -53,7 +61,7 @@ type Shortcut = {
id: string;
title: string;
group: Section;
- icon: string;
+ icon?: string;
/** Its keys, one cap each. */
keys: string[];
keywords?: string[];
@@ -301,6 +309,58 @@ const SHORTCUTS: Shortcut[] = [
run: redoEdit,
available: ({ history }) => history.canRedo(),
},
+ {
+ id: "copy",
+ title: "Copy",
+ group: "Edit",
+ keys: ["⌘", "C"],
+ keywords: ["clipboard"],
+ run: copySelection,
+ available: selection,
+ },
+ {
+ id: "cut",
+ title: "Cut",
+ group: "Edit",
+ keys: ["⌘", "X"],
+ keywords: ["clipboard"],
+ run: cutSelection,
+ available: selection,
+ },
+ {
+ id: "paste",
+ title: "Paste",
+ group: "Edit",
+ keys: ["⌘", "V"],
+ keywords: ["clipboard"],
+ run: pasteSelection,
+ available: ({ editor }) => editor.canPaste(),
+ },
+ {
+ id: "duplicate",
+ title: "Duplicate",
+ group: "Edit",
+ keys: ["⌘", "D"],
+ keywords: ["copy", "clone"],
+ run: duplicateSelection,
+ available: selection,
+ },
+ {
+ id: "delete",
+ title: "Delete",
+ group: "Edit",
+ keys: ["⌫"],
+ keywords: ["remove"],
+ run: deleteSelection,
+ available: selection,
+ },
+ {
+ id: "select-all",
+ title: "Select all",
+ group: "Edit",
+ keys: ["⌘", "A"],
+ run: selectAll,
+ },
];
/** Every shortcut as a command, sharing one read of what they work on. */
@@ -309,8 +369,9 @@ export function ShortcutCommands() {
const { nodes } = useSelection();
const scene = useActiveScene();
const history = getEditHistory(world);
+ const editor = getDocumentEditor(world);
- const state = (): ShortcutState => ({ nodes: nodes(), history, scene: scene() });
+ const state = (): ShortcutState => ({ nodes: nodes(), history, editor, scene: scene() });
return <For each={SHORTCUTS}>{(shortcut) => <ShortcutCommand shortcut={shortcut} state={state} />}</For>;
}
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index 57f120c1..443be335 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -816,6 +816,11 @@ export class DocumentEditor {
this.clipboard = { trees, parent: getParentEntity(roots[0]!)?.get(Source)?.value };
}
+ /** Whether `paste` has anything to insert. Not reactive: read it when asked. */
+ public canPaste(): boolean {
+ return this.clipboard.trees.length > 0;
+ }
+
/**
* Inserts what `copy` took under `parent`, in front of `anchor` or last,
* with the same props it was copied with — same position, if the parent is
diff --git a/apps/web/src/engine/input/input-system.ts b/apps/web/src/engine/input/input-system.ts
index 92428f39..453b7320 100644
--- a/apps/web/src/engine/input/input-system.ts
+++ b/apps/web/src/engine/input/input-system.ts
@@ -2,14 +2,14 @@
* 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 { HitRegions, Tool, ToolType, Dragging, Hovering, isPointerInEntity, pointInQuad } from '@diffusionstudio/runtime';
+import { HitRegions, RenderSurface, Tool, ToolType, Dragging, Hovering, getSelection, isPointerInEntity, pointInQuad } from '@diffusionstudio/runtime';
import { getEditHistory } from '../history';
import { Keys, Pointer, PointerEvents } from '../traits';
import { getToolCursor, updateCursor } from './cursor';
import { handleCanvasInteraction, handleGeometryInteraction } from './interactions';
-import type { World } from 'koota';
+import type { Entity, World } from 'koota';
import type { CursorType } from './cursor';
import type { CanvasPointerEvent, DispatchedPointerEvent, HitRegion, PointerEventType } from '@diffusionstudio/runtime';
@@ -41,6 +41,32 @@ function hitTest(world: World, event: CanvasPointerEvent): HitRegion | null {
return null;
};
+/**
+ * The nodes a right-click at `event` is on: the one under the pointer, the
+ * node a header or tool overlay belongs to, or the selection when it lands
+ * on the selection's box or handles. Empty over the bare stage. Tested
+ * against the regions the last frame drew, so it answers between ticks.
+ */
+export function nodesAt(world: World, event: MouseEvent): Entity[] {
+ const canvas = world.get(RenderSurface)?.canvas;
+ if (!(canvas instanceof HTMLCanvasElement)) return [];
+
+ const rect = canvas.getBoundingClientRect();
+ const resolution = world.get(RenderSurface)?.resolution ?? 1;
+ const region = hitTest(world, {
+ type: 'pointerdown',
+ clientX: (event.clientX - rect.left) * resolution,
+ clientY: (event.clientY - rect.top) * resolution,
+ button: event.button,
+ } as CanvasPointerEvent);
+
+ if (region === null) return [];
+ if (region.target.kind === 'entity') return [region.target.id];
+ if (region.target.entity) return [region.target.entity];
+ if (region.target.id === 'canvas') return [];
+ return getSelection(world);
+}
+
function dispatch(
world: World,
region: HitRegion,