Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Edit text on preview double-click

Igor Samokhovetsdc39aeb8ba2f
diff --git a/packages/studio/src/components/InspectorSequenceSection.tsx b/packages/studio/src/components/InspectorSequenceSection.tsx
index f50af61d15..6c028db0ac 100644
--- a/packages/studio/src/components/InspectorSequenceSection.tsx
+++ b/packages/studio/src/components/InspectorSequenceSection.tsx
@@ -42,6 +42,7 @@ import {
getBorderRadiusConversion,
getBorderRadiusConversionChanges,
} from './Timeline/border-radius-representation';
+import {requestFocusInspectorField} from './Timeline/focus-inspector-field';
import {getCurrentFps, getCurrentFrame} from './Timeline/imperative-state';
import {saveSequenceProps} from './Timeline/save-sequence-prop';
import {
@@ -323,8 +324,12 @@ export const InspectorSequenceSection: React.FC<{
includeTextContent: true,
includeSourceControls: true,
});
- const {inspectorRevealRequest, selectedItems, selectItems} =
- useTimelineSelection();
+ const {
+ consumeInspectorRevealRequest,
+ inspectorRevealRequest,
+ selectedItems,
+ selectItems,
+ } = useTimelineSelection();
const selectedEffect =
selectedItems.length === 1 &&
(selectedItems[0].type === 'sequence-effect' ||
@@ -699,6 +704,64 @@ export const InspectorSequenceSection: React.FC<{
sectionExpansionOverrides,
],
);
+ useEffect(() => {
+ if (
+ readOnlyStudio ||
+ !isStudioInteractivityEnabled() ||
+ inspectorRevealRequest?.item.type !== 'sequence-prop' ||
+ inspectorRevealRequest.item.key !== 'children' ||
+ selectedItems.length !== 1 ||
+ getTimelineSelectionKey(selectedItems[0]) !==
+ getTimelineSelectionKey(inspectorRevealRequest.item) ||
+ getTimelineSequenceSelectionKey(
+ inspectorRevealRequest.item.nodePathInfo,
+ ) !== sequenceKey
+ ) {
+ return;
+ }
+
+ const group = controlGroups.find((candidate) =>
+ candidate.rows.some(
+ ({node}) => node.kind === 'field' && node.field?.key === 'children',
+ ),
+ );
+ if (!group) {
+ return;
+ }
+
+ if (!isControlGroupExpanded(group)) {
+ const expansionKey = getInspectorSectionExpansionKey({
+ nodePathInfo,
+ sectionId: group.id,
+ });
+ setSectionExpansionOverrides((previous) => {
+ const next = {...previous, [expansionKey]: true};
+ persistInspectorSectionExpansionOverrides(next);
+ return next;
+ });
+ return;
+ }
+
+ // Ref registration happens during commit, including when expanding Text
+ // mounted the textarea. Consume the request so later remounts do not focus it.
+ if (
+ requestFocusInspectorField({
+ fieldKey: 'children',
+ nodePath: nodePathInfo.sequenceSubscriptionKey,
+ })
+ ) {
+ consumeInspectorRevealRequest(inspectorRevealRequest.token);
+ }
+ }, [
+ consumeInspectorRevealRequest,
+ controlGroups,
+ inspectorRevealRequest,
+ isControlGroupExpanded,
+ nodePathInfo,
+ readOnlyStudio,
+ selectedItems,
+ sequenceKey,
+ ]);
const toggleControlGroup = useCallback(
(group: InspectorControlGroup) => {
const expansionKey = getInspectorSectionExpansionKey({
diff --git a/packages/studio/src/components/SelectedOutlineElement.tsx b/packages/studio/src/components/SelectedOutlineElement.tsx
index a61efbab59..f8438607a8 100644
--- a/packages/studio/src/components/SelectedOutlineElement.tsx
+++ b/packages/studio/src/components/SelectedOutlineElement.tsx
@@ -14,6 +14,7 @@ import {
openOriginalPositionInEditor,
} from '../helpers/open-in-editor';
import {SetSelectedModalContext} from '../state/modals';
+import {SidebarContext} from '../state/sidebar';
import {Transform3DModeStateContext} from '../state/transform-3d-mode';
import {useConfirmationDialog} from './ConfirmationDialog';
import {useSelectComposition} from './InitialCompositionLoader';
@@ -106,6 +107,14 @@ const SelectedOutlineElementUnmemoized: React.FC<
canOpenInEditor: editorAvailable,
});
const {setPropStatuses} = useContext(Internals.VisualModeSettersContext);
+ const propStatusesRef = useContext(
+ Internals.VisualModePropStatusesRefContext,
+ );
+ const {
+ setRightSidebarTemporaryExpansion,
+ setSidebarCollapsedState,
+ sidebarCollapsedStateRight,
+ } = useContext(SidebarContext);
const updateResolvedStackTrace = useContext(
Internals.SequenceStackTracesUpdateContext,
);
@@ -156,6 +165,46 @@ const SelectedOutlineElementUnmemoized: React.FC<
compositions,
singleChildComponent: doubleClickTarget.sequence.singleChildComponent,
});
+ const nodePath = doubleClickTarget.nodePathInfo.sequenceSubscriptionKey;
+ const childrenField = doubleClickTarget.sequence.controls
+ ? Internals.getFlatSchemaWithAllKeys(
+ doubleClickTarget.sequence.controls.schema,
+ ).children
+ : null;
+ const childrenStatus = Internals.getPropStatusesCtx(
+ propStatusesRef.current,
+ nodePath,
+ )?.children;
+ if (
+ button === 0 &&
+ !sequenceWasDragged &&
+ connectedCompositions.length !== 1 &&
+ previewServerState.type === 'connected' &&
+ isStudioInteractivityEnabled() &&
+ currentSelection.current.canSelect &&
+ (childrenField?.type === 'string' ||
+ childrenField?.type === 'text-content') &&
+ childrenStatus?.status === 'static'
+ ) {
+ if (sidebarCollapsedStateRight === 'collapsed') {
+ setSidebarCollapsedState({left: null, right: 'expanded'});
+ } else if (sidebarCollapsedStateRight === 'responsive') {
+ setRightSidebarTemporaryExpansion(true);
+ }
+
+ currentSelection.current.selectItems(
+ [
+ {
+ type: 'sequence-prop',
+ nodePathInfo: doubleClickTarget.nodePathInfo,
+ key: 'children',
+ },
+ ],
+ {revealInInspector: true},
+ );
+ return true;
+ }
+
const action = getSequenceDoubleClickAction({
button,
canOpenSource: defaultOpenInTarget !== null,
@@ -197,10 +246,16 @@ const SelectedOutlineElementUnmemoized: React.FC<
},
[
compositions,
+ currentSelection,
defaultEditorId,
defaultOpenInTarget,
+ previewServerState.type,
+ propStatusesRef,
resolveOriginalLocation,
selectComposition,
+ setRightSidebarTemporaryExpansion,
+ setSidebarCollapsedState,
+ sidebarCollapsedStateRight,
],
);
diff --git a/packages/studio/src/components/SelectedOutlinePolygon.tsx b/packages/studio/src/components/SelectedOutlinePolygon.tsx
index 4decf78703..02c29abbfe 100644
--- a/packages/studio/src/components/SelectedOutlinePolygon.tsx
+++ b/packages/studio/src/components/SelectedOutlinePolygon.tsx
@@ -46,7 +46,10 @@ import type {
TimelineSelection,
TimelineSelectionInteraction,
} from './Timeline/TimelineSelection';
-import {useCurrentTimelineSelectionStateAsRef} from './Timeline/TimelineSelection';
+import {
+ isTimelineSelectionModifierEvent,
+ useCurrentTimelineSelectionStateAsRef,
+} from './Timeline/TimelineSelection';
const {
CanvasOutlinePolygon,
@@ -342,6 +345,10 @@ const SelectedOutlinePolygonUnmemoized: React.FC<{
const performDoubleClick = React.useCallback(
(event: React.MouseEvent<SVGPolygonElement>) => {
+ if (isTimelineSelectionModifierEvent(event)) {
+ return;
+ }
+
const target = getTarget();
if (target === undefined) {
return;
diff --git a/packages/studio/src/components/Timeline/TimelineSelection.tsx b/packages/studio/src/components/Timeline/TimelineSelection.tsx
index 7bbda5f762..80f69ecbc6 100644
--- a/packages/studio/src/components/Timeline/TimelineSelection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSelection.tsx
@@ -465,6 +465,7 @@ type TimelineSelectionContextValue = {
readonly inspectorRevealRequest: TimelineSelectionRevealRequest | null;
readonly revealRequest: TimelineSelectionRevealRequest | null;
readonly consumeRevealRequest: (token: number) => void;
+ readonly consumeInspectorRevealRequest: (token: number) => void;
readonly selectedItems: readonly TimelineSelection[];
readonly isSelected: (item: TimelineSelection) => boolean;
readonly selectItem: (
@@ -494,6 +495,7 @@ const defaultTimelineSelectionContextValue: TimelineSelectionContextValue = {
inspectorRevealRequest: null,
revealRequest: null,
consumeRevealRequest: () => undefined,
+ consumeInspectorRevealRequest: () => undefined,
selectedItems: [],
isSelected: () => false,
selectItem: () => undefined,
@@ -1189,6 +1191,11 @@ export const TimelineSelectionProvider: React.FC<{
const consumeRevealRequest = useCallback((token: number) => {
setRevealRequest((current) => (current?.token === token ? null : current));
}, []);
+ const consumeInspectorRevealRequest = useCallback((token: number) => {
+ setInspectorRevealRequest((current) =>
+ current?.token === token ? null : current,
+ );
+ }, []);
const requestRevealSelectionItemInInspector = useCallback(
(item: TimelineSelection) => {
inspectorRevealTokenCounter.current += 1;
@@ -1461,6 +1468,7 @@ export const TimelineSelectionProvider: React.FC<{
inspectorRevealRequest,
revealRequest,
consumeRevealRequest,
+ consumeInspectorRevealRequest,
selectedItems: availableSelectedItems,
isSelected,
selectItem,
@@ -1476,6 +1484,7 @@ export const TimelineSelectionProvider: React.FC<{
inspectorRevealRequest,
revealRequest,
consumeRevealRequest,
+ consumeInspectorRevealRequest,
availableSelectedItems,
isSelected,
selectItem,
diff --git a/packages/studio/src/components/Timeline/focus-inspector-field.ts b/packages/studio/src/components/Timeline/focus-inspector-field.ts
index 201d91f216..36b0bdcc7c 100644
--- a/packages/studio/src/components/Timeline/focus-inspector-field.ts
+++ b/packages/studio/src/components/Timeline/focus-inspector-field.ts
@@ -47,11 +47,15 @@ export const requestFocusInspectorField = ({
serializeRegistryKey({fieldKey, nodePath}),
);
- requestAnimationFrame(() => {
- element?.scrollIntoView({block: 'center'});
- element?.focus();
- element?.select();
- });
+ if (!element || !element.isConnected) {
+ return false;
+ }
+
+ // Called after the Inspector has mounted the requested field.
+ element.scrollIntoView({block: 'center'});
+ element.focus();
+ element.select();
+ return true;
};
export const registerCommitPendingInspectorField = ({