fix(studio): keep timeline clip selected on reclick and clear keyframes on Escape

This commit is contained in:
Miguel Angel Simon Sierra
2026-07-15 19:22:39 -04:00
parent ce60b85738
commit 6a51a2f9f1
4 changed files with 271 additions and 10 deletions
@@ -0,0 +1,136 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import { TimelineLanes } from "./TimelineLanes";
import { getTrackStyle } from "./timelineIcons";
import { defaultTimelineTheme } from "./timelineTheme";
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
configurable: true,
value: true,
});
const firstClip: TimelineElement = {
id: "clip-1",
tag: "div",
start: 0,
duration: 1,
track: 0,
};
const secondClip: TimelineElement = {
id: "clip-2",
tag: "div",
start: 1,
duration: 1,
track: 0,
};
beforeEach(() => {
document.body.innerHTML = "";
usePlayerStore.getState().reset();
});
function renderLanes({
elements = [firstClip],
selectedElementId,
selectedElementIds = new Set<string>(),
}: {
elements?: TimelineElement[];
selectedElementId: string | null;
selectedElementIds?: Set<string>;
}) {
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
const setSelectedElementId = vi.fn((id: string | null) => {
usePlayerStore.getState().setSelectedElementId(id);
});
const onSelectElement = vi.fn();
const trackStyle = getTrackStyle("div");
act(() => {
root.render(
<TimelineLanes
pps={100}
trackContentWidth={500}
theme={defaultTimelineTheme}
displayTrackOrder={[0]}
trackOrder={[0]}
tracks={[[0, elements]]}
trackStyles={new Map([[0, trackStyle]])}
selectedElementId={selectedElementId}
selectedElementIds={selectedElementIds}
hoveredClip={null}
draggedClip={null}
draggedElement={null}
multiDragPreview={null}
blockedClipRef={{ current: null }}
suppressClickRef={{ current: false }}
scrollRef={React.createRef<HTMLDivElement>()}
setHoveredClip={vi.fn()}
setShowPopover={vi.fn()}
setRangeSelection={vi.fn()}
setResizingClip={vi.fn()}
setDraggedClip={vi.fn()}
setSelectedElementId={setSelectedElementId}
syncClipDragAutoScroll={vi.fn()}
shiftClickClipRef={{ current: null }}
getPreviewElement={(element) => element}
getTrackStyle={getTrackStyle}
selectedKeyframes={new Set()}
currentTime={0}
onSelectElement={onSelectElement}
onToggleTrackHidden={undefined}
onResizeElement={undefined}
onMoveElement={undefined}
onRazorSplit={undefined}
onRazorSplitAll={undefined}
/>,
);
});
return { host, root, setSelectedElementId, onSelectElement };
}
function clickClip(host: HTMLElement, id: string): void {
const clip = host.querySelector<HTMLElement>(`[data-el-id="${id}"]`);
if (!clip) throw new Error(`Expected timeline clip ${id}`);
act(() => {
clip.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
}
describe("TimelineLanes selection", () => {
it("keeps an already-selected clip selected on a plain click", () => {
usePlayerStore.getState().setSelectedElementId(firstClip.id);
const harness = renderLanes({ selectedElementId: firstClip.id });
clickClip(harness.host, firstClip.id);
expect(harness.setSelectedElementId).toHaveBeenCalledWith(firstClip.id);
expect(harness.setSelectedElementId).not.toHaveBeenCalledWith(null);
expect(harness.onSelectElement).toHaveBeenCalledWith(firstClip);
expect(usePlayerStore.getState().selectedElementId).toBe(firstClip.id);
act(() => harness.root.unmount());
});
it("narrows a marquee selection to the clicked clip", () => {
usePlayerStore.getState().setSelection([firstClip.id, secondClip.id], firstClip.id);
const harness = renderLanes({
elements: [firstClip, secondClip],
selectedElementId: firstClip.id,
selectedElementIds: new Set([firstClip.id, secondClip.id]),
});
clickClip(harness.host, secondClip.id);
expect(harness.setSelectedElementId).toHaveBeenCalledWith(secondClip.id);
expect(harness.onSelectElement).toHaveBeenCalledWith(secondClip);
expect([...usePlayerStore.getState().selectedElementIds]).toEqual([secondClip.id]);
act(() => harness.root.unmount());
});
});
@@ -412,14 +412,11 @@ export function TimelineLanes({
}
return;
}
// Plain click single-selects: drop any marquee multi-selection.
// Only a click on the PRIMARY selection toggles it off — a click
// on a marquee-selected clip narrows the selection to that clip.
const hadMultiSelection = selectedElementIds.size > 0;
// Plain click always selects this clip and drops any marquee
// multi-selection, narrowing it to the clicked clip.
usePlayerStore.getState().clearSelectedElementIds();
const nextElement =
selectedElementId === elementKey && !hadMultiSelection ? null : el;
setSelectedElementId(nextElement ? elementKey : null);
const nextElement = el;
setSelectedElementId(elementKey);
onSelectElement?.(nextElement);
}
}
@@ -0,0 +1,123 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
import { useTimelineRangeSelection } from "./useTimelineRangeSelection";
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
configurable: true,
value: true,
});
beforeEach(() => {
document.body.innerHTML = "";
usePlayerStore.getState().reset();
});
function pointerEvent(type: string, init: PointerEventInit): Event {
if (typeof PointerEvent === "function") return new PointerEvent(type, init);
return new MouseEvent(type, init);
}
function renderRangeSelection() {
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
const scrollRef = React.createRef<HTMLDivElement>();
const ppsRef = { current: 100 };
const dragScrollRaf = { current: 0 };
const isDragging = { current: false };
const elements: TimelineElement[] = [];
const elementsRef = { current: elements };
const trackOrder: number[] = [];
const trackOrderRef = { current: trackOrder };
function Harness() {
const selection = useTimelineRangeSelection({
scrollRef,
ppsRef,
effectiveDuration: 10,
pps: 100,
seekFromX: vi.fn(),
autoScrollDuringDrag: vi.fn(),
dragScrollRaf,
isDragging,
setShowPopover: vi.fn(),
elementsRef,
trackOrderRef,
});
return (
<div
ref={scrollRef}
onPointerDown={selection.handlePointerDown}
onPointerMove={selection.handlePointerMove}
onPointerUp={selection.handlePointerUp}
/>
);
}
act(() => {
root.render(<Harness />);
});
const surface = scrollRef.current;
if (!surface) throw new Error("Expected timeline surface");
Object.defineProperty(surface, "setPointerCapture", {
configurable: true,
value: vi.fn(),
});
return { root, surface };
}
describe("useTimelineRangeSelection selection clearing", () => {
it("clears element and keyframe selections on Escape with no marquee", () => {
const harness = renderRangeSelection();
const store = usePlayerStore.getState();
store.setSelectedElementId("clip-1");
store.toggleSelectedKeyframe("clip-1:50");
act(() => {
window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
});
const state = usePlayerStore.getState();
expect(state.selectedElementId).toBeNull();
expect(state.selectedElementIds.size).toBe(0);
expect(state.selectedKeyframes.size).toBe(0);
act(() => harness.root.unmount());
});
it("clears a keyframe-only selection on Escape with no marquee", () => {
const harness = renderRangeSelection();
usePlayerStore.getState().toggleSelectedKeyframe("clip-1:50");
act(() => {
window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
});
expect(usePlayerStore.getState().selectedKeyframes.size).toBe(0);
act(() => harness.root.unmount());
});
it("keeps empty timeline clicks as the element deselection path", () => {
const harness = renderRangeSelection();
usePlayerStore.getState().setSelection(["clip-1", "clip-2"], "clip-1");
act(() => {
harness.surface.dispatchEvent(
pointerEvent("pointerdown", { bubbles: true, button: 0, clientX: 100, clientY: 100 }),
);
harness.surface.dispatchEvent(
pointerEvent("pointerup", { bubbles: true, button: 0, clientX: 100, clientY: 100 }),
);
});
const state = usePlayerStore.getState();
expect(state.selectedElementId).toBeNull();
expect(state.selectedElementIds.size).toBe(0);
act(() => harness.root.unmount());
});
});
@@ -396,7 +396,7 @@ export function useTimelineRangeSelection({
}, [isDragging, dragScrollRaf, seekFromX, finishRangeSelection, finishMarquee]);
// Escape: cancel an in-flight marquee (restores the pre-drag selection);
// otherwise clear any lingering multi-selection.
// otherwise clear all element and keyframe selection.
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.key !== "Escape") return;
@@ -414,10 +414,15 @@ export function useTimelineRangeSelection({
}
return;
}
// Escape with no marquee clears the whole selection — primary AND set.
// Escape with no marquee clears the whole selection: primary, set, and keyframes.
// setSelectedElementId(null) also collapses the multi-select set.
if (store.selectedElementId || store.selectedElementIds.size > 0) {
if (
store.selectedElementId ||
store.selectedElementIds.size > 0 ||
store.selectedKeyframes.size > 0
) {
store.setSelectedElementId(null);
store.clearSelectedKeyframes();
}
};
window.addEventListener("keydown", onKeyDown);