Repository navigation
Cover Block: Add a Media section to the block inspector panel #82751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
10277d8
035447b
312c81f
c9cbb11
529e5a5
cc284c4
9d2840b
f2faae4
75373d6
eaad239
2d303ba
6f8f6ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,8 +23,13 @@ | |
| import { __ } from '@wordpress/i18n'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
| import { getFilename } from '@wordpress/url'; | ||
| import { Link } from '@wordpress/ui'; | ||
| import { COVER_MIN_HEIGHT, mediaPosition } from '../shared'; | ||
| import { | ||
| ALLOWED_MEDIA_TYPES, | ||
| COVER_MIN_HEIGHT, | ||
| mediaPosition, | ||
| } from '../shared'; | ||
| import { unlock } from '../../lock-unlock'; | ||
| import { useToolsPanelDropdownMenuProps } from '../../utils/hooks'; | ||
| import { | ||
|
|
@@ -35,6 +40,7 @@ | |
| } from '../../utils/style-state'; | ||
| import { DEFAULT_MEDIA_SIZE_SLUG } from '../constants'; | ||
| import PosterImage from '../../utils/poster-image'; | ||
| import { MediaControl } from '../../utils/media-control'; | ||
|
|
||
| const { | ||
| cleanEmptyObject, | ||
|
|
@@ -98,8 +104,13 @@ | |
| setOverlayColor, | ||
| coverRef, | ||
| currentSettings, | ||
| onSelectMedia, | ||
| onUploadError, | ||
| toggleUseFeaturedImage, | ||
| updateDimRatio, | ||
| onClearMedia, | ||
| featuredImage, | ||
| isSelected, | ||
| } ) { | ||
| const { | ||
| useFeaturedImage, | ||
|
|
@@ -138,6 +149,24 @@ | |
| }, | ||
| [ clientId ] | ||
| ); | ||
| /* | ||
| * The block only resolves the featured image record, so the record of a | ||
| * media selection of its own is resolved here to name it in the panel. | ||
| */ | ||
| const selectedMedia = useSelect( | ||
| ( select ) => | ||
| id && ! useFeaturedImage | ||
| ? select( coreStore ).getEntityRecord( | ||
| 'postType', | ||
| 'attachment', | ||
| id, | ||
| { context: 'view' } | ||
| ) | ||
| : undefined, | ||
| [ id, useFeaturedImage ] | ||
| ); | ||
| const mediaRecord = useFeaturedImage ? featuredImage : selectedMedia; | ||
|
|
||
| const hasSelectedStyleState = | ||
| ! isDefaultBlockStyleState( selectedStyleState ); | ||
| const selectedStyleStateKey = getStyleStateKey( selectedStyleState ); | ||
|
|
@@ -277,8 +306,49 @@ | |
|
|
||
| const dropdownMenuProps = useToolsPanelDropdownMenuProps(); | ||
|
|
||
| /* | ||
| * Rendered whether or not media has been set, so that a cover in its setup | ||
| * state can be given media from the inspector as well as from the toolbar. | ||
| * The settings below stay behind their own condition, having nothing to | ||
| * configure until there is media to configure. | ||
| */ | ||
| const mediaInspectorPanel = ( | ||
| <InspectorControls group="content"> | ||
| <ToolsPanel | ||
| label={ __( 'Media' ) } | ||
| resetAll={ onClearMedia } | ||
| dropdownMenuProps={ dropdownMenuProps } | ||
| > | ||
| <ToolsPanelItem | ||
| label={ __( 'Media' ) } | ||
| hasValue={ () => !! url || !! useFeaturedImage } | ||
| onDeselect={ onClearMedia } | ||
| isShownByDefault | ||
| > | ||
| <MediaControl | ||
| mediaId={ id } | ||
| mediaUrl={ url } | ||
| filename={ | ||
| mediaRecord?.media_details?.sizes?.full?.file || | ||
| mediaRecord?.slug || | ||
| getFilename( url ) | ||
| } | ||
| allowedTypes={ ALLOWED_MEDIA_TYPES } | ||
| onSelect={ onSelectMedia } | ||
| onError={ onUploadError } | ||
| onReset={ onClearMedia } | ||
| useFeaturedImage={ useFeaturedImage } | ||
| onToggleFeaturedImage={ toggleUseFeaturedImage } | ||
| emptyLabel={ __( 'Add media' ) } | ||
| /> | ||
| </ToolsPanelItem> | ||
| </ToolsPanel> | ||
| </InspectorControls> | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| { isSelected && mediaInspectorPanel } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the same change is required as the other PR, either inline the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, moved the selection check onto the panel itself, same as Media & Text. |
||
| { ( !! url || useFeaturedImage ) && ( | ||
| <InspectorControls> | ||
| <ToolsPanel | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -66,6 +66,16 @@ async function openStylesTabIfAvailable() { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| async function openSettingsTabIfAvailable() { | ||||||
| const settingsTab = screen.queryByRole( 'tab', { | ||||||
| name: 'Settings', | ||||||
| } ); | ||||||
|
|
||||||
| if ( settingsTab ) { | ||||||
| await userEvent.click( settingsTab ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| describe( 'Cover block', () => { | ||||||
| beforeEach( () => registerCoreBlocks() ); | ||||||
|
|
||||||
|
|
@@ -207,11 +217,12 @@ describe( 'Cover block', () => { | |||||
| } ) | ||||||
| ).not.toBeInTheDocument(); | ||||||
| } ); | ||||||
| test( 'does not display settings tab when media settings are empty', async () => { | ||||||
| test( 'does not display media settings panel when media settings are empty', async () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's confusing because this PR adds a Media panel in the content tab, but this test is referring to just the 'Settings' panel, so I suggest changing it to this
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, renamed it as suggested. |
||||||
| await createAndSelectBlock(); | ||||||
|
|
||||||
| await openSettingsTabIfAvailable(); | ||||||
| expect( | ||||||
| screen.queryByRole( 'tab', { | ||||||
| screen.queryByRole( 'heading', { | ||||||
| name: 'Settings', | ||||||
| } ) | ||||||
| ).not.toBeInTheDocument(); | ||||||
|
|
@@ -227,6 +238,7 @@ describe( 'Cover block', () => { | |||||
| } ); | ||||||
|
|
||||||
| await selectBlock( 'Block: Cover' ); | ||||||
| await openSettingsTabIfAvailable(); | ||||||
| expect( | ||||||
| await screen.findByRole( 'heading', { name: 'Settings' } ) | ||||||
| ).toBeInTheDocument(); | ||||||
|
|
@@ -241,6 +253,7 @@ describe( 'Cover block', () => { | |||||
| 'has-parallax' | ||||||
| ); | ||||||
| await selectBlock( 'Block: Cover' ); | ||||||
| await openSettingsTabIfAvailable(); | ||||||
| await userEvent.click( | ||||||
| await screen.findByLabelText( 'Fixed background' ) | ||||||
| ); | ||||||
|
|
@@ -257,6 +270,7 @@ describe( 'Cover block', () => { | |||||
| 'is-repeated' | ||||||
| ); | ||||||
| await selectBlock( 'Block: Cover' ); | ||||||
| await openSettingsTabIfAvailable(); | ||||||
| await userEvent.click( | ||||||
| await screen.findByLabelText( 'Repeated background' ) | ||||||
| ); | ||||||
|
|
@@ -271,6 +285,7 @@ describe( 'Cover block', () => { | |||||
| } ); | ||||||
|
|
||||||
| await selectBlock( 'Block: Cover' ); | ||||||
| await openSettingsTabIfAvailable(); | ||||||
| await userEvent.clear( await screen.findByLabelText( 'Left' ) ); | ||||||
| await userEvent.type( screen.getByLabelText( 'Left' ), '100' ); | ||||||
|
|
||||||
|
|
@@ -288,6 +303,7 @@ describe( 'Cover block', () => { | |||||
| } ); | ||||||
|
|
||||||
| await selectBlock( 'Block: Cover' ); | ||||||
| await openSettingsTabIfAvailable(); | ||||||
| await userEvent.type( | ||||||
| await screen.findByLabelText( 'Alternative text' ), | ||||||
| 'Me' | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty much the same as the code on 197-210, so I think it might be a mistake.
The conditions are slightly different (
isImageBackgroundinstead ofuseFeaturedImage), but I think it makes little difference practically.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems other blocks don't display a preview for the featured image here. Is there a reason we're handling this differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talldan Good catch, removed the duplicate fetch and reused the existing one.
@Mamaduka No good reason, it now matches the other blocks. Fixed it.