22 * External dependencies
33 */
44import classnames from 'classnames' ;
5- import { isEmpty , concat , differenceBy , some , find } from 'lodash' ;
5+ import { isEmpty , concat , find } from 'lodash' ;
66
77/**
88 * WordPress dependencies
@@ -23,7 +23,6 @@ import {
2323 InspectorControls ,
2424 useBlockProps ,
2525} from '@wordpress/block-editor' ;
26- import { store as coreStore } from '@wordpress/core-data' ;
2726import { Platform , useEffect , useMemo } from '@wordpress/element' ;
2827import { __ , sprintf } from '@wordpress/i18n' ;
2928import { useSelect , useDispatch } from '@wordpress/data' ;
@@ -51,6 +50,8 @@ import {
5150} from './constants' ;
5251import useImageSizes from './use-image-sizes' ;
5352import useShortCodeTransform from './use-short-code-transform' ;
53+ import useGetNewImages from './use-get-new-images' ;
54+ import useGetMedia from './use-get-media' ;
5455
5556const MAX_COLUMNS = 8 ;
5657const linkOptions = [
@@ -122,35 +123,27 @@ function GalleryEdit( props ) {
122123 const images = useMemo (
123124 ( ) =>
124125 innerBlockImages ?. map ( ( block ) => ( {
126+ clientId : block . clientId ,
125127 id : block . attributes . id ,
126128 url : block . attributes . url ,
127129 attributes : block . attributes ,
130+ fromSavedContent : Boolean ( block . originalContent ) ,
128131 } ) ) ,
129132 [ innerBlockImages ]
130133 ) ;
131134
132- const imageData = useSelect (
133- ( select ) => {
134- if (
135- ! innerBlockImages ?. length ||
136- some (
137- innerBlockImages ,
138- ( imageBlock ) => ! imageBlock . attributes . id
139- )
140- ) {
141- return imageData ;
142- }
135+ const imageData = useGetMedia ( innerBlockImages ) ;
143136
144- const imageIds = innerBlockImages . map (
145- ( imageBlock ) => imageBlock . attributes . id
146- ) ;
137+ const newImages = useGetNewImages ( images , imageData ) ;
147138
148- const getMediaItems = select ( coreStore ) . getMediaItems ;
149-
150- return getMediaItems ( { include : imageIds } ) ;
151- } ,
152- [ innerBlockImages ]
153- ) ;
139+ useEffect ( ( ) => {
140+ newImages ?. forEach ( ( newImage ) => {
141+ updateBlockAttributes ( newImage . clientId , {
142+ ...buildImageAttributes ( false , newImage ) ,
143+ id : newImage . id ,
144+ } ) ;
145+ } ) ;
146+ } , [ newImages ] ) ;
154147
155148 const shortCodeImages = useShortCodeTransform ( shortCodeTransforms ) ;
156149
@@ -241,17 +234,22 @@ function GalleryEdit( props ) {
241234 const existingImageBlocks = ! newFileUploads
242235 ? innerBlockImages . filter ( ( block ) =>
243236 processedImages . find (
244- ( img ) => img . url === block . attributes . url
237+ ( img ) => img . id === block . attributes . id
245238 )
246239 )
247240 : innerBlockImages ;
248241
249- const newImages = differenceBy ( processedImages , images , 'url' ) ;
242+ const newImageList = processedImages . filter (
243+ ( img ) =>
244+ ! existingImageBlocks . find (
245+ ( existingImg ) => img . id === existingImg . attributes . id
246+ )
247+ ) ;
250248
251- const newBlocks = newImages . map ( ( image ) => {
249+ const newBlocks = newImageList . map ( ( image ) => {
252250 return createBlock ( 'core/image' , {
253- ...buildImageAttributes ( false , image ) ,
254251 id : image . id ,
252+ url : image . url ,
255253 } ) ;
256254 } ) ;
257255
0 commit comments