Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/components/timeline/interaction/interaction-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ export class InteractionController implements TimelineInteractionRegistration {
) as HTMLElement | null;
if (!clipElement) return;

// A handle is grabbed a few px inside the edge; keeping that offset moves the edge by the pointer's displacement
const edgeTime = edge === "left" ? clip.config.start : clip.config.start + clip.config.length;

this.beginPointerInteraction(e);
this.state = createResizingState(clipRef, clipElement, edge, clip.config.start, clip.config.length);
this.state = createResizingState(clipRef, clipElement, edge, clip.config.start, clip.config.length, sec(this.pointerTime(e) - edgeTime));

this.buildSnapPointsForClip(clipRef);

Expand Down Expand Up @@ -279,9 +282,9 @@ export class InteractionController implements TimelineInteractionRegistration {
// Get clip element's current screen position
const clipRect = clipElement.getBoundingClientRect();

// Calculate drag offsets - distance from mouse to clip's top-left corner
const dragOffsetX = e.clientX - clipRect.left;
const dragOffsetY = e.clientY - clipRect.top;
// Grab point at pointerdown; the threshold-crossing event is already displaced from it
const dragOffsetX = state.startPoint.x - clipRect.left;
const dragOffsetY = state.startPoint.y - clipRect.top;

// Make clip element follow mouse with position: fixed
clipElement.style.position = "fixed";
Expand All @@ -301,14 +304,12 @@ export class InteractionController implements TimelineInteractionRegistration {
this.feedbackElements.container.appendChild(ghost);

this.beginPointerInteraction(e);
this.state = createDraggingState(state, clipElement, ghost, dragOffsetX, dragOffsetY, originalStyles, clip.config.length, e.altKey);

// Position ghost at current clip position initially
const tracksOffset = getTracksOffsetInFeedbackLayer(this.feedbackElements.container, this.tracksContainer);
ghost.style.left = `${timeToViewX(sec(clip.config.start), pps)}px`;
ghost.style.top = `${this.getTrackYPosition(clipRef.trackIndex) + 4 + tracksOffset}px`;
const dragging = createDraggingState(state, clipElement, ghost, dragOffsetX, dragOffsetY, originalStyles, clip.config.length, e.altKey);
this.state = dragging;

this.buildSnapPointsForClip(clipRef);
// Positions the clip, ghost and feedback for the threshold-crossing event
this.handleDragMove(e, dragging);
}

private handleDragMove(e: PointerEvent, state: DraggingState): void {
Expand Down Expand Up @@ -477,8 +478,7 @@ export class InteractionController implements TimelineInteractionRegistration {
const tracksOffset = getTracksOffsetInFeedbackLayer(this.feedbackElements.container, this.tracksContainer);
const feedbackConfig = { pixelsPerSecond: pps, scrollLeft: scrollX, tracksOffset };

const x = e.clientX - rect.left + scrollX;
let time: Seconds = viewXToTime(x, pps);
let time = sec(this.pointerTime(e) - state.grabOffset);

// Apply snapping
const snappedTime = this.applySnap(time);
Expand Down Expand Up @@ -764,12 +764,7 @@ export class InteractionController implements TimelineInteractionRegistration {
private completeResize(e: PointerEvent, state: ResizingState): void {
const { clipRef, edge, originalStart, originalLength } = state;

const rect = this.tracksContainer.getBoundingClientRect();
const scrollX = this.tracksContainer.scrollLeft;
const pps = this.stateManager.getViewport().pixelsPerSecond;

const x = e.clientX - rect.left + scrollX;
let time: Seconds = viewXToTime(x, pps);
let time = sec(this.pointerTime(e) - state.grabOffset);

// Apply snapping
const snappedTime = this.applySnap(time);
Expand Down Expand Up @@ -850,6 +845,12 @@ export class InteractionController implements TimelineInteractionRegistration {
this.edit.executeEditCommand(cmd);
}

/** Timeline time under the pointer */
private pointerTime(e: PointerEvent): Seconds {
const rect = this.tracksContainer.getBoundingClientRect();
return viewXToTime(e.clientX - rect.left + this.tracksContainer.scrollLeft, this.stateManager.getViewport().pixelsPerSecond);
}

/**
* Longest length a resize may grow the clip to, or null when unbounded.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/components/timeline/interaction/interaction-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export interface ResizingState {
readonly edge: "left" | "right";
readonly originalStart: Seconds;
readonly originalLength: Seconds;
/** Pointer time minus edge time at pointerdown */
readonly grabOffset: Seconds;
}

// ─── Constants ─────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -115,9 +117,10 @@ export function createResizingState(
clipElement: HTMLElement,
edge: "left" | "right",
originalStart: Seconds,
originalLength: Seconds
originalLength: Seconds,
grabOffset: Seconds
): ResizingState {
return { type: "resizing", clipRef, clipElement, edge, originalStart, originalLength };
return { type: "resizing", clipRef, clipElement, edge, originalStart, originalLength, grabOffset };
}

// ─── State Update Functions ────────────────────────────────────────────────
Expand Down
107 changes: 107 additions & 0 deletions tests/interaction-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,113 @@ describe("InteractionController", () => {
});
});

// ─── Pointer Offset Tests ────────────────────────────────────────────────

describe("pointer offsets", () => {
// Clip at 1s for 2s: left edge at view x 116, right edge at 316 (16px padding, 100px/s)
beforeEach(() => {
mockStateManager.setTestTracks([createMockTrack(0, [{ start: 1, length: 2 }])] as MockTrack[]);
controller = new InteractionController(
mockEdit as never,
mockStateManager as never,
mockTrackList as never,
mockDOM.tracksContainer,
mockDOM.feedbackLayer
);
controller.mount();
});

const clipElement = () => mockDOM.tracksContainer.querySelector(".ss-clip") as HTMLElement;
const lastCommand = (type: string) =>
mockEdit.executeEditCommand.mock.calls
.map(([cmd]) => cmd as Record<string, number> & { type: string })
.filter(cmd => cmd.type === type)
.pop();

const placedClip = () => {
const clip = clipElement();
clip.getBoundingClientRect = () => ({ left: 116, top: 0, width: 200, height: 40, right: 316, bottom: 40, x: 116, y: 0, toJSON: () => ({}) });
return clip;
};

it("moves a dragged clip by the pointer's displacement since pointerdown", () => {
const clip = placedClip();

clip.dispatchEvent(createPointerEvent("pointerdown", { clientX: 150, clientY: 20 }));
// The first move lands 20px right and 10px down from the pointerdown, well beyond the 3px threshold
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 170, clientY: 30 }));
expect(clip.style.left).toBe("136px");
expect(clip.style.top).toBe("10px");

document.dispatchEvent(createPointerEvent("pointermove", { clientX: 350, clientY: 30 }));
document.dispatchEvent(createPointerEvent("pointerup", { clientX: 350, clientY: 30 }));

expect(lastCommand("MoveClipCommand")?.["newTime"]).toBeCloseTo(3);
});

