UNPKG

remotion

Version:

Make videos programmatically

155 lines (154 loc) • 6.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeEffectiveSchemaValuesDotNotation = exports.isKeyframedStatus = exports.getStaticDragOverrideValue = exports.makeKeyframedDragOverride = exports.makeStaticDragOverride = exports.DEFAULT_LINEAR_EASING = void 0; const find_props_to_delete_js_1 = require("./find-props-to-delete.js"); const get_effective_visual_mode_value_js_1 = require("./get-effective-visual-mode-value.js"); const interpolate_keyframed_status_js_1 = require("./interpolate-keyframed-status.js"); exports.DEFAULT_LINEAR_EASING = { type: 'linear', }; const getEasingIndexToDuplicate = ({ insertedKeyframeIndex, easingLength, keyframeCount, }) => { const isSplittingExistingSegment = insertedKeyframeIndex > 0 && insertedKeyframeIndex < keyframeCount - 1; if (!isSplittingExistingSegment || easingLength === 0) { return null; } return Math.min(insertedKeyframeIndex - 1, easingLength - 1); }; const makeStaticDragOverride = (value) => { return { type: 'static', value }; }; exports.makeStaticDragOverride = makeStaticDragOverride; const makeKeyframedDragOverride = ({ status, frame, value, }) => { const existingIndex = status.keyframes.findIndex((keyframe) => keyframe.frame === frame); const keyframes = existingIndex === -1 ? [...status.keyframes, { frame, value }].sort((first, second) => first.frame - second.frame) : status.keyframes.map((keyframe, index) => index === existingIndex ? { frame, value } : keyframe); const easing = [...status.easing]; if (existingIndex === -1) { const insertedKeyframeIndex = keyframes.findIndex((keyframe) => keyframe.frame === frame); const easingIndexToDuplicate = getEasingIndexToDuplicate({ insertedKeyframeIndex, easingLength: easing.length, keyframeCount: keyframes.length, }); const easingToDuplicate = easingIndexToDuplicate === null ? exports.DEFAULT_LINEAR_EASING : easing[easingIndexToDuplicate]; easing.splice(insertedKeyframeIndex, 0, easingToDuplicate); } while (easing.length < keyframes.length - 1) { easing.push(exports.DEFAULT_LINEAR_EASING); } if (easing.length > keyframes.length - 1) { easing.length = keyframes.length - 1; } return { type: 'keyframed', status: { ...status, keyframes, easing, }, }; }; exports.makeKeyframedDragOverride = makeKeyframedDragOverride; const getStaticDragOverrideValue = (dragOverrideValue) => { if ((dragOverrideValue === null || dragOverrideValue === void 0 ? void 0 : dragOverrideValue.type) !== 'static') { return undefined; } return dragOverrideValue.value; }; exports.getStaticDragOverrideValue = getStaticDragOverrideValue; const isKeyframedStatus = (status) => { return status !== null && status.status === 'keyframed'; }; exports.isKeyframedStatus = isKeyframedStatus; const findFieldInSchema = (schema, key) => { if (key in schema) { return schema[key]; } for (const field of Object.values(schema)) { if (field.type !== 'enum') { continue; } for (const variant of Object.values(field.variants)) { const found = findFieldInSchema(variant, key); if (found) { return found; } } } return undefined; }; const computeEffectiveSchemaValuesDotNotation = ({ schema, currentValue, overrideValues, propStatus, frame, }) => { var _a; var _b; const merged = {}; const propsToDelete = new Set(); for (const key of Object.keys(currentValue)) { const status = (_b = propStatus === null || propStatus === void 0 ? void 0 : propStatus[key]) !== null && _b !== void 0 ? _b : null; const field = findFieldInSchema(schema, key); if ((field === null || field === void 0 ? void 0 : field.type) === 'hidden') { continue; } let value; if (status === null) { value = currentValue[key]; } else if ((0, exports.isKeyframedStatus)(status)) { if ((field === null || field === void 0 ? void 0 : field.type) === 'array' || (field === null || field === void 0 ? void 0 : field.keyframable) === false) { value = currentValue[key]; } else { const dragOverride = (0, get_effective_visual_mode_value_js_1.resolveDragOverrideValue)({ dragOverrideValue: overrideValues[key], frame, }); if (dragOverride.type === 'resolved') { value = dragOverride.value; } else if (frame !== null) { const interpolated = (0, interpolate_keyframed_status_js_1.interpolateKeyframedStatus)({ forceSpringAllowTail: null, frame, status, }); value = interpolated !== null && interpolated !== void 0 ? interpolated : currentValue[key]; } else { value = currentValue[key]; } } } else if (status.status === 'computed') { value = currentValue[key]; } else { value = (0, get_effective_visual_mode_value_js_1.getEffectiveVisualModeValue)({ propStatus: status, dragOverrideValue: overrideValues[key], defaultValue: field === null || field === void 0 ? void 0 : field.default, frame, shouldResortToDefaultValueIfUndefined: false, }); } if (value === undefined) { propsToDelete.add(key); } merged[key] = value; } for (const key of Object.keys(overrideValues)) { if (((_a = schema[key]) === null || _a === void 0 ? void 0 : _a.type) === 'enum') { const propsToDeleteForKey = (0, find_props_to_delete_js_1.findPropsToDelete)({ schema, key, value: merged[key], }); for (const propToDelete of propsToDeleteForKey) { propsToDelete.add(propToDelete); } } } return { merged, propsToDelete }; }; exports.computeEffectiveSchemaValuesDotNotation = computeEffectiveSchemaValuesDotNotation;