Skip to content

Commit d103af6

Browse files
committed
feat(studio): arrow-key nudge step math (unwired)
What: domEditNudge — pure nudge-step math (direction × modifier → delta, commit debounce constant) with tests. Shipped unwired. Why: small standalone concept consumed by the canvas nudge hook in the chrome-components PR. How: new files; one TEMP(studio-dnd) ignoreExports entry until the consumer lands (removed by the app-shell swap PR). Test plan: bunx vitest run domEditNudge.test.ts; tsc --noEmit; fallow audit clean.
1 parent 138ede6 commit d103af6

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎.fallowrc.jsonc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
"packages/cli/src/cloud/_gen/**",
9494
],
9595
"ignoreExports": [
96+
// TEMP(studio-dnd): consumers land later in the stack; removed by studio-dnd/pr22.
97+
{
98+
"file": "packages/studio/src/components/editor/domEditNudge.ts",
99+
"exports": ["CANVAS_NUDGE_COMMIT_DEBOUNCE_MS"],
100+
},
96101
// TEMP(studio-dnd): consumers land later in the stack; removed by studio-dnd/pr22.
97102
{
98103
"file": "packages/studio/src/components/editor/canvasContextMenuZOrder.ts",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
CANVAS_NUDGE_SHIFT_STEP_PX,
4+
CANVAS_NUDGE_STEP_PX,
5+
canCanvasNudgeTargets,
6+
resolveCanvasNudgeDelta,
7+
} from "./domEditNudge";
8+
9+
function mockKeyboardEvent(
10+
key: string,
11+
overrides: Partial<Pick<KeyboardEvent, "altKey" | "ctrlKey" | "metaKey" | "shiftKey">> = {},
12+
): Pick<KeyboardEvent, "altKey" | "ctrlKey" | "metaKey" | "shiftKey" | "key"> {
13+
return {
14+
altKey: false,
15+
ctrlKey: false,
16+
metaKey: false,
17+
shiftKey: false,
18+
key,
19+
...overrides,
20+
};
21+
}
22+
23+
describe("resolveCanvasNudgeDelta", () => {
24+
it("maps plain arrows to 1px composition deltas", () => {
25+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowLeft"))).toEqual({
26+
dx: -CANVAS_NUDGE_STEP_PX,
27+
dy: 0,
28+
});
29+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowRight"))).toEqual({
30+
dx: CANVAS_NUDGE_STEP_PX,
31+
dy: 0,
32+
});
33+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowUp"))).toEqual({
34+
dx: 0,
35+
dy: -CANVAS_NUDGE_STEP_PX,
36+
});
37+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowDown"))).toEqual({
38+
dx: 0,
39+
dy: CANVAS_NUDGE_STEP_PX,
40+
});
41+
});
42+
43+
it("maps Shift+arrow to 10px deltas", () => {
44+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowRight", { shiftKey: true }))).toEqual({
45+
dx: CANVAS_NUDGE_SHIFT_STEP_PX,
46+
dy: 0,
47+
});
48+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowUp", { shiftKey: true }))).toEqual({
49+
dx: 0,
50+
dy: -CANVAS_NUDGE_SHIFT_STEP_PX,
51+
});
52+
});
53+
54+
it("ignores browser and app shortcut chords", () => {
55+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowLeft", { altKey: true }))).toBeNull();
56+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowLeft", { ctrlKey: true }))).toBeNull();
57+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("ArrowLeft", { metaKey: true }))).toBeNull();
58+
});
59+
60+
it("ignores non-arrow keys", () => {
61+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("Escape"))).toBeNull();
62+
expect(resolveCanvasNudgeDelta(mockKeyboardEvent("a"))).toBeNull();
63+
});
64+
});
65+
66+
describe("canCanvasNudgeTargets", () => {
67+
const movable = { capabilities: { canApplyManualOffset: true } };
68+
const locked = { capabilities: { canApplyManualOffset: false } };
69+
70+
it("requires at least one target", () => {
71+
expect(canCanvasNudgeTargets([])).toBe(false);
72+
});
73+
74+
it("allows only when every target accepts a manual offset", () => {
75+
expect(canCanvasNudgeTargets([movable])).toBe(true);
76+
expect(canCanvasNudgeTargets([movable, movable])).toBe(true);
77+
expect(canCanvasNudgeTargets([locked])).toBe(false);
78+
expect(canCanvasNudgeTargets([movable, locked])).toBe(false);
79+
});
80+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Pure helpers for the canvas arrow-key nudge (DomEditOverlay).
3+
*
4+
* Mirrors captions/keyboard.ts conventions; the two nudge surfaces can't
5+
* double-fire because PreviewOverlays mounts CaptionOverlay and DomEditOverlay
6+
* mutually exclusively (caption edit mode returns before the DOM overlay).
7+
*/
8+
9+
const CANVAS_NUDGE_KEYS = new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
10+
11+
export const CANVAS_NUDGE_STEP_PX = 1;
12+
export const CANVAS_NUDGE_SHIFT_STEP_PX = 10;
13+
/** One undo entry per key burst: the commit fires after this idle gap. */
14+
export const CANVAS_NUDGE_COMMIT_DEBOUNCE_MS = 400;
15+
16+
type CanvasNudgeKeyEvent = Pick<
17+
KeyboardEvent,
18+
"altKey" | "ctrlKey" | "metaKey" | "shiftKey" | "key"
19+
>;
20+
21+
/**
22+
* Arrow key → composition-px delta (Shift = 10). Null when the key is not a
23+
* plain/Shift arrow, so browser and app shortcut chords pass through.
24+
*/
25+
export function resolveCanvasNudgeDelta(
26+
event: CanvasNudgeKeyEvent,
27+
): { dx: number; dy: number } | null {
28+
if (event.metaKey || event.ctrlKey || event.altKey) return null;
29+
if (!CANVAS_NUDGE_KEYS.has(event.key)) return null;
30+
const step = event.shiftKey ? CANVAS_NUDGE_SHIFT_STEP_PX : CANVAS_NUDGE_STEP_PX;
31+
return {
32+
dx: event.key === "ArrowLeft" ? -step : event.key === "ArrowRight" ? step : 0,
33+
dy: event.key === "ArrowUp" ? -step : event.key === "ArrowDown" ? step : 0,
34+
};
35+
}
36+
37+
export interface CanvasNudgeTarget {
38+
capabilities: { canApplyManualOffset: boolean };
39+
}
40+
41+
/** A nudge needs at least one target and every target must accept manual offsets. */
42+
export function canCanvasNudgeTargets(targets: ReadonlyArray<CanvasNudgeTarget>): boolean {
43+
return targets.length > 0 && targets.every((t) => t.capabilities.canApplyManualOffset);
44+
}

0 commit comments

Comments
 (0)