-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the setting of the default attributes on new blocks to a single …
…useEffect (#29328) Co-authored-by: Glen Davies <[email protected]>
- Loading branch information
1 parent
a111532
commit 6370194
Showing
3 changed files
with
135 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { some } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
export default function useGetMedia( innerBlockImages ) { | ||
const [ currentImageMedia, setCurrentImageMedia ] = useState( [] ); | ||
|
||
const imageMedia = useSelect( | ||
( select ) => { | ||
if ( | ||
! innerBlockImages?.length || | ||
some( | ||
innerBlockImages, | ||
( imageBlock ) => ! imageBlock.attributes.id | ||
) | ||
) { | ||
return currentImageMedia; | ||
} | ||
|
||
const imageIds = innerBlockImages.map( | ||
( imageBlock ) => imageBlock.attributes.id | ||
); | ||
|
||
if ( imageIds.length === 0 ) { | ||
return currentImageMedia; | ||
} | ||
const getMedia = select( coreStore ).getMedia; | ||
const newImageMedia = imageIds.map( ( img ) => { | ||
return getMedia( img ); | ||
} ); | ||
|
||
if ( newImageMedia.some( ( img ) => ! img ) ) { | ||
return currentImageMedia; | ||
} | ||
|
||
return newImageMedia; | ||
}, | ||
[ innerBlockImages ] | ||
); | ||
|
||
if ( imageMedia?.length !== currentImageMedia.length ) { | ||
setCurrentImageMedia( imageMedia ); | ||
return imageMedia; | ||
} | ||
|
||
return currentImageMedia; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useState } from '@wordpress/element'; | ||
|
||
export default function useGetNewImages( images, imageData ) { | ||
const [ currentImages, setCurrentImages ] = useState( [] ); | ||
|
||
return useMemo( () => getNewImages(), [ images, imageData ] ); | ||
|
||
function getNewImages() { | ||
let imagesUpdated = false; | ||
|
||
// First lets check if any images have been deleted. | ||
const newCurrentImages = currentImages.filter( ( currentImg ) => | ||
images.find( ( img ) => { | ||
return currentImg.clientId === img.clientId; | ||
} ) | ||
); | ||
|
||
if ( newCurrentImages.length < currentImages.length ) { | ||
imagesUpdated = true; | ||
} | ||
|
||
// Now lets see if we have any images hydrated from saved content and if so | ||
// add them to currentImages state. | ||
images.forEach( ( image ) => { | ||
if ( | ||
image.fromSavedContent && | ||
! newCurrentImages.find( | ||
( currentImage ) => currentImage.id === image.id | ||
) | ||
) { | ||
imagesUpdated = true; | ||
newCurrentImages.push( image ); | ||
} | ||
} ); | ||
|
||
// Now check for any new images that have been added to InnerBlocks and for which | ||
// we have the imageData we need for setting default block attributes. | ||
const newImages = images.filter( | ||
( image ) => | ||
! newCurrentImages.find( | ||
( currentImage ) => image.id && currentImage.id === image.id | ||
) && | ||
imageData?.find( ( img ) => img.id === image.id ) && | ||
! image.fromSavedConent | ||
); | ||
|
||
if ( imagesUpdated || newImages?.length > 0 ) { | ||
setCurrentImages( [ ...newCurrentImages, ...newImages ] ); | ||
} | ||
|
||
return newImages.length > 0 ? newImages : null; | ||
} | ||
} |