"use client"; import { ArrowDownIcon, ArrowUpIcon, BoxIcon, CodeIcon, CopyIcon, ImageIcon, LayersIcon, Music2Icon, PlusIcon, ScissorsIcon, SquareIcon, Trash2Icon, VideoIcon, } from "lucide-react"; import React from "react"; import { Button } from "@/components/ui/button"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuShortcut, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger, } from "@/components/ui/context-menu"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { getNodeReference, type Layer } from "../../model/layers"; import { getLayerSchema, hasTimingProps } from "../../model/schemas"; import { useEditor } from "../../state/editor-context"; import { ToolbarButton } from "../TopBar"; export const useLayerCommands = (layer: Layer) => { const { actions } = useEditor(); const node = getNodeReference(layer.selectionItem); const schema = getLayerSchema(layer); const source = layer.source; return { node, canSplit: node !== null && hasTimingProps(schema), reveal: () => { if (source) { actions.reveal( source.filePath, source.location?.line ?? null, source.location?.column ?? null, ); } }, remove: () => node && void actions.deleteNodes([node]), duplicate: () => node && void actions.duplicateNodes([node]), split: () => node && void actions.splitLayersAtPlayhead([layer]), wrapInSequence: () => node && void actions.wrapNode(node, "Sequence"), wrapInAbsoluteFill: () => node && void actions.wrapNode(node, "AbsoluteFill"), moveUp: () => node && void actions.moveSelectedNodesOneStep(node, "up"), moveDown: () => node && void actions.moveSelectedNodesOneStep(node, "down"), }; }; export const LayerContextMenu: React.FC<{ readonly layer: Layer; readonly children: React.ReactNode; }> = ({ layer, children }) => { const commands = useLayerCommands(layer); const { actions } = useEditor(); const hasSource = layer.source !== null; return ( {children} event.preventDefault()} className="min-w-[200px]" > actions.selectLayer(layer, { shiftKey: false, toggleKey: false }) } > Select Reveal in code Duplicate ⌘D Split at playhead ⌘⇧D Wrap in <Sequence> <AbsoluteFill> Move up ⌥↑ Move down ⌥↓ Delete ⌫ ); }; export const LayerActionBar: React.FC<{ readonly layer: Layer }> = ({ layer, }) => { const commands = useLayerCommands(layer); const hasSource = layer.source !== null; return (
Wrap in <Sequence> <AbsoluteFill>
); }; const promptForUrl = (label: string) => { const url = window.prompt(`${label} URL`, "https://"); return url && url !== "https://" ? url.trim() : null; }; export const AddLayerMenu: React.FC<{ readonly size?: "sm" | "xs" }> = ({ size = "xs", }) => { const { actions, activeComposition, mainFile, state } = useEditor(); const components = Object.keys(state.files) .filter((filePath) => /\.tsx$/.test(filePath) && filePath !== mainFile) .flatMap((filePath) => { const matches = [ ...state.files[filePath].matchAll( /export\s+(?:const|function)\s+([A-Z]\w*)/g, ), ]; return matches.map((match) => ({ filePath, name: match[1] })); }) .filter(({ name }) => name !== "RemotionRoot"); return ( Insert at the playhead void actions.addSolid()}> Solid color { const url = promptForUrl("Image"); if (url) void actions.addMedia("image", url); }} > Image from URL… { const url = promptForUrl("Video"); if (url) void actions.addMedia("video", url); }} > Video from URL… { const url = promptForUrl("Audio"); if (url) void actions.addMedia("audio", url); }} > Audio from URL… {components.length > 0 ? ( <> Components in this project {components.map(({ filePath, name }) => ( { // Import path relative to the file the component is // inserted into (the composition's component file). const from = (mainFile ?? activeComposition!.filePath) .split("/") .slice(0, -1); const target = filePath.replace(/\.tsx$/, "").split("/"); const common = from.findIndex( (segment, index) => segment !== target[index], ); const up = common === -1 ? 0 : from.length - common; const rest = target.slice( common === -1 ? from.length : common, ); const importPath = `${up === 0 ? "./" : "../".repeat(up)}${rest.join("/")}`; void actions.addComponent(name, importPath); }} > {name} {filePath.split("/").at(-1)} ))} ) : null} ); };