it("drops a clip where the threshold-crossing move left it", () => {
const clip = placedClip();

clip.dispatchEvent(createPointerEvent("pointerdown", { clientX: 150, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 350, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointerup", { clientX: 350, clientY: 20 }));

expect(lastCommand("MoveClipCommand")?.["newTime"]).toBeCloseTo(3);
});

it("moves a right edge grabbed inside the clip by the pointer's displacement", () => {
const handle = clipElement().querySelector(".ss-clip-resize-handle.right") as HTMLElement;

handle.dispatchEvent(createPointerEvent("pointerdown", { clientX: 311, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 411, clientY: 20 }));
expect(Number(clipElement().style.getPropertyValue("--clip-length"))).toBeCloseTo(3);

document.dispatchEvent(createPointerEvent("pointerup", { clientX: 411, clientY: 20 }));
expect(lastCommand("ResizeClipCommand")?.["length"]).toBeCloseTo(3);
});

it("measures the grab offset in the scrolled timeline", () => {
mockDOM.tracksContainer.scrollLeft = 200;
const handle = clipElement().querySelector(".ss-clip-resize-handle.right") as HTMLElement;

// Right edge at view x 316 sits at client x 116 when scrolled 200px
handle.dispatchEvent(createPointerEvent("pointerdown", { clientX: 111, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 211, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointerup", { clientX: 211, clientY: 20 }));

expect(lastCommand("ResizeClipCommand")?.["length"]).toBeCloseTo(3);
});

it("snaps the displaced edge, not the pointer, to a neighbouring clip", () => {
mockStateManager.setTestTracks([
createMockTrack(0, [
{ start: 1, length: 2 },
{ start: 3.5, length: 2 }
])
] as MockTrack[]);
const handle = clipElement().querySelector(".ss-clip-resize-handle.right") as HTMLElement;

// Grabbed 8px inside: the edge reaches 3.41s, within the 10px snap of 3.5s, while the pointer is at 3.33s
handle.dispatchEvent(createPointerEvent("pointerdown", { clientX: 308, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 349, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointerup", { clientX: 349, clientY: 20 }));

expect(lastCommand("ResizeClipCommand")?.["length"]).toBeCloseTo(2.5);
});

it("moves a left edge grabbed inside the clip by the pointer's displacement", () => {
const handle = clipElement().querySelector(".ss-clip-resize-handle.left") as HTMLElement;

handle.dispatchEvent(createPointerEvent("pointerdown", { clientX: 121, clientY: 20 }));
document.dispatchEvent(createPointerEvent("pointermove", { clientX: 171, clientY: 20 }));
expect(Number(clipElement().style.getPropertyValue("--clip-start"))).toBeCloseTo(1.5);

document.dispatchEvent(createPointerEvent("pointerup", { clientX: 171, clientY: 20 }));
expect(lastCommand("MoveClipCommand")?.["newTime"]).toBeCloseTo(1.5);
expect(lastCommand("ResizeClipCommand")?.["length"]).toBeCloseTo(1.5);
});
});

// ─── Interrupted Interaction Tests ───────────────────────────────────────

describe("interrupted interactions", () => {
Expand Down
Loading