Skip to content

Commit

Permalink
Respect sort order from Media Library (#30070)
Browse files Browse the repository at this point in the history
Co-authored-by: Glen Davies <glen.davies@a8c.com>
  • Loading branch information
glendaviesnz and Glen Davies committed Mar 22, 2021
1 parent 2507999 commit 4e57fb3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ function GalleryEdit( props ) {
return file;
} );

// Because we are reusing existing innerImage blocks any reordering
// done in the media libary will be lost so we need to reapply that ordering
// once the new image blocks are merged in with existing.
const newOrderMap = processedImages.reduce(
( result, image, index ) => (
( result[ image.id ] = index ), result
),
{}
);

const existingImageBlocks = ! newFileUploads
? innerBlockImages.filter( ( block ) =>
processedImages.find(
Expand All @@ -255,7 +265,11 @@ function GalleryEdit( props ) {

replaceInnerBlocks(
clientId,
concat( existingImageBlocks, newBlocks )
concat( existingImageBlocks, newBlocks ).sort(
( a, b ) =>
newOrderMap[ a.attributes.id ] -
newOrderMap[ b.attributes.id ]
)
);
}

Expand Down

0 comments on commit 4e57fb3

Please sign in to comment.