1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import { useEffect, useReducer, useRef } from "react";
import { useEditor } from "@/editor/use-editor";
import { useShiftKey } from "@/hooks/use-shift-key";
import { useElementSelection } from "@/timeline/hooks/element/use-element-selection";
import { useTimelineStore } from "@/timeline/timeline-store";
import { registerCanceller } from "@/editor/cancel-interaction";
import {
ResizeController,
type ResizeConfig,
} from "@/timeline/controllers/resize-controller";
import type { ResizeSide } from "@/timeline/group-resize";
import type { SnapPoint } from "@/timeline/snapping";
import type { TimelineElement } from "@/timeline";
export type { ResizeSide };
interface UseTimelineResizeProps {
zoomLevel: number;
onSnapPointChange?: (snapPoint: SnapPoint | null) => void;
}
export function useTimelineResize({
zoomLevel,
onSnapPointChange,
}: UseTimelineResizeProps) {
const editor = useEditor();
const isShiftHeldRef = useShiftKey();
const snappingEnabled = useTimelineStore((state) => state.snappingEnabled);
const { selectedElements } = useElementSelection();
const config: ResizeConfig = {
zoomLevel,
snappingEnabled,
isShiftHeld: () => isShiftHeldRef.current,
getSceneTracks: () => editor.scenes.getActiveScene().tracks,
getCurrentPlayheadTime: () => editor.playback.getCurrentTime(),
getActiveProjectFps: () => editor.project.getActive()?.settings.fps ?? null,
selectedElements,
discardPreview: () => editor.timeline.discardPreview(),
previewElements: (updates) =>
editor.timeline.previewElements({
updates: updates.map(({ trackId, elementId, patch }) => ({
trackId,
elementId,
updates: patch as Partial<TimelineElement>,
})),
}),
commitElements: (updates) =>
editor.timeline.updateElements({
updates: updates.map(({ trackId, elementId, patch }) => ({
trackId,
elementId,
patch: patch as Partial<TimelineElement>,
})),
}),
onSnapPointChange,
};
const configRef = useRef(config);
configRef.current = config;
const controllerRef = useRef<ResizeController | null>(null);
if (!controllerRef.current) {
controllerRef.current = new ResizeController({ configRef });
}
const controller = controllerRef.current;
const [, rerender] = useReducer((n: number) => n + 1, 0);
useEffect(() => controller.subscribe(rerender), [controller]);
useEffect(() => {
if (!controller.isResizing) return;
return registerCanceller({ fn: () => controller.cancel() });
}, [controller.isResizing, controller]);
useEffect(() => () => controller.destroy(), [controller]);
return {
isResizing: controller.isResizing,
handleResizeStart: controller.onResizeStart,
};
}
|