Skip to content

The VS Code of video editing, built for agents.

TypeScript

feat: add 'Use as mask' shortcut and implement mask application logic

konstantin-paulus783edc219723
diff --git a/apps/web/src/components/commands/node-menu.tsx b/apps/web/src/components/commands/node-menu.tsx
index 1ba27f6c..d8cb4c1b 100644
--- a/apps/web/src/components/commands/node-menu.tsx
+++ b/apps/web/src/components/commands/node-menu.tsx
@@ -49,6 +49,7 @@ const CLIPBOARD = ["shortcut:copy", "shortcut:cut", "shortcut:paste", "shortcut:
const STRUCTURE = [
"shortcut:group",
"shortcut:ungroup",
+ "shortcut:use-as-mask",
"shortcut:wrap-in-scene",
"shortcut:wrap-in-sequence",
"shortcut:unwrap-sequence",
diff --git a/apps/web/src/components/commands/shortcuts.tsx b/apps/web/src/components/commands/shortcuts.tsx
index 4ab9ec01..189ff4f7 100644
--- a/apps/web/src/components/commands/shortcuts.tsx
+++ b/apps/web/src/components/commands/shortcuts.tsx
@@ -20,12 +20,14 @@ import {
import { useCommand } from "@/context/commands";
import { clipboardHasMedia } from "@/lib/clipboard";
import {
+ applySelectionAsMask,
copySelection,
cutSelection,
deleteSelection,
duplicateSelection,
getDocumentEditor,
getEditHistory,
+ getSelectionMask,
groupSelection,
pasteSelection,
redoEdit,
@@ -56,6 +58,7 @@ type ShortcutState = {
history: EditHistory;
editor: DocumentEditor;
scene: Entity | null;
+ world: World;
};
type Shortcut = {
@@ -104,6 +107,16 @@ const SHORTCUTS: Shortcut[] = [
run: ungroupSelection,
available: ({ nodes }) => nodes.some((node) => isGroupLike(node) || isSequence(node)),
},
+ {
+ id: "use-as-mask",
+ title: "Use as mask",
+ group: "Object",
+ icon: "mask-small",
+ keys: ["⌃", "⌘", "M"],
+ keywords: ["matte", "clip"],
+ run: applySelectionAsMask,
+ available: ({ nodes, world }) => nodes.length === 2 && getSelectionMask(world) !== null,
+ },
{
id: "wrap-in-scene",
title: "Wrap in scene",
@@ -378,7 +391,7 @@ export function ShortcutCommands() {
const history = getEditHistory(world);
const editor = getDocumentEditor(world);
- const state = (): ShortcutState => ({ nodes: nodes(), history, editor, scene: scene() });
+ const state = (): ShortcutState => ({ nodes: nodes(), history, editor, scene: scene(), world });
return <For each={SHORTCUTS}>{(shortcut) => <ShortcutCommand shortcut={shortcut} state={state} />}</For>;
}
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index d6c8894a..ae41fd1a 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -39,7 +39,7 @@ import { Not, Or } from 'koota';
import { claimClipboard, hasMedia, readClipboardMedia } from '@/lib/clipboard';
import { pasteClipboardMedia } from '../asset-actions';
import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';
-import { applyMasks, cancelMask } from '../mask';
+import { applyMasks, applySelectionAsMask, cancelMask } from '../mask';
import { getDocumentEditor } from '../editor';
import { groupSelection, ungroupSelection, unwrapSequenceSelection, wrapSelectionInScene, wrapSelectionInSequence } from '../group';
import { getEditHistory } from '../history';
@@ -536,6 +536,7 @@ const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['d', 'mod', '!shift'], action: duplicateSelection },
{ keys: ['g', 'mod', '!shift'], action: groupSelection },
{ keys: ['g', 'mod', 'shift'], action: ungroupSelection },
+ { keys: ['m', 'meta', 'control'], action: applySelectionAsMask },
{ keys: ['enter', 'mod', '!shift', '!alt'], action: wrapSelectionInScene },
{ keys: ['enter', 'mod', 'alt', '!shift'], action: wrapSelectionInSequence },
{ keys: ['enter', 'mod', 'alt', 'shift'], action: unwrapSequenceSelection },
diff --git a/apps/web/src/engine/mask.tsx b/apps/web/src/engine/mask.tsx
index 00269b94..3cb3833a 100644
--- a/apps/web/src/engine/mask.tsx
+++ b/apps/web/src/engine/mask.tsx
@@ -13,6 +13,7 @@ import {
entityPivot,
entityOffset,
entityWorldMat,
+ getEntityChildren,
getEntityTree,
getParentEntity,
getSelection,
@@ -92,19 +93,60 @@ export function applyMasks(world: World): void {
const picks = getMaskPicks(world);
if (!target || picks.length === 0) return;
+ maskWith(world, target, picks);
+ leaveMaskTool(world, target);
+}
+
+/**
+ * The selection as "Use as mask" reads it, the way Figma does: of two
+ * selected layers side by side, the one behind is the mask and the one in
+ * front is what it masks. Null for any other selection, or when the one
+ * behind cannot be a mask (see `canMaskWith`).
+ */
+export function getSelectionMask(world: World): { mask: Entity; target: Entity } | null {
+ const selected = new Set(getSelection(world));
+ const first = [...selected][0];
+ if (!first || selected.size !== 2) return null;
+
+ const parent = getParentEntity(first);
+ if (!parent || isMaskContainer(parent)) return null;
+
+ // Back to front, as the file lists them.
+ const [mask, target] = getEntityChildren(world, parent).filter((entity) => selected.has(entity));
+ if (!mask || !target || !canMaskWith(world, target, mask)) return null;
+ return { mask, target };
+}
+
+/**
+ * "Use as mask": of two selected layers, the one behind becomes a `<mask>`
+ * of the one in front, moved into it where it stands. One undo step; the
+ * masked layer ends up selected.
+ */
+export function applySelectionAsMask(world: World): void {
+ const selection = getSelectionMask(world);
+ if (!selection) return;
+
+ if (maskWith(world, selection.target, [selection.mask])) {
+ getDocumentEditor(world).select(selection.target);
+ }
+}
+
+/**
+ * Gives `target` a new `<mask>` holding `layers`, each moved into it where it
+ * stands (see `applyMasks`). Returns the mask, or null when none of them
+ * could be moved.
+ */
+function maskWith(world: World, target: Entity, layers: Entity[]): Entity | null {
const editor = getDocumentEditor(world);
const computed = store(world, Computed);
const flip = store(world, Flip);
const targetInverse = invert2D(entityWorldMat(world, target));
const [mask] = editor.insertElement(target, () => <MaskElement />);
- if (!mask) {
- leaveMaskTool(world, target);
- return;
- }
+ if (!mask) return null;
let moved = 0;
- for (const layer of picks) {
+ for (const layer of layers) {
const eid = layer.id();
const width = computed.width[eid] ?? 0;
const height = computed.height[eid] ?? 0;
@@ -143,9 +185,11 @@ export function applyMasks(world: World): void {
}
// An empty mask would show nothing of the target; it is not worth the file.
- if (moved === 0) editor.remove(mask);
-
- leaveMaskTool(world, target);
+ if (moved === 0) {
+ editor.remove(mask);
+ return null;
+ }
+ return mask;
}
/** Puts the tool down without applying anything; the selection goes back to the target. */