diff --git a/packages/block-editor/src/components/media-placeholder/index.native.js b/packages/block-editor/src/components/media-placeholder/index.native.js index fd7fc0e1fa625..c1a4def4e0e8c 100644 --- a/packages/block-editor/src/components/media-placeholder/index.native.js +++ b/packages/block-editor/src/components/media-placeholder/index.native.js @@ -15,12 +15,21 @@ import { } from '@wordpress/block-editor'; import { Dashicon } from '@wordpress/components'; import { withPreferredColorScheme } from '@wordpress/compose'; +import { useRef } from '@wordpress/element'; /** * Internal dependencies */ import styles from './styles.scss'; +// remove duplicates after gallery append +const dedupMedia = ( media ) => + uniqWith( + media, + ( media1, media2 ) => + media1.id === media2.id || media1.url === media2.url + ); + function MediaPlaceholder( props ) { const { addToGallery, @@ -35,19 +44,16 @@ function MediaPlaceholder( props ) { value = [], } = props; + // use ref to keep media array current for callbacks during rerenders + const mediaRef = useRef( value ); + mediaRef.current = value; + + // append and deduplicate media array for gallery use case const setMedia = multiple && addToGallery ? ( selected ) => onSelect( - uniqWith( - [ ...value, ...selected ], - ( media1, media2 ) => { - return ( - media1.id === media2.id || - media1.url === media2.url - ); - } - ) + dedupMedia( [ ...mediaRef.current, ...selected ] ) ) : onSelect; diff --git a/packages/block-library/src/gallery/gallery-image.native.js b/packages/block-library/src/gallery/gallery-image.native.js index 337f2bdc4bb73..8cba7acd900dc 100644 --- a/packages/block-library/src/gallery/gallery-image.native.js +++ b/packages/block-library/src/gallery/gallery-image.native.js @@ -145,15 +145,15 @@ class GalleryImage extends Component { } finishMediaUploadWithSuccess( payload ) { - this.props.setAttributes( { - id: payload.mediaServerId, - url: payload.mediaUrl, - } ); - this.setState( { isUploadInProgress: false, didUploadFail: false, } ); + + this.props.setAttributes( { + id: payload.mediaServerId, + url: payload.mediaUrl, + } ); } finishMediaUploadWithFailure() { diff --git a/packages/block-library/src/gallery/gallery.native.js b/packages/block-library/src/gallery/gallery.native.js index 2ff4f0d470798..27dc15df623e9 100644 --- a/packages/block-library/src/gallery/gallery.native.js +++ b/packages/block-library/src/gallery/gallery.native.js @@ -7,6 +7,7 @@ import { isEmpty } from 'lodash'; /** * Internal dependencies */ +import { mediaUploadSync } from 'react-native-gutenberg-bridge'; import GalleryImage from './gallery-image'; import { defaultColumnsNumber } from './shared'; import styles from './gallery-styles.scss'; @@ -17,7 +18,7 @@ import Tiles from './tiles'; */ import { __, sprintf } from '@wordpress/i18n'; import { BlockCaption } from '@wordpress/block-editor'; -import { useState } from '@wordpress/element'; +import { useState, useEffect } from '@wordpress/element'; const TILE_SPACING = 15; @@ -27,6 +28,7 @@ const MAX_DISPLAYED_COLUMNS_NARROW = 2; export const Gallery = ( props ) => { const [ isCaptionSelected, setIsCaptionSelected ] = useState( false ); + useEffect( mediaUploadSync, [] ); const { clientId, @@ -98,7 +100,7 @@ export const Gallery = ( props ) => { key={ img.id || img.url } url={ img.url } alt={ img.alt } - id={ img.id } + id={ parseInt( img.id, 10 ) } // make id an integer explicitly isCropped={ imageCrop } isFirstItem={ index === 0 } isLastItem={ index + 1 === images.length }