Skip to content
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

Package blocks: Introduce a utility for getting the block image #68910

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __experimentalGetBlockImage as getBlockImage } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -74,8 +75,26 @@ export default function useListViewImages( { clientId, isExpanded } ) {
},
[ clientId ]
);

const images = useMemo( () => {
const blockImageURL = getBlockImage(
block.name,
block.attributes,
'list-view'
);
if ( blockImageURL ) {
return [
{
url: blockImageURL,
alt:
block.attributes?.alt ||
block.attributes?.mediaAlt ||
'',
clientId: block.clientId,
},
];
}

// Fallback to custom logic for core/image or core/gallery.
return getImagesFromBlock( block, isExpanded );
}, [ block, isExpanded ] );

Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ export const settings = {
},
],
},
__experimentalImage( attributes, { context } ) {
if (
attributes.backgroundType === 'image' &&
context === 'list-view' &&
attributes?.url
) {
return attributes?.url;
}
},
transforms,
save,
edit,
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/media-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export const settings = {
},
],
},
__experimentalImage( attributes, { context } ) {
if (
attributes.mediaType === 'image' &&
context === 'list-view' &&
attributes?.mediaUrl
) {
return attributes?.mediaUrl;
}
},
transforms,
edit,
save,
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export {
__experimentalSanitizeBlockAttributes,
getBlockAttributesNamesByRole,
__experimentalGetBlockAttributesNamesByRole,
getBlockImage as __experimentalGetBlockImage,
} from './utils';

// Templates are, in a general sense, a basic collection of block nodes with any
Expand Down
19 changes: 19 additions & 0 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,22 @@ export function omit( object, keys ) {
Object.entries( object ).filter( ( [ key ] ) => ! keys.includes( key ) )
);
}

/**
* Return the block image URL.
*
* @param {string|Object} blockTypeOrName The block type or name.
* @param {Object} attributes The block's attributes.
* @param {string} context The context in which the block is being displayed.
*
* @return {string|null} The block image URL or null.
*/
export function getBlockImage( blockTypeOrName, attributes, context ) {
const blockType = normalizeBlockType( blockTypeOrName );
const { __experimentalImage: getImage } = blockType;
const url = getImage && getImage( attributes, { context } );
if ( url ) {
return url;
}
return null;
}
Loading