UNPKG

remotion

Version:

Make videos programmatically

105 lines (104 loc) 5.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemotionRoot = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const CompositionManager_js_1 = require("./CompositionManager.js"); const EditorProps_js_1 = require("./EditorProps.js"); const buffering_js_1 = require("./buffering.js"); const delay_render_js_1 = require("./delay-render.js"); const log_level_context_js_1 = require("./log-level-context.js"); const nonce_js_1 = require("./nonce.js"); const prefetch_state_js_1 = require("./prefetch-state.js"); const random_js_1 = require("./random.js"); const timeline_position_state_js_1 = require("./timeline-position-state.js"); const duration_state_js_1 = require("./video/duration-state.js"); const RemotionRoot = ({ children, numberOfAudioTags, logLevel, onlyRenderComposition, currentCompositionMetadata, audioLatencyHint, }) => { const [remotionRootId] = (0, react_1.useState)(() => String((0, random_js_1.random)(null))); const [frame, setFrame] = (0, react_1.useState)(() => (0, timeline_position_state_js_1.getInitialFrameState)()); const [playing, setPlaying] = (0, react_1.useState)(false); const imperativePlaying = (0, react_1.useRef)(false); const [fastRefreshes, setFastRefreshes] = (0, react_1.useState)(0); const [manualRefreshes, setManualRefreshes] = (0, react_1.useState)(0); const [playbackRate, setPlaybackRate] = (0, react_1.useState)(1); const audioAndVideoTags = (0, react_1.useRef)([]); if (typeof window !== 'undefined') { // eslint-disable-next-line react-hooks/rules-of-hooks (0, react_1.useLayoutEffect)(() => { window.remotion_setFrame = (f, composition, attempt) => { window.remotion_attempt = attempt; const id = (0, delay_render_js_1.delayRender)(`Setting the current frame to ${f}`); let asyncUpdate = true; setFrame((s) => { var _a; const currentFrame = (_a = s[composition]) !== null && _a !== void 0 ? _a : window.remotion_initialFrame; // Avoid cloning the object if (currentFrame === f) { asyncUpdate = false; return s; } return { ...s, [composition]: f, }; }); // After setting the state, need to wait until it is applied in the next cycle if (asyncUpdate) { requestAnimationFrame(() => (0, delay_render_js_1.continueRender)(id)); } else { (0, delay_render_js_1.continueRender)(id); } }; window.remotion_isPlayer = false; }, []); } const timelineContextValue = (0, react_1.useMemo)(() => { return { frame, playing, imperativePlaying, rootId: remotionRootId, playbackRate, setPlaybackRate, audioAndVideoTags, }; }, [frame, playbackRate, playing, remotionRootId]); const setTimelineContextValue = (0, react_1.useMemo)(() => { return { setFrame, setPlaying, }; }, []); const nonceContext = (0, react_1.useMemo)(() => { let counter = 0; return { getNonce: () => counter++, fastRefreshes, manualRefreshes, }; }, [fastRefreshes, manualRefreshes]); const setNonceContext = (0, react_1.useMemo)(() => { return { increaseManualRefreshes: () => { setManualRefreshes((i) => i + 1); }, }; }, []); (0, react_1.useEffect)(() => { if (typeof __webpack_module__ !== 'undefined') { if (__webpack_module__.hot) { __webpack_module__.hot.addStatusHandler((status) => { if (status === 'idle') { setFastRefreshes((i) => i + 1); } }); } } }, []); const logging = (0, react_1.useMemo)(() => { return { logLevel, mountTime: Date.now() }; }, [logLevel]); return ((0, jsx_runtime_1.jsx)(log_level_context_js_1.LogLevelContext.Provider, { value: logging, children: (0, jsx_runtime_1.jsx)(nonce_js_1.NonceContext.Provider, { value: nonceContext, children: (0, jsx_runtime_1.jsx)(nonce_js_1.SetNonceContext.Provider, { value: setNonceContext, children: (0, jsx_runtime_1.jsx)(timeline_position_state_js_1.TimelineContext.Provider, { value: timelineContextValue, children: (0, jsx_runtime_1.jsx)(timeline_position_state_js_1.SetTimelineContext.Provider, { value: setTimelineContextValue, children: (0, jsx_runtime_1.jsx)(EditorProps_js_1.EditorPropsProvider, { children: (0, jsx_runtime_1.jsx)(prefetch_state_js_1.PrefetchProvider, { children: (0, jsx_runtime_1.jsx)(CompositionManager_js_1.CompositionManagerProvider, { numberOfAudioTags: numberOfAudioTags, onlyRenderComposition: onlyRenderComposition, currentCompositionMetadata: currentCompositionMetadata, audioLatencyHint: audioLatencyHint, children: (0, jsx_runtime_1.jsx)(duration_state_js_1.DurationsContextProvider, { children: (0, jsx_runtime_1.jsx)(buffering_js_1.BufferingProvider, { children: children }) }) }) }) }) }) }) }) }) })); }; exports.RemotionRoot = RemotionRoot;