Skip to content

The VS Code of video editing, built for agents.

TypeScript

Implement remove functionality for editor and enhance keyboard shortcuts

konstantin-paulus1b8f4ab19527
diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.ts
index 090a3002..712b96e9 100644
--- a/apps/desktop/src/edit.ts
+++ b/apps/desktop/src/edit.ts
@@ -63,7 +63,18 @@ export interface SourceMove {
before?: string;
}
-export type SourceEdit = SourceSet | SourceInsert | SourceMove;
+/**
+ * Removes the element named by `source` from the file, and with it everything
+ * it contains: its children are its text, and go the way a move takes them
+ * along. Addressed like a move — an unnamed element is a position, and cutting
+ * text renumbers positions, so the element is found before anything is cut.
+ */
+export interface SourceRemove {
+ kind: "remove";
+ source: string;
+}
+
+export type SourceEdit = SourceSet | SourceInsert | SourceMove | SourceRemove;
export interface WriteResult {
/** Sources that could not be written, as `id` or `id (prop)`. */
@@ -604,6 +615,11 @@ class SourceWriter {
continue;
}
+ if (edit.kind === "remove") {
+ if (!this.removeElement(file, sourceFile, edit, ids)) skipped.push(edit.source);
+ continue;
+ }
+
const locator = locate(edit.source, ids)?.locator;
if (locator === undefined) {
skipped.push(edit.source);
@@ -757,6 +773,28 @@ class SourceWriter {
insertChild(sourceFile, parent, reindent(text, indent, elementOf(parent).getChildIndentationText()), before);
return true;
}
+
+ /**
+ * Cuts one element, subtree and all, out of the tree. False when the source
+ * does not name an element of this file — including one already gone: a
+ * remove of what a remove in this same write took along with its parent
+ * finds nothing, and that is not a failure of the file.
+ */
+ private removeElement(
+ file: string,
+ sourceFile: SourceFile,
+ edit: SourceRemove,
+ ids: Record<string, string>,
+ ): boolean {
+ const address = locate(edit.source, ids);
+ const locator = address?.file === file ? address.locator : undefined;
+ const tag = locator === undefined ? undefined : findTag(sourceFile, locator);
+ if (!tag) return false;
+
+ matchIndentation(sourceFile);
+ cutElement(sourceFile, elementOf(tag));
+ return true;
+ }
}
/** Stamps every source file of the project. Writes nothing to a fully named project. */
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.ts
index 378ace5b..f4891d08 100644
--- a/apps/web/src/engine/create-engine.ts
+++ b/apps/web/src/engine/create-engine.ts
@@ -2,11 +2,12 @@
* 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 { renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, RenderSurface, Time, ToolType, Tool, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';
+import { renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';
import { hudSystem } from './hud';
import { createSignal, type Accessor, type Setter } from 'solid-js';
import { Hud, Keys, Pointer, PointerEvents, SnapLines } from './traits';
import { inputSystem } from './input/input-system';
+import { shortcutSystem } from './input/shortcuts';
import type { RuntimeWorld } from '@diffusionstudio/runtime';
import type { CanvasPointerEvent, PointerEventType } from '@diffusionstudio/runtime';
@@ -73,30 +74,42 @@ class Engine {
}
private readonly onBlur = () => {
- this.world.get(Keys)!.clear();
+ // Key-up never arrives when focus leaves mid-hold (⌘-tab, devtools).
+ this.world.get(Keys)!.held.clear();
}
+ /**
+ * Only records: what a press does is decided once a frame by the shortcut
+ * system. Presses in text fields belong to the field. The one thing
+ * decided here is whether the browser gets the event, since that cannot
+ * wait for the frame — backspace would have navigated by then.
+ */
private readonly onKeyDown = (event: KeyboardEvent) => {
if (
- this.world.get(Tool)?.value === ToolType.HAND
- || this.world.get(Keys)?.has(' ')
+ event.target instanceof HTMLInputElement
+ || event.target instanceof HTMLTextAreaElement
+ || (event.target instanceof HTMLElement && event.target.isContentEditable)
) return;
const keys = this.world.get(Keys)!;
- keys.add(event.key.toLowerCase());
+ const key = event.key.toLowerCase();
+ const isMod = event.key === 'Meta' || event.key === 'Control';
- if (event.key === 'Meta' || event.key === 'Control') {
- keys.add('mod');
- }
+ keys.held.add(key);
+ if (isMod) keys.held.add('mod');
+ if (!event.repeat) this.world.set(Keys, { justPressed: true });
+
+ event.preventDefault();
}
private readonly onKeyUp = (event: KeyboardEvent) => {
const keys = this.world.get(Keys)!;
- keys.delete(event.key.toLowerCase());
+ const key = event.key.toLowerCase();
+ const isMod = event.key === 'Meta' || event.key === 'Control';
- if (event.key === 'Meta' || event.key === 'Control') {
- keys.delete('mod');
- }
+ keys.held.delete(key);
+ if (isMod) keys.held.delete('mod');
+ this.world.set(Keys, { justLifted: true });
};
private readonly loop = (timestamp: number): void => {
@@ -111,6 +124,11 @@ class Engine {
this.runSystems();
+ const keys = this.world.get(Keys);
+ if (keys?.justPressed || keys?.justLifted) {
+ this.world.set(Keys, { justPressed: false, justLifted: false });
+ }
+
this.rafId = requestAnimationFrame(this.loop);
}
@@ -151,6 +169,7 @@ class Engine {
private runSystems(): void {
inputSystem(this.world);
+ shortcutSystem(this.world);
playbackSystem(this.world);
motionSystem(this.world);
transformSystem(this.world);
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index ce227549..9330d56c 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -67,7 +67,17 @@ export interface MoveEdit {
before?: string;
}
-export type EntityEdit = PropEdit | InsertEdit | MoveEdit;
+/**
+ * An element the editor deleted, descendants included: they were its content
+ * in the file and went with it on the canvas, so only the top of a deleted
+ * subtree is reported. The entity is already gone when this is.
+ */
+export interface RemoveEdit {
+ kind: 'remove';
+ source: string;
+}
+
+export type EntityEdit = PropEdit | InsertEdit | MoveEdit | RemoveEdit;
/**
* Sources of elements the editor created that no write has named yet. Shaped
@@ -303,6 +313,37 @@ export class DocumentEditor {
return true;
}
+ /**
+ * Deletes `entities` and everything under them, and reports it so the
+ * elements leave the file too. Only the tops of the doomed subtrees are
+ * reported: a descendant of another doomed entity goes with it, in the file
+ * as on the canvas. The stage, and anything the file does not know (no
+ * source), stays. Returns the entities that were removed at the top level.
+ */
+ public remove(entities: Entity | Entity[]): Entity[] {
+ const doomed = new Set(
+ (Array.isArray(entities) ? entities : [entities]).filter(
+ (entity) => entity.isAlive() && !entity.has(Stage) && !!entity.get(Source)?.value,
+ ),
+ );
+ const roots = [...doomed].filter((entity) => {
+ for (let parent = getParentEntity(entity); parent; parent = getParentEntity(parent)) {
+ if (doomed.has(parent)) return false;
+ }
+ return true;
+ });
+
+ const document = this.document;
+ for (const entity of roots) {
+ if (!entity.isAlive()) continue;
+ const source = entity.get(Source)!.value;
+ document.removeNode({ entity: getParentEntity(entity) ?? document.stage.entity }, { entity });
+ this.sink?.({ kind: 'remove', source });
+ }
+
+ return roots;
+ }
+
/**
* Re-stamps entities whose element earned a name, as `old source -> new`.
* Queried rather than remembered: the entities that survived are the ones
diff --git a/apps/web/src/engine/hud/hud-system.ts b/apps/web/src/engine/hud/hud-system.ts
index 4fd14b42..fce304bb 100644
--- a/apps/web/src/engine/hud/hud-system.ts
+++ b/apps/web/src/engine/hud/hud-system.ts
@@ -72,7 +72,7 @@ export function hudSystem(world: World): void {
function drawSnapLines(world: World, ctx: Ctx2D, resolution: number): void {
const lines = world.get(SnapLines)?.list ?? [];
// Mod suspends snapping, so the guides go with it.
- if (lines.length === 0 || world.get(Keys)?.has('mod')) return;
+ if (lines.length === 0 || world.get(Keys)?.held.has('mod')) return;
const cross = Math.round(3 * resolution);
diff --git a/apps/web/src/engine/input/index.ts b/apps/web/src/engine/input/index.ts
index 243de954..24aaf440 100644
--- a/apps/web/src/engine/input/index.ts
+++ b/apps/web/src/engine/input/index.ts
@@ -5,4 +5,5 @@
export * from './cursor';
export * from './input-system';
export * from './interactions';
+export * from './shortcuts';
export * from './snapping';
diff --git a/apps/web/src/engine/input/input-system.ts b/apps/web/src/engine/input/input-system.ts
index 0b041a7a..1f52c3f5 100644
--- a/apps/web/src/engine/input/input-system.ts
+++ b/apps/web/src/engine/input/input-system.ts
@@ -63,7 +63,7 @@ let hovered: HitRegion | null = null;
let dragged: HitRegion | null = null;
export function inputSystem(world: World) {
- const panning = world.get(Tool)?.value === ToolType.HAND || (world.get(Keys)?.has(' ') ?? false);
+ const panning = world.get(Tool)?.value === ToolType.HAND || (world.get(Keys)?.held.has(' ') ?? false);
const queue = world.get(PointerEvents)?.queue ?? [];
const regions = world.get(HitRegions)?.list ?? [];
diff --git a/apps/web/src/engine/input/interactions.ts b/apps/web/src/engine/input/interactions.ts
index 877c8068..d191c81d 100644
--- a/apps/web/src/engine/input/interactions.ts
+++ b/apps/web/src/engine/input/interactions.ts
@@ -71,7 +71,7 @@ const HANDLE_CURSOR: Record<Handle, CursorType> = {
const isHandle = (id: string): id is Handle => id in HANDLE_FACTOR;
-const keys = (world: World): Set<string> => world.get(Keys) ?? new Set<string>();
+const keys = (world: World): Set<string> => world.get(Keys)?.held ?? new Set<string>();
const resolution = (world: World): number => world.get(RenderSurface)?.resolution ?? 1;
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
new file mode 100644
index 00000000..0c17c298
--- /dev/null
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -0,0 +1,61 @@
+/* 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/. */
+
+/**
+ * Keyboard commands on the canvas: what a key press does, as opposed to the
+ * held keys that modify a gesture. The DOM listener only records (see
+ * `Keys`); the shortcut system runs once a frame and, when a key was just
+ * pressed, matches what is held against the table below and runs the action.
+ */
+
+import { Selected } from '@diffusionstudio/runtime';
+
+import { getDocumentEditor } from '../editor';
+import { Keys } from '../traits';
+
+import type { World } from 'koota';
+
+interface Shortcut {
+ /**
+ * Keys to hold down for the shortcut to trigger. Joined with '+' to form a set of keys.
+ */
+ key: string;
+ mod?: boolean;
+ shift?: boolean;
+ alt?: boolean;
+ action: (world: World) => void;
+}
+
+export function deleteSelection(world: World): void {
+ const selected = [...world.query(Selected)];
+
+ if (selected.length) {
+ getDocumentEditor(world).remove(selected);
+ }
+};
+
+const SHORTCUTS: readonly Shortcut[] = [
+ { key: 'backspace', action: deleteSelection },
+ { key: 'delete', action: deleteSelection },
+];
+
+/**
+ * On a frame with a fresh press, runs the shortcut the held keys spell, then
+ * resets the frame's press and lift flags: it is the last reader of them.
+ */
+export function shortcutSystem(world: World): void {
+ const keys = world.get(Keys);
+ if (!keys?.justPressed) return;
+
+
+ const shortcut = SHORTCUTS.find(
+ (shortcut) =>
+ shortcut.key.split('+').every(key => keys.held.has(key)) &&
+ keys.held.has('mod') === !!shortcut.mod &&
+ keys.held.has('shift') === !!shortcut.shift &&
+ keys.held.has('alt') === !!shortcut.alt,
+ );
+
+ shortcut?.action(world);
+}
diff --git a/apps/web/src/engine/traits.ts b/apps/web/src/engine/traits.ts
index f28dfdfa..85d9ca92 100644
--- a/apps/web/src/engine/traits.ts
+++ b/apps/web/src/engine/traits.ts
@@ -30,11 +30,19 @@ export const Pointer = trait({
});
/**
- * Keys currently held, lowercased, with 'mod' standing in for meta/control so
- * a shortcut does not have to know which platform it is on. Mutated in place:
- * an AoS trait hands back the set itself.
+ * The keyboard, lowercased, with 'mod' standing in for meta/control so a
+ * shortcut does not have to know which platform it is on. `held` is what is
+ * down right now (what a gesture reads its modifiers from, mutated in
+ * place); `justPressed` and `justLifted` say whether a key went down or up
+ * since the last frame, for whatever acts on a press rather than a hold —
+ * which key it was is what `held` holds by then. Both flags are reset by the
+ * shortcut system, so anything reading them runs before it.
*/
-export const Keys = trait(() => new Set<string>());
+export const Keys = trait({
+ held: () => new Set<string>(),
+ justPressed: false,
+ justLifted: false,
+});
export type SnapLine = { from: Point; to: Point };
diff --git a/apps/web/src/projects/edits.ts b/apps/web/src/projects/edits.ts
index 6663fb21..6525a14b 100644
--- a/apps/web/src/projects/edits.ts
+++ b/apps/web/src/projects/edits.ts
@@ -8,7 +8,7 @@ import { toast } from 'somoto';
import { writeProject } from './host';
-import type { EntityEdit, InsertEdit, MoveEdit } from '@/engine/editor';
+import type { EntityEdit, InsertEdit, MoveEdit, RemoveEdit } from '@/engine/editor';
import type { SourceEdit, WriteResult } from './host';
import type { World } from 'koota';
@@ -27,9 +27,12 @@ class EditWriter {
// of them), then the moves, then per element, per prop: the last value for
// a prop is the only one worth writing, an element written twice is
// written once, and only where an element ended up is worth moving it to.
+ // Removes last: nothing else addressed to a removed element is worth
+ // writing, and once cut, an unnamed element's neighbours are elsewhere.
private inserts = new Map<string, InsertEdit>();
private moves = new Map<string, MoveEdit>();
private pending = new Map<string, InsertEdit['props']>();
+ private removes = new Set<string>();
// Pending sources a write is out for: edits to them wait for the answer.
private inflight = new Set<string>();
private timer: ReturnType<typeof setTimeout> | undefined;
@@ -46,6 +49,8 @@ class EditWriter {
if (edit.kind === 'insert') {
this.inserts.set(edit.source, edit);
+ } else if (edit.kind === 'remove') {
+ this.remove(edit);
} else if (edit.kind === 'move') {
// Where an element still waiting to be inserted goes is part of how
// it is inserted, not a move of it.
@@ -79,6 +84,53 @@ class EditWriter {
this.disposed = true;
}
+ /**
+ * Forgets everything owed to an element that is going, and to whatever
+ * was to be inserted under it: the entities went with it, so those inserts
+ * have no element to become. An element the file never had (an insert
+ * still waiting here) is simply not inserted; one it has, or that a write
+ * out right now is naming, is removed by name.
+ */
+ private remove(edit: RemoveEdit): void {
+ const doomed = new Set([edit.source]);
+ let grew = true;
+ while (grew) {
+ grew = false;
+ for (const insert of this.inserts.values()) {
+ if (doomed.has(insert.parent) && !doomed.has(insert.source)) {
+ doomed.add(insert.source);
+ grew = true;
+ }
+ }
+ }
+
+ // An insert still waiting here never reached the file: dropping it is
+ // the whole removal. Anything else the file has, or a write out right
+ // now is naming, and it goes from there by name.
+ if (!this.inserts.has(edit.source)) this.removes.add(edit.source);
+ for (const source of doomed) {
+ this.inserts.delete(source);
+ this.pending.delete(source);
+ this.moves.delete(source);
+ }
+
+ // Placement in front of an element that is gone means "last" now, the
+ // same answer the write gives to an anchor it cannot find.
+ for (const [source, insert] of this.inserts) {
+ if (insert.before !== undefined && doomed.has(insert.before)) {
+ const { before: _, ...rest } = insert;
+ this.inserts.set(source, rest);
+ }
+ }
+ for (const [source, move] of [...this.moves]) {
+ if (doomed.has(move.parent)) this.moves.delete(source);
+ else if (move.before !== undefined && doomed.has(move.before)) {
+ const { before: _, ...rest } = move;
+ this.moves.set(source, rest);
+ }
+ }
+ }
+
private schedule(): void {
clearTimeout(this.timer);
this.timer = setTimeout(() => this.flush(), DEBOUNCE);
@@ -86,7 +138,7 @@ class EditWriter {
private flush(): void {
this.timer = undefined;
- if (!this.inserts.size && !this.moves.size && !this.pending.size) return;
+ if (!this.inserts.size && !this.moves.size && !this.pending.size && !this.removes.size) return;
// Anything addressed through an element whose insert is still out
// waits for its name: edits to it, and inserts or moves under or
@@ -95,6 +147,7 @@ class EditWriter {
const heldInserts = new Map([...this.inserts].filter(([, insert]) => waits(insert.parent) || waits(insert.before)));
const heldMoves = new Map([...this.moves].filter(([source, move]) => waits(source) || waits(move.parent) || waits(move.before)));
const held = new Map([...this.pending].filter(([source]) => waits(source)));
+ const heldRemoves = new Set([...this.removes].filter(waits));
const edits: SourceEdit[] = [
...[...this.inserts.values()]
.filter((insert) => !heldInserts.has(insert.source))
@@ -120,6 +173,11 @@ class EditWriter {
...[...this.pending]
.filter(([source]) => !held.has(source))
.map(([source, props]): SourceEdit => ({ kind: 'set', source, props })),
+ // Last: cutting an unnamed element moves the positions of everything
+ // after it, and nothing above is addressed to what is being removed.
+ ...[...this.removes]
+ .filter((source) => !heldRemoves.has(source))
+ .map((source): SourceEdit => ({ kind: 'remove', source })),
];
if (!edits.length) return;
@@ -127,6 +185,7 @@ class EditWriter {
this.inserts = heldInserts;
this.moves = heldMoves;
this.pending = held;
+ this.removes = heldRemoves;
writeProject(this.dir, edits)
.then((result) => this.report(result))
@@ -188,13 +247,20 @@ class EditWriter {
this.moves.delete(source);
this.moves.set(next, { ...move, source: next, parent, ...(before === undefined ? {} : { before }) });
}
+ for (const source of [...this.removes]) {
+ const next = rename(source);
+ if (next === source) continue;
+ this.removes.delete(source);
+ // An element the file never had needs no removing from it.
+ if (next) this.removes.add(next);
+ }
for (const source of this.inflight) {
if (!ids[source]) editor.discardPending(source);
}
this.inflight = new Set();
// What was held back has its names now.
- if (this.inserts.size || this.moves.size || this.pending.size) this.schedule();
+ if (this.inserts.size || this.moves.size || this.pending.size || this.removes.size) this.schedule();
}
}