From 2ef8762270ac8a68dc40afa4fa31b0c5ae5bc078 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 13 Sep 2022 17:52:52 +0100 Subject: [PATCH] Update: Improve content locking on media & text block. (#44036) --- packages/block-library/src/media-text/edit.js | 74 +++++++++++-------- .../src/media-text/media-container.js | 5 +- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/packages/block-library/src/media-text/edit.js b/packages/block-library/src/media-text/edit.js index 41d33ab14e4e5a..5a88830d385d18 100644 --- a/packages/block-library/src/media-text/edit.js +++ b/packages/block-library/src/media-text/edit.js @@ -137,7 +137,7 @@ function attributesFromMedia( { }; } -function MediaTextEdit( { attributes, isSelected, setAttributes } ) { +function MediaTextEdit( { attributes, isSelected, setAttributes, clientId } ) { const { focalPoint, href, @@ -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(); @@ -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 ) @@ -322,24 +330,31 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) { <> { mediaTextGeneralSettings } - - setAttributes( { mediaPosition: 'left' } ) } - /> - - setAttributes( { mediaPosition: 'right' } ) - } - /> + { ! isContentLocked && ( + <> + + + setAttributes( { mediaPosition: 'left' } ) + } + /> + + setAttributes( { mediaPosition: 'right' } ) + } + /> + + ) } + { mediaType === 'image' && ( { mediaPosition !== 'right' &&
} diff --git a/packages/block-library/src/media-text/media-container.js b/packages/block-library/src/media-text/media-container.js index fb209cff01035f..f2bdb475bae345 100644 --- a/packages/block-library/src/media-text/media-container.js +++ b/packages/block-library/src/media-text/media-container.js @@ -113,6 +113,7 @@ function MediaContainer( props, ref ) { mediaWidth, onSelectMedia, onWidthChange, + isContentLocked, } = props; const isTemporaryMedia = ! mediaId && isBlobURL( mediaUrl ); @@ -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 =