Repository navigation
Gallery: Simplify inner image handlers - #86767
Conversation
Move image attribute helpers to shared.js with tests, update images through one helper, and look up media by id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: -81 B (0%) Total Size: 8.64 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Navigates the list via UP/DOWN arrow keys from action buttons in
|
andrewserong
left a comment
There was a problem hiding this comment.
Smoke-testing well for me, thanks for the thorough testing instructions! This looks like a nice refactor to me 👍
| const changedAttributes = {}; | ||
| const blocks = []; | ||
| getBlock( clientId ).innerBlocks.forEach( ( block ) => { | ||
| blocks.push( block.clientId ); | ||
| const image = block.attributes.id | ||
| ? imageData.find( ( { id } ) => id === block.attributes.id ) | ||
| : null; | ||
| changedAttributes[ block.clientId ] = getImageSizeAttributes( | ||
| image, | ||
| newSizeSlug | ||
| ); | ||
| } ); | ||
| updateBlockAttributes( blocks, changedAttributes, { | ||
| uniqueByBlock: true, | ||
| } ); | ||
| updateInnerImages( ( block, media ) => | ||
| getImageSizeAttributes( media, newSizeSlug ) |
There was a problem hiding this comment.
I'm really liking all this de-duplication 👍
| const imageArray = newFileUploads | ||
| ? Array.from( selectedImages ).map( ( file ) => { | ||
| if ( ! file.url ) { | ||
| return { | ||
| blob: createBlobURL( file ), | ||
| }; | ||
| } | ||
|
|
||
| return file; | ||
| } ) | ||
| : selectedImages; | ||
| // Uploaded files become blobs before validation, so only picked or | ||
| // dropped files can be rejected. | ||
| const imageArray = Array.from( selectedImages, ( file ) => | ||
| file.url || ( ! newFileUploads && ! isValidFileType( file ) ) | ||
| ? file | ||
| : { blob: file.blob || createBlobURL( file ) } | ||
| ); |
There was a problem hiding this comment.
Not at all a blocker, but this block stood out to me as slightly harder to read since it's handling both cases within the one map callback in Array.from, whereas having the ternary on the outside in the trunk case made the branching read a little clearer to me.
No need to change this if you prefer this form, though! The logic looks fine and this is such a minor nit.
There was a problem hiding this comment.
It definitely takes some time to get used to it. Had the same reaction and flagged during local review; now I prefer this version 😅
There was a problem hiding this comment.
Had the same reaction and flagged during local review; now I prefer this version 😅
That makes sense! I think often I run into the mistake of just reading the diff rather than the final state of the file which often looks better than when I'm just focusing on what changed.
Anyhow, thanks again for all the work here, Gallery is such an important block, good to tidy things up!
Move image attribute helpers to shared.js with tests, update images through one helper, and look up media by id. Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
What?
Follow up to #86648.
Moves the attributes an added image takes from the gallery into a pure
getImageAttributesinshared.jswith unit tests, and routes the Link, Open in new tab, Resolution, and Aspect ratio changes through oneupdateInnerImageshelper. No user-visible change.Why?
Review on #86648 asked to move testable pieces out of
edit.jsx(about 1,270 lines). Each of those handlers repeated the same loop over the inner blocks, and three media lookups scannedimageDatawithfindonce per image.How?
Handlers read
getBlocksandgetSettingsfrom the staticuseSelect( blockEditorStore )getter instead of returning them frommapSelect. Media lookups use the existingmediaByIdmap, including the sort helpers inorder-images.js, andupdateImagesmatches existing images with aSetand creates blob URLs in one pass.Testing Instructions
Testing Instructions for Keyboard
Same as above.
Use of AI Tools
Assisted by Claude.