Skip to content

🎥 Make videos programmatically with React

Elements: Fix Studio installation without a selected composition

JonnyBurger6c1d4215c44f
diff --git a/packages/docs/src/components/Elements/ElementLibrary.module.css b/packages/docs/src/components/Elements/ElementLibrary.module.css
index b8b06d1f54..01f6bf8077 100644
--- a/packages/docs/src/components/Elements/ElementLibrary.module.css
+++ b/packages/docs/src/components/Elements/ElementLibrary.module.css
@@ -37,9 +37,6 @@
line-height: 1.25;
opacity: 0;
pointer-events: none;
- transition:
- transform 200ms cubic-bezier(0.2, 0.8, 0.2, 1),
- opacity 200ms ease;
}
.cardItem:hover .installAction,
@@ -49,17 +46,6 @@
pointer-events: auto;
}
-.installAction button:focus-visible {
- outline: 2px solid var(--ifm-color-primary);
- outline-offset: 2px;
-}
-
-@media (prefers-reduced-motion: reduce) {
- .installAction {
- transition: none;
- }
-}
-
.library .card {
display: flex;
height: 100%;
diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsx
index de6a276fb5..a24acb5dde 100644
--- a/packages/docs/src/components/Elements/ElementLibrary.tsx
+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx
@@ -1,6 +1,15 @@
-import {installInStudio, setStudioDragData} from '@remotion/studio-protocol';
-import React, {useEffect, useMemo, useRef, useState} from 'react';
-import {BlueButton} from '../../../components/layout/Button';
+import {
+ installInStudio,
+ isInsideStudio,
+ setStudioDragData,
+} from '@remotion/studio-protocol';
+import React, {
+ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import type {ElementDefinition} from './element-definitions';
import {
createElementPayloadFromDefinition,
@@ -12,6 +21,7 @@ import {
type ElementCategory,
} from './element-library-data';
import {ELEMENT_PREVIEW_BACKGROUND} from './ElementPreviewComposition';
+import {InlineStudioButton} from './InlineStudioButton';
import styles from './ElementLibrary.module.css';
const reducedMotionQuery = '(prefers-reduced-motion: reduce)';
@@ -38,9 +48,10 @@ const usePrefersReducedMotion = () => {
const ElementCard: React.FC<{
readonly definition: ElementDefinition;
+ readonly isEmbeddedInStudio: boolean;
readonly prefersReducedMotion: boolean;
readonly sourceCode: string;
-}> = ({definition, prefersReducedMotion, sourceCode}) => {
+}> = ({definition, isEmbeddedInStudio, prefersReducedMotion, sourceCode}) => {
const [isFocused, setIsFocused] = useState(false);
const [isPointerOver, setIsPointerOver] = useState(false);
const [playbackFailed, setPlaybackFailed] = useState(false);
@@ -124,7 +135,7 @@ const ElementCard: React.FC<{
return;
}
- setWasSentToStudio(true);
+ setWasSentToStudio(!isEmbeddedInStudio);
if (window.location.origin === 'https://www.remotion.dev') {
navigator.sendBeacon(
@@ -144,10 +155,14 @@ const ElementCard: React.FC<{
};
const installButtonLabel = isInstalling
- ? 'Finding Studio…'
+ ? isEmbeddedInStudio
+ ? 'Using…'
+ : 'Finding Studio…'
: wasSentToStudio
? 'Sent to Studio'
- : 'Install in Studio';
+ : isEmbeddedInStudio
+ ? 'Use'
+ : 'Install in Studio';
return (
<li
@@ -205,17 +220,15 @@ const ElementCard: React.FC<{
</div>
</a>
<div aria-live="polite" className={styles.installAction}>
- <BlueButton
+ <InlineStudioButton
aria-label={`${installButtonLabel} – ${definition.displayName}`}
- fullWidth={false}
+ icon={null}
loading={isInstalling}
onClick={installElement}
- size="sm"
- style={{padding: '5px 8px'}}
title="Install in the most recently focused Remotion Studio"
>
{installButtonLabel}
- </BlueButton>
+ </InlineStudioButton>
</div>
</li>
);
@@ -223,9 +236,15 @@ const ElementCard: React.FC<{
const ElementGrid: React.FC<{
readonly definitions: readonly ElementDefinition[];
+ readonly isEmbeddedInStudio: boolean;
readonly prefersReducedMotion: boolean;
readonly sourceCodeBySlug: Readonly<Record<string, string>>;
-}> = ({definitions, prefersReducedMotion, sourceCodeBySlug}) => {
+}> = ({
+ definitions,
+ isEmbeddedInStudio,
+ prefersReducedMotion,
+ sourceCodeBySlug,
+}) => {
return (
// The Algolia recordExtractor must remove this subtree before extracting records.
// This marker requires explicit crawler configuration; it is not built in.
@@ -246,6 +265,7 @@ const ElementGrid: React.FC<{
<ElementCard
key={definition.slug}
definition={definition}
+ isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCode={sourceCode}
/>
@@ -261,6 +281,11 @@ export const ElementLibrary: React.FC<{
}> = ({category, sourceCodeBySlug}) => {
const sections = getElementLibrarySections(category);
const prefersReducedMotion = usePrefersReducedMotion();
+ const [isEmbeddedInStudio, setIsEmbeddedInStudio] = useState(false);
+
+ useLayoutEffect(() => {
+ setIsEmbeddedInStudio(isInsideStudio());
+ }, []);
return (
<div className={styles.library}>
@@ -274,6 +299,7 @@ export const ElementLibrary: React.FC<{
>
<ElementGrid
definitions={section.definitions}
+ isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCodeBySlug={sourceCodeBySlug}
/>
@@ -294,6 +320,7 @@ export const ElementLibrary: React.FC<{
</h2>
<ElementGrid
definitions={section.definitions}
+ isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCodeBySlug={sourceCodeBySlug}
/>
diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsx
index 1f8cb66f38..3ea57174c2 100644
--- a/packages/docs/src/components/Elements/ElementPage.tsx
+++ b/packages/docs/src/components/Elements/ElementPage.tsx
@@ -165,7 +165,10 @@ export const ElementPage: React.FC<ElementPageProps> = ({
const {target} = result;
setInstallStatus({
type: 'success',
- message: `Sent to ${target.projectName ?? 'Remotion Studio'} (currently ${target.compositionId}). Confirm the installation destination in Studio.`,
+ message:
+ target.compositionId === null
+ ? `Sent to ${target.projectName ?? 'Remotion Studio'}. Confirm the installation destination in Studio.`
+ : `Sent to ${target.projectName ?? 'Remotion Studio'} (currently ${target.compositionId}). Confirm the installation destination in Studio.`,
});
}
@@ -275,11 +278,17 @@ export const ElementPage: React.FC<ElementPageProps> = ({
onClick={installElement}
size="sm"
style={{padding: '7px 12px'}}
- title="Install in the most recently focused Remotion Studio"
+ title={
+ isEmbeddedInStudio
+ ? 'Use this Element in Studio'
+ : 'Install in the most recently focused Remotion Studio'
+ }
>
{installStatus.type === 'installing'
? 'Finding Studio…'
- : 'Install in Studio'}
+ : isEmbeddedInStudio
+ ? 'Use'
+ : 'Install in Studio'}
</BlueButton>
{isEmbeddedInStudio === false ? (
<div
diff --git a/packages/docs/src/components/Elements/InlineStudioButton.module.css b/packages/docs/src/components/Elements/InlineStudioButton.module.css
new file mode 100644
index 0000000000..ec3e045af3
--- /dev/null
+++ b/packages/docs/src/components/Elements/InlineStudioButton.module.css
@@ -0,0 +1,63 @@
+.button {
+ position: relative;
+ z-index: 1;
+ display: inline-flex;
+ flex: 0 0 auto;
+ gap: 4px;
+ align-items: center;
+ justify-content: center;
+ min-width: 54px;
+ height: 30px;
+ margin: 0;
+ padding: 0 9px 0 7px;
+ border: 0;
+ border-radius: 7px;
+ background: var(--ifm-color-emphasis-100);
+ color: var(--ifm-color-emphasis-800);
+ cursor: pointer;
+ font: inherit;
+ font-size: 0.8125rem;
+ font-weight: 600;
+ line-height: 1;
+}
+
+.button:hover:not(:disabled) {
+ background: var(--ifm-color-emphasis-200);
+ color: var(--ifm-color-emphasis-800);
+}
+
+.button:disabled {
+ cursor: default;
+ opacity: 0.65;
+}
+
+.button:focus-visible {
+ outline: 2px solid var(--ifm-color-primary);
+ outline-offset: 2px;
+}
+
+.icon {
+ display: inline-grid;
+ width: 12px;
+ height: 16px;
+ flex: 0 0 auto;
+ place-items: center;
+ font-size: 1rem;
+ font-weight: 400;
+ line-height: 1rem;
+}
+
+.touchTarget {
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ width: max(100%, 48px);
+ height: max(100%, 48px);
+ transform: translate(-50%, -50%);
+}
+
+@media (pointer: fine) {
+ .touchTarget {
+ display: none;
+ }
+}
diff --git a/packages/docs/src/components/Elements/InlineStudioButton.tsx b/packages/docs/src/components/Elements/InlineStudioButton.tsx
new file mode 100644
index 0000000000..e42eb7de40
--- /dev/null
+++ b/packages/docs/src/components/Elements/InlineStudioButton.tsx
@@ -0,0 +1,33 @@
+import React, {type ButtonHTMLAttributes, type ReactNode} from 'react';
+import styles from './InlineStudioButton.module.css';
+
+export const InlineStudioButton: React.FC<
+ ButtonHTMLAttributes<HTMLButtonElement> & {
+ readonly icon: ReactNode | null;
+ readonly loading: boolean;
+ }
+> = ({children, disabled, icon, loading, ...props}) => {
+ return (
+ <button
+ {...props}
+ aria-busy={loading}
+ className={styles.button}
+ disabled={disabled || loading}
+ type="button"
+ >
+ {loading ? (
+ '…'
+ ) : (
+ <>
+ {icon === null ? null : (
+ <span aria-hidden="true" className={styles.icon}>
+ {icon}
+ </span>
+ )}
+ {children}
+ </>
+ )}
+ <span aria-hidden="true" className={styles.touchTarget} />
+ </button>
+ );
+};
diff --git a/packages/docs/src/components/Elements/ThirdPartyElementLibraries.module.css b/packages/docs/src/components/Elements/ThirdPartyElementLibraries.module.css
index 03a2e667e3..2832d2238c 100644
--- a/packages/docs/src/components/Elements/ThirdPartyElementLibraries.module.css
+++ b/packages/docs/src/components/Elements/ThirdPartyElementLibraries.module.css
@@ -91,64 +91,6 @@
outline-offset: -2px;
}
-.addAction {
- position: relative;
- z-index: 1;
- display: inline-flex;
- flex: 0 0 auto;
- gap: 4px;
- align-items: center;
- justify-content: center;
- min-width: 54px;
- height: 30px;
- margin: 0;
- padding: 0 9px 0 7px;
- border: 0;
- border-radius: 7px;
- background: var(--ifm-color-emphasis-100);
- color: var(--ifm-color-emphasis-800);
- cursor: pointer;
- font: inherit;
- font-size: 0.8125rem;
- font-weight: 600;
- line-height: 1;
-}
-
-.addAction:hover:not(:disabled) {
- background: var(--ifm-color-emphasis-200);
- color: var(--ifm-color-primary);
-}
-
-.addActionIcon {
- display: inline-grid;
- width: 12px;
- height: 16px;
- flex: 0 0 auto;
- place-items: center;
- font-size: 1rem;
- font-weight: 400;
- line-height: 1rem;
-}
-
-.touchTarget {
- position: absolute;
- top: 50%;
- left: 50%;
- width: max(100%, 48px);
- height: max(100%, 48px);
- transform: translate(-50%, -50%);
-}
-
-.addAction:disabled {
- cursor: default;
- opacity: 0.65;
-}
-
-.addAction:focus-visible {
- outline: 2px solid var(--ifm-color-primary);
- outline-offset: 2px;
-}
-
.loadingStatus,
.successStatus,
.errorStatus {
@@ -171,12 +113,6 @@
color: var(--ifm-color-danger-dark);
}
-@media (pointer: fine) {
- .touchTarget {
- display: none;
- }
-}
-
@container third-party-libraries (min-width: 640px) {
.list {
column-count: 2;
@@ -188,7 +124,7 @@
padding: 16px;
}
- .addAction {
+ .libraryRow button {
height: 36px;
padding-right: 11px;
padding-left: 9px;
diff --git a/packages/docs/src/components/Elements/ThirdPartyElementLibraries.tsx b/packages/docs/src/components/Elements/ThirdPartyElementLibraries.tsx
index c2a6a447c9..3b8a9e092d 100644
--- a/packages/docs/src/components/Elements/ThirdPartyElementLibraries.tsx
+++ b/packages/docs/src/components/Elements/ThirdPartyElementLibraries.tsx
@@ -3,6 +3,7 @@ import {
type AddElementLibraryToStudioErrorCode,
} from '@remotion/studio-protocol';
import React, {useCallback, useId, useState} from 'react';
+import {InlineStudioButton} from './InlineStudioButton';
import {
thirdPartyElementLibraries,
type ThirdPartyElementLibrary,
@@ -108,36 +109,24 @@ const ThirdPartyElementLibraryItem: React.FC<{
{library.displayName}
</a>
</h3>
- <button
+ <InlineStudioButton
aria-describedby={addState.type === 'idle' ? undefined : statusId}
aria-label={
isLoading
? `Adding ${library.displayName} to Studio`
: `Add ${library.displayName} to Studio`
}
- aria-busy={isLoading}
- className={styles.addAction}
- disabled={isLoading}
+ icon="+"
+ loading={isLoading}
onClick={addToStudio}
title={
isLoading
? `Adding ${library.displayName} to Studio`
: `Add ${library.displayName} to Studio`
}
- type="button"
>
- {isLoading ? (
- '…'
- ) : (
- <>
- <span aria-hidden="true" className={styles.addActionIcon}>
- +
- </span>
- Add to Studio
- </>
- )}
- <span aria-hidden="true" className={styles.touchTarget} />
- </button>
+ Add to Studio
+ </InlineStudioButton>
</div>
{status}
</div>
diff --git a/packages/studio-protocol/src/add-element-library-to-studio.ts b/packages/studio-protocol/src/add-element-library-to-studio.ts
index c69718fae6..ef2c2baf41 100644
--- a/packages/studio-protocol/src/add-element-library-to-studio.ts
+++ b/packages/studio-protocol/src/add-element-library-to-studio.ts
@@ -66,6 +66,11 @@ export type StudioProtocolAddElementLibraryRequest = {
readonly displayName: string | null;
};
+export type StudioProtocolIframeAddElementLibraryRequest = Omit<
+ StudioProtocolAddElementLibraryRequest,
+ 'targetId'
+>;
+
const studioProtocolAddElementLibraryRequestSchema = z.object({
operation: z.literal('add-element-library'),
protocol: z.literal('remotion-studio-protocol'),
@@ -75,6 +80,31 @@ const studioProtocolAddElementLibraryRequestSchema = z.object({
displayName: z.nullable(z.string()),
});
+const studioProtocolIframeAddElementLibraryRequestSchema = z.object({
+ operation: z.literal('add-element-library'),
+ protocol: z.literal('remotion-studio-protocol'),
+ protocolVersion: z.literal(1),
+ url: z.string(),
+ displayName: z.nullable(z.string()),
+});
+
+const addElementLibraryToStudioResultSchema = z.union([
+ z.object({
+ success: z.literal(true),
+ status: z.literal('awaiting-confirmation'),
+ target: z.object({
+ projectName: z.nullable(z.string()),
+ studioOrigin: z.string(),
+ studioVersion: z.string(),
+ }),
+ }),
+ z.object({
+ success: z.literal(false),
+ code: z.literal('no-configurable-target'),
+ message: z.string(),
+ }),
+]);
+
export const parseStudioProtocolAddElementLibraryRequest = (
value: unknown,
): StudioProtocolAddElementLibraryRequest | null => {
@@ -85,6 +115,16 @@ export const parseStudioProtocolAddElementLibraryRequest = (
return parsed.success ? parsed.data : null;
};
+export const parseStudioProtocolIframeAddElementLibraryRequest = (
+ value: unknown,
+): StudioProtocolIframeAddElementLibraryRequest | null => {
+ const parsed = z.safeParse(
+ studioProtocolIframeAddElementLibraryRequestSchema,
+ value,
+ );
+ return parsed.success ? parsed.data : null;
+};
+
const failure = (
code: AddElementLibraryToStudioErrorCode,
message: string,
@@ -272,11 +312,72 @@ export const addElementLibraryToStudioWithDependencies = async (
);
};
-export const addElementLibraryToStudio = ({
+const addElementLibraryToParentStudio = (
+ request: StudioProtocolIframeAddElementLibraryRequest,
+): Promise<AddElementLibraryToStudioResult | null> => {
+ if (
+ typeof window === 'undefined' ||
+ window.parent === window ||
+ typeof MessageChannel === 'undefined'
+ ) {
+ return Promise.resolve(null);
+ }
+
+ return new Promise((resolve) => {
+ const channel = new MessageChannel();
+ const timeout = setTimeout(() => {
+ channel.port1.close();
+ resolve(null);
+ }, 500);
+
+ channel.port1.onmessage = (event) => {
+ const response = z.safeParse(
+ addElementLibraryToStudioResultSchema,
+ event.data,
+ );
+ if (!response.success) {
+ return;
+ }
+
+ clearTimeout(timeout);
+ channel.port1.postMessage(null);
+ channel.port1.onmessage = null;
+ resolve(response.data);
+ };
+
+ window.parent.postMessage(request, '*', [channel.port2]);
+ });
+};
+
+export const addElementLibraryToStudio = async ({
displayName,
url,
-}: AddElementLibraryToStudioInput): Promise<AddElementLibraryToStudioResult> =>
- addElementLibraryToStudioWithDependencies(
+}: AddElementLibraryToStudioInput): Promise<AddElementLibraryToStudioResult> => {
+ if (typeof url === 'string') {
+ try {
+ const parsedUrl = new URL(url);
+ const normalizedDisplayName = displayName?.trim() ?? null;
+ if (
+ (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') &&
+ normalizedDisplayName !== ''
+ ) {
+ const parentResult = await addElementLibraryToParentStudio({
+ operation: 'add-element-library',
+ protocol: 'remotion-studio-protocol',
+ protocolVersion: 1,
+ url: parsedUrl.href,
+ displayName: normalizedDisplayName,
+ });
+ if (parentResult !== null) {
+ return parentResult;
+ }
+ }
+ } catch {
+ // The shared validation below returns the public invalid URL result.
+ }
+ }
+
+ return addElementLibraryToStudioWithDependencies(
{displayName: displayName ?? null, url},
{
fetchFn: fetch,
@@ -288,3 +389,4 @@ export const addElementLibraryToStudio = ({
ports: studioProtocolProbePorts,
},
);
+};
diff --git a/packages/studio-protocol/src/index.ts b/packages/studio-protocol/src/index.ts
index 99a4dfa77b..ce2d6533a8 100644
--- a/packages/studio-protocol/src/index.ts
+++ b/packages/studio-protocol/src/index.ts
@@ -1,6 +1,7 @@
import {
addElementLibraryToStudioWithDependencies,
parseStudioProtocolAddElementLibraryRequest,
+ parseStudioProtocolIframeAddElementLibraryRequest,
} from './add-element-library-to-studio';
import {
makeBrowserStudioUrl,
@@ -128,6 +129,7 @@ export const StudioProtocolInternals = {
parseDragData,
parseStudioElementPayload,
parseStudioProtocolAddElementLibraryRequest,
+ parseStudioProtocolIframeAddElementLibraryRequest,
parseStudioProtocolIframeInstallRequest,
parseStudioProtocolDescriptor,
parseStudioProtocolInstallRequest,
diff --git a/packages/studio-protocol/src/install-in-studio.ts b/packages/studio-protocol/src/install-in-studio.ts
index 5ba7693dc7..a929551fbd 100644
--- a/packages/studio-protocol/src/install-in-studio.ts
+++ b/packages/studio-protocol/src/install-in-studio.ts
@@ -36,7 +36,7 @@ export type InstallInStudioResult =
readonly status: 'awaiting-confirmation';
readonly target: {
readonly projectName: string | null;
- readonly compositionId: string;
+ readonly compositionId: string | null;
readonly studioOrigin: string;
readonly studioVersion: string;
};
@@ -90,7 +90,7 @@ const installInStudioResultSchema = z.union([
status: z.literal('awaiting-confirmation'),
target: z.object({
projectName: z.nullable(z.string()),
- compositionId: z.string(),
+ compositionId: z.nullable(z.string()),
studioOrigin: z.string(),
studioVersion: z.string(),
}),
@@ -216,7 +216,7 @@ export const installInStudioWithDependencies = async (
return failure(
'no-compatible-studio',
- 'Start Remotion Studio and open a composition, then try again.',
+ 'Start Remotion Studio, then try again.',
);
}
@@ -261,7 +261,7 @@ export const installInStudioWithDependencies = async (
if (!selected || selectedTarget === null || selectedTarget === undefined) {
return failure(
'no-installable-target',
- 'Focus a composition in a Remotion Studio that is not read-only, then try again.',
+ 'Focus a Remotion Studio that is not read-only, then try again.',
);
}
diff --git a/packages/studio-protocol/src/studio-discovery.ts b/packages/studio-protocol/src/studio-discovery.ts
index 501acbc4dd..0577984828 100644
--- a/packages/studio-protocol/src/studio-discovery.ts
+++ b/packages/studio-protocol/src/studio-discovery.ts
@@ -7,7 +7,7 @@ export type StudioProtocolTarget = {
};
export type StudioProtocolInstallTarget = StudioProtocolTarget & {
- readonly compositionId: string;
+ readonly compositionId: string | null;
};
export type StudioProtocolInstallCapability = {
@@ -66,7 +66,7 @@ const installTargetSchema = z.looseObject({
id: z.string().check(z.minLength(1)),
expiresAt: z.number(),
lastFocusedAt: z.number(),
- compositionId: z.string().check(z.minLength(1)),
+ compositionId: z.nullable(z.string().check(z.minLength(1))),
});
const installCapabilitySchema = z.looseObject({
type: z.literal('install-element'),
diff --git a/packages/studio-protocol/src/test/install-in-studio.test.ts b/packages/studio-protocol/src/test/install-in-studio.test.ts
index f19d909ce6..4595e904c7 100644
--- a/packages/studio-protocol/src/test/install-in-studio.test.ts
+++ b/packages/studio-protocol/src/test/install-in-studio.test.ts
@@ -280,8 +280,7 @@ test('distinguishes a compatible Studio without an installable target', async ()
).toEqual({
success: false,
code: 'no-installable-target',
- message:
- 'Focus a composition in a Remotion Studio that is not read-only, then try again.',
+ message: 'Focus a Remotion Studio that is not read-only, then try again.',
});
});
@@ -298,7 +297,7 @@ test('returns an actionable result when no Studio is running', async () => {
expect(result).toEqual({
success: false,
code: 'no-compatible-studio',
- message: 'Start Remotion Studio and open a composition, then try again.',
+ message: 'Start Remotion Studio, then try again.',
});
expect(requests).toEqual([
'http://localhost:3000/api/studio-protocol',
@@ -321,7 +320,7 @@ test('ignores a non-Studio development server on a probed port', async () => {
expect(result).toEqual({
success: false,
code: 'no-compatible-studio',
- message: 'Start Remotion Studio and open a composition, then try again.',
+ message: 'Start Remotion Studio, then try again.',
});
});
@@ -393,7 +392,7 @@ test('keeps the generic result when permission is not definitively denied', asyn
expect(result).toEqual({
success: false,
code: 'no-compatible-studio',
- message: 'Start Remotion Studio and open a composition, then try again.',
+ message: 'Start Remotion Studio, then try again.',
});
}
});
diff --git a/packages/studio-server/src/preview-server/studio-protocol/handle-discovery.ts b/packages/studio-server/src/preview-server/studio-protocol/handle-discovery.ts
index 8eadf450f9..c30bd6ebf4 100644
--- a/packages/studio-server/src/preview-server/studio-protocol/handle-discovery.ts
+++ b/packages/studio-server/src/preview-server/studio-protocol/handle-discovery.ts
@@ -51,14 +51,8 @@ const getLiveStudioTarget = (requestId: string) => {
const isElementRequestTarget = (
target: ElementInstallTarget | null,
): target is ElementInstallTarget & {
- readonly compositionFile: string;
- readonly compositionId: string;
readonly lastFocusedAt: number;
-} =>
- target !== null &&
- target.compositionFile !== null &&
- target.compositionId !== null &&
- target.lastFocusedAt !== null;
+} => target !== null && target.lastFocusedAt !== null;
const getProject = ({
gitSource,
diff --git a/packages/studio-server/src/preview-server/studio-protocol/handle-install.ts b/packages/studio-server/src/preview-server/studio-protocol/handle-install.ts
index 29a205821a..873bcd5be5 100644
--- a/packages/studio-server/src/preview-server/studio-protocol/handle-install.ts
+++ b/packages/studio-server/src/preview-server/studio-protocol/handle-install.ts
@@ -33,10 +33,6 @@ const deliverElementInstall = ({
readonly origin: string;
readonly target: NonNullable<ReturnType<typeof getElementInstallTarget>>;
}): boolean => {
- if (target.compositionFile === null || target.compositionId === null) {
- return false;
- }
-
const installRequest: ElementInstallRequest = {
id: `${Date.now()}-${Math.random().toString(16).slice(2)}`,
clientId: target.clientId,
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index c326653ec6..99a8aa198f 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -1190,8 +1190,8 @@ export type ElementInstallRequest = {
id: string;
clientId: string;
createdAt: number;
- compositionFile: string;
- compositionId: string;
+ compositionFile: string | null;
+ compositionId: string | null;
element: InstallableElement;
from: number | null;
position: InsertableCompositionElementPosition | null;
diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsx
index fecf712553..250a6c98d5 100644
--- a/packages/studio/src/components/Canvas.tsx
+++ b/packages/studio/src/components/Canvas.tsx
@@ -1,9 +1,5 @@
import type {Size} from '@remotion/player';
-import {
- StudioProtocolInternals,
- type InstallInStudioResult,
-} from '@remotion/studio-protocol';
-import type {ElementInstallRequest} from '@remotion/studio-shared';
+import {StudioProtocolInternals} from '@remotion/studio-protocol';
import React, {
useCallback,
useContext,
@@ -15,7 +11,6 @@ import React, {
import type {CanvasContent} from 'remotion';
import {Internals, watchStaticFile, type PreviewSize} from 'remotion';
import {getStaticFiles} from '../api/get-static-files';
-import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {getClipboardFigmaHtml} from '../helpers/clipboard-figma';
import {getClipboardImageFiles} from '../helpers/clipboard-images';
@@ -36,7 +31,6 @@ import {
getEffectiveTranslation,
getUnboundedCenterPointWhileScrolling,
} from '../helpers/get-effective-translation';
-import {getMissingPackages} from '../helpers/install-required-package';
import {useCachedCompositionComponentInfo} from '../helpers/open-in-editor';
import {
MAX_ZOOM,
@@ -54,7 +48,6 @@ import {EditorShowGuidesContext} from '../state/editor-guides';
import {EditorSnappingContext} from '../state/editor-snapping';
import {EditorZoomGesturesContext} from '../state/editor-zoom-gestures';
import {SetSelectedModalContext} from '../state/modals';
-import {callApi} from './call-api';
import {handleCanvasCaptureDrop} from './canvas-capture-drop';
import {getCompositionDragPreviewMetadata} from './composition-drag-data';
import {
@@ -68,15 +61,8 @@ import {EditorRulers} from './EditorRuler';
import {useIsRulerVisible} from './EditorRuler/use-is-ruler-visible';
import {getEffectDragData} from './effect-drag-and-drop';
import {getElementDragData, hasElementDragType} from './element-drag-and-drop';
-import {prepareElementInstall} from './element-install-api';
-import {
- enqueueElementInstallRequest,
- subscribeToElementInstallRequests,
-} from './element-install-request';
import {handleDrop} from './handle-drop';
import {
- getElementPositionForDrop,
- getFromForDrop,
hasSvgFile,
importAssets,
importFigmaClipboard,
@@ -246,23 +232,10 @@ export const Canvas: React.FC<{
const {editorSnapping} = useContext(EditorSnappingContext);
const {compositions} = useContext(Internals.CompositionManager);
const {setCurrentAssetMetadata} = useContext(Internals.CompositionSetters);
- const {previewServerState, subscribeToEvent} = useContext(
- StudioServerConnectionCtx,
- );
- const previewServerClientId =
- previewServerState.type === 'connected'
- ? previewServerState.clientId
- : null;
+ const {previewServerState} = useContext(StudioServerConnectionCtx);
const [isAddingAsset, setIsAddingAsset] = useState(false);
const [compositionDropPreview, setCompositionDropPreview] =
useState<CompositionDropPreview | null>(null);
- const [pendingElementInstallRequests, setPendingElementInstallRequests] =
- useState<ElementInstallRequest[]>([]);
- const [activeElementInstallRequest, setActiveElementInstallRequest] =
- useState<ElementInstallRequest | null>(null);
- const lastFocusedAtRef = useRef<number | null>(
- typeof document === 'undefined' || document.hasFocus() ? Date.now() : null,
- );
const [assetResolution, setAssetResolution] = useState<AssetMetadata | null>(
null,
);
@@ -290,7 +263,7 @@ export const Canvas: React.FC<{
compositionId: currentCompositionId,
});
const canReceiveElementInstallRequest =
- previewServerClientId !== null &&
+ previewServerState.type === 'connected' &&
!window.remotion_isReadOnlyStudio &&
currentCompositionId !== null &&
compositionFile !== null;
@@ -771,346 +744,6 @@ export const Canvas: React.FC<{
fetchMetadata();
}, [fetchMetadata]);
- const updateElementInstallTarget = useCallback(
- (requestId: string) => {
- if (previewServerClientId === null) {
- return;
- }
-
- callApi('/api/update-element-install-target', {
- requestId,
- clientId: previewServerClientId,
- compositionFile: canReceiveElementInstallRequest
- ? compositionFile
- : null,
- compositionId: canReceiveElementInstallRequest
- ? currentCompositionId
- : null,
- lastFocusedAt: lastFocusedAtRef.current,
- readOnly: window.remotion_isReadOnlyStudio,
- studioUrl: window.location.href,
- }).catch(() => undefined);
- },
- [
- canReceiveElementInstallRequest,
- compositionFile,
- currentCompositionId,
- previewServerClientId,
- ],
- );
-
- useEffect(() => {
- const markFocused = () => {
- lastFocusedAtRef.current = Date.now();
- };
-
- window.addEventListener('focus', markFocused);
- document.addEventListener('pointerdown', markFocused, {capture: true});
-
- return () => {
- window.removeEventListener('focus', markFocused);
- document.removeEventListener('pointerdown', markFocused, {capture: true});
- };
- }, []);
-
- useEffect(() => {
- return subscribeToEvent('request-element-install-target', (event) => {
- if (event.type !== 'request-element-install-target') {
- return;
- }
-
- updateElementInstallTarget(event.requestId);
- });
- }, [subscribeToEvent, updateElementInstallTarget]);
-
- useEffect(() => {
- if (previewServerClientId === null) {
- return;
- }
-
- return subscribeToEvent('element-install-request', (event) => {
- if (
- event.type !== 'element-install-request' ||
- event.request.clientId !== previewServerClientId
- ) {
- return;
- }
-
- enqueueElementInstallRequest(event.request);
- });
- }, [previewServerClientId, subscribeToEvent]);
-
- useEffect(() => {
- const onMessage = (event: MessageEvent) => {
- const elementLibrary = document.querySelector<HTMLIFrameElement>(
- 'iframe[data-remotion-element-library]',
- );
- if (
- event.source !== elementLibrary?.contentWindow ||
- !StudioProtocolInternals.isAllowedStudioProtocolPageOrigin(event.origin)
- ) {
- return;
- }
-
- const payload =
- StudioProtocolInternals.parseStudioProtocolIframeInstallRequest(
- event.data,
- );
- const responsePort = event.ports[0];
- if (payload === null || responsePort === undefined) {
- return;
- }
-
- const canInstall =
- canReceiveElementInstallRequest &&
- compositionFile !== null &&
- currentCompositionId !== null &&
- previewServerClientId !== null;
- const request: ElementInstallRequest | null = canInstall
- ? {
- clientId: previewServerClientId,
- compositionFile,
- compositionId: currentCompositionId,
- createdAt: Date.now(),
- element: {
- ...payload.element,
- durationInFrames: payload.element.durationInFrames ?? null,
- initialProps: payload.element.initialProps ?? null,
- installationMode: payload.element.installationMode ?? null,
- },
- from: null,
- id: crypto.randomUUID(),
- position: null,
- source: {origin: event.origin, type: 'studio-protocol'},
- }
- : null;
- const result: InstallInStudioResult = canInstall
- ? {
- success: true,
- status: 'awaiting-confirmation',
- target: {
- compositionId: currentCompositionId,
- projectName: window.remotion_projectName,
- studioOrigin: window.location.origin,
- studioVersion: window.remotion_version,
- },
- }
- : {
- success: false,
- code: 'no-installable-target',
- message:
- 'Focus a composition in a Remotion Studio that is not read-only, then try again.',
- };
-
- const timeout = window.setTimeout(() => responsePort.close(), 1000);
- responsePort.onmessage = () => {
- window.clearTimeout(timeout);
- responsePort.close();
- if (request !== null) {
- enqueueElementInstallRequest(request);
- }
- };
-
- responsePort.postMessage(result);
- };
-
- window.addEventListener('message', onMessage);
- return () => window.removeEventListener('message', onMessage);
- }, [
- canReceiveElementInstallRequest,
- compositionFile,
- currentCompositionId,
- previewServerClientId,
- ]);
-
- useEffect(() => {
- return subscribeToElementInstallRequests((request) => {
- const isDragAndDrop = request.source.type === 'drag-and-drop';
- const requestWithDefaults = isDragAndDrop
- ? request
- : {
- ...request,
- from:
- request.from ??
- getFromForDrop({
- durationInFrames: request.element.durationInFrames,
- from: getCurrentFrame(),
- preferCompositionStart: true,
- }),
- position:
- request.position ??
- getElementPositionForDrop({
- dimensions: request.element.dimensions,
- dropPosition:
- contentDimensions === null || contentDimensions === 'none'
- ? null
- : {
- centerX: contentDimensions.width / 2,
- centerY: contentDimensions.height / 2,
- },
- }),
- };
- setPendingElementInstallRequests((requests) => [
- ...requests,
- requestWithDefaults,
- ]);
- });
- }, [contentDimensions]);
-
- useEffect(() => {
- if (
- !canReceiveElementInstallRequest ||
- compositionFile === null ||
- currentCompositionId === null
- ) {
- return;
- }
-
- const initialElement =
- getBrowserStudioOperations()?.consumeInitialElement() ?? null;
- if (initialElement === null) {
- return;
- }
-
- enqueueElementInstallRequest({
- clientId: 'browser-studio',
- compositionFile,
- compositionId: currentCompositionId,
- createdAt: Date.now(),
- element: initialElement.element,
- from: null,
- id: crypto.randomUUID(),
- position: null,
- source: {
- origin: initialElement.sourceOrigin,
- type: 'browser-studio-link',
- },
- });
- }, [canReceiveElementInstallRequest, compositionFile, currentCompositionId]);
-
- useEffect(() => {
- if (
- activeElementInstallRequest !== null ||
- pendingElementInstallRequests.length === 0
- ) {
- return;
- }
-
- const [nextRequest, ...remainingRequests] = pendingElementInstallRequests;
- if (!nextRequest) {
- throw new Error('Expected pending Element install request');
- }
-
- setActiveElementInstallRequest(nextRequest);
- setPendingElementInstallRequests(remainingRequests);
- }, [activeElementInstallRequest, pendingElementInstallRequests]);
-
- const closeElementInstallDialog = useCallback(() => {
- setSelectedModal(null);
- setActiveElementInstallRequest(null);
- }, [setSelectedModal]);
-
- useEffect(() => {
- if (activeElementInstallRequest === null) {
- return;
- }
-
- let canceled = false;
- const handleInstallRequest = async () => {
- try {
- const [newPreflight, currentPreflight] = await Promise.all([
- prepareElementInstall({
- installationName: null,
- destination: {
- type: 'new-composition',
- compositionFile: null,
- },
- element: activeElementInstallRequest.element,
- }),
- prepareElementInstall({
- installationName: null,
- destination: {
- type: 'current-composition',
- compositionFile: activeElementInstallRequest.compositionFile,
- compositionId: activeElementInstallRequest.compositionId,
- },
- element: activeElementInstallRequest.element,
- }),
- ]);
- if (canceled) {
- return;
- }
-
- if (!newPreflight.success) {
- showNotification(
- `Could not review Element installation: ${newPreflight.reason}`,
- 4000,
- );
- setActiveElementInstallRequest(null);
- return;
- }
-
- const declaredDependencies = Array.from(
- new Map(
- activeElementInstallRequest.element.dependencies.map(
- (dependency) => [dependency.name, dependency],
- ),
- ).values(),
- );
- const missingPackages = getMissingPackages(declaredDependencies).map(
- (dependency) =>
- dependency.version === null
- ? dependency.name
- : `${dependency.name}@${dependency.version}`,
- );
- const {source} = activeElementInstallRequest;
- const sourceLabel =
- source.type === 'studio-protocol'
- ? source.origin
- : source.type === 'browser-studio-link'
- ? (source.origin ?? 'Unverified Browser Studio link')
- : 'Unverified drag-and-drop payload';
- const sourceIsUnverified =
- source.type === 'drag-and-drop' ||
- (source.type === 'browser-studio-link' && source.origin === null);
- const currentPlan = currentPreflight.success
- ? currentPreflight.plan
- : null;
- setSelectedModal({
- type: 'element-install',
- currentPlan,
- missingPackages,
- newPlan: newPreflight.plan,
- onClose: closeElementInstallDialog,
- request: activeElementInstallRequest,
- sourceIsUnverified,
- sourceLabel,
- });
- } catch (error) {
- if (canceled) {
- return;
- }
-
- showNotification(
- `Could not review Element installation: ${
- error instanceof Error ? error.message : String(error)
- }`,
- 4000,
- );
- setActiveElementInstallRequest(null);
- }
- };
-
- handleInstallRequest();
- return () => {
- canceled = true;
- };
- }, [
- activeElementInstallRequest,
- closeElementInstallDialog,
- setSelectedModal,
- ]);
-
const onDragOver = useCallback(
(event: DragEvent) => {
if (!isSupportedDropEvent(event) || !isDragEventInsideCanvas(event)) {
diff --git a/packages/studio/src/components/Editor.tsx b/packages/studio/src/components/Editor.tsx
index cbabe3dae9..0e737cca5a 100644
--- a/packages/studio/src/components/Editor.tsx
+++ b/packages/studio/src/components/Editor.tsx
@@ -13,6 +13,7 @@ import {TimelineZoomContext} from '../state/timeline-zoom';
import {HigherZIndex} from '../state/z-index';
import {CanvasCaptureDropHandler} from './CanvasCaptureDropHandler';
import {EditorContent} from './EditorContent';
+import {ElementInstallRequestHandler} from './ElementInstallRequestHandler';
import {ForceSpecificCursor} from './ForceSpecificCursor';
import {Modals} from './Modals';
import {NotificationCenter} from './Notifications/NotificationCenter';
@@ -139,6 +140,7 @@ export const Editor: React.FC<{
</ScaleLockProvider>
</Internals.CurrentScaleContext.Provider>
</RefreshCanvasSizeContext.Provider>
+ <ElementInstallRequestHandler />
<Modals readOnlyStudio={readOnlyStudio} />
<NotificationCenter />
</SequencePropsSubscriptionProvider>
diff --git a/packages/studio/src/components/ElementInstallConfirmation.tsx b/packages/studio/src/components/ElementInstallConfirmation.tsx
index 30a74cfd18..1b197820d6 100644
--- a/packages/studio/src/components/ElementInstallConfirmation.tsx
+++ b/packages/studio/src/components/ElementInstallConfirmation.tsx
@@ -373,6 +373,16 @@ export const ElementInstallConfirmation: React.FC<{
() => makeSourceControlsVisible(request.element.sourceCode),
[request.element.sourceCode],
);
+ const currentDestination = useMemo(() => {
+ if (request.compositionFile === null || request.compositionId === null) {
+ return null;
+ }
+
+ return {
+ compositionFile: request.compositionFile,
+ compositionId: request.compositionId,
+ };
+ }, [request.compositionFile, request.compositionId]);
const config = Internals.useUnsafeVideoConfig();
const {canvasContent, compositions} = useContext(
Internals.CompositionManager,
@@ -657,6 +667,23 @@ export const ElementInstallConfirmation: React.FC<{
return;
}
+ const destination =
+ mode === 'current-composition'
+ ? currentDestination === null
+ ? null
+ : {
+ type: 'current-composition' as const,
+ compositionFile: currentDestination.compositionFile,
+ compositionId: currentDestination.compositionId,
+ }
+ : {
+ type: 'new-composition' as const,
+ compositionFile: selectedPlan.compositionFile,
+ };
+ if (destination === null) {
+ return;
+ }
+
let canceled = false;
(async () => {
let candidate = requestedName ?? elementBaseName;
@@ -664,17 +691,7 @@ export const ElementInstallConfirmation: React.FC<{
while (true) {
const result = await prepareElementInstall({
installationName: candidate,
- destination:
- mode === 'current-composition'
- ? {
- type: 'current-composition',
- compositionFile: request.compositionFile,
- compositionId: request.compositionId,
- }
- : {
- type: 'new-composition',
- compositionFile: selectedPlan.compositionFile,
- },
+ destination,
element: request.element,
});
if (canceled) return;
@@ -746,6 +763,7 @@ export const ElementInstallConfirmation: React.FC<{
elementBaseName,
requestedName,
mode,
+ currentDestination,
refreshPlan,
request,
selectedPlan,
@@ -773,7 +791,7 @@ export const ElementInstallConfirmation: React.FC<{
? !activePlan.expectedFileState.exists
: requestedName !== null))) &&
(mode === 'current-composition'
- ? currentPlan !== null
+ ? currentPlan !== null && currentDestination !== null
: newCompositionValuesAreValid &&
folderTargetIsReady &&
selectedNewCompositionPlan !== null);
@@ -783,17 +801,24 @@ export const ElementInstallConfirmation: React.FC<{
return;
}
+ const destination =
+ mode === 'new-composition'
+ ? {
+ compositionFile: selectedPlan.compositionFile,
+ compositionId: newCompositionId,
+ }
+ : currentDestination;
+ if (destination === null) {
+ return;
+ }
+
setSubmitting(true);
const installed = await insertElement({
installationName: overwriteExisting
? (existingDestination?.name ?? installationName)
: installationName,
- compositionFile:
- mode === 'new-composition'
- ? selectedPlan.compositionFile
- : request.compositionFile,
- compositionId:
- mode === 'new-composition' ? newCompositionId : request.compositionId,
+ compositionFile: destination.compositionFile,
+ compositionId: destination.compositionId,
element: request.element,
// The insert operation revalidates this even if the name preflight is pending.
expectedFileState: activePlan?.expectedFileState ?? {exists: false},
@@ -839,6 +864,7 @@ export const ElementInstallConfirmation: React.FC<{
installationName,
overwriteExisting,
canSubmit,
+ currentDestination,
folderSymbolicatedStack,
mode,
newCompositionCodemod,
@@ -936,7 +962,7 @@ export const ElementInstallConfirmation: React.FC<{
</div>
</div>
- {currentPlan === null ? (
+ {currentPlan === null && request.compositionId !== null ? (
<div style={warningStyle} role="status">
<WarningTriangle style={warningIconStyle} />
<p style={warningDescriptionStyle}>
diff --git a/packages/studio/src/components/ElementInstallRequestHandler.tsx b/packages/studio/src/components/ElementInstallRequestHandler.tsx
new file mode 100644
index 0000000000..64056e36bb
--- /dev/null
+++ b/packages/studio/src/components/ElementInstallRequestHandler.tsx
@@ -0,0 +1,408 @@
+import {
+ StudioProtocolInternals,
+ type InstallInStudioResult,
+} from '@remotion/studio-protocol';
+import type {ElementInstallRequest} from '@remotion/studio-shared';
+import {
+ type FC,
+ useCallback,
+ useContext,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
+import {Internals} from 'remotion';
+import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
+import {StudioServerConnectionCtx} from '../helpers/client-id';
+import {getMissingPackages} from '../helpers/install-required-package';
+import {SetSelectedModalContext} from '../state/modals';
+import {callApi} from './call-api';
+import {prepareElementInstall} from './element-install-api';
+import {
+ enqueueElementInstallRequest,
+ subscribeToElementInstallRequests,
+} from './element-install-request';
+import {getElementPositionForDrop, getFromForDrop} from './import-assets';
+import {showNotification} from './Notifications/NotificationCenter';
+import {getCurrentFrame} from './Timeline/imperative-state';
+import {useResolvedStack} from './Timeline/use-resolved-stack';
+
+export const ElementInstallRequestHandler: FC = () => {
+ const {canvasContent, compositions} = useContext(
+ Internals.CompositionManager,
+ );
+ const config = Internals.useUnsafeVideoConfig();
+ const {setSelectedModal} = useContext(SetSelectedModalContext);
+ const {previewServerState, subscribeToEvent} = useContext(
+ StudioServerConnectionCtx,
+ );
+ const previewServerClientId =
+ previewServerState.type === 'connected'
+ ? previewServerState.clientId
+ : null;
+ const [pendingRequests, setPendingRequests] = useState<
+ ElementInstallRequest[]
+ >([]);
+ const [activeRequest, setActiveRequest] =
+ useState<ElementInstallRequest | null>(null);
+ const lastFocusedAtRef = useRef<number | null>(
+ typeof document === 'undefined' || document.hasFocus() ? Date.now() : null,
+ );
+
+ const currentCompositionId =
+ canvasContent?.type === 'composition' ? canvasContent.compositionId : null;
+ const currentComposition = useMemo(() => {
+ if (currentCompositionId === null) {
+ return null;
+ }
+
+ return (
+ compositions.find(
+ (composition) => composition.id === currentCompositionId,
+ ) ?? null
+ );
+ }, [compositions, currentCompositionId]);
+ const resolvedCompositionLocation = useResolvedStack(
+ currentComposition?.stack ?? null,
+ );
+ const compositionFile = resolvedCompositionLocation?.source ?? null;
+ const canInstallElement =
+ previewServerClientId !== null && !window.remotion_isReadOnlyStudio;
+ const hasCurrentComposition =
+ canInstallElement &&
+ currentCompositionId !== null &&
+ compositionFile !== null;
+
+ const updateElementInstallTarget = useCallback(
+ (requestId: string) => {
+ if (previewServerClientId === null) {
+ return;
+ }
+
+ callApi('/api/update-element-install-target', {
+ requestId,
+ clientId: previewServerClientId,
+ compositionFile: hasCurrentComposition ? compositionFile : null,
+ compositionId: hasCurrentComposition ? currentCompositionId : null,
+ lastFocusedAt: lastFocusedAtRef.current,
+ readOnly: window.remotion_isReadOnlyStudio,
+ studioUrl: window.location.href,
+ }).catch(() => undefined);
+ },
+ [
+ compositionFile,
+ currentCompositionId,
+ hasCurrentComposition,
+ previewServerClientId,
+ ],
+ );
+
+ useEffect(() => {
+ const markFocused = () => {
+ lastFocusedAtRef.current = Date.now();
+ };
+
+ window.addEventListener('focus', markFocused);
+ document.addEventListener('pointerdown', markFocused, {capture: true});
+
+ return () => {
+ window.removeEventListener('focus', markFocused);
+ document.removeEventListener('pointerdown', markFocused, {capture: true});
+ };
+ }, []);
+
+ useEffect(() => {
+ return subscribeToEvent('request-element-install-target', (event) => {
+ if (event.type !== 'request-element-install-target') {
+ return;
+ }
+
+ updateElementInstallTarget(event.requestId);
+ });
+ }, [subscribeToEvent, updateElementInstallTarget]);
+
+ useEffect(() => {
+ if (previewServerClientId === null) {
+ return;
+ }
+
+ return subscribeToEvent('element-install-request', (event) => {
+ if (
+ event.type !== 'element-install-request' ||
+ event.request.clientId !== previewServerClientId
+ ) {
+ return;
+ }
+
+ enqueueElementInstallRequest(event.request);
+ });
+ }, [previewServerClientId, subscribeToEvent]);
+
+ useEffect(() => {
+ const onMessage = (event: MessageEvent) => {
+ const elementLibrary = document.querySelector<HTMLIFrameElement>(
+ 'iframe[data-remotion-element-library]',
+ );
+ if (
+ event.source !== elementLibrary?.contentWindow ||
+ !StudioProtocolInternals.isAllowedStudioProtocolPageOrigin(event.origin)
+ ) {
+ return;
+ }
+
+ const payload =
+ StudioProtocolInternals.parseStudioProtocolIframeInstallRequest(
+ event.data,
+ );
+ const responsePort = event.ports[0];
+ if (payload === null || responsePort === undefined) {
+ return;
+ }
+
+ const request: ElementInstallRequest | null = canInstallElement
+ ? {
+ clientId: previewServerClientId,
+ compositionFile,
+ compositionId: currentCompositionId,
+ createdAt: Date.now(),
+ element: {
+ ...payload.element,
+ durationInFrames: payload.element.durationInFrames ?? null,
+ initialProps: payload.element.initialProps ?? null,
+ installationMode: payload.element.installationMode ?? null,
+ },
+ from: null,
+ id: crypto.randomUUID(),
+ position: null,
+ source: {origin: event.origin, type: 'studio-protocol'},
+ }
+ : null;
+ const result: InstallInStudioResult = canInstallElement
+ ? {
+ success: true,
+ status: 'awaiting-confirmation',
+ target: {
+ compositionId: currentCompositionId,
+ projectName: window.remotion_projectName,
+ studioOrigin: window.location.origin,
+ studioVersion: window.remotion_version,
+ },
+ }
+ : {
+ success: false,
+ code: 'no-installable-target',
+ message:
+ 'Open a Remotion Studio that is not read-only, then try again.',
+ };
+
+ const timeout = window.setTimeout(() => responsePort.close(), 1000);
+ responsePort.onmessage = () => {
+ window.clearTimeout(timeout);
+ responsePort.close();
+ if (request !== null) {
+ enqueueElementInstallRequest(request);
+ }
+ };
+
+ responsePort.postMessage(result);
+ };
+
+ window.addEventListener('message', onMessage);
+ return () => window.removeEventListener('message', onMessage);
+ }, [
+ canInstallElement,
+ compositionFile,
+ currentCompositionId,
+ previewServerClientId,
+ ]);
+
+ useEffect(() => {
+ return subscribeToElementInstallRequests((request) => {
+ const shouldAddCompositionDefaults =
+ request.source.type !== 'drag-and-drop' &&
+ request.compositionFile !== null &&
+ request.compositionId !== null;
+ const requestWithDefaults = shouldAddCompositionDefaults
+ ? {
+ ...request,
+ from:
+ request.from ??
+ getFromForDrop({
+ durationInFrames: request.element.durationInFrames,
+ from: getCurrentFrame(),
+ preferCompositionStart: true,
+ }),
+ position:
+ request.position ??
+ getElementPositionForDrop({
+ dimensions: request.element.dimensions,
+ dropPosition:
+ config === null
+ ? null
+ : {
+ centerX: config.width / 2,
+ centerY: config.height / 2,
+ },
+ }),
+ }
+ : request;
+ setPendingRequests((requests) => [...requests, requestWithDefaults]);
+ });
+ }, [config]);
+
+ useEffect(() => {
+ if (!canInstallElement) {
+ return;
+ }
+
+ const initialElement =
+ getBrowserStudioOperations()?.consumeInitialElement() ?? null;
+ if (initialElement === null) {
+ return;
+ }
+
+ enqueueElementInstallRequest({
+ clientId: 'browser-studio',
+ compositionFile,
+ compositionId: currentCompositionId,
+ createdAt: Date.now(),
+ element: initialElement.element,
+ from: null,
+ id: crypto.randomUUID(),
+ position: null,
+ source: {
+ origin: initialElement.sourceOrigin,
+ type: 'browser-studio-link',
+ },
+ });
+ }, [canInstallElement, compositionFile, currentCompositionId]);
+
+ useEffect(() => {
+ if (activeRequest !== null || pendingRequests.length === 0) {
+ return;
+ }
+
+ const [nextRequest, ...remainingRequests] = pendingRequests;
+ if (!nextRequest) {
+ throw new Error('Expected pending Element install request');
+ }
+
+ setActiveRequest(nextRequest);
+ setPendingRequests(remainingRequests);
+ }, [activeRequest, pendingRequests]);
+
+ const closeElementInstallDialog = useCallback(() => {
+ setSelectedModal(null);
+ setActiveRequest(null);
+ }, [setSelectedModal]);
+
+ useEffect(() => {
+ if (activeRequest === null) {
+ return;
+ }
+
+ let canceled = false;
+ const handleInstallRequest = async () => {
+ try {
+ const currentDestination =
+ activeRequest.compositionFile === null ||
+ activeRequest.compositionId === null
+ ? null
+ : {
+ compositionFile: activeRequest.compositionFile,
+ compositionId: activeRequest.compositionId,
+ };
+ const [newPreflight, currentPreflight] = await Promise.all([
+ prepareElementInstall({
+ installationName: null,
+ destination: {
+ type: 'new-composition',
+ compositionFile: null,
+ },
+ element: activeRequest.element,
+ }),
+ currentDestination === null
+ ? null
+ : prepareElementInstall({
+ installationName: null,
+ destination: {
+ type: 'current-composition',
+ compositionFile: currentDestination.compositionFile,
+ compositionId: currentDestination.compositionId,
+ },
+ element: activeRequest.element,
+ }),
+ ]);
+ if (canceled) {
+ return;
+ }
+
+ if (!newPreflight.success) {
+ showNotification(
+ `Could not review Element installation: ${newPreflight.reason}`,
+ 4000,
+ );
+ setActiveRequest(null);
+ return;
+ }
+
+ const declaredDependencies = Array.from(
+ new Map(
+ activeRequest.element.dependencies.map((dependency) => [
+ dependency.name,
+ dependency,
+ ]),
+ ).values(),
+ );
+ const missingPackages = getMissingPackages(declaredDependencies).map(
+ (dependency) =>
+ dependency.version === null
+ ? dependency.name
+ : `${dependency.name}@${dependency.version}`,
+ );
+ const {source} = activeRequest;
+ const sourceLabel =
+ source.type === 'studio-protocol'
+ ? source.origin
+ : source.type === 'browser-studio-link'
+ ? (source.origin ?? 'Unverified Browser Studio link')
+ : 'Unverified drag-and-drop payload';
+ const sourceIsUnverified =
+ source.type === 'drag-and-drop' ||
+ (source.type === 'browser-studio-link' && source.origin === null);
+ const currentPlan =
+ currentPreflight?.success === true ? currentPreflight.plan : null;
+ setSelectedModal({
+ type: 'element-install',
+ currentPlan,
+ missingPackages,
+ newPlan: newPreflight.plan,
+ onClose: closeElementInstallDialog,
+ request: activeRequest,
+ sourceIsUnverified,
+ sourceLabel,
+ });
+ } catch (error) {
+ if (canceled) {
+ return;
+ }
+
+ showNotification(
+ `Could not review Element installation: ${
+ error instanceof Error ? error.message : String(error)
+ }`,
+ 4000,
+ );
+ setActiveRequest(null);
+ }
+ };
+
+ handleInstallRequest();
+ return () => {
+ canceled = true;
+ };
+ }, [activeRequest, closeElementInstallDialog, setSelectedModal]);
+
+ return null;
+};
diff --git a/packages/studio/src/components/Modals.tsx b/packages/studio/src/components/Modals.tsx
index 7a3cc414ce..00a723bc88 100644
--- a/packages/studio/src/components/Modals.tsx
+++ b/packages/studio/src/components/Modals.tsx
@@ -1,4 +1,8 @@
-import React, {useContext, useEffect} from 'react';
+import {
+ StudioProtocolInternals,
+ type AddElementLibraryToStudioResult,
+} from '@remotion/studio-protocol';
+import React, {useCallback, useContext, useEffect} from 'react';
import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {getStudioAskAIEnabled} from '../helpers/studio-runtime-config';
@@ -48,6 +52,66 @@ export const Modals: React.FC<{
const canRender = previewServerState.type === 'connected';
const isBrowserStudio = getBrowserStudioOperations() !== null;
const confirm = useConfirmationDialog();
+ const requestElementLibraryAddition = useCallback(
+ async ({
+ displayName,
+ origin,
+ url,
+ }: {
+ readonly displayName: string | null;
+ readonly origin: string;
+ readonly url: string;
+ }) => {
+ const confirmed = await confirm({
+ title: 'Add Element Library',
+ message: (
+ <ElementLibraryAddConfirmation
+ displayName={displayName}
+ origin={origin}
+ url={url}
+ />
+ ),
+ confirmLabel: 'Add Element Library',
+ cancelLabel: 'Cancel',
+ });
+ if (!confirmed) {
+ return;
+ }
+
+ if (previewServerState.type !== 'connected') {
+ showNotification(
+ 'Could not add Element Library: Studio disconnected',
+ 4000,
+ );
+ return;
+ }
+
+ try {
+ const result = await callApi('/api/update-config', {
+ clientId: previewServerState.clientId,
+ updates: [
+ {
+ setter: 'addElementLibrary',
+ type: 'set',
+ value: displayName === null ? {url} : {url, displayName},
+ },
+ ],
+ });
+ if (!result.success) {
+ showNotification(
+ `Could not add Element Library: ${result.reason}`,
+ 4000,
+ );
+ }
+ } catch (error) {
+ showNotification(
+ `Could not add Element Library: ${(error as Error).message}`,
+ 4000,
+ );
+ }
+ },
+ [confirm, previewServerState],
+ );
useEffect(() => {
if (isBrowserStudio) {
@@ -77,60 +141,71 @@ export const Modals: React.FC<{
return;
}
- (async () => {
- const confirmed = await confirm({
- title: 'Add Element Library',
- message: (
- <ElementLibraryAddConfirmation
- displayName={event.displayName}
- origin={event.origin}
- url={event.url}
- />
- ),
- confirmLabel: 'Add Element Library',
- cancelLabel: 'Cancel',
- });
- if (!confirmed) {
- return;
- }
+ requestElementLibraryAddition(event);
+ });
+ }, [isBrowserStudio, requestElementLibraryAddition, subscribeToEvent]);
- if (previewServerState.type !== 'connected') {
- showNotification(
- 'Could not add Element Library: Studio disconnected',
- 4000,
- );
- return;
- }
+ useEffect(() => {
+ const onMessage = (event: MessageEvent) => {
+ const elementLibrary = document.querySelector<HTMLIFrameElement>(
+ 'iframe[data-remotion-element-library]',
+ );
+ if (
+ event.source !== elementLibrary?.contentWindow ||
+ !StudioProtocolInternals.isAllowedStudioProtocolPageOrigin(event.origin)
+ ) {
+ return;
+ }
- try {
- const result = await callApi('/api/update-config', {
- clientId: previewServerState.clientId,
- updates: [
- {
- setter: 'addElementLibrary',
- type: 'set',
- value:
- event.displayName === null
- ? {url: event.url}
- : {url: event.url, displayName: event.displayName},
- },
- ],
- });
- if (!result.success) {
- showNotification(
- `Could not add Element Library: ${result.reason}`,
- 4000,
- );
+ const request =
+ StudioProtocolInternals.parseStudioProtocolIframeAddElementLibraryRequest(
+ event.data,
+ );
+ const responsePort = event.ports[0];
+ if (request === null || responsePort === undefined) {
+ return;
+ }
+
+ const canAddLibrary =
+ !isBrowserStudio &&
+ !readOnlyStudio &&
+ previewServerState.type === 'connected';
+ const result: AddElementLibraryToStudioResult = canAddLibrary
+ ? {
+ success: true,
+ status: 'awaiting-confirmation',
+ target: {
+ projectName: window.remotion_projectName,
+ studioOrigin: window.location.origin,
+ studioVersion: window.remotion_version,
+ },
}
- } catch (error) {
- showNotification(
- `Could not add Element Library: ${(error as Error).message}`,
- 4000,
- );
+ : {
+ success: false,
+ code: 'no-configurable-target',
+ message: 'Open a writable Remotion Studio, then try again.',
+ };
+
+ const timeout = window.setTimeout(() => responsePort.close(), 1000);
+ responsePort.onmessage = () => {
+ window.clearTimeout(timeout);
+ responsePort.close();
+ if (canAddLibrary) {
+ requestElementLibraryAddition({...request, origin: event.origin});
}
- })();
- });
- }, [confirm, isBrowserStudio, previewServerState, subscribeToEvent]);
+ };
+
+ responsePort.postMessage(result);
+ };
+
+ window.addEventListener('message', onMessage);
+ return () => window.removeEventListener('message', onMessage);
+ }, [
+ isBrowserStudio,
+ previewServerState.type,
+ readOnlyStudio,
+ requestElementLibraryAddition,
+ ]);
return (
<>