Skip to content

Gallery: Simplify inner image handlers - #86767

Merged
Mamaduka merged 1 commit into
trunkfrom
update/gallery-inner-image-handlers
Nov 13, 2026
Merged

Mamaduka merged 1 commit into
trunkfrom
update/gallery-inner-image-handlers

Conversation

@Mamaduka

Copy link
Copy Markdown
Member

What?

Follow up to #86648.

Moves the attributes an added image takes from the gallery into a pure getImageAttributes in shared.js with unit tests, and routes the Link, Open in new tab, Resolution, and Aspect ratio changes through one updateInnerImages helper. 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 scanned imageData with find once per image.

How?

Handlers read getBlocks and getSettings from the static useSelect( blockEditorStore ) getter instead of returning them from mapSelect. Media lookups use the existing mediaById map, including the sort helpers in order-images.js, and updateImages matches existing images with a Set and creates blob URLs in one pass.

Testing Instructions

  1. Open a post or page.
  2. Insert a Gallery block and add three large images from the Media Library.
  3. In the toolbar Link menu, choose each option in turn. Every image's link follows.
  4. In the Settings tab, toggle Open images in new tab, then change Resolution and Aspect ratio. Every image follows each change.
  5. Open Settings options and choose Reset all. Every image returns to Large, Original, and not opening in a new tab.
  6. Set Order by to A → Z. The images sort by title.
  7. Use Add > Upload to add two images. They are added after the existing images, in the order picked.

Testing Instructions for Keyboard

Same as above.

Use of AI Tools

Assisted by Claude.

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>
@Mamaduka Mamaduka added [Type] Code Quality Issues or PRs that relate to code quality [Package] Block library /packages/block-library [Block] Gallery Affects the Gallery Block - used to display groups of images labels Nov 12, 2026
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: -81 B (0%)

Total Size: 8.64 MB

📦 View Changed
Filename Size Change
build/scripts/block-library/index.min.js 427 kB -81 B (-0.02%)

3b7a867 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric d7ddf63 trunk % Change
timeToFirstByte 62.1 ms +2.98% -4.83% 57.85 ms +2.16% -2.85% 7.35%
largestContentfulPaint 100 ms +4% -8% 92 ms +6.52% -8.7% 8.7%
lcpMinusTtfb 36.85 ms +16.55% -14.11% 31.7 ms +20.19% -8.68% 16.25%
wpBeforeTemplate 31.91 ms +5.55% -2.66% 30.15 ms +4.11% -1.53% 5.84%
wpTemplate 25.18 ms +5.96% -4.57% 23.6 ms +4.03% -6.53% 6.69%
wpTotal 58.23 ms +2.64% -5.22% 53.75 ms +2.31% -2.59% 8.33%
wpMemoryUsage 7.72 MB +0% -0% 7.69 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric d7ddf63 trunk % Change
timeToFirstByte 51.2 ms +7.52% -2.54% 49.1 ms +7.64% -3.46% 4.28%
largestContentfulPaint 90 ms +6.67% -2.22% 86 ms +6.98% -2.33% 4.65%
lcpMinusTtfb 38.45 ms +5.33% -2.47% 36.6 ms +3.55% -5.05% 5.05%
wpBeforeTemplate 27.72 ms +6.78% -0.76% 27.56 ms +10.6% -1.42% 0.58%
wpTemplate 19.01 ms +4.89% -3.05% 17.73 ms +9.14% -5.58% 7.22%
wpTotal 47.82 ms +6.29% -2.89% 45.79 ms +6.9% -3.39% 4.43%
wpMemoryUsage 6.41 MB +0% -0% 6.38 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric d7ddf63 trunk % Change
mediaProcessingJpeg 408.8 ms +2.59% -0.54% 408.53 ms +0.31% -0.34% 0.07%
mediaProcessingAvif 6209.64 ms +0.18% -0.58% 6214.08 ms +0.34% -0.4% -0.07%
mediaProcessingJpegToAvif 4295.45 ms +0.19% -0.17% 4284.85 ms +0.1% -0.28% 0.25%

media-upload

Metric d7ddf63 trunk % Change
jpegUploadProcessing 2023.68 ms +3.19% -1.68% 2008.77 ms +0.39% -0.57% 0.74%
pngUploadProcessing 282.29 ms +2.81% -3.63% 288.21 ms +2.71% -2.28% -2.05%
largeJpegUploadProcessing 2000.29 ms +0.43% -0.35% 2004.34 ms +0.66% -1.05% -0.2%
multipleImageUploadProcessing 2091.75 ms +1.42% -1.07% 2134.13 ms +0.46% -0.81% -1.99%

post-editor

