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

Image/Gallery block: remove use of unstableOnFocus #31796

Merged
merged 4 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function ImageEdit( {
insertBlocksAfter,
noticeOperations,
onReplace,
clientId,
} ) {
const {
url = '',
Expand Down Expand Up @@ -300,6 +301,7 @@ export function ImageEdit( {
onSelectURL={ onSelectURL }
onUploadError={ onUploadError }
containerRef={ ref }
clientId={ clientId }
/>
) }
{ ! url && (
Expand Down
32 changes: 7 additions & 25 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default function Image( {
onSelectURL,
onUploadError,
containerRef,
clientId,
} ) {
const captionRef = useRef();
const prevUrl = usePrevious( url );
Expand All @@ -107,8 +108,8 @@ export default function Image( {
multiImageSelection:
multiSelectedClientIds.length &&
multiSelectedClientIds.every(
( clientId ) =>
getBlockName( clientId ) === 'core/image'
( _clientId ) =>
getBlockName( _clientId ) === 'core/image'
),
};
},
Expand All @@ -125,12 +126,13 @@ export default function Image( {
] );
}
);
const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );
const { replaceBlocks, toggleSelection, selectionChange } = useDispatch(
blockEditorStore
);
const { createErrorNotice, createSuccessNotice } = useDispatch(
noticesStore
);
const isLargeViewport = useViewportMatch( 'medium' );
const [ captionFocused, setCaptionFocused ] = useState( false );
const isWideAligned = includes( [ 'wide', 'full' ], align );
const [ { naturalWidth, naturalHeight }, setNaturalSize ] = useState( {} );
const [ isEditingImage, setIsEditingImage ] = useState( false );
Expand All @@ -147,12 +149,6 @@ export default function Image( {
// Check if the cover block is registered.
const coverBlockExists = !! getBlockType( 'core/cover' );

useEffect( () => {
if ( ! isSelected ) {
setCaptionFocused( false );
}
}, [ isSelected ] );

// If an image is externally hosted, try to fetch the image data. This may
// fail if the image host doesn't allow CORS with the domain. If it works,
// we can enable a button in the toolbar to upload the image.
Expand Down Expand Up @@ -203,18 +199,6 @@ export default function Image( {
setAttributes( { title: value } );
}

function onFocusCaption() {
if ( ! captionFocused ) {
setCaptionFocused( true );
}
}

function onImageClick() {
if ( captionFocused ) {
setCaptionFocused( false );
}
}

function updateAlt( newAlt ) {
setAttributes( { alt: newAlt } );
}
Expand Down Expand Up @@ -417,7 +401,7 @@ export default function Image( {
<img
src={ temporaryURL || url }
alt={ defaultedAlt }
onClick={ onImageClick }
onClick={ () => selectionChange( clientId ) }
ellatrix marked this conversation as resolved.
Show resolved Hide resolved
onError={ () => onImageError() }
onLoad={ ( event ) => {
setNaturalSize(
Expand Down Expand Up @@ -564,11 +548,9 @@ export default function Image( {
aria-label={ __( 'Image caption text' ) }
placeholder={ __( 'Add caption' ) }
value={ caption }
unstableOnFocus={ onFocusCaption }
onChange={ ( value ) =>
setAttributes( { caption: value } )
}
isSelected={ captionFocused }
inlineToolbar
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter( createBlock( 'core/paragraph' ) )
Expand Down