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

Update: Improve content locking on media & text block. #44036

Merged
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
74 changes: 45 additions & 29 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function attributesFromMedia( {
};
}

function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
function MediaTextEdit( { attributes, isSelected, setAttributes, clientId } ) {
const {
focalPoint,
href,
Expand All @@ -157,12 +157,24 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
} = attributes;
const mediaSizeSlug = attributes.mediaSizeSlug || DEFAULT_MEDIA_SIZE_SLUG;

const image = useSelect(
( select ) =>
mediaId && isSelected
? select( coreStore ).getMedia( mediaId, { context: 'view' } )
: null,
[ isSelected, mediaId ]
const { imageSizes, image, isContentLocked } = useSelect(
( select ) => {
const { __unstableGetContentLockingParent, getSettings } =
select( blockEditorStore );
return {
isContentLocked:
!! __unstableGetContentLockingParent( clientId ),
image:
mediaId && isSelected
? select( coreStore ).getMedia( mediaId, {
context: 'view',
} )
: null,
imageSizes: getSettings()?.imageSizes,
};
},

[ isSelected, mediaId, clientId ]
);

const refMediaContainer = useRef();
Expand Down Expand Up @@ -213,10 +225,6 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
setAttributes( { verticalAlignment: alignment } );
};

const imageSizes = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();
return settings?.imageSizes;
}, [] );
const imageSizeOptions = map(
filter( imageSizes, ( { slug } ) =>
getImageSourceUrlBySizeSlug( image, slug )
Expand Down Expand Up @@ -322,24 +330,31 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
<>
<InspectorControls>{ mediaTextGeneralSettings }</InspectorControls>
<BlockControls group="block">
<BlockVerticalAlignmentControl
onChange={ onVerticalAlignmentChange }
value={ verticalAlignment }
/>
<ToolbarButton
icon={ pullLeft }
title={ __( 'Show media on left' ) }
isActive={ mediaPosition === 'left' }
onClick={ () => setAttributes( { mediaPosition: 'left' } ) }
/>
<ToolbarButton
icon={ pullRight }
title={ __( 'Show media on right' ) }
isActive={ mediaPosition === 'right' }
onClick={ () =>
setAttributes( { mediaPosition: 'right' } )
}
/>
{ ! isContentLocked && (
<>
<BlockVerticalAlignmentControl
onChange={ onVerticalAlignmentChange }
value={ verticalAlignment }
/>
<ToolbarButton
icon={ pullLeft }
title={ __( 'Show media on left' ) }
isActive={ mediaPosition === 'left' }
onClick={ () =>
setAttributes( { mediaPosition: 'left' } )
}
/>
<ToolbarButton
icon={ pullRight }
title={ __( 'Show media on right' ) }
isActive={ mediaPosition === 'right' }
onClick={ () =>
setAttributes( { mediaPosition: 'right' } )
}
/>
</>
) }

{ mediaType === 'image' && (
<ImageURLInputUI
url={ href || '' }
Expand Down Expand Up @@ -373,6 +388,7 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
mediaType,
mediaUrl,
mediaWidth,
isContentLocked,
} }
/>
{ mediaPosition !== 'right' && <div { ...innerBlocksProps } /> }
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/media-text/media-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function MediaContainer( props, ref ) {
mediaWidth,
onSelectMedia,
onWidthChange,
isContentLocked,
} = props;

const isTemporaryMedia = ! mediaId && isBlobURL( mediaUrl );
Expand All @@ -131,8 +132,8 @@ function MediaContainer( props, ref ) {
commitWidthChange( parseInt( elt.style.width ) );
};
const enablePositions = {
right: mediaPosition === 'left',
left: mediaPosition === 'right',
right: ! isContentLocked && mediaPosition === 'left',
left: ! isContentLocked && mediaPosition === 'right',
};

const backgroundStyles =
Expand Down