Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: add clean-up command to arrange selected nodes in a grid layout

konstantin-paulusefbc257512ea
diff --git a/apps/web/src/components/commands/clean-up.tsx b/apps/web/src/components/commands/clean-up.tsx
new file mode 100644
index 00000000..c7c68d48
--- /dev/null
+++ b/apps/web/src/components/commands/clean-up.tsx
@@ -0,0 +1,25 @@
+/* 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 { useWorld } from "@diffusionstudio/koota-solid";
+import { useCommand } from "@/context/commands";
+import { cleanUpSelection, getAlignableSelection, useSelection } from "@/engine";
+
+/** Lays a selection of several out in a grid on the canvas (see `cleanUpSelection`). */
+export function CleanUpCommand() {
+ const world = useWorld();
+ const { nodes } = useSelection();
+
+ useCommand({
+ id: "clean-up",
+ title: "Clean up",
+ group: "Object",
+ icon: "view-grid",
+ keywords: ["tidy", "grid", "arrange", "layout", "align", "distribute"],
+ available: () => nodes().length > 1 && getAlignableSelection(world).length > 1,
+ run: () => cleanUpSelection(world),
+ });
+
+ return null;
+}
diff --git a/apps/web/src/components/commands/index.tsx b/apps/web/src/components/commands/index.tsx
index 4198b3ca..10b7224a 100644
--- a/apps/web/src/components/commands/index.tsx
+++ b/apps/web/src/components/commands/index.tsx
@@ -4,6 +4,7 @@
import { ToolType } from "@diffusionstudio/runtime";
import { AutoCaptionsCommand } from "./auto-captions";
+import { CleanUpCommand } from "./clean-up";
import { EditWithPromptCommand } from "./edit-with-prompt";
import { ExportCurrentFrameCommand } from "./export-current-frame";
import { ExportVideoCommand } from "./export-video";
@@ -47,6 +48,7 @@ export function CanvasCommands() {
<AutoCaptionsCommand />
<RemoveSilenceCommand />
<MediaAssetCommands />
+ <CleanUpCommand />
<ExportVideoCommand />
<ExportCurrentFrameCommand />
<ToolCommand tool={ToolType.MOVE} />
diff --git a/apps/web/src/components/commands/node-menu.tsx b/apps/web/src/components/commands/node-menu.tsx
index bdadd43d..1ba27f6c 100644
--- a/apps/web/src/components/commands/node-menu.tsx
+++ b/apps/web/src/components/commands/node-menu.tsx
@@ -64,7 +64,7 @@ export const CANVAS_MENU: NodeMenuLayout = {
nodes: [
CLIPBOARD,
STRUCTURE,
- ["shortcut:bring-to-front", "shortcut:send-to-back", "shortcut:toggle-hidden"],
+ ["clean-up", "shortcut:bring-to-front", "shortcut:send-to-back", "shortcut:toggle-hidden"],
MEDIA,
ASSET,
],
diff --git a/apps/web/src/engine/align.ts b/apps/web/src/engine/align.ts
index b112f565..6c142fb0 100644
--- a/apps/web/src/engine/align.ts
+++ b/apps/web/src/engine/align.ts
@@ -20,6 +20,7 @@ import {
entityWorldMat,
getParentEntity,
getParentNode,
+ getViewMatrix,
invert2D,
isScene,
store,
@@ -159,3 +160,88 @@ export function distributeSelection(world: World, axis: Axis): void {
cursor += size(box) + gap;
}
}
+
+/** Between nodes and between rows of a cleaned-up selection, in document space. */
+const CLEAN_UP_GAP = 40;
+
+type Size = { width: number; height: number };
+
+/** `sizes`, by index, in rows no wider than `maxWidth` (a row always takes one), `gap` apart. */
+function flowRows(sizes: Size[], order: number[], maxWidth: number, gap: number): number[][] {
+ const rows: number[][] = [];
+ let width = 0;
+ for (const index of order) {
+ const row = rows.at(-1);
+ const next = width + gap + sizes[index]!.width;
+ if (row && next <= maxWidth) {
+ row.push(index);
+ width = next;
+ } else {
+ rows.push([index]);
+ width = sizes[index]!.width;
+ }
+ }
+ return rows;
+}
+
+/**
+ * Where each of `sizes` goes, as offsets from the layout's top-left: like a
+ * Finder clean up, largest first, in rows read left to right with `gap`
+ * between neighbours and between rows, each node top-aligned in its row.
+ * Sorted by size, nodes alike end up side by side, so nodes of one size line
+ * up in columns too. Of the row widths that fit a whole number of the
+ * largest nodes in the first row, the one that makes the squarest block.
+ */
+export function cleanUpLayout(sizes: Size[], gap: number): { x: number; y: number }[] {
+ const area = (size: Size) => size.width * size.height;
+ const order = sizes.map((_, index) => index).sort((a, b) => area(sizes[b]!) - area(sizes[a]!) || a - b);
+
+ let best: { rows: number[][]; score: number } | undefined;
+ let firstRow = -gap;
+ for (const index of order) {
+ firstRow += gap + sizes[index]!.width;
+ const rows = flowRows(sizes, order, firstRow, gap);
+ const width = Math.max(...rows.map((row) => row.reduce((sum, i) => sum + sizes[i]!.width, 0) + (row.length - 1) * gap));
+ const height = rows.reduce((sum, row) => sum + Math.max(...row.map((i) => sizes[i]!.height)), 0) + (rows.length - 1) * gap;
+ const score = Math.max(width / height, height / width);
+ if (!best || score < best.score) best = { rows, score };
+ }
+
+ const offsets = sizes.map(() => ({ x: 0, y: 0 }));
+ let y = 0;
+ for (const row of best?.rows ?? []) {
+ let x = 0;
+ for (const index of row) {
+ offsets[index] = { x, y };
+ x += sizes[index]!.width + gap;
+ }
+ y += Math.max(...row.map((index) => sizes[index]!.height)) + gap;
+ }
+ return offsets;
+}
+
+/**
+ * Lays the selected nodes out from the selection's top-left corner (see
+ * `cleanUpLayout`), with a gap that stays the same at any zoom. Needs two.
+ */
+export function cleanUpSelection(world: World): void {
+ const entities = getAlignableSelection(world);
+ if (entities.length < 2) return;
+
+ // The boxes are in device space: the gap is scaled by the camera's zoom.
+ const view = getViewMatrix(world);
+ const gap = CLEAN_UP_GAP * Math.sqrt(Math.abs(view.a * view.d - view.b * view.c));
+
+ const boxes = entities.map((entity) => boundsOf(world, entity));
+ const mask = union(boxes);
+ const offsets = cleanUpLayout(
+ boxes.map((box) => ({ width: box.maxX - box.minX, height: box.maxY - box.minY })),
+ gap,
+ );
+
+ entities.forEach((entity, index) => {
+ const box = boxes[index]!;
+ const offset = offsets[index]!;
+ moveByWorldDelta(world, entity, mask.minX + offset.x - box.minX, mask.minY + offset.y - box.minY);
+ });
+}