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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
"use client";
import { useState } from "react";
import type { ParamValues } from "@/params";
import type { Effect } from "@/effects/types";
import type { EffectElement, VisualElement } from "@/timeline";
import { effectsRegistry } from "@/effects";
import { useEditor } from "@/editor/use-editor";
import { useElementPreview } from "@/timeline/hooks/use-element-preview";
import {
Section,
SectionContent,
SectionHeader,
SectionTitle,
SectionFields,
} from "@/components/section";
import { PropertyParamField } from "@/components/editor/panels/properties/components/property-param-field";
import { Button } from "@/components/ui/button";
import { HugeiconsIcon } from "@hugeicons/react";
import {
Delete02Icon,
ViewIcon,
ViewOffSlashIcon,
MagicWand05Icon,
} from "@hugeicons/core-free-icons";
import { cn } from "@/utils/ui";
import { Separator } from "@/components/ui/separator";
import { useAssetsPanelStore } from "@/components/editor/panels/assets/assets-panel-store";
export function StandaloneEffectTab({
element,
trackId,
}: {
element: EffectElement;
trackId: string;
}) {
const { renderElement, previewUpdates, commit } = useElementPreview({
trackId,
elementId: element.id,
fallback: element,
});
const effect: Effect = {
id: element.id,
type: element.effectType,
params: element.params,
enabled: true,
};
const previewParam = (key: string) => (value: number | string | boolean) => {
previewUpdates({
params: { ...(renderElement as EffectElement).params, [key]: value },
});
};
return (
<div className="flex flex-col h-full">
<div className="border-b px-3.5 h-11 shrink-0 flex items-center">
<SectionTitle>Effect</SectionTitle>
</div>
<EffectSection
effect={effect}
renderParams={(renderElement as EffectElement).params}
previewParam={previewParam}
onCommit={commit}
/>
</div>
);
}
export function ClipEffectsTab({
element,
trackId,
}: {
element: VisualElement;
trackId: string;
}) {
const [dragIndex, setDragIndex] = useState<number | null>(null);
const [dropIndex, setDropIndex] = useState<number | null>(null);
const editor = useEditor();
const { renderElement, previewUpdates, commit } = useElementPreview({
trackId,
elementId: element.id,
fallback: element,
});
const effects: Effect[] = element.effects ?? [];
const getRenderParams = ({ effectId }: { effectId: string }): ParamValues => {
return (
(renderElement as VisualElement).effects?.find((ef) => ef.id === effectId)
?.params ??
effects.find((ef) => ef.id === effectId)?.params ??
{}
);
};
const buildPreviewParam =
(effectId: string) =>
(key: string) =>
(value: number | string | boolean) => {
const updatedEffects = (
(renderElement as VisualElement).effects ?? []
).map((existing) =>
existing.id !== effectId
? existing
: { ...existing, params: { ...existing.params, [key]: value } },
);
previewUpdates({ effects: updatedEffects });
};
const handleDragStart = ({ index }: { index: number }) => setDragIndex(index);
const handleDragOver = ({
event,
index,
}: {
event: React.DragEvent;
index: number;
}) => {
event.preventDefault();
if (index !== dropIndex) setDropIndex(index);
};
const handleDrop = ({ toIndex }: { toIndex: number }) => {
if (dragIndex !== null && dragIndex !== toIndex) {
editor.timeline.reorderClipEffects({
trackId,
elementId: element.id,
fromIndex: dragIndex,
toIndex,
});
}
setDragIndex(null);
setDropIndex(null);
};
const handleDragEnd = () => {
setDragIndex(null);
setDropIndex(null);
};
return (
<div className="flex flex-col h-full">
<div className="border-b px-3.5 h-11 shrink-0 flex items-center">
<SectionTitle>Effects</SectionTitle>
</div>
{effects.length === 0 ? (
<EmptyView />
) : (
<ul className="flex flex-col">
{effects.map((effect, index) => {
const resolvedDragIndex = dragIndex ?? -1;
const isDragging = dragIndex === index;
const isDropTarget =
dropIndex === index && dragIndex !== null && dragIndex !== index;
const showTopDropIndicator =
isDropTarget && index < resolvedDragIndex;
const showBottomDropIndicator =
isDropTarget && index > resolvedDragIndex;
return (
<li
key={effect.id}
draggable
onDragStart={() => handleDragStart({ index })}
onDragOver={(event) => handleDragOver({ event, index })}
onDrop={() => handleDrop({ toIndex: index })}
onDragEnd={handleDragEnd}
className={cn(
"group list-none",
isDragging && "opacity-40",
showTopDropIndicator && "border-t-2 border-primary",
showBottomDropIndicator && "border-b-2 border-primary",
)}
>
<EffectSection
effect={effect}
renderParams={getRenderParams({ effectId: effect.id })}
previewParam={buildPreviewParam(effect.id)}
onCommit={commit}
onToggle={() =>
editor.timeline.toggleClipEffect({
trackId,
elementId: element.id,
effectId: effect.id,
})
}
onRemove={() =>
editor.timeline.removeClipEffect({
trackId,
elementId: element.id,
effectId: effect.id,
})
}
/>
</li>
);
})}
</ul>
)}
</div>
);
}
function EmptyView() {
const setActiveTab = useAssetsPanelStore((s) => s.setActiveTab);
return (
<div className="flex flex-col h-full items-center justify-center gap-4 text-center">
<HugeiconsIcon
icon={MagicWand05Icon}
className="size-10 text-muted-foreground"
strokeWidth={1}
/>
<div className="flex flex-col gap-2">
<h3 className="font-medium text-foreground">No effects</h3>
<p className="text-muted-foreground text-sm text-balance max-w-44">
Add effects to this layer from the Assets panel.
</p>
</div>
<Button
variant="default"
size="sm"
onClick={() => setActiveTab("effects")}
>
Open effects
</Button>
</div>
);
}
function EffectSection({
effect,
renderParams,
previewParam,
onCommit,
onToggle,
onRemove,
}: {
effect: Effect;
renderParams: ParamValues;
previewParam: (key: string) => (value: number | string | boolean) => void;
onCommit: () => void;
onToggle?: () => void;
onRemove?: () => void;
}) {
const definition = effectsRegistry.get(effect.type);
return (
<Section
sectionKey={onToggle ? `clip-effect:${effect.id}` : undefined}
showTopBorder={false}
>
<SectionHeader
className={cn(onToggle && "cursor-move")}
trailing={
onToggle && (
<div className="flex items-center gap-1">
<Button
variant={effect.enabled ? "secondary" : "ghost"}
size="icon"
aria-label={`Toggle ${definition.name}`}
onClick={onToggle}
>
<HugeiconsIcon
icon={effect.enabled ? ViewIcon : ViewOffSlashIcon}
/>
</Button>
<Button
variant="ghost"
size="icon"
aria-label={`Remove ${definition.name}`}
onClick={onRemove}
>
<HugeiconsIcon icon={Delete02Icon} />
</Button>
</div>
)
}
>
<SectionTitle
className={cn(onToggle && !effect.enabled && "text-muted-foreground")}
>
{definition.name}
</SectionTitle>
</SectionHeader>
<SectionContent
className={cn("p-0", onToggle && !effect.enabled && "opacity-50")}
>
<SectionFields>
{definition.params.map((param) => (
<div key={param.key} className="flex flex-col gap-3.5">
<div className="px-4">
<PropertyParamField
param={param}
value={renderParams[param.key] ?? param.default}
onPreview={previewParam(param.key)}
onCommit={onCommit}
/>
</div>
<Separator />
</div>
))}
</SectionFields>
</SectionContent>
</Section>
);
}
|