From 14dd781d11e12067a4067c4104fd6a6fb9c32364 Mon Sep 17 00:00:00 2001 From: dazzatronus Date: Tue, 1 Dec 2026 23:49:58 +1100 Subject: [PATCH] fix: keep dragged clips and trim edges under the pointer --- .../interaction/interaction-controller.ts | 37 +++--- .../timeline/interaction/interaction-state.ts | 7 +- tests/interaction-controller.test.ts | 107 ++++++++++++++++++ 3 files changed, 131 insertions(+), 20 deletions(-) diff --git a/src/components/timeline/interaction/interaction-controller.ts b/src/components/timeline/interaction/interaction-controller.ts index d7e411f6..161bc531 100644 --- a/src/components/timeline/interaction/interaction-controller.ts +++ b/src/components/timeline/interaction/interaction-controller.ts @@ -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); @@ -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"; @@ -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 { @@ -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); @@ -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); @@ -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. */ diff --git a/src/components/timeline/interaction/interaction-state.ts b/src/components/timeline/interaction/interaction-state.ts index aba49f08..a7a24f10 100644 --- a/src/components/timeline/interaction/interaction-state.ts +++ b/src/components/timeline/interaction/interaction-state.ts @@ -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 ───────────────────────────────────────────────────────────── @@ -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 ──────────────────────────────────────────────── diff --git a/tests/interaction-controller.test.ts b/tests/interaction-controller.test.ts index a9779fdb..d7ca7f24 100644 --- a/tests/interaction-controller.test.ts +++ b/tests/interaction-controller.test.ts @@ -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 & { 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", () => {