Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 10 additions & 45 deletions packages/media-editor/src/components/media-editor-panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ export interface MediaEditorPanelProps extends MediaEditorPanelRegistration {
interface MediaEditorPanelsContextValue {
register: ( panel: MediaEditorPanelRegistration, instance: object ) => void;
unregister: ( name: string, instance: object ) => void;
/** Panel name to the mounted panel that has it. */
owners: Record< string, object >;
}

const MediaEditorPanelsContext =
Expand All @@ -62,10 +60,7 @@ export function getPanelSlotName( name: string ) {
*/
export function useMediaEditorPanels() {
const [ panels, setPanels ] = useState<
Record<
string,
{ registration: MediaEditorPanelRegistration; owner: object }
>
Record< string, MediaEditorPanelRegistration >
>( {} );
// The first panel to announce a name keeps it. A ref, so panels
// announcing in the same commit see each other.
Expand All @@ -74,26 +69,18 @@ export function useMediaEditorPanels() {
( panel: MediaEditorPanelRegistration, instance: object ) => {
const owner = ownersRef.current.get( panel.name );
if ( owner && owner !== instance ) {
warning(
`A media editor panel named "${ panel.name }" is already registered, so this one is not shown. Give each panel a unique name.`
throw new Error(
`A media editor panel named "${ panel.name }" is already registered. Give each panel a unique name.`
);
return;
}
ownersRef.current.set( panel.name, instance );
setPanels( ( current ) => {
// Skip unchanged details: an inline `mediaTypes` array would
// otherwise re-render the editor and the panel in a loop.
const entry = current[ panel.name ];
if (
entry?.owner === instance &&
fastDeepEqual( entry.registration, panel )
) {
if ( fastDeepEqual( current[ panel.name ], panel ) ) {
return current;
}
return {
...current,
[ panel.name ]: { registration: panel, owner: instance },
};
return { ...current, [ panel.name ]: panel };
} );
},
[]
Expand All @@ -112,25 +99,11 @@ export function useMediaEditorPanels() {
return next;
} );
}, [] );
const owners = useMemo(
() =>
Object.fromEntries(
Object.entries( panels ).map( ( [ name, { owner } ] ) => [
name,
owner,
] )
),
[ panels ]
);
const contextValue = useMemo(
() => ( { register, unregister, owners } ),
[ register, unregister, owners ]
);
const registrations = useMemo(
() =>
Object.values( panels ).map( ( { registration } ) => registration ),
[ panels ]
() => ( { register, unregister } ),
[ register, unregister ]
);
const registrations = useMemo( () => Object.values( panels ), [ panels ] );
return { panels: registrations, contextValue };
}

Expand All @@ -147,10 +120,9 @@ export default function MediaEditorPanel( {
'MediaEditorPanel is rendered outside the media editor.'
);
}
const { register, unregister, owners } = context;
const { register, unregister } = context;
// Identifies this mounted panel.
const [ instance ] = useState( () => ( {} ) );
const owner = owners[ name ];
// Layout effects, so the tab exists before the sidebar paints. Two of
// them: details may change on any render, but the tab only goes when
// the panel unmounts or changes its name.
Expand All @@ -168,18 +140,11 @@ export default function MediaEditorPanel( {
}
} );
register( { name, title, mediaTypes }, instance );
// When two panels share a name, the second waits for the first to
// unmount. `owner` changes at that point, which re-runs this effect
// and gives the second panel the tab.
}, [ name, title, mediaTypes, register, owner, instance ] );
}, [ name, title, mediaTypes, register, instance ] );
useLayoutEffect( () => {
return () => unregister( name, instance );
}, [ name, unregister, instance ] );

if ( owner !== instance ) {
return null;
}

return (
<Fill name={ getPanelSlotName( name ) }>
{ ( fillProps ) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,40 +287,46 @@ describe( 'MediaEditorPanel', () => {
).toThrow( 'MediaEditorPanel is rendered outside the media editor.' );
} );

it( 'keeps the first of two panels that share a name, and warns', () => {
function Toggle() {
const [ shown, setShown ] = useState( true );
it( 'throws when another mounted panel has its name', () => {
expect( () =>
render(
<Editor>
<MediaEditorPanel name="plugin/same" title="First">
first
</MediaEditorPanel>
<MediaEditorPanel name="plugin/same" title="Second">
second
</MediaEditorPanel>
</Editor>
)
).toThrow(
'A media editor panel named "plugin/same" is already registered.'
);
} );

it( 'frees a name when its panel unmounts, for a panel that mounts in the same render', () => {
function Swap() {
const [ swapped, setSwapped ] = useState( false );
return (
<>
<button onClick={ () => setShown( false ) }>hide</button>
{ shown && (
<MediaEditorPanel name="plugin/same" title="First">
first
</MediaEditorPanel>
) }
<button onClick={ () => setSwapped( true ) }>swap</button>
<MediaEditorPanel
key={ swapped ? 'second' : 'first' }
name="plugin/same"
title={ swapped ? 'Second' : 'First' }
>
{ swapped ? 'second' : 'first' }
</MediaEditorPanel>
</>
);
}
render(
<Editor>
<Toggle />
<MediaEditorPanel name="plugin/same" title="Second">
second
</MediaEditorPanel>
<Swap />
</Editor>
);

expect( mockedWarning ).toHaveBeenCalledWith(
expect.stringContaining( '"plugin/same"' )
);
expect( screen.getByTestId( 'tabs' ) ).toHaveTextContent(
/^plugin\/same$/
);
expect(
screen.getByRole( 'region', { name: 'First' } )
).toHaveTextContent( /^first$/ );

fireEvent.click( screen.getByRole( 'button', { name: 'hide' } ) );
fireEvent.click( screen.getByRole( 'button', { name: 'swap' } ) );

expect(
screen.getByRole( 'region', { name: 'Second' } )
Expand Down
3 changes: 3 additions & 0 deletions packages/media-editor/src/components/media-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,9 @@ function MediaEditorContent( {
>
<MediaForm />
</MediaEditorPanel>
{ /* After Core's panels: when a plugin reuses one of
their names, the plugin's panel is the one that
throws, and `PluginArea` catches it. */ }
<PluginArea
scope={ MEDIA_EDITOR_PLUGIN_SCOPE }
onError={ handlePluginError }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,32 @@ describe( 'Media editor panels', () => {
unregisterPlugin( 'test-media-panels' );
}
} );

it( 'drops a plugin whose panel takes a name already in use, and says so', async () => {
registerPlugin( 'test-duplicate-panel', {
scope: MEDIA_EDITOR_PLUGIN_SCOPE,
render: () => (
<MediaEditorPanel name="core/details" title="Duplicate">
Never shown
</MediaEditorPanel>
),
} );
try {
await setup();

expect( await screen.findByTestId( 'snackbar' ) ).toHaveTextContent(
'The "test-duplicate-panel" plugin has encountered an error and cannot be rendered.'
);
// React reports the error it handed to the plugin's boundary.
expect( console ).toHaveErrored();
expect(
screen.getByRole( 'tab', { name: 'Details' } )
).toBeInTheDocument();
expect(
screen.queryByRole( 'tab', { name: 'Duplicate' } )
).not.toBeInTheDocument();
} finally {
unregisterPlugin( 'test-duplicate-panel' );
}
} );
} );
Loading