-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathStoryboardBoard.tsx
More file actions
526 lines (498 loc) · 18.1 KB
/
Copy pathStoryboardBoard.tsx
File metadata and controls
526 lines (498 loc) · 18.1 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/** @jsxImportSource @emotion/react */
/**
* StoryboardBoard
*
* The board header (title, brief, style, aspect ratio, shot count, and a
* "Direct" trigger) plus a responsive grid of {@link ShotCard}s. The Direct
* button is a placeholder that forwards to an `onDirect` callback so a parent
* can wire it to a real Director run.
*/
import React, { memo, useCallback, useMemo, useState } from "react";
import { css } from "@emotion/react";
import { useTheme } from "@mui/material/styles";
import type { Theme } from "@mui/material/styles";
import {
FlexColumn,
FlexRow,
FormField,
FormGrid,
FormSection,
Panel,
TextInput,
SelectField,
EditorButton,
UndoRedoButtons,
EmptyState,
Dialog,
Divider,
Text,
Caption,
CONTROL,
SPACING,
getSpacingPx,
BORDER_RADIUS,
MOTION
} from "../ui_primitives";
import {
useBoard,
useStoryboardStore,
useStoryboardCanUndo,
useStoryboardCanRedo
} from "../../stores/storyboard/StoryboardStore";
import { useGenerateShot } from "../../hooks/storyboard/useGenerateShot";
import {
useImageModelsByProvider,
type ImageModelTask
} from "../../hooks/useModelsByProvider";
import LanguageModelSelect from "../properties/LanguageModelSelect";
import ImageModelSelect from "../properties/ImageModelSelect";
import VideoModelSelect from "../properties/VideoModelSelect";
import ShotCard from "./ShotCard";
import StoryboardEntitiesField from "./StoryboardEntitiesField";
interface StoryboardBoardProps {
boardId: string;
readOnly?: boolean;
/** Wired by the parent to a Director run; receives the requested shot count. */
onDirect?: (shotCount: number) => void;
/** True while a Director run is in flight (disables and relabels the button). */
directing?: boolean;
/** Error from the last Director run, shown under the header fields. */
directError?: string | null;
/** Wired by the parent to the timeline handoff. */
onAssemble?: () => void;
/** True while assembly is in flight. */
assembling?: boolean;
/** Error from the last assembly, shown under the header fields. */
assembleError?: string | null;
}
const ASPECT_OPTIONS = [
{ value: "16:9", label: "16:9 — Widescreen" },
{ value: "9:16", label: "9:16 — Vertical" },
{ value: "1:1", label: "1:1 — Square" },
{ value: "4:3", label: "4:3 — Classic" },
{ value: "21:9", label: "21:9 — Cinematic" }
] as const;
const SHOT_COUNT_OPTIONS = [3, 4, 5, 6, 8, 10, 12].map((n) => ({
value: n,
label: `${n} shots`
}));
// Stills can come from a plain generator or an editing model; the latter can
// take entity reference images, so the picker offers both.
const STILL_MODEL_TASKS: ImageModelTask[] = ["text_to_image", "image_to_image"];
// The model pickers are custom buttons, not InputBase controls; hold them at
// the shared form-control height. Scoped to the picker's own class so no
// other button that ends up inside the field is affected.
const modelFieldSx = {
"& .select-model-button": { minHeight: `${CONTROL.height.lg}px` }
} as const;
const styles = (theme: Theme) =>
css({
height: "100%",
overflowY: "auto",
padding: getSpacingPx(SPACING.lg),
display: "flex",
flexDirection: "column",
gap: getSpacingPx(SPACING.lg),
// Reclaim horizontal room for shots on phones.
[theme.breakpoints.down("sm")]: {
padding: getSpacingPx(SPACING.md),
gap: getSpacingPx(SPACING.md)
},
// Children must keep their natural height so the container scrolls;
// otherwise the header panel gets flex-shrunk under the grid.
"> *": { flexShrink: 0 },
".shot-list": {
display: "flex",
flexDirection: "column",
gap: getSpacingPx(SPACING.md)
},
// ── Directing: ghost cards materializing while the screenplay is written ──
"@keyframes storyboard-ghost-in": {
from: { opacity: 0, transform: "translateY(8px) scale(0.98)" },
to: { opacity: 1, transform: "translateY(0) scale(1)" }
},
"@keyframes storyboard-shimmer": {
from: { backgroundPosition: "200% 0" },
to: { backgroundPosition: "-200% 0" }
},
"@keyframes storyboard-spark": {
"0%, 100%": { opacity: 0.35, transform: "scale(0.9) rotate(0deg)" },
"50%": { opacity: 0.9, transform: "scale(1.15) rotate(90deg)" }
},
".directing-line": {
color: theme.vars.palette.primary.main,
backgroundImage: `linear-gradient(90deg, ${theme.vars.palette.primary.main}, ${theme.vars.palette.text.secondary}, ${theme.vars.palette.primary.main})`,
backgroundSize: "200% 100%",
backgroundClip: "text",
WebkitBackgroundClip: "text",
WebkitTextFillColor: "transparent",
animation: `storyboard-shimmer ${MOTION.spin} infinite`
},
".ghost-card": {
border: `1px solid ${theme.vars.palette.divider}`,
borderRadius: BORDER_RADIUS.md,
padding: getSpacingPx(SPACING.md),
display: "grid",
gridTemplateColumns: "minmax(220px, 300px) minmax(0, 1fr)",
gap: getSpacingPx(SPACING.md),
alignItems: "start",
opacity: 0,
animation: `storyboard-ghost-in ${MOTION.slow} both`,
"@media (max-width: 720px)": {
gridTemplateColumns: "minmax(0, 1fr)"
}
},
".ghost-lines": {
display: "flex",
flexDirection: "column",
gap: getSpacingPx(SPACING.sm)
},
".ghost-frame": {
aspectRatio: "16 / 9",
borderRadius: BORDER_RADIUS.sm,
display: "grid",
placeItems: "center",
backgroundImage: `linear-gradient(100deg, ${theme.vars.palette.c_overlay_subtle} 40%, ${theme.vars.palette.action.selected} 50%, ${theme.vars.palette.c_overlay_subtle} 60%)`,
backgroundSize: "200% 100%",
animation: `storyboard-shimmer ${MOTION.spin} infinite`
},
".ghost-line": {
height: "10px",
borderRadius: BORDER_RADIUS.pill,
backgroundImage: `linear-gradient(100deg, ${theme.vars.palette.c_overlay_subtle} 40%, ${theme.vars.palette.action.selected} 50%, ${theme.vars.palette.c_overlay_subtle} 60%)`,
backgroundSize: "200% 100%",
animation: `storyboard-shimmer ${MOTION.spin} infinite`
},
".spark": {
fontSize: "1.5em",
color: theme.vars.palette.primary.main,
animation: `storyboard-spark ${MOTION.pulse} infinite`
},
"@media (prefers-reduced-motion: reduce)": {
".ghost-frame, .ghost-line, .directing-line, .spark": {
animation: "none"
},
".ghost-card": { animation: "none", opacity: 1 }
},
".header-fields": {
color: theme.vars.palette.text.secondary,
// Keep the writing fields at a readable measure on wide screens.
maxWidth: "1100px",
// Rule between the columns, dropped when they stack.
".settings": {
paddingLeft: getSpacingPx(SPACING.xl),
borderLeft: `1px solid ${theme.vars.palette.divider}`,
"@media (max-width: 860px)": {
paddingLeft: 0,
borderLeft: "none"
}
}
}
});
const StoryboardBoardInner: React.FC<StoryboardBoardProps> = ({
boardId,
readOnly,
onDirect,
directing,
directError,
onAssemble,
assembling,
assembleError
}) => {
const theme = useTheme();
const {
title,
brief,
style,
entityIds,
aspectRatio,
directorModel,
imageModel,
videoModel,
shots
} = useBoard(boardId);
const setTitle = useStoryboardStore((state) => state.setTitle);
const setBrief = useStoryboardStore((state) => state.setBrief);
const setStyle = useStoryboardStore((state) => state.setStyle);
const setAspectRatio = useStoryboardStore((state) => state.setAspectRatio);
const setDirectorModel = useStoryboardStore((state) => state.setDirectorModel);
const setImageModel = useStoryboardStore((state) => state.setImageModel);
const setVideoModel = useStoryboardStore((state) => state.setVideoModel);
const undo = useStoryboardStore((state) => state.undo);
const redo = useStoryboardStore((state) => state.redo);
const canUndo = useStoryboardCanUndo(boardId);
const canRedo = useStoryboardCanRedo(boardId);
const onUndo = useCallback(() => undo(boardId), [undo, boardId]);
const onRedo = useCallback(() => redo(boardId), [redo, boardId]);
const [shotCount, setShotCount] = useState<number>(6);
const [confirmRedirect, setConfirmRedirect] = useState(false);
const hasShots = shots.length > 0;
// Entity reference images only reach generation through an editing model;
// warn when entities are attached but the still model can't take them.
const { models: imageModels } = useImageModelsByProvider();
const stillModelDetails = imageModel?.id
? imageModels.find((m) => m.id === imageModel.id)
: undefined;
const entitiesNeedEditModel =
entityIds.length > 0 &&
!stillModelDetails?.supported_tasks?.includes("image_to_image");
const runDirect = useCallback(() => {
onDirect?.(shotCount);
}, [onDirect, shotCount]);
// Directing rewrites the whole screenplay, replacing every existing shot
// (and its generated stills and clips). Confirm before clobbering work.
const handleDirect = useCallback(() => {
if (hasShots) {
setConfirmRedirect(true);
return;
}
runDirect();
}, [hasShots, runDirect]);
const handleConfirmRedirect = useCallback(() => {
setConfirmRedirect(false);
runDirect();
}, [runDirect]);
const hasRenderedShot = useMemo(
() => shots.some((s) => s.status === "rendered" && !!s.clip?.asset_id),
[shots]
);
const pendingStills = useMemo(
() =>
shots.filter(
(s) => !s.keyframe && (s.status === "planned" || s.status === "failed")
),
[shots]
);
const pendingClips = useMemo(
() =>
shots.filter(
(s) =>
!!s.keyframe &&
!s.clip &&
s.status !== "keyframe_generating" &&
s.status !== "clip_generating"
),
[shots]
);
const { generateKeyframe, generateClip } = useGenerateShot();
const handleGenerateAllStills = useCallback(() => {
for (const shot of pendingStills) {
void generateKeyframe(boardId, shot);
}
}, [pendingStills, generateKeyframe, boardId]);
const handleGenerateAllClips = useCallback(() => {
for (const shot of pendingClips) {
void generateClip(boardId, shot);
}
}, [pendingClips, generateClip, boardId]);
return (
<div css={styles(theme)} className="storyboard-board">
{!readOnly && (
<Panel padding="normal" className="header-fields">
<FlexColumn gap={4}>
<FormGrid>
<FormSection label="Screenplay">
<FormField label="Title">
<TextInput
value={title}
placeholder="Untitled film"
onChange={(e) => setTitle(boardId, e.target.value)}
/>
</FormField>
<FormField label="Brief">
<TextInput
value={brief}
placeholder="Your film in one or two sentences"
onChange={(e) => setBrief(boardId, e.target.value)}
multiline
rows={3}
/>
</FormField>
<FormField label="Style">
<TextInput
value={style}
placeholder="Palette, light, lens, texture"
onChange={(e) => setStyle(boardId, e.target.value)}
/>
</FormField>
<FormField label="Entities">
<StoryboardEntitiesField
boardId={boardId}
entityIds={entityIds}
/>
</FormField>
</FormSection>
<FormSection label="Direction" className="settings">
<FormField label="Screenplay model" sx={modelFieldSx}>
<LanguageModelSelect
value={directorModel?.id ?? ""}
onChange={(value) => setDirectorModel(boardId, value)}
/>
</FormField>
<FormField label="Still model" sx={modelFieldSx}>
<ImageModelSelect
value={imageModel?.id ?? ""}
task={STILL_MODEL_TASKS}
onChange={(value) => setImageModel(boardId, value)}
/>
{entitiesNeedEditModel && (
<Text size="small" color="warning">
Entities carry reference images, but this model only
takes text. Pick an image-to-image model to use them.
</Text>
)}
</FormField>
<FormField label="Clip model" sx={modelFieldSx}>
<VideoModelSelect
value={videoModel?.id ?? ""}
task="image_to_video"
onChange={(value) => setVideoModel(boardId, value)}
/>
</FormField>
<FormField label="Aspect ratio">
<SelectField
label="Aspect ratio"
value={aspectRatio}
onChange={(value) => setAspectRatio(boardId, value)}
options={ASPECT_OPTIONS}
/>
</FormField>
<FormField label="Shots">
<SelectField
label="Shots"
value={shotCount}
onChange={(value) => setShotCount(Number(value))}
options={SHOT_COUNT_OPTIONS}
/>
</FormField>
</FormSection>
</FormGrid>
<Divider />
<FlexRow gap={2} align="center" justify="space-between" wrap>
<Text size="small" color={directError || assembleError ? "error" : "secondary"}>
{directError ??
assembleError ??
(hasShots
? "Re-directing rewrites the screenplay and replaces every shot."
: "Direct writes the screenplay and seeds your shots.")}
</Text>
<FlexRow gap={2} align="center">
<UndoRedoButtons
canUndo={canUndo}
canRedo={canRedo}
onUndo={onUndo}
onRedo={onRedo}
undoTooltip="Undo (⌘Z)"
redoTooltip="Redo (⌘⇧Z)"
/>
<EditorButton
variant="outlined"
onClick={handleGenerateAllStills}
disabled={pendingStills.length === 0 || directing}
>
{`✦ Generate all stills${pendingStills.length > 0 ? ` (${pendingStills.length})` : ""}`}
</EditorButton>
<EditorButton
variant="outlined"
onClick={handleGenerateAllClips}
disabled={pendingClips.length === 0 || directing}
>
{`✦ Generate all clips${pendingClips.length > 0 ? ` (${pendingClips.length})` : ""}`}
</EditorButton>
<EditorButton
variant="outlined"
onClick={onAssemble}
disabled={!onAssemble || assembling || !hasRenderedShot}
>
{assembling ? "Assembling…" : "Assemble timeline"}
</EditorButton>
<EditorButton
variant="contained"
color="primary"
onClick={handleDirect}
disabled={!onDirect || directing}
>
{directing ? "Directing…" : hasShots ? "Re-direct" : "Direct"}
</EditorButton>
</FlexRow>
</FlexRow>
</FlexColumn>
</Panel>
)}
<Dialog
open={confirmRedirect}
onClose={() => setConfirmRedirect(false)}
title="Re-direct this storyboard?"
onConfirm={handleConfirmRedirect}
confirmText="Re-direct"
destructive
>
<FlexColumn gap={1}>
<Text size="small">
{`Directing writes a new screenplay and replaces all ${shots.length} current shot${shots.length === 1 ? "" : "s"}.`}
</Text>
<Caption color="secondary">
Generated stills and clips stay in your asset library, but the
shots on this board are rebuilt from scratch.
</Caption>
</FlexColumn>
</Dialog>
{directing ? (
<>
<Text size="small" className="directing-line">
✦ The director is writing your screenplay…
</Text>
<div className="shot-list">
{Array.from({ length: shotCount }).map((_, i) => (
<div
key={i}
className="ghost-card"
style={{ animationDelay: `${i * 140}ms` }}
>
<div className="ghost-frame">
<span className="spark">✦</span>
</div>
<div className="ghost-lines">
<div className="ghost-line" style={{ width: "40%" }} />
<div className="ghost-line" style={{ width: "85%" }} />
<div className="ghost-line" style={{ width: "60%" }} />
</div>
</div>
))}
</div>
</>
) : shots.length === 0 ? (
<EmptyState
variant="empty"
title="No shots yet"
description={
readOnly
? "This storyboard has no shots."
: "Write a brief and press Direct to generate a screenplay of shots."
}
/>
) : (
<>
<Text size="small" color="secondary">
{`${shots.length} shot${shots.length === 1 ? "" : "s"}`}
</Text>
<div className="shot-list">
{shots.map((shot, i) => (
<ShotCard
key={shot.id}
boardId={boardId}
shot={shot}
readOnly={readOnly}
isFirst={i === 0}
isLast={i === shots.length - 1}
/>
))}
</div>
</>
)}
</div>
);
};
export const StoryboardBoard = memo(StoryboardBoardInner);
StoryboardBoard.displayName = "StoryboardBoard";
export default StoryboardBoard;