The VS Code of video editing, built for agents.
TypeScript
Implement Clip Path Tool and Enhance Toolbar Functionality
konstantin-paulusa6e27ef3f24f
diff --git a/apps/web/src/components/canvas/clip-path-bar.tsx b/apps/web/src/components/canvas/clip-path-bar.tsxnew file mode 100644index 00000000..50340330--- /dev/null+++ b/apps/web/src/components/canvas/clip-path-bar.tsx@@ -0,0 +1,68 @@+/* 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, createEffect } from "solid-js";+import { useTrait, useWorld } from "@diffusionstudio/koota-solid";+import { Name } from "@diffusionstudio/runtime";+import { Button } from "@/components/ui/button";+import { Separator } from "@/components/ui/separator";+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";+import { useDerived } from "@/engine/hooks";+import { applyClipPaths, cancelClipPath, getClipPathPicks, getClipPathTarget } from "@/engine/clip-path";++import type { Entity } from "koota";++/**+ * The clip path tool's action bar, over the canvas while the tool is up: how+ * many of the selected shapes would clip the target, and the way out —+ * Cancel leaves the document as it was, Confirm makes them clip paths of the+ * target.+ */+export function ClipPathBar() {+ const world = useWorld();++ const target = useDerived(getClipPathTarget);+ const picks = useDerived(() => getClipPathPicks(world), sameEntities);++ // Nothing to aim at (the target was deleted, or undone away): the tool has no work left.+ createEffect(() => {+ if (!target()) cancelClipPath(world);+ });++ return (+ <Show when={target()}>+ {(node) => (+ <div class="absolute bottom-16 left-1/2 -translate-x-1/2 z-10 rounded-xl px-1.5 py-1 bg-background border border-border flex gap-1 items-center">+ <span class="px-2 text-xs text-muted-foreground whitespace-nowrap">+ {picks().length === 0+ ? "Pick a shape to use as the clipping source"+ : `${picks().length} ${picks().length === 1 ? "shape" : "shapes"} selected`}+ </span>+ <Separator orientation="vertical" class="min-h-5" />+ <Tooltip>+ <TooltipTrigger as={Button} variant="ghost" class="text-muted-foreground" onClick={() => cancelClipPath(world)}>+ Cancel+ </TooltipTrigger>+ <TooltipContent shortcut="Esc">Cancel</TooltipContent>+ </Tooltip>+ <Tooltip>+ <TooltipTrigger as={Button} disabled={picks().length === 0} onClick={() => applyClipPaths(world)}>+ Confirm+ </TooltipTrigger>+ <TooltipContent shortcut="⌘↵">Clip <TargetName target={node()} /></TooltipContent>+ </Tooltip>+ </div>+ )}+ </Show>+ );+}++function TargetName(props: { target: Entity }) {+ const name = useTrait(() => props.target, Name);+ return <span class="text-foreground">{name()?.value || "this layer"}</span>;+}++function sameEntities(a: Entity[], b: Entity[]): boolean {+ return a.length === b.length && a.every((entity, i) => entity === b[i]);+}diff --git a/apps/web/src/components/canvas/toolbar.tsx b/apps/web/src/components/canvas/toolbar.tsxindex 4fa77453..f49d29a2 100644--- a/apps/web/src/components/canvas/toolbar.tsx+++ b/apps/web/src/components/canvas/toolbar.tsx@@ -17,10 +17,11 @@ import {import { PromptInput } from "../genai/prompt-input";import { ActionBar } from "../genai/action-bar";import { ObjectMaskBar } from "./object-mask-bar";-import { For, Show, createMemo } from "solid-js";+import { ClipPathBar } from "./clip-path-bar";+import { For, Match, Show, Switch, createEffect, createMemo } from "solid-js";import { Tool, ToolType } from "@diffusionstudio/runtime";import { useWorld } from "@diffusionstudio/koota-solid";-import { useTool } from "@/engine";+import { clearClipPathTarget, useTool } from "@/engine";import { usePromptInput } from "@/context/prompt-input";const CURSOR_TOOLS = [@@ -35,6 +36,12 @@ export function Toolbar() {const selectedTool = useTool();const cursorTool = createMemo(() => CURSOR_TOOLS.find((cursor) => cursor.tool === selectedTool()));+ createEffect(() => {+ if (selectedTool() !== ToolType.CLIP_PATH) {+ clearClipPathTarget();+ }+ });+const handleToolChange = (tool: ToolType) => {world.set(Tool, { value: tool });}@@ -45,12 +52,14 @@ export function Toolbar() {<PromptInput initialConfig={promptInputConfig()} /></Show><Show when={!promptInputOpen()}>- <Show- when={selectedTool() === ToolType.OBJECT_MASK}- fallback={<ActionBar openPromptInput={openPromptInput} />}- >- <ObjectMaskBar />- </Show>+ <Switch fallback={<ActionBar openPromptInput={openPromptInput} />}>+ <Match when={selectedTool() === ToolType.OBJECT_MASK}>+ <ObjectMaskBar />+ </Match>+ <Match when={selectedTool() === ToolType.CLIP_PATH}>+ <ClipPathBar />+ </Match>+ </Switch></Show><div class="absolute bottom-4 left-1/2 -translate-x-1/2 rounded-xl p-1.5 bg-background border border-border-strong flex gap-2 items-center z-10"><div class="flex gap-1">diff --git a/apps/web/src/components/sidebar-right/inspector/clip-paths.tsx b/apps/web/src/components/sidebar-right/inspector/clip-paths.tsxindex 7bccbe3b..e1ca205c 100644--- a/apps/web/src/components/sidebar-right/inspector/clip-paths.tsx+++ b/apps/web/src/components/sidebar-right/inspector/clip-paths.tsx@@ -9,19 +9,12 @@ import { Icon } from "@/components/ui/icon";import { ItemRow } from "@/components/ui/item-row";import { PanelSection } from "@/components/ui/panel-section";import { useTrait, useWorld } from "@diffusionstudio/koota-solid";-import { Rect } from "@diffusionstudio/reconciler";-import { Cache, Computed, Name, getNextName } from "@diffusionstudio/runtime";+import { Cache, Name } from "@diffusionstudio/runtime";+import { beginClipPathFor } from "@/engine/clip-path";import { useDerived, useEditor } from "@/engine/hooks";import type { Entity } from "koota";-/**- * Where a new clip path sits in its parent, px. Offset rather than flush so- * the clip path shows itself the moment it is added: at the parent's own size- * it clips a band off two edges instead of landing invisibly on top of it.- */-const CLIP_PATH_INSET = 20;-// Stable identity, so a node without clip paths does not resample every tick.const NO_CLIP_PATHS: Entity[] = [];@@ -33,9 +26,9 @@ type ClipPathsSettingsProps = {* The `<rect clipPath>` children of the selected node: the boxes it is* clipped to (several intersect). A clip path is a rect like any other, so it* has no inspector of its own — a row selects it and the transform, time and- * appearance panels are then its own. Its `fill`, `opacity` and paint- * children have no effect (a clip path is never drawn), which is why the plus- * authors neither.+ * appearance panels are then its own. The plus picks up the clip path tool,+ * whose bar over the canvas turns the rects selected there into clip paths+ * of this node.*/export function ClipPathsSettings(props: ClipPathsSettingsProps) {const world = useWorld();@@ -45,23 +38,6 @@ export function ClipPathsSettings(props: ClipPathsSettingsProps) {// Cache is derived state, written without change events.const clipPaths = useDerived(() => entity().get(Cache)?.clipPaths ?? NO_CLIP_PATHS);- const handleAppendClipPath = () => {- const node = entity();- const computed = node.get(Computed);- const width = Math.round(computed?.width ?? 0);- const height = Math.round(computed?.height ?? 0);- // A clip path without a size of its own is 500x500, which is the honest- // answer for a parent that has no box yet (an empty group).- const size = width > 0 && height > 0 ? { width, height } : {};-- const [clipPath] = editor.insertElement(node, () => (- <Rect clipPath name={getNextName(world, "Clip Path")} x={CLIP_PATH_INSET} y={CLIP_PATH_INSET} {...size} />- ));-- // The clip path is what the user came to place, so the selection moves to it.- if (clipPath) editor.select(clipPath);- };-return (<PanelSectiontitle="Clip Paths"@@ -72,7 +48,7 @@ export function ClipPathsSettings(props: ClipPathsSettingsProps) {size="icon"variant="ghost"class="text-muted-foreground"- onClick={handleAppendClipPath}+ onClick={() => beginClipPathFor(world, entity())}><Icon name="plus-add" /></TooltipTrigger>diff --git a/apps/web/src/engine/clip-path.ts b/apps/web/src/engine/clip-path.tsnew file mode 100644index 00000000..cd801e2c--- /dev/null+++ b/apps/web/src/engine/clip-path.ts@@ -0,0 +1,142 @@+/* 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 {+ Computed,+ Flip,+ Source,+ Tool,+ ToolType,+ decompose2D,+ entityAnchor,+ entityOffset,+ entityWorldMat,+ getEntityTree,+ getIntrinsicPaint,+ getSelection,+ invert2D,+ isClipPath,+ isShape,+ multiply2D,+ store,+ translate2D,+} from '@diffusionstudio/runtime';+import { createSignal } from 'solid-js';++import { getDocumentEditor } from './editor';+import { editTransform } from './input/interactions';++import type { Entity, World } from 'koota';+import type { TransformWrite } from './input/interactions';++const round2 = (value: number): number => Math.round(value * 100) / 100;++// The node the tool is aimed at, reactive so the bar follows it.+const [clipPathTarget, setClipPathTarget] = createSignal<Entity | null>(null);++/** The node the picked rects will clip, reactive; null while the tool is down. */+export { clipPathTarget };++export function getClipPathTarget(): Entity | null {+ const target = clipPathTarget();+ return target?.isAlive() ? target : null;+}++/** Picks the tool to add clip paths to `target`. The selection stays on it until a rect is picked. */+export function beginClipPathFor(world: World, target: Entity): void {+ setClipPathTarget(target);+ world.set(Tool, { value: ToolType.CLIP_PATH });+}++/** Forgets the target: the tool is down. */+export function clearClipPathTarget(): void {+ setClipPathTarget(null);+}++/**+ * Whether `entity` can become a clip path of `target`: a plain rect (a clip+ * path is always a `<rect>`, so media, text and containers are out), not one+ * already, and not the target or something the target sits inside — a node+ * cannot be moved into its own subtree.+ */+export function canClipWith(world: World, target: Entity, entity: Entity): boolean {+ if (entity === target || !entity.isAlive() || !entity.get(Source)?.value) return false;+ if (!isShape(entity) || isClipPath(entity) || getIntrinsicPaint(entity) !== undefined) return false;+ return !getEntityTree(world, entity).includes(target);+}++/** The selected rects Confirm would apply to the target, in selection order. */+export function getClipPathPicks(world: World): Entity[] {+ const target = getClipPathTarget();+ if (!target) return [];+ return getSelection(world).filter((entity) => canClipWith(world, target, entity));+}++/**+ * Makes the picked rects clip paths of the target, one undo step, and puts+ * the tool down with the target selected. Each rect keeps its place on the+ * canvas: its matrix is carried from the parent it had into the target's+ * space and written back as `x`, `y` and `rotation`, with whatever scale the+ * move adds (a target drawn at half size, say) folded into its `width` and+ * `height` rather than its own scale.+ */+export function applyClipPaths(world: World): void {+ const target = getClipPathTarget();+ const picks = getClipPathPicks(world);+ if (!target || picks.length === 0) return;++ const editor = getDocumentEditor(world);+ const computed = store(world, Computed);+ const flip = store(world, Flip);+ const targetInverse = invert2D(entityWorldMat(world, target));++ for (const rect of picks) {+ const eid = rect.id();+ const width = computed.width[eid] ?? 0;+ const height = computed.height[eid] ?? 0;+ const anchor = entityAnchor(world, rect);++ // Where it is drawn now, pivot folded in (see `bakeContainerInto`),+ // taken into the target's space before the move changes its parent.+ const placed = decompose2D(multiply2D(+ targetInverse,+ multiply2D(entityWorldMat(world, rect), translate2D(anchor.x * width, anchor.y * height)),+ ));++ if (!editor.reparent(rect, target)) continue;+ editor.editProperty(rect, 'clipPath', true);++ // The scale the rect carries of its own stays its own; the rest is the move's.+ const ownX = (computed.scaleX[eid] ?? 1) * (flip.x[eid] ?? 1);+ const ownY = (computed.scaleY[eid] ?? 1) * (flip.y[eid] ?? 1);+ const nextWidth = ownX === 0 ? width : Math.round(width * Math.abs(placed.scaleX / ownX));+ const nextHeight = ownY === 0 ? height : Math.round(height * Math.abs(placed.scaleY / ownY));++ const offset = entityOffset(world, rect);+ const x = Math.round(placed.x - anchor.x * nextWidth - offset.x);+ const y = Math.round(placed.y - anchor.y * nextHeight - offset.y);+ const rotation = round2(placed.rotation);++ const writes: TransformWrite[] = [];+ if (x !== Math.round(computed.positionX[eid] ?? 0)) writes.push(['x', x]);+ if (y !== Math.round(computed.positionY[eid] ?? 0)) writes.push(['y', y]);+ if (rotation !== round2(computed.rotation[eid] ?? 0)) writes.push(['rotation', rotation]);+ if (nextWidth !== Math.round(width)) writes.push(['width', nextWidth]);+ if (nextHeight !== Math.round(height)) writes.push(['height', nextHeight]);+ if (writes.length) editTransform(world, editor, rect, writes);+ }++ leaveClipPathTool(world, target);+}++/** Puts the tool down without applying anything; the selection goes back to the target. */+export function cancelClipPath(world: World): void {+ leaveClipPathTool(world, getClipPathTarget());+}++function leaveClipPathTool(world: World, target: Entity | null): void {+ clearClipPathTarget();+ if (target) getDocumentEditor(world).select(target);+ if (world.get(Tool)?.value === ToolType.CLIP_PATH) world.set(Tool, { value: ToolType.MOVE });+}diff --git a/apps/web/src/engine/index.ts b/apps/web/src/engine/index.tsindex 1aed3a88..c0a4948b 100644--- a/apps/web/src/engine/index.ts+++ b/apps/web/src/engine/index.ts@@ -24,3 +24,4 @@ export * from './asset-actions';export * from './keyframes';export * from './align';export * from './object-mask';+export * from './clip-path';diff --git a/apps/web/src/engine/input/cursor.ts b/apps/web/src/engine/input/cursor.tsindex 577de4da..6e7f42c5 100644--- a/apps/web/src/engine/input/cursor.ts+++ b/apps/web/src/engine/input/cursor.ts@@ -92,6 +92,7 @@ const TOOL_CURSORS: Record<ToolType, { idle: CursorType; pressed?: CursorType }>[ToolType.TEXT]: { idle: 'crosshair' },[ToolType.TEXT_EDIT]: { idle: 'text' },[ToolType.OBJECT_MASK]: { idle: 'crosshair' },+ [ToolType.CLIP_PATH]: { idle: 'default' },};/** The cursor the armed tool asks for, pressed or at rest. The object mask's brush draws its own ring, so it hides the cursor. */diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.tsindex 4f878ac9..32a56cc3 100644--- a/apps/web/src/engine/input/shortcuts.ts+++ b/apps/web/src/engine/input/shortcuts.ts@@ -34,6 +34,7 @@ import {import { Not, Or } from 'koota';import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';+import { applyClipPaths, cancelClipPath } from '../clip-path';import { getDocumentEditor } from '../editor';import { groupSelection, ungroupSelection, unwrapSequenceSelection, wrapSelectionInScene, wrapSelectionInSequence } from '../group';import { getEditHistory } from '../history';@@ -452,7 +453,12 @@ const maskStrokeToUndo = (world: World): boolean => {return objectMaskTool(world) && track?.status === 'seeded' && track.strokes.length > 0;};+const clipPathTool = (world: World): boolean => world.get(Tool)?.value === ToolType.CLIP_PATH;+const PRESSED_SHORTCUTS: readonly Shortcut[] = [+ // Clip path tool shortcuts, ahead of the Esc and ⌘↵ the rest of the editor answers to.+ { keys: ['escape'], action: cancelClipPath, active: clipPathTool },+ { keys: ['enter', 'mod', '!shift', '!alt'], action: applyClipPaths, active: clipPathTool },{ keys: ['z', 'mod', '!shift'], action: undoEdit },{ keys: ['z', 'mod', 'shift'], action: redoEdit },{ keys: ['backspace'], action: deleteSelection },diff --git a/packages/runtime/src/constants.ts b/packages/runtime/src/constants.tsindex 670df7ec..a0959441 100644--- a/packages/runtime/src/constants.ts+++ b/packages/runtime/src/constants.ts@@ -177,4 +177,5 @@ export enum ToolType {TEXT,TEXT_EDIT,OBJECT_MASK,+ CLIP_PATH,}