Skip to content

🎥 Make videos programmatically with React

Add eye toggle to effect timeline rows to persist `disabled` to source

JonnyBurgere7f5d5478de6
diff --git a/packages/core/src/effects/create-effect.ts b/packages/core/src/effects/create-effect.ts
index 40c23c46f5..2e30b480ad 100644
--- a/packages/core/src/effects/create-effect.ts
+++ b/packages/core/src/effects/create-effect.ts
@@ -1,9 +1,20 @@
+import type {SequenceFieldSchema} from '../sequence-field-schema.js';
import type {
EffectDefinition,
EffectDescriptor,
EffectFactory,
} from './effect-types.js';
+// Framework-level field that every effect inherits. Mirrors `hidden` for
+// sequences: rendered as the eye toggle on the timeline effect row and saved
+// to source via `/api/save-effect-props`. `getEffectFieldsToShow` filters it
+// out of the regular field list so the toggle is the only control.
+export const disabledEffectField: SequenceFieldSchema = {
+ type: 'boolean',
+ default: false,
+ description: 'Disabled',
+};
+
// Defines an effect and returns a factory that produces per-frame descriptors.
// The returned function is the public API effect authors expose to users:
//
@@ -27,6 +38,10 @@ export const createEffect = <P, S>(
const disabled = (params as {disabled?: boolean})?.disabled ?? false;
return `${userCalculateKey(params as P)}-disabled-${disabled}`;
},
+ schema: {
+ disabled: disabledEffectField,
+ ...(definition.schema ?? {}),
+ },
};
const factory = (
params: P & {readonly disabled?: boolean} = {} as P & {
diff --git a/packages/core/src/test/create-effect-disabled.test.ts b/packages/core/src/test/create-effect-disabled.test.ts
index 0b0426a8de..d00abab396 100644
--- a/packages/core/src/test/create-effect-disabled.test.ts
+++ b/packages/core/src/test/create-effect-disabled.test.ts
@@ -40,3 +40,39 @@ test('definition.calculateKey reflects `disabled` for override merge path', () =
const disabledKey = desc.definition.calculateKey({amount: 1, disabled: true});
expect(enabledKey).not.toBe(disabledKey);
});
+
+test('createEffect injects `disabled` into the schema for save-effect-props', () => {
+ const foo = createEffect<FooParams, null>({
+ type: 'test/foo',
+ label: 'Foo',
+ backend: '2d',
+ calculateKey: (p) => `foo-${p.amount}`,
+ setup: () => null,
+ apply: () => undefined,
+ cleanup: () => undefined,
+ schema: {
+ amount: {type: 'number', default: 0, description: 'Amount'},
+ },
+ });
+ const desc = foo({amount: 1});
+ expect(desc.definition.schema?.disabled).toEqual({
+ type: 'boolean',
+ default: false,
+ description: 'Disabled',
+ });
+ expect(desc.definition.schema?.amount).toEqual({
+ type: 'number',
+ default: 0,
+ description: 'Amount',
+ });
+});
+
+test('createEffect injects `disabled` even when the effect declares no schema', () => {
+ const foo = makeFoo();
+ const desc = foo({amount: 1});
+ expect(desc.definition.schema?.disabled).toEqual({
+ type: 'boolean',
+ default: false,
+ description: 'Disabled',
+ });
+});
diff --git a/packages/studio-shared/src/schema-field-info.ts b/packages/studio-shared/src/schema-field-info.ts
index 0c8327e7bd..23f0da4193 100644
--- a/packages/studio-shared/src/schema-field-info.ts
+++ b/packages/studio-shared/src/schema-field-info.ts
@@ -120,6 +120,12 @@ export const getEffectFieldsToShow = (
return null;
}
+ // `disabled` is represented as the eye icon on the effect timeline row,
+ // so we don't render it as a regular field in the expanded section.
+ if (key === 'disabled') {
+ return null;
+ }
+
if (SUPPORTED_SCHEMA_TYPES.indexOf(typeName) === -1) {
throw new Error(`Unsupported field type: ${typeName}`);
}
diff --git a/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx b/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx
new file mode 100644
index 0000000000..eda28bbcee
--- /dev/null
+++ b/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx
@@ -0,0 +1,131 @@
+import React, {useCallback, useContext, useMemo} from 'react';
+import type {SequencePropsSubscriptionKey, SequenceSchema} from 'remotion';
+import {Internals} from 'remotion';
+import type {CodePosition} from '../../error-overlay/react-overlay/utils/get-source-map';
+import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
+import type {GetIsExpanded} from '../ExpandedTracksProvider';
+import {Padder} from './Padder';
+import {saveEffectProp} from './save-effect-prop';
+import {TimelineExpandArrowButton} from './TimelineExpandArrowButton';
+import {TimelineLayerEye, TimelineLayerEyeSpacer} from './TimelineLayerEye';
+
+const groupRowBase: React.CSSProperties = {
+ display: 'flex',
+ alignItems: 'center',
+};
+
+const rowLabel: React.CSSProperties = {
+ fontSize: 12,
+ color: 'rgba(255, 255, 255, 0.8)',
+ userSelect: 'none',
+};
+
+export const TimelineEffectGroupRow: React.FC<{
+ readonly label: string;
+ readonly nodePathInfo: SequenceNodePathInfo;
+ readonly effectIndex: number;
+ readonly effectSchema: SequenceSchema;
+ readonly nodePath: SequencePropsSubscriptionKey;
+ readonly validatedLocation: CodePosition;
+ readonly nestedDepth: number;
+ readonly style: React.CSSProperties;
+ readonly getIsExpanded: GetIsExpanded;
+ readonly toggleTrack: (nodePathInfo: SequenceNodePathInfo) => void;
+}> = ({
+ label,
+ nodePathInfo,
+ effectIndex,
+ effectSchema,
+ nodePath,
+ validatedLocation,
+ nestedDepth,
+ style,
+ getIsExpanded,
+ toggleTrack,
+}) => {
+ const {codeValues} = useContext(Internals.VisualModeCodeValuesContext);
+ const {setCodeValues} = useContext(Internals.VisualModeSettersContext);
+
+ const effectStatus = useMemo(
+ () =>
+ Internals.getEffectCodeValuesCtx({
+ codeValues,
+ nodePath,
+ effectIndex,
+ }),
+ [codeValues, nodePath, effectIndex],
+ );
+
+ const disabledStatus =
+ effectStatus.type === 'can-update-effect'
+ ? (effectStatus.props?.disabled ?? null)
+ : null;
+
+ const isDisabled = useMemo(() => {
+ if (disabledStatus && disabledStatus.canUpdate) {
+ return Boolean(disabledStatus.codeValue);
+ }
+
+ return false;
+ }, [disabledStatus]);
+
+ const canToggle = disabledStatus !== null && disabledStatus.canUpdate;
+
+ const onToggle = useCallback(
+ (type: 'enable' | 'disable') => {
+ if (!canToggle) {
+ return;
+ }
+
+ const newValue = type !== 'enable';
+ const fieldSchema = effectSchema.disabled;
+ const defaultValue =
+ fieldSchema && fieldSchema.type === 'boolean'
+ ? JSON.stringify(fieldSchema.default)
+ : null;
+
+ saveEffectProp({
+ fileName: validatedLocation.source,
+ nodePath,
+ effectIndex,
+ fieldKey: 'disabled',
+ value: newValue,
+ defaultValue,
+ schema: effectSchema,
+ setCodeValues,
+ });
+ },
+ [
+ canToggle,
+ effectIndex,
+ effectSchema,
+ nodePath,
+ setCodeValues,
+ validatedLocation.source,
+ ],
+ );
+
+ const isExpanded = getIsExpanded(nodePathInfo);
+ const mergedStyle = useMemo(
+ (): React.CSSProperties => ({...groupRowBase, ...style}),
+ [style],
+ );
+
+ return (
+ <div style={mergedStyle}>
+ <Padder depth={nestedDepth + 1} />
+ {canToggle ? (
+ <TimelineLayerEye type="eye" hidden={isDisabled} onInvoked={onToggle} />
+ ) : (
+ <TimelineLayerEyeSpacer />
+ )}
+ <TimelineExpandArrowButton
+ isExpanded={isExpanded}
+ onClick={() => toggleTrack(nodePathInfo)}
+ label={`${label} section`}
+ disabled={false}
+ />
+ <span style={rowLabel}>{label}</span>
+ </div>
+ );
+};
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
index cb3138ef8f..61a8cd507d 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedRow.tsx
@@ -12,6 +12,7 @@ import {
import type {GetIsExpanded} from '../ExpandedTracksProvider';
import {Padder} from './Padder';
import {TimelineEffectFieldRow} from './TimelineEffectFieldRow';
+import {TimelineEffectGroupRow} from './TimelineEffectGroupRow';
import {TimelineExpandArrowButton} from './TimelineExpandArrowButton';
import {TimelineFieldRow} from './TimelineFieldRow';
import {INDENT} from './TimelineListItem';
@@ -71,6 +72,23 @@ export const TimelineExpandedRow: React.FC<{
);
if (node.kind === 'group') {
+ if (node.effectInfo) {
+ return (
+ <TimelineEffectGroupRow
+ label={node.label}
+ nodePathInfo={node.nodePathInfo}
+ effectIndex={node.effectInfo.effectIndex}
+ effectSchema={node.effectInfo.effectSchema}
+ nodePath={nodePath}
+ validatedLocation={validatedLocation}
+ nestedDepth={nestedDepth}
+ style={groupStyle}
+ getIsExpanded={getIsExpanded}
+ toggleTrack={toggleTrack}
+ />
+ );
+ }
+
const isExpanded = getIsExpanded(node.nodePathInfo);
return (
<div style={groupStyle}>
diff --git a/packages/studio/src/components/Timeline/save-effect-prop.ts b/packages/studio/src/components/Timeline/save-effect-prop.ts
new file mode 100644
index 0000000000..06ed865ae2
--- /dev/null
+++ b/packages/studio/src/components/Timeline/save-effect-prop.ts
@@ -0,0 +1,63 @@
+import {optimisticUpdateForEffectCodeValues} from '@remotion/studio-shared';
+import type {SequencePropsSubscriptionKey, SequenceSchema} from 'remotion';
+import {callApi} from '../call-api';
+import {enqueueSavePropChange} from './save-prop-queue';
+import type {SetCodeValues} from './save-sequence-prop';
+
+export const saveEffectProp = ({
+ fileName,
+ nodePath,
+ effectIndex,
+ fieldKey,
+ value,
+ defaultValue,
+ schema,
+ setCodeValues,
+}: {
+ fileName: string;
+ nodePath: SequencePropsSubscriptionKey;
+ effectIndex: number;
+ fieldKey: string;
+ value: unknown;
+ defaultValue: string | null;
+ schema: SequenceSchema;
+ setCodeValues: SetCodeValues;
+}): Promise<void> => {
+ return enqueueSavePropChange({
+ nodePath,
+ setCodeValues,
+ applyOptimistic: (prev) =>
+ optimisticUpdateForEffectCodeValues({
+ previous: prev,
+ effectIndex,
+ fieldKey,
+ value,
+ schema,
+ }),
+ apiCall: () =>
+ callApi('/api/save-effect-props', {
+ fileName,
+ sequenceNodePath: nodePath,
+ effectIndex,
+ key: fieldKey,
+ value: JSON.stringify(value),
+ defaultValue,
+ schema,
+ }),
+ mergeServerResponse: (prev, data) => {
+ if (!prev.canUpdate) {
+ return prev;
+ }
+
+ const idx = prev.effects.findIndex((e) => e.effectIndex === effectIndex);
+ if (idx === -1) {
+ return {...prev, effects: [...prev.effects, data]};
+ }
+
+ const nextEffects = [...prev.effects];
+ nextEffects[idx] = data;
+ return {...prev, effects: nextEffects};
+ },
+ errorLabel: 'Could not save effect prop',
+ });
+};
diff --git a/packages/studio/src/components/Timeline/save-sequence-prop.ts b/packages/studio/src/components/Timeline/save-sequence-prop.ts
index f16019480c..6902818c26 100644
--- a/packages/studio/src/components/Timeline/save-sequence-prop.ts
+++ b/packages/studio/src/components/Timeline/save-sequence-prop.ts
@@ -7,7 +7,7 @@ import type {
import {callApi} from '../call-api';
import {enqueueSavePropChange} from './save-prop-queue';
-type SetCodeValues = (
+export type SetCodeValues = (
nodePath: SequencePropsSubscriptionKey,
values: (
prev: CanUpdateSequencePropsResponse,
diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.ts
index 7f9e6ad68f..5313e21291 100644
--- a/packages/studio/src/helpers/timeline-layout.ts
+++ b/packages/studio/src/helpers/timeline-layout.ts
@@ -9,7 +9,11 @@ import {
type SequenceControls,
type SequenceSchemaFieldInfo,
} from '@remotion/studio-shared';
-import type {GetDragOverrides, TSequence} from 'remotion';
+import type {
+ GetDragOverrides,
+ SequenceSchema as SequenceSchemaShape,
+ TSequence,
+} from 'remotion';
import type {GetIsExpanded} from '../components/ExpandedTracksProvider';
import type {SequenceNodePathInfo} from './get-timeline-sequence-sort-key';
@@ -41,12 +45,21 @@ export const EXPANDED_SECTION_PADDING_RIGHT = 10;
export type TimelineFieldOnSave = (value: unknown) => Promise<void>;
export type TimelineFieldOnDragValueChange = (value: unknown) => void;
+export type TimelineEffectGroupInfo = {
+ readonly effectIndex: number;
+ readonly effectSchema: SequenceSchemaShape;
+};
+
export type TimelineTreeNode =
| {
readonly kind: 'group';
readonly nodePathInfo: SequenceNodePathInfo;
readonly label: string;
readonly children: TimelineTreeNode[];
+ // Present when this group represents a single effect (not the outer
+ // "Effects" container). Lets the row component render the eye toggle and
+ // wire `disabled` saves without re-deriving the effect index.
+ readonly effectInfo: TimelineEffectGroupInfo | null;
}
| {
readonly kind: 'field';
@@ -79,6 +92,7 @@ export const buildTimelineTree = ({
numberOfSequencesWithThisNodePath: 0,
},
label: 'Effects',
+ effectInfo: null,
children: sequence.effects.map((effect, i): TimelineTreeNode => {
const effectFields = getEffectFieldsToShow(effect, i);
return {
@@ -90,6 +104,9 @@ export const buildTimelineTree = ({
numberOfSequencesWithThisNodePath: 0,
},
label: effect.label,
+ effectInfo: effect.schema
+ ? {effectIndex: i, effectSchema: effect.schema}
+ : null,
children: effectFields.map(
(f): TimelineTreeNode => ({
kind: 'field',