Skip to content

Commit

Permalink
Update: Improve content locking on media & text block. (#44036)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta authored Sep 13, 2022
1 parent e435711 commit 2ef8762
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
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

0 comments on commit 2ef8762

Please sign in to comment.