Keep trim handles within timeline bounds
This commit is contained in:
1 parent
25cd44ecb3
commit
1abcbf62a4
8 files changed
+90
-8
No files matched your search
@@ -53,6 +53,7 @@ import {
|
||||
getMobilePinchZoomState,
|
||||
getTimelineTrackWidthPercent,
|
||||
getTimelineVisibleDuration,
|
||||
getTimelineVisibleDurationForPixelScale,
|
||||
getTimelineZoomForVisibleDuration,
|
||||
getTimelineZoomLabel,
|
||||
} from "../lib/timelineScale.js";
|
||||
@@ -592,7 +593,11 @@ export function Timeline({
|
||||
useEffect(() => {
|
||||
const startTrimScaleLock = (event) => {
|
||||
const pixelsPerSecond = Number(event.detail?.pixelsPerSecond) || 0;
|
||||
const visibleDuration = Number(event.detail?.visibleDuration) || 0;
|
||||
const isMobileTrimViewport = window.matchMedia?.("(max-width: 760px)").matches;
|
||||
const mobileScaleBasisWidth = window.matchMedia?.("(max-width: 390px)").matches ? 480 : 520;
|
||||
const visibleDuration = isMobileTrimViewport
|
||||
? getTimelineVisibleDurationForPixelScale(pixelsPerSecond, mobileScaleBasisWidth)
|
||||
: Number(event.detail?.visibleDuration) || 0;
|
||||
if (!(pixelsPerSecond > 0) || !(visibleDuration > 0)) return;
|
||||
const lock = { pixelsPerSecond, visibleDuration };
|
||||
trimScaleLockRef.current = lock;
|
||||
@@ -1391,7 +1396,8 @@ export function Timeline({
|
||||
setOverlayPromotionTarget(null);
|
||||
}
|
||||
if (promoteToMain) return;
|
||||
const delta = getTimelineDragTimeDelta({ clientX: moveEvent.clientX, startX, scrollOffset, contentWidth, timelineDuration });
|
||||
const dragClientX = autoScroller?.getDragClientX(moveEvent.clientX) ?? moveEvent.clientX;
|
||||
const delta = getTimelineDragTimeDelta({ clientX: dragClientX, startX, scrollOffset, contentWidth, timelineDuration });
|
||||
setVisualOverlaySegments((items) => items.map((item) => {
|
||||
if (item.id !== segment.id) return item;
|
||||
if (mode === "move") return { ...item, start: Math.max(0, Math.min(timelineDuration - initialDuration, initialStart + delta)) };
|
||||
|
||||
@@ -41,9 +41,10 @@ export function createImageResizeControl(d) {
|
||||
editingStarted = true;
|
||||
d.pauseForTimelineEdit?.();
|
||||
}
|
||||
const pointerX = clientX - rect.left + scrollOffset;
|
||||
const dragClientX = autoScroller.getDragClientX(clientX);
|
||||
const pointerX = dragClientX - rect.left + scrollOffset;
|
||||
const raw = getTimelineDragTimeDelta({
|
||||
clientX, startX: rect.left, scrollOffset, contentWidth: rect.width, timelineDuration,
|
||||
clientX: dragClientX, startX: rect.left, scrollOffset, contentWidth: rect.width, timelineDuration,
|
||||
});
|
||||
const clamped = Math.max(0.5, Math.min(MAX_TIMELINE_DURATION_SECONDS, raw));
|
||||
const snap = snapPoints.map((point) => ({ ...point, distance: Math.abs(pointerX - (point.time / timelineDuration) * rect.width) }))
|
||||
|
||||
@@ -29,6 +29,18 @@ export function getTrimScrollSettleStep(scrollLeft, nativeMaximum, maxStep = 6)
|
||||
return Math.max(maximum, current - step);
|
||||
}
|
||||
|
||||
export function getTimelineTrimDragClientX(clientX, rect, inset = 10) {
|
||||
if (!rect || !Number.isFinite(clientX) || !(rect.width > 0)) return clientX;
|
||||
const safeInset = Math.max(0, Math.min(rect.width / 2, Number(inset) || 0));
|
||||
return Math.max(rect.left + safeInset, Math.min(rect.right - safeInset, clientX));
|
||||
}
|
||||
|
||||
export function getMobileTrimReleaseScrollLeft(scrollLeft, trackWidth) {
|
||||
const current = Math.max(0, Number(scrollLeft) || 0);
|
||||
const width = Math.max(0, Number(trackWidth) || 0);
|
||||
return width > 0 ? Math.min(current, width) : current;
|
||||
}
|
||||
|
||||
export function getTimelineEdgeAutoScrollStep(clientX, rect, { threshold = 48, forwardMaxStep = 14, backwardMaxStep = 6 } = {}) {
|
||||
if (!rect || !Number.isFinite(clientX) || rect.width <= 0) return 0;
|
||||
const getStep = (distanceFromEdge, maxStep) => {
|
||||
@@ -134,6 +146,11 @@ export function createTimelineEdgeAutoScroller({ trackElement, pointerType, time
|
||||
getScrollOffset() {
|
||||
return enabled ? logicalScrollOffset : 0;
|
||||
},
|
||||
getDragClientX(clientX) {
|
||||
return enabled
|
||||
? getTimelineTrimDragClientX(clientX, scrollElement.getBoundingClientRect())
|
||||
: clientX;
|
||||
},
|
||||
stop() {
|
||||
if (frameId) win.cancelAnimationFrame(frameId);
|
||||
frameId = 0;
|
||||
@@ -143,6 +160,9 @@ export function createTimelineEdgeAutoScroller({ trackElement, pointerType, time
|
||||
trackElement?.classList?.remove("is-trimming");
|
||||
rulerElement?.classList?.remove("is-trimming");
|
||||
syncContentWidth();
|
||||
if (isMobile) {
|
||||
scrollElement.scrollLeft = getMobileTrimReleaseScrollLeft(scrollElement.scrollLeft, previousContentWidth);
|
||||
}
|
||||
if (spacerElement) {
|
||||
let settleFrameId = 0;
|
||||
const removeSpacer = () => {
|
||||
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
createTimelineEdgeAutoScroller,
|
||||
getTimelineDragTimeDelta,
|
||||
getTimelineEdgeAutoScrollStep,
|
||||
getTimelineTrimDragClientX,
|
||||
getMobileTrimReleaseScrollLeft,
|
||||
getTrimScrollSettleStep,
|
||||
getTrimTrailingSpacerGeometry,
|
||||
getTrimLockedTrackWidth,
|
||||
@@ -40,6 +42,17 @@ describe("mobile timeline edge auto-scroll", () => {
|
||||
expect(getTrimScrollSettleStep(553, 550)).toBe(550);
|
||||
});
|
||||
|
||||
it("keeps desktop and mobile trim handles inside the visible viewport", () => {
|
||||
expect(getTimelineTrimDragClientX(520, rect)).toBe(490);
|
||||
expect(getTimelineTrimDragClientX(80, rect)).toBe(110);
|
||||
expect(getTimelineTrimDragClientX(300, rect)).toBe(300);
|
||||
});
|
||||
|
||||
it("prevents the mobile fixed playhead from ending beyond the project", () => {
|
||||
expect(getMobileTrimReleaseScrollLeft(900, 700)).toBe(700);
|
||||
expect(getMobileTrimReleaseScrollLeft(500, 700)).toBe(500);
|
||||
});
|
||||
|
||||
it("does not scroll inside the safe center region", () => {
|
||||
expect(getTimelineEdgeAutoScrollStep(300, rect)).toBe(0);
|
||||
});
|
||||
@@ -52,7 +65,7 @@ describe("mobile timeline edge auto-scroll", () => {
|
||||
const track = {
|
||||
classList: makeClassList(trackClasses),
|
||||
closest: () => ({ querySelector: () => ruler }),
|
||||
parentElement: { scrollLeft: 0 },
|
||||
parentElement: { scrollLeft: 0, getBoundingClientRect: () => rect },
|
||||
};
|
||||
const scroller = createTimelineEdgeAutoScroller({
|
||||
trackElement: track,
|
||||
@@ -89,12 +102,38 @@ describe("mobile timeline edge auto-scroll", () => {
|
||||
expect(scrollElement.scrollLeft).toBe(120);
|
||||
});
|
||||
|
||||
it("clamps only mobile release scrolling to the final track end", () => {
|
||||
const createScroller = (isMobile, pointerType) => {
|
||||
const scrollElement = {
|
||||
clientWidth: 400,
|
||||
scrollLeft: 900,
|
||||
getBoundingClientRect: () => rect,
|
||||
};
|
||||
const track = {
|
||||
classList: { add() {}, remove() {} },
|
||||
closest: () => null,
|
||||
getBoundingClientRect: () => ({ width: 700 }),
|
||||
parentElement: scrollElement,
|
||||
};
|
||||
const scroller = createTimelineEdgeAutoScroller({
|
||||
trackElement: track,
|
||||
pointerType,
|
||||
win: { matchMedia: () => ({ matches: isMobile }), requestAnimationFrame: () => 1, cancelAnimationFrame() {} },
|
||||
});
|
||||
scroller.stop();
|
||||
return scrollElement.scrollLeft;
|
||||
};
|
||||
|
||||
expect(createScroller(true, "touch")).toBe(700);
|
||||
expect(createScroller(false, "mouse")).toBe(900);
|
||||
});
|
||||
|
||||
it("enables the same edge-scroll lifecycle for desktop mouse trimming", () => {
|
||||
const values = new Set();
|
||||
const track = {
|
||||
classList: { add: (value) => values.add(value), remove: (value) => values.delete(value) },
|
||||
closest: () => null,
|
||||
parentElement: { scrollLeft: 0 },
|
||||
parentElement: { scrollLeft: 0, getBoundingClientRect: () => rect },
|
||||
};
|
||||
const scroller = createTimelineEdgeAutoScroller({
|
||||
trackElement: track,
|
||||
@@ -102,6 +141,7 @@ describe("mobile timeline edge auto-scroll", () => {
|
||||
win: { matchMedia: () => ({ matches: false }), requestAnimationFrame: () => 1, cancelAnimationFrame() {} },
|
||||
});
|
||||
expect(values.has("is-trimming")).toBe(true);
|
||||
expect(scroller.getDragClientX(520)).toBe(490);
|
||||
scroller.stop();
|
||||
expect(values.has("is-trimming")).toBe(false);
|
||||
});
|
||||
|
||||
@@ -173,7 +173,8 @@ export function createTimelineMoveControls(d) {
|
||||
if (!moved) d.pauseForTimelineEdit?.();
|
||||
moved = true; e.preventDefault();
|
||||
autoScroller?.update(e.clientX);
|
||||
const delta = getTimelineDragTimeDelta({ clientX: e.clientX, startX, scrollOffset, contentWidth: rect.width, timelineDuration });
|
||||
const dragClientX = autoScroller?.getDragClientX(e.clientX) ?? e.clientX;
|
||||
const delta = getTimelineDragTimeDelta({ clientX: dragClientX, startX, scrollOffset, contentWidth: rect.width, timelineDuration });
|
||||
let nextStart = edge === "start"
|
||||
? Math.max(0, Math.min(originalEnd - MIN_VISUAL_SEGMENT_SECONDS, originalStart + delta))
|
||||
: originalStart;
|
||||
|
||||
@@ -147,7 +147,8 @@ export function createTimelineReorderControls(d) {
|
||||
if (!state.dragging && Math.abs(moveEvent.clientX - startX) < 3) return;
|
||||
if (!state.dragging) d.pauseForTimelineEdit?.();
|
||||
autoScroller?.update(moveEvent.clientX);
|
||||
const delta = getTimelineDragTimeDelta({ clientX: moveEvent.clientX, startX, scrollOffset, contentWidth: trackWidth, timelineDuration: d.timelineDuration });
|
||||
const dragClientX = autoScroller?.getDragClientX(moveEvent.clientX) ?? moveEvent.clientX;
|
||||
const delta = getTimelineDragTimeDelta({ clientX: dragClientX, startX, scrollOffset, contentWidth: trackWidth, timelineDuration: d.timelineDuration });
|
||||
const minimumDuration = 0.2;
|
||||
let previewStart = edge === "start"
|
||||
? Math.max(0, Math.min(state.originalEnd - minimumDuration, state.originalStart + delta))
|
||||
|
||||
@@ -46,6 +46,12 @@ export function getTimelineZoomForVisibleDuration(visibleDuration) {
|
||||
return clampTimelineZoom((low + high) / 2);
|
||||
}
|
||||
|
||||
export function getTimelineVisibleDurationForPixelScale(pixelsPerSecond, scaleBasisWidth) {
|
||||
const scale = Math.max(0, Number(pixelsPerSecond) || 0);
|
||||
const basis = Math.max(0, Number(scaleBasisWidth) || 0);
|
||||
return scale > 0 ? basis / scale : 0;
|
||||
}
|
||||
|
||||
export function getTimelineAutoFitZoom(contentDuration, fillRatio = 0.82) {
|
||||
const safeDuration = Math.max(0.5, Number(contentDuration) || 0.5);
|
||||
const safeFillRatio = Math.max(0.5, Math.min(0.9, Number(fillRatio) || 0.82));
|
||||
|
||||
@@ -5,10 +5,17 @@ import {
|
||||
getMobilePinchZoomState,
|
||||
getTimelineRulerTicks,
|
||||
getTimelineTrackWidthPercent,
|
||||
getTimelineVisibleDurationForPixelScale,
|
||||
getTimelineZoomForVisibleDuration,
|
||||
} from "./timelineScale.js";
|
||||
|
||||
describe("timeline ruler density", () => {
|
||||
it("keeps mobile trim release on the canonical track-width scale", () => {
|
||||
const pixelsPerSecond = 65;
|
||||
expect(getTimelineVisibleDurationForPixelScale(pixelsPerSecond, 520)).toBe(8);
|
||||
expect(getTimelineVisibleDurationForPixelScale(pixelsPerSecond, 260)).toBe(4);
|
||||
});
|
||||
|
||||
it("raises mobile major tick spacing so short-timeline labels do not overlap", () => {
|
||||
const ticks = getTimelineRulerTicks(4, 3, 0, 4, { minimumMajorStep: 0.68 });
|
||||
const labels = ticks.filter((tick) => tick.isMajor).map((tick) => tick.label);
|
||||
|
||||
Reference in new issue
Block a user