Metric d7ddf63 trunk % Change
serverResponse 338.7 ms +4.17% -4.67% 308.56 ms +4.74% -3.45% 9.77%
firstPaint 366.26 ms +13.71% -11.13% 333.92 ms +9.91% -6.07% 9.68%
domContentLoaded 681.82 ms +6.34% -1.45% 650.74 ms +1.67% -1.12% 4.78%
loaded 683 ms +6.33% -1.49% 651.71 ms +1.65% -1.13% 4.8%
firstContentfulPaint 384.11 ms +13.91% -1.26% 369.61 ms +1.16% -0.7% 3.92%
firstBlock 1822.29 ms +4.35% -1.59% 1752.25 ms +0.48% -1.53% 4%
type 15.31 ms +5.75% -13% 16.12 ms +1.24% -10.61% -5.02%
typeWithoutInspector 15.4 ms +1.17% -7.53% 15.73 ms +6.48% -7.25% -2.1%
typeWithTopToolbar 21.88 ms +3.06% -10.01% 20.16 ms +6.3% -9.92% 8.53%
typeContainer 7.17 ms +14.23% -5.72% 7.35 ms +13.06% -4.49% -2.45%
focus 93.1 ms +3.57% -6.4% 99.42 ms +6.32% -3.44% -6.36%
firstFocus 210.25 ms +0% -0% 217.97 ms +0% -0% -3.54%
selectAll 324.86 ms +2.86% -4.37% 334.48 ms +5.46% -0.89% -2.88%
undo 24.98 ms +7.01% -2.52% 24.47 ms +7.89% -4.86% 2.08%
listViewOpen 62 ms +3.92% -2.5% 59.42 ms +9.61% -4.01% 4.34%
inserterOpen 33.25 ms +6.86% -1.5% 35.93 ms +10.8% -7.74% -7.46%
inserterHover 9.67 ms +5.69% -5.27% 10.13 ms +8.09% -5.23% -4.54%
inserterSearch 6.69 ms +8.67% -2.54% 6.91 ms +4.92% -2.6% -3.18%
loadPatterns 525.97 ms +4.11% -4.02% 513.37 ms +1.56% -2.69% 2.45%
wpTotal 330.04 ms +3.89% -4.81% 299.8 ms +4.96% -3.59% 10.09%
wpMemoryUsage 14.78 MB +0% -0% 14.74 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric d7ddf63 trunk % Change
serverResponse 485.25 ms +1.89% -2.02% 475 ms +2.07% -6.87% 2.16%
firstPaint 427.67 ms +7.44% -3.36% 456.39 ms +5.34% -10.76% -6.29%
domContentLoaded 851.39 ms +1.29% -1.23% 851.91 ms +0.92% -0.45% -0.06%
loaded 852.71 ms +1.26% -1.24% 853.15 ms +0.89% -0.48% -0.05%
firstContentfulPaint 473.01 ms +0.54% -1.72% 475.86 ms +1.23% -2.49% -0.6%
firstBlock 2627.04 ms +1.48% -1.56% 2649.3 ms +0.89% -1.03% -0.84%
type 19.06 ms +15.63% -8.08% 20.44 ms +4.21% -12.28% -6.75%
navigate 126.06 ms +3.95% -8.07% 121.8 ms +4.79% -4.21% 3.5%
loadPatterns 1498.13 ms +2.79% -3.79% 1358.78 ms +8.45% -1.8% 10.26%
loadPages 986.26 ms +1.68% -1.33% 994.71 ms +2.6% -2.01% -0.85%
wpTotal 475.63 ms +1.78% -2.08% 464.95 ms +2.17% -6.94% 2.3%
wpMemoryUsage 12.79 MB +0% -0% 12.76 MB +0% -0% 0.24%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

3b7a867 Run

🏁 Flaky tests

Some 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 /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' })
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' }) with timeout 5000ms
  - waiting for getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' })
    14 × locator resolved to <button type="button" tabindex="-1" class="components-button is-small" id="view-list-0-168-primary-action-edit-post">Edit</button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:243:5
Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 6,
+   "offset": 6,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
should reset a named font size using input field in /test/e2e/specs/editor/various/font-size-picker.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:paragraph {"fontSize":"small"} -->
+ <!-- wp:paragraph {"fontSize":"tiny"} -->
- <p class="has-small-font-size">Paragraph with font size reset using input field</p>
+ <p class="has-tiny-font-size">Paragraph with font size reset using input field</p>
  <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/font-size-picker.spec.js:216:6

3b7a867 Run

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smoke-testing well for me, thanks for the thorough testing instructions! This looks like a nice refactor to me 👍

Comment on lines -698 to +623
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 )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really liking all this de-duplication 👍

Comment on lines -473 to +418
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 ) }
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely takes some time to get used to it. Had the same reaction and flagged during local review; now I prefer this version 😅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@Mamaduka
Mamaduka merged commit 44f129e into trunk Nov 13, 2026
120 checks passed
@Mamaduka
Mamaduka deleted the update/gallery-inner-image-handlers branch November 13, 2026 05:47
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 13, 2026
Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Gallery Affects the Gallery Block - used to display groups of images [Package] Block library /packages/block-library [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants