Skip to content

🎥 Make videos programmatically with React

Fix Studio selection after wrapping HTML in canvas

Jonny Burgerfc64331fb35e
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.ts
index d75d69d400..2afd8f4bc7 100644
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@ -1429,6 +1429,7 @@ export const createBrowserStudioOperations = ({
const wrapNode: BrowserStudioOperations['wrapNode'] = ({
fileName,
+ compositionId,
nodePath,
wrapper,
width,
@@ -1469,9 +1470,19 @@ export const createBrowserStudioOperations = ({
: {},
}),
});
+ const insertedNodePath = result.nodePathRemappings.find(
+ (remapping) => remapping.oldNodePath === null,
+ )?.newNodePath;
const nodePathMutation = controller.applyMutation({
undoRedoNavigation: null,
- timelineSelection: null,
+ timelineSelection:
+ compositionId && insertedNodePath
+ ? {
+ compositionId,
+ absolutePath: filePath,
+ nodePath: insertedNodePath,
+ }
+ : null,
fileName,
mutate: (current) => applyCodemodChanges(current, result.changes),
nodePathMutationFiles: getNodePathMutationFiles(result),
diff --git a/packages/core/src/with-interactivity-schema.ts b/packages/core/src/with-interactivity-schema.ts
index 03aa599aa4..9951f946a1 100644
--- a/packages/core/src/with-interactivity-schema.ts
+++ b/packages/core/src/with-interactivity-schema.ts
@@ -155,8 +155,6 @@ export const mergeValues = ({
return merged;
};
-const stackToOverrideMap: Record<string, string> = {};
-
export type WithInteractivitySchemaOptions<
S extends InteractivitySchema,
Props extends object,
@@ -211,6 +209,9 @@ export const withInteractivitySchema = <
const schemaWithSequenceName = extendSchemaWithSequenceName(schema);
const flatSchema = getFlatSchemaWithAllKeys(schemaWithSequenceName);
const flatKeys = Object.keys(flatSchema);
+ // A structural edit can put a different component at the same stack.
+ // Only reuse overrides for instances of this component.
+ const stackToOverrideMap: Record<string, string> = {};
const Wrapped = forwardRef<unknown, Props>((props, ref) => {
const {
diff --git a/packages/studio-server/src/preview-server/routes/wrap-node.ts b/packages/studio-server/src/preview-server/routes/wrap-node.ts
index 6bb8b36004..66f382f582 100644
--- a/packages/studio-server/src/preview-server/routes/wrap-node.ts
+++ b/packages/studio-server/src/preview-server/routes/wrap-node.ts
@@ -18,7 +18,7 @@ import {
} from './source-file-write-queue';
export const wrapNodeHandler: ApiHandler<WrapNodeRequest, WrapNodeResponse> = ({
- input: {fileName, nodePath, wrapper, width, height, timing},
+ input: {fileName, compositionId, nodePath, wrapper, width, height, timing},
remotionRoot,
logLevel,
}) => {
@@ -78,11 +78,21 @@ export const wrapNodeHandler: ApiHandler<WrapNodeRequest, WrapNodeResponse> = ({
const output = change.nextContents;
const remappings = result.nodePathRemappings.map(
- ({oldNodePath, newNodePath}) => ({oldNodePath, newNodePath}),
+ ({oldNodePath, newNodePath, oldJsxName, newJsxName}) => ({
+ oldNodePath,
+ newNodePath,
+ oldJsxName,
+ newJsxName,
+ }),
);
+ const insertedNodePath = remappings.find(
+ (remapping) => remapping.oldNodePath === null,
+ )?.newNodePath;
const nodePathMutation = broadcastSequenceNodePathMutation(
[{absolutePath, remappings}],
- null,
+ compositionId && insertedNodePath
+ ? {compositionId, absolutePath, nodePath: insertedNodePath}
+ : null,
);
const line = result.logLine;
pushTransactionToUndoStack({
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 13c6853152..506f73a85c 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -963,6 +963,7 @@ export type NodeWrapper =
export type WrapNodeRequest = {
fileName: string;
+ compositionId?: string | null;
nodePath: SequenceNodePath;
wrapper: NodeWrapper | null;
width: number | null;
diff --git a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
index 5ba1038af2..d563657f1c 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceWrapAction.tsx
@@ -41,6 +41,9 @@ export const SequenceWrapAction: React.FC<{
};
}> = ({nodePathInfo, track, sourceActionsDisabled, sourceLocation}) => {
const {setSelectedModal} = useContext(SetSelectedModalContext);
+ const {canvasContent} = useContext(Internals.CompositionManager);
+ const compositionId =
+ canvasContent?.type === 'composition' ? canvasContent.compositionId : null;
const sequencesRef = useContext(Internals.SequenceManagerRefContext);
const overrideIdToNodePathMappingsRef = useContext(
OverrideIdToNodePathMappingsRefContext,
@@ -130,6 +133,7 @@ export const SequenceWrapAction: React.FC<{
try {
const eligibility = await wrapNode({
fileName: nodePath.absolutePath,
+ compositionId,
nodePath: nodePath.nodePath,
wrapper: null,
width: null,
@@ -165,6 +169,7 @@ export const SequenceWrapAction: React.FC<{
const result = await wrapNode({
fileName: nodePath.absolutePath,
+ compositionId,
nodePath: nodePath.nodePath,
wrapper,
width,
@@ -182,6 +187,7 @@ export const SequenceWrapAction: React.FC<{
},
[
busy,
+ compositionId,
nodePathInfo.sequenceSubscriptionKey,
overrideIdToNodePathMappingsRef,
sequence.controls?.componentName,
diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsx
index fbfbe638ef..d9f2d8d9ca 100644
--- a/packages/studio/src/components/Timeline/Timeline.tsx
+++ b/packages/studio/src/components/Timeline/Timeline.tsx
@@ -18,6 +18,7 @@ import {
clearInsertedElementSelection,
getInsertedElementSelection,
subscribeToInsertedElementSelection,
+ type PendingInsertedElementSelection,
} from '../../helpers/inserted-element-selection';
import {isStudioInteractivityEnabled} from '../../helpers/interactivity-enabled';
import {useIsStill} from '../../helpers/is-current-selected-still';
@@ -431,17 +432,39 @@ const TimelineInner: React.FC = () => {
sequences,
canvasContent?.type === 'composition' ? canvasContent.compositionId : null,
);
- const pendingInsertedElementSelection = useSyncExternalStore(
- subscribeToInsertedElementSelection,
- getInsertedElementSelection,
- getInsertedElementSelection,
- );
const {fastRefreshes} = useContext(FastRefreshContext);
const pendingSelectionStart = useRef<{
- selection: NonNullable<typeof pendingInsertedElementSelection>;
+ selection: PendingInsertedElementSelection;
fastRefreshes: number;
existingSequenceIds: Set<string>;
} | null>(null);
+ const selectionBaselineRef = useRef({fastRefreshes, timeline});
+ selectionBaselineRef.current = {fastRefreshes, timeline};
+ const subscribeToPendingSelection = useCallback((listener: () => void) => {
+ return subscribeToInsertedElementSelection(() => {
+ const selection = getInsertedElementSelection();
+ if (selection !== null) {
+ // Capture before React handles the notification. Fast Refresh may have
+ // already committed the inserted sequence by the next layout effect.
+ pendingSelectionStart.current = {
+ selection,
+ fastRefreshes: selectionBaselineRef.current.fastRefreshes,
+ existingSequenceIds: new Set(
+ selectionBaselineRef.current.timeline.map(
+ (track) => track.sequence.id,
+ ),
+ ),
+ };
+ }
+
+ listener();
+ });
+ }, []);
+ const pendingInsertedElementSelection = useSyncExternalStore(
+ subscribeToPendingSelection,
+ getInsertedElementSelection,
+ getInsertedElementSelection,
+ );
const currentSelection = useCurrentTimelineSelectionStateAsRef();
useLayoutEffect(() => {
if (pendingInsertedElementSelection === null) {
@@ -465,9 +488,7 @@ const TimelineInner: React.FC = () => {
selection: pendingInsertedElementSelection,
fastRefreshes,
existingSequenceIds: new Set(
- timeline
- .filter(matchesInsertedNodePath)
- .map((track) => track.sequence.id),
+ timeline.map((track) => track.sequence.id),
),
};
return;