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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import { getKeyframeById } from "@/animation";
import type { SelectedKeyframeRef } from "@/animation/types";
import type { TimelineElement } from "@/timeline";
import { PasteKeyframesCommand } from "@/commands/timeline";
import type {
ClipboardHandler,
KeyframeClipboardCurvePatch,
KeyframeClipboardItem,
} from "../types";
function resolveSingleSourceElement({
selectedKeyframes,
}: {
selectedKeyframes: SelectedKeyframeRef[];
}) {
const firstKeyframe = selectedKeyframes[0];
if (!firstKeyframe) {
return null;
}
const sourceElement = {
trackId: firstKeyframe.trackId,
elementId: firstKeyframe.elementId,
};
const isSingleSource = selectedKeyframes.every(
(keyframe) =>
keyframe.trackId === sourceElement.trackId &&
keyframe.elementId === sourceElement.elementId,
);
return isSingleSource ? sourceElement : null;
}
function getCurvePatches({
element,
propertyPath,
keyframeId,
}: {
element: TimelineElement;
propertyPath: KeyframeClipboardItem["propertyPath"];
keyframeId: string;
}): KeyframeClipboardCurvePatch[] {
const binding = element.animations?.bindings[propertyPath];
if (!binding) {
return [];
}
return binding.components.flatMap((component) => {
const channel = element.animations?.channels[component.channelId];
if (channel?.kind !== "scalar") {
return [];
}
const keyframe = channel.keys.find(
(candidate) => candidate.id === keyframeId,
);
if (!keyframe) {
return [];
}
return [
{
componentKey: component.key,
patch: {
leftHandle: keyframe.leftHandle ?? null,
rightHandle: keyframe.rightHandle ?? null,
segmentToNext: keyframe.segmentToNext,
tangentMode: keyframe.tangentMode,
},
},
];
});
}
function buildClipboardItem({
element,
propertyPath,
keyframeId,
}: {
element: TimelineElement;
propertyPath: KeyframeClipboardItem["propertyPath"];
keyframeId: string;
}): (Omit<KeyframeClipboardItem, "timeOffset"> & { time: number }) | null {
const keyframe = getKeyframeById({
animations: element.animations,
propertyPath,
keyframeId,
});
if (!keyframe) {
return null;
}
return {
propertyPath,
time: keyframe.time,
value: keyframe.value,
interpolation: keyframe.interpolation,
curvePatches: getCurvePatches({
element,
propertyPath,
keyframeId,
}),
};
}
export const KeyframesClipboardHandler = {
type: "keyframes",
canCopy({ selectedKeyframes }) {
return selectedKeyframes.length > 0;
},
copy({ editor, selectedKeyframes }) {
const sourceElement = resolveSingleSourceElement({ selectedKeyframes });
if (!sourceElement) {
return null;
}
const [result] = editor.timeline.getElementsWithTracks({
elements: [sourceElement],
});
if (!result) {
return null;
}
const rawItems = selectedKeyframes.flatMap((keyframeRef) => {
const item = buildClipboardItem({
element: result.element,
propertyPath: keyframeRef.propertyPath,
keyframeId: keyframeRef.keyframeId,
});
return item ? [item] : [];
});
if (rawItems.length === 0) {
return null;
}
const minTime = Math.min(...rawItems.map((item) => item.time));
const items = rawItems
.map(({ time, ...item }) => ({
...item,
timeOffset: time - minTime,
}))
.sort(
(left, right) =>
left.timeOffset - right.timeOffset ||
left.propertyPath.localeCompare(right.propertyPath),
);
return {
type: "keyframes",
sourceElement,
items,
};
},
paste(entry, { selectedElements, time }) {
const targetElement = selectedElements[0];
if (!targetElement || entry.items.length === 0) {
return null;
}
return new PasteKeyframesCommand({
trackId: targetElement.trackId,
elementId: targetElement.elementId,
time,
clipboardItems: entry.items,
});
},
} satisfies ClipboardHandler<"keyframes">;
|