|
1 | | -import { useState, useCallback, useRef, useEffect, useMemo, type ReactNode } from "react"; |
| 1 | +import { |
| 2 | + useState, |
| 3 | + useCallback, |
| 4 | + useRef, |
| 5 | + useEffect, |
| 6 | + useMemo, |
| 7 | + type MouseEvent, |
| 8 | + type ReactNode, |
| 9 | +} from "react"; |
2 | 10 | import { useMountEffect } from "./hooks/useMountEffect"; |
3 | 11 | import { NLELayout } from "./components/nle/NLELayout"; |
4 | 12 | import { SourceEditor } from "./components/editor/SourceEditor"; |
5 | 13 | import { LeftSidebar } from "./components/sidebar/LeftSidebar"; |
6 | 14 | import { RenderQueue } from "./components/renders/RenderQueue"; |
7 | 15 | import { useRenderQueue } from "./components/renders/useRenderQueue"; |
8 | | -import { CompositionThumbnail, VideoThumbnail, usePlayerStore } from "./player"; |
| 16 | +import { CompositionThumbnail, VideoThumbnail, liveTime, usePlayerStore } from "./player"; |
9 | 17 | import { AudioWaveform } from "./player/components/AudioWaveform"; |
10 | 18 | import type { TimelineElement } from "./player"; |
11 | 19 | import { LintModal } from "./components/LintModal"; |
@@ -40,6 +48,8 @@ import { |
40 | 48 | getTimelineToggleTitle, |
41 | 49 | shouldHandleTimelineToggleHotkey, |
42 | 50 | } from "./utils/timelineDiscovery"; |
| 51 | +import { buildFrameCaptureFilename, buildFrameCaptureUrl } from "./utils/frameCapture"; |
| 52 | +import { Camera } from "./icons/SystemIcons"; |
43 | 53 |
|
44 | 54 | interface EditingFile { |
45 | 55 | path: string; |
@@ -264,6 +274,7 @@ export function StudioApp() { |
264 | 274 | const [globalDragOver, setGlobalDragOver] = useState(false); |
265 | 275 | const [appToast, setAppToast] = useState<AppToast | null>(null); |
266 | 276 | const [timelineVisible, setTimelineVisible] = useState(true); |
| 277 | + const [captureFrameTime, setCaptureFrameTime] = useState(0); |
267 | 278 | const dragCounterRef = useRef(0); |
268 | 279 | const toastTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
269 | 280 | const lastBlockedTimelineToastAtRef = useRef(0); |
@@ -298,6 +309,26 @@ export function StudioApp() { |
298 | 309 | const toggleTimelineVisibility = useCallback(() => { |
299 | 310 | setTimelineVisible((visible) => !visible); |
300 | 311 | }, []); |
| 312 | + const toggleLeftSidebar = useCallback(() => { |
| 313 | + setLeftCollapsed((collapsed) => !collapsed); |
| 314 | + }, []); |
| 315 | + const refreshCaptureFrameTime = useCallback(() => { |
| 316 | + setCaptureFrameTime(usePlayerStore.getState().currentTime); |
| 317 | + }, []); |
| 318 | + |
| 319 | + useMountEffect(() => { |
| 320 | + setCaptureFrameTime(usePlayerStore.getState().currentTime); |
| 321 | + return liveTime.subscribe(setCaptureFrameTime); |
| 322 | + }); |
| 323 | + |
| 324 | + const captureFrameHref = projectId |
| 325 | + ? buildFrameCaptureUrl({ |
| 326 | + projectId, |
| 327 | + compositionPath: activeCompPath, |
| 328 | + currentTime: captureFrameTime, |
| 329 | + }) |
| 330 | + : "#"; |
| 331 | + const captureFrameFilename = buildFrameCaptureFilename(activeCompPath, captureFrameTime); |
301 | 332 | useMountEffect(() => () => { |
302 | 333 | if (toastTimerRef.current) clearTimeout(toastTimerRef.current); |
303 | 334 | }); |
@@ -496,6 +527,28 @@ export function StudioApp() { |
496 | 527 | > |
497 | 528 | + |
498 | 529 | </button> |
| 530 | + <button |
| 531 | + type="button" |
| 532 | + onClick={toggleTimelineVisibility} |
| 533 | + className="ml-1 flex h-7 w-7 items-center justify-center rounded-md text-neutral-500 transition-colors hover:bg-neutral-900 hover:text-neutral-200" |
| 534 | + title={getTimelineToggleTitle(true)} |
| 535 | + aria-label="Hide timeline editor" |
| 536 | + > |
| 537 | + <svg |
| 538 | + width="14" |
| 539 | + height="14" |
| 540 | + viewBox="0 0 24 24" |
| 541 | + fill="none" |
| 542 | + stroke="currentColor" |
| 543 | + strokeWidth="1.8" |
| 544 | + strokeLinecap="round" |
| 545 | + strokeLinejoin="round" |
| 546 | + aria-hidden="true" |
| 547 | + > |
| 548 | + <path d="M5 7h14" /> |
| 549 | + <path d="m8 11 4 4 4-4" /> |
| 550 | + </svg> |
| 551 | + </button> |
499 | 552 | </div> |
500 | 553 | </div> |
501 | 554 | </div> |
@@ -787,6 +840,42 @@ export function StudioApp() { |
787 | 840 | toastTimerRef.current = setTimeout(() => setAppToast(null), 4000); |
788 | 841 | }, []); |
789 | 842 |
|
| 843 | + const handleCaptureFrameClick = useCallback( |
| 844 | + async (event: MouseEvent<HTMLAnchorElement>) => { |
| 845 | + if (!projectId) return; |
| 846 | + event.preventDefault(); |
| 847 | + |
| 848 | + const currentTime = usePlayerStore.getState().currentTime; |
| 849 | + setCaptureFrameTime(currentTime); |
| 850 | + const href = buildFrameCaptureUrl({ |
| 851 | + projectId, |
| 852 | + compositionPath: activeCompPath, |
| 853 | + currentTime, |
| 854 | + }); |
| 855 | + const filename = buildFrameCaptureFilename(activeCompPath, currentTime); |
| 856 | + |
| 857 | + try { |
| 858 | + const response = await fetch(href, { cache: "no-store" }); |
| 859 | + if (!response.ok) { |
| 860 | + throw new Error(`Capture failed (${response.status})`); |
| 861 | + } |
| 862 | + const blob = await response.blob(); |
| 863 | + const blobUrl = URL.createObjectURL(blob); |
| 864 | + const link = document.createElement("a"); |
| 865 | + link.href = blobUrl; |
| 866 | + link.download = filename; |
| 867 | + document.body.appendChild(link); |
| 868 | + link.click(); |
| 869 | + link.remove(); |
| 870 | + setTimeout(() => URL.revokeObjectURL(blobUrl), 0); |
| 871 | + } catch (err) { |
| 872 | + const message = err instanceof Error ? err.message : "Capture failed"; |
| 873 | + showToast(message); |
| 874 | + } |
| 875 | + }, |
| 876 | + [activeCompPath, projectId, showToast], |
| 877 | + ); |
| 878 | + |
790 | 879 | const handleTimelineElementDelete = useCallback( |
791 | 880 | async (element: TimelineElement) => { |
792 | 881 | const pid = projectIdRef.current; |
@@ -1345,55 +1434,19 @@ export function StudioApp() { |
1345 | 1434 | </div> |
1346 | 1435 | {/* Right: toolbar buttons */} |
1347 | 1436 | <div className="flex items-center gap-1.5"> |
1348 | | - <button |
1349 | | - onClick={() => setLeftCollapsed((v) => !v)} |
1350 | | - className={`h-7 w-7 flex items-center justify-center rounded-md border transition-colors ${ |
1351 | | - !leftCollapsed |
1352 | | - ? "text-studio-accent bg-studio-accent/10 border-studio-accent/30" |
1353 | | - : "bg-transparent border-transparent text-neutral-500 hover:text-neutral-300 hover:bg-neutral-800" |
1354 | | - }`} |
1355 | | - title={leftCollapsed ? "Show sidebar" : "Hide sidebar"} |
| 1437 | + <a |
| 1438 | + href={captureFrameHref} |
| 1439 | + download={captureFrameFilename} |
| 1440 | + onClick={handleCaptureFrameClick} |
| 1441 | + onFocus={refreshCaptureFrameTime} |
| 1442 | + onPointerDown={refreshCaptureFrameTime} |
| 1443 | + className="h-7 flex items-center gap-1.5 px-2.5 rounded-md text-[11px] font-medium border border-neutral-700 text-neutral-300 transition-colors hover:border-neutral-500 hover:bg-neutral-800" |
| 1444 | + title="Capture current frame" |
| 1445 | + aria-label="Capture current frame" |
1356 | 1446 | > |
1357 | | - <svg |
1358 | | - width="14" |
1359 | | - height="14" |
1360 | | - viewBox="0 0 24 24" |
1361 | | - fill="none" |
1362 | | - stroke="currentColor" |
1363 | | - strokeWidth="1.5" |
1364 | | - strokeLinecap="round" |
1365 | | - strokeLinejoin="round" |
1366 | | - > |
1367 | | - <rect x="3" y="3" width="18" height="18" rx="2" /> |
1368 | | - <path d="M9 3v18" /> |
1369 | | - </svg> |
1370 | | - </button> |
1371 | | - <button |
1372 | | - type="button" |
1373 | | - onClick={toggleTimelineVisibility} |
1374 | | - className={`h-7 flex items-center gap-1.5 px-2.5 rounded-md text-[11px] font-medium border transition-colors ${ |
1375 | | - timelineVisible |
1376 | | - ? "text-studio-accent bg-studio-accent/10 border-studio-accent/30" |
1377 | | - : "text-neutral-300 border-neutral-700 hover:border-neutral-500 hover:bg-neutral-800" |
1378 | | - }`} |
1379 | | - title={getTimelineToggleTitle(timelineVisible)} |
1380 | | - aria-label={timelineVisible ? "Hide timeline editor" : "Show timeline editor"} |
1381 | | - > |
1382 | | - <svg |
1383 | | - width="14" |
1384 | | - height="14" |
1385 | | - viewBox="0 0 24 24" |
1386 | | - fill="none" |
1387 | | - stroke="currentColor" |
1388 | | - strokeWidth="1.5" |
1389 | | - strokeLinecap="round" |
1390 | | - > |
1391 | | - <rect x="3" y="13" width="18" height="8" rx="1" /> |
1392 | | - <line x1="3" y1="9" x2="21" y2="9" /> |
1393 | | - <line x1="3" y1="5" x2="21" y2="5" /> |
1394 | | - </svg> |
1395 | | - <span>Timeline</span> |
1396 | | - </button> |
| 1447 | + <Camera size={14} /> |
| 1448 | + <span>Capture</span> |
| 1449 | + </a> |
1397 | 1450 | <button |
1398 | 1451 | onClick={() => setRightCollapsed((v) => !v)} |
1399 | 1452 | className={`h-7 flex items-center gap-1.5 px-2.5 rounded-md text-[11px] font-medium border transition-colors ${ |
@@ -1422,7 +1475,32 @@ export function StudioApp() { |
1422 | 1475 | {/* Main content: sidebar + preview + right panel */} |
1423 | 1476 | <div className="flex flex-1 min-h-0"> |
1424 | 1477 | {/* Left sidebar: Compositions + Assets (resizable, collapsible) */} |
1425 | | - {!leftCollapsed && ( |
| 1478 | + {leftCollapsed ? ( |
| 1479 | + <div className="flex w-10 flex-shrink-0 flex-col items-center border-r border-neutral-800/50 bg-neutral-950 pt-1"> |
| 1480 | + <button |
| 1481 | + type="button" |
| 1482 | + onClick={toggleLeftSidebar} |
| 1483 | + className="flex h-8 w-8 items-center justify-center rounded-md border border-transparent text-neutral-500 transition-colors hover:border-neutral-800 hover:bg-neutral-900 hover:text-neutral-300" |
| 1484 | + title="Show sidebar" |
| 1485 | + aria-label="Show sidebar" |
| 1486 | + > |
| 1487 | + <svg |
| 1488 | + width="14" |
| 1489 | + height="14" |
| 1490 | + viewBox="0 0 24 24" |
| 1491 | + fill="none" |
| 1492 | + stroke="currentColor" |
| 1493 | + strokeWidth="1.5" |
| 1494 | + strokeLinecap="round" |
| 1495 | + strokeLinejoin="round" |
| 1496 | + aria-hidden="true" |
| 1497 | + > |
| 1498 | + <path d="M5 4v16" /> |
| 1499 | + <path d="m10 7 5 5-5 5" /> |
| 1500 | + </svg> |
| 1501 | + </button> |
| 1502 | + </div> |
| 1503 | + ) : ( |
1426 | 1504 | <LeftSidebar |
1427 | 1505 | width={leftWidth} |
1428 | 1506 | projectId={projectId} |
@@ -1469,6 +1547,7 @@ export function StudioApp() { |
1469 | 1547 | } |
1470 | 1548 | onLint={handleLint} |
1471 | 1549 | linting={linting} |
| 1550 | + onToggleCollapse={toggleLeftSidebar} |
1472 | 1551 | /> |
1473 | 1552 | )} |
1474 | 1553 |
|
|
0 commit comments