Skip to content

Image Editor: Describe the edit as one record, not a list of operations - #86630

Merged
ramonjd merged 12 commits into
trunkfrom
refactor/image-editor-edit-type-name
Nov 11, 2026
Merged

ramonjd merged 12 commits into
trunkfrom
refactor/image-editor-edit-type-name

Conversation

@ramonjd

@ramonjd ramonjd commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

What?

mapEditToOriginal becomes getImageEdit, returning one record instead of a list of tagged operations:

interface ImageEdit {
	flip?: Flip;
	rotation?: number;
	crop?: NormalizedRect;
}

Apply the parts in that order. Absent means nothing to do. null means no image has loaded.

Why?

The list did not represent an edit "state", but a description of edits.

ImageEditorState holds one flip, one rotation and one cropRect; there is no such thing as two crops, or rotate-crop-rotate, and the order is fixed in code rather than chosen by the caller.

The list was the REST /edit endpoint's payload shape. So we were importing backend logic unnecessarily.

The main goal here is for reading and writing to share a type, and to ensure the API makes sense for both programmatic and procedural transforms.

After this PR, the next step is applying an edit from code, for an agent cropping on someone's behalf. That is applyImageEdit( state, edit ) against this same ImageEdit, so changing one part of an edit is:

applyImageEdit( state, { ...getImageEdit( state ), rotation: 90 } );

With a list, that is a find-and-splice, and "apply this list" first has to define what a second crop in it means

Testing Instructions

There should be no regressions.

Insert an image block, open the media editor, crop and rotate, then save. The saved file should be what you framed.

2026-11-11.15.31.30.mp4

Check Image Editor > Canvas/Debug

Screenshot 2026-11-11 at 3 32 25 pm

"Original" in these names only ever meant the image loaded in
state.image. The name could not say that on its own, so the README, the
recipes and the function's own doc comment each carried a note
explaining it: "here, 'original' means the image loaded into this
editor, even if that file was edited before."

Rename the pair for what they are. getImageEdits returns the edits, and
ImageEdit is one of them. The package's own prose already called them
edits, so the names now match the words around them, and the three
explanatory notes are gone.

Nothing published changes: the package is 0.1.0-prerelease and neither
name has shipped. The type is also about to be the input to applying an
edit from code, not only the output of reading one, which "original"
would have read strangely for.
@github-actions

github-actions Bot commented Nov 11, 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: ramonjd <ramonopoly@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: 0 B

Total Size: 8.63 MB

ff8350d Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric e66fee7 trunk % Change
timeToFirstByte 44.2 ms +5.77% -6.56% 43.8 ms +4.22% -6.05% 0.91%
largestContentfulPaint 68 ms +5.88% -5.88% 68 ms +5.88% -2.94% 0%
lcpMinusTtfb 23.5 ms +14.89% -5.74% 24 ms +30% -5.63% -2.08%
wpBeforeTemplate 22.73 ms +11.39% -3.61% 22.78 ms +9.96% -3.47% -0.22%
wpTemplate 17.12 ms +8.53% -7.54% 17.3 ms +3.18% -7.05% -1.04%
wpTotal 40.78 ms +5.89% -6.28% 40.56 ms +4.41% -6.19% 0.54%
wpMemoryUsage 7.72 MB +0% -0% 7.68 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric e66fee7 trunk % Change
timeToFirstByte 35.7 ms +7.56% -3.08% 35.2 ms +9.37% -1.7% 1.42%
largestContentfulPaint 64 ms +6.25% -0% 64 ms +6.25% -0% 0%
lcpMinusTtfb 29 ms +2.93% -3.79% 28.8 ms +2.08% -5.38% 0.69%
wpBeforeTemplate 19.56 ms +12.93% -2.51% 19.31 ms +6.99% -0.57% 1.29%
wpTemplate 12.99 ms +3.93% -4.54% 13 ms +5.46% -1.77% -0.08%
wpTotal 32.91 ms +8.54% -3.52% 32.37 ms +9.21% -1.17% 1.67%
wpMemoryUsage 6.41 MB +0% -0% 6.37 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric e66fee7 trunk % Change
mediaProcessingJpeg 325.29 ms +0.42% -0.58% 323.55 ms +0.81% -0.28% 0.54%
mediaProcessingAvif 4821.36 ms +0.53% -0.01% 4860.82 ms +0.47% -0.19% -0.81%
mediaProcessingJpegToAvif 3375.34 ms +0.25% -0.3% 3393.73 ms +0.21% -0.29% -0.54%

media-upload

Metric e66fee7 trunk % Change
jpegUploadProcessing 1478.39 ms +0.65% -1.52% 1454.55 ms +0.94% -0.41% 1.64%
pngUploadProcessing 237.55 ms +1.57% -1.24% 229.86 ms +5.01% -3.78% 3.35%
largeJpegUploadProcessing 1448.13 ms +0.33% -0.83% 1449.31 ms +0.52% -0.82% -0.08%
multipleImageUploadProcessing 1499.58 ms +1.56% -0.28% 1491.45 ms +1.36% -0.97% 0.55%

post-editor

Metric e66fee7 trunk % Change
serverResponse 268.5 ms +0.37% -10.69% 271.1 ms +3.47% -12% -0.96%
firstPaint 261.82 ms +21.96% -4.21% 280.48 ms +7.06% -10.06% -6.65%
domContentLoaded 529.88 ms +2.7% -3.49% 532.33 ms +2.08% -1.34% -0.46%
loaded 530.7 ms +2.69% -3.5% 533.09 ms +2.06% -1.31% -0.45%
firstContentfulPaint 307.21 ms +3.94% -5.74% 310.42 ms +0.71% -3.27% -1.03%
firstBlock 1378.65 ms +10.03% -1.56% 1414.7 ms +2.94% -2.13% -2.55%
type 13.28 ms +1.51% -6.02% 12.35 ms +11.9% -3.08% 7.53%
typeWithoutInspector 15.24 ms +3.87% -15.75% 12.34 ms +3.97% -5.02% 23.5%
typeWithTopToolbar 18.32 ms +3.22% -4.2% 18.31 ms +15.4% -20.43% 0.05%
typeContainer 6.87 ms +14.12% -12.81% 6.55 ms +9.47% -12.37% 4.89%
focus 79.78 ms +5.09% -0.68% 73.84 ms +6.46% -5.86% 8.04%
firstFocus 186.02 ms +0% -0% 174.18 ms +0% -0% 6.8%
selectAll 254.65 ms +4.35% -3.39% 241.86 ms +1.48% -1.41% 5.29%
undo 22.27 ms +7.86% -7.09% 22.62 ms +4.91% -7.91% -1.55%
listViewOpen 50.58 ms +8.86% -11.98% 48 ms +5.02% -10.33% 5.37%
inserterOpen 27.7 ms +7.26% -6.28% 27.5 ms +9.42% -4.55% 0.73%
inserterHover 8.75 ms +11.2% -5.83% 8.69 ms +8.29% -6.33% 0.69%
inserterSearch 6.58 ms +2.74% -8.05% 6.46 ms +3.72% -3.56% 1.86%
loadPatterns 430.74 ms +5.8% -2.36% 452.57 ms +1.15% -4.99% -4.82%
wpTotal 261.63 ms +0.32% -10.95% 264.26 ms +3.08% -12.37% -1%
wpMemoryUsage 14.77 MB +0% -0% 14.74 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric e66fee7 trunk % Change
serverResponse 398.4 ms +1.59% -6.87% 396.37 ms +1.11% -6.71% 0.51%
firstPaint 370.52 ms +17.66% -5.73% 376.27 ms +15.35% -5.01% -1.53%
domContentLoaded 789.58 ms +1.58% -1.68% 768.33 ms +1.05% -0.33% 2.77%
loaded 790.68 ms +1.58% -1.69% 769.33 ms +1.05% -0.34% 2.78%
firstContentfulPaint 437.71 ms +1.52% -2.41% 428.49 ms +3.42% -2% 2.15%
firstBlock 2371.17 ms +7.21% -2.57% 2306.14 ms +0.42% -1.17% 2.82%
type 15.99 ms +5.82% -9.51% 15.64 ms +4.22% -12.66% 2.24%
navigate 108.54 ms +1.51% -2.68% 104.91 ms +10.34% -6.71% 3.46%
loadPatterns 1248.18 ms +9% -6.95% 1244.41 ms +8.79% -8.66% 0.3%
loadPages 911.06 ms +2.79% -1.25% 912.62 ms +4.54% -2.61% -0.17%
wpTotal 389.27 ms +1.62% -6.97% 387.34 ms +0.95% -6.84% 0.5%
wpMemoryUsage 12.79 MB +0% -0% 12.75 MB +0% -0% 0.27%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

ff8350d 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.

As a user I want to be able to create a navigation overlay for a specific navigation block in /test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js, passed after 1 failed attempt.
TimeoutError: locator.click: Timeout 10000ms exceeded.
Call log:
  - waiting for getByRole('region', { name: /(Editor publish|Save panel)/ }).getByRole('button', { name: 'Save', exact: true })

    at Editor.saveSiteEditorEntities (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/editor/site-editor.ts:48:5)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js:91:4
moves selected text within a paragraph in /test/e2e/specs/editor/various/draggable-blocks.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "second First third",
+       "content": "First second third",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/draggable-blocks.spec.js:1209:41
should select with shift + click in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
-   1,
    2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:380:5

ff8350d Run

@github-actions github-actions Bot added the [Package] Image Editor Related to packages/image-editor label Nov 11, 2026
getImageEdits threw away more than it fixed. mapEditToOriginal said three
useful things: "map" that the result is derived rather than stored,
"edit" singular because a person makes one edit, and "to ..." that the
steps have a target. Only "original" was wrong, and only because it
needed a note in three places to say it meant the image in state.image.

So change that word and nothing else: mapEditToImage. The footnotes stay
gone, and the rest of the sentence survives.

The type becomes ImageEditStep. One flip, rotate or crop is a step of the
edit, not an edit of its own, so an array of them is one edit in several
parts rather than several edits. That reads correctly at the call site
this type is about to gain, where code hands the steps back in.
@ramonjd ramonjd changed the title Image Editor: Rename mapEditToOriginal to getImageEdits Image Editor: Rename mapEditToOriginal to mapEditToImage Nov 11, 2026
The editor holds one mirror, one rotation and one crop. Never two crops,
never rotate-crop-rotate. And the order they apply in is fixed in the
code, not chosen by whoever calls this.

So the list of tagged operations expressed nothing the state could hold.
It was the REST /edit endpoint's payload shape, exported as though it
were the package's own vocabulary. That is also why the type was hard to
name: "step" and "operation" are words for elements of a list, and there
was no list to have elements of.

getImageEdit now returns one ImageEdit with an optional flip, rotation
and crop, applied in that order, each absent when it changes nothing.
No image loaded returns null, which the empty array used to conflate
with "nothing to apply".

Two of the three callers got shorter: get-output-size reads two fields
instead of scanning for tagged entries, and the renderer asks whether
there is a crop instead of searching for one. The media editor builds
the modifiers array itself, which is the one place an ordered list of
tagged operations is the right shape, because that is the endpoint's
contract rather than ours.
@ramonjd ramonjd changed the title Image Editor: Rename mapEditToOriginal to mapEditToImage Image Editor: Describe the edit as one record, not a list of operations Nov 11, 2026
The type doc led with "an edit to apply", which reads as a procedure. It
is the edit itself: how the image is edited. Applying it is what a
consumer then does with it, which the next sentence still covers.
@ramonjd ramonjd self-assigned this Nov 11, 2026
@ramonjd ramonjd added the [Type] Code Quality Issues or PRs that relate to code quality label Nov 11, 2026
@ramonjd
ramonjd requested review from andrewserong and a balanced review from Copilot November 11, 2026 04:23

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ramonjd

ramonjd commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Sorry, don't mean to overburden you @andrewserong

This one would be good to get in before the next GB release. It corrects a weird public function name mapEditToOriginal, and more closely couples the public read and write state functions.

Comment thread packages/image-editor/src/index.ts Resolved
Comment thread packages/image-editor/CHANGELOG.md Resolved
getImageEdit returns one record, so the data no longer carries the order
to apply its parts in. getCropModifiers writes that order by hand, and
the one test that checks what it sends covered a rotation alone.

That test now flips, rotates and crops before saving, and expects the
flip, the rotation and the crop in that order, with the crop in
percentages of the rotated image.
The type's comment argued with the list it replaced, which nobody reading
the merged file has seen. It now says what the edit is, and each field
carries its own note, so the note shows when hovering the field.

Two of those notes are corrections. `rotation` is measured after the
flip, so with one axis flipped it is 360 minus `state.rotation`. `crop`
is a fraction of the rotated image, but `NormalizedRect` said every crop
area was a fraction of the view. That sentence now sits on
`state.cropRect`, the field it describes.

A crop is left out when it is within 0.1% of the whole image, which is
not "a rounding error" on a large one. The comments say "nearly full"
again.

Also here: "flip" for the thing the new text called a "mirror", the last
wording from the old name ("the mapper", "the edits'"), one note about
previously edited files instead of three, a one-sentence changelog
entry, and no comment above the export, where it restated the line
below it.
A pass for anything this branch adds that it can do without.

- The crop note no longer points at `state.cropRect`. That field and
  `NormalizedRect` each say what they measure against.
- The function comment keeps the one rule the type cannot show, that a
  crop within `CROP_TOLERANCE` of the whole image is left out. The flip
  and rotation cases restated "absent when it changes nothing".
- `readEdit` in the parity test loses a comment that described its three
  lines.
- The restore test's title is one line again, and so is its comment.
- The recipe said a locked ratio "can produce one". It now says a crop,
  as the README does.
The field is `flip`, so its note saying "mirror along each axis" used a
second word for the same thing. With one word the note only repeats the
field's name and type, so it goes.

The note on `state.cropRect` said the image was "turned by its
rotation". It now says the image is at that rotation.

@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.

I gotta dash off so just adding a tentative approval in case you need to get this in quickly!

It's smoke-testing well for me with crops, rotation, and flips (tested with just one, just two, and all three) 👍

I like the tidiness of an object for getImageEdit. I suppose the downside is that the consumer (e.g. use-save-media-editor.ts) needs to know which order to apply these operations in, so it possibly shifts a tiny bit more work to the consumer in this case than prior to this PR.

I didn't have time to verify some Claude claims when running through this PR quickly. The main thing to double-check is whether there's anywhere else that we're referring to the old behaviour, and do a pass on the comments.

But, all up this sounds good to me! Happy to give it a closer look tomorrow if you'd prefer 🙂

The second sentence said the shape and the crop's units differ, which the
lines below it and the `Modifier` type already show. The first now says
what the order means for this code: send the parts in the edit's order.
The note on `state.cropRect` said the view "is the image at its rotation
snapped to a quarter turn, not the image itself". That holds only at
zoom 1 with no pan. The view has that image's shape, and the image is
zoomed and panned under it.

The comment on `getImageEdit` said the edit is described against "the
file". The package uses "file" for the saved file, which can differ from
the image in `state.image`. It now says "the image".

The same comment said a nearly full crop "is left out" where the others
say `getImageEdit` omits it.
@ramonjd

ramonjd commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Thanks for testing @andrewserong

needs to know which order to apply these operations in, so it possibly shifts a tiny bit more work to the consumer in this case than prior to this PR

You're right about the order. The consumer has to know about it.

For now I've added a test that fails if the three ifs in use-save-media-editor.ts are shuffled.

For a follow up: a helper that hands the parts back in order. It should belong in the image-editor package because it's not optional.

function mapImageEdit< T >(
	edit: ImageEdit,
	to: {
		flip: ( flip: Flip ) => T;
		rotation: ( degrees: number ) => T;
		crop: ( rect: NormalizedRect ) => T;
	}
): T[];

const modifiers = mapImageEdit( edit, {
	flip: ( flip ) => ( { type: 'flip', args: { flip } } ),
	rotation: ( angle ) => ( { type: 'rotate', args: { angle } } ),
	crop: ( { x, y, width, height } ) => ( {
		type: 'crop',
		args: { left: x * 100, top: y * 100, width: width * 100, height: height * 100 },
	} ),
} );

The main thing I wanted was for the API to deal with both of these situations using the same shape:

  1. what is the edit right now?
  2. here is the edit I want

This PR gives reading and writing one type, ImageEdit and does the first point with getImageEdit.

The writing, applyImageEdit, is next in a follow up along with the helper above.

Comment on lines +209 to +218
const edit = getImageEdit( controller.state );
const modifiers = [];

if ( edit?.flip ) {
modifiers.push( { type: 'flip', args: { flip: edit.flip } } );
}
if ( edit?.rotation !== undefined ) {
modifiers.push( { type: 'rotate', args: { angle: edit.rotation } } );
}
if ( edit?.crop ) {

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.

Follow up - add a helper so consumers don't need to do this when creating the final cropped image instructions.

@ramonjd

ramonjd commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Since the new package is targeting the 24.5 milestone, and it was merged after 24.4 RC was cut there's less pressure. Sorry! I'll still merge so I can follow up and test.

Nothing has been published, so replacing `mapEditToOriginal` breaks
nobody. The first release notes would have announced a break from a name
no consumer ever had. The entry now says what ships: `getImageEdit`
returns the edit as one `ImageEdit` record.
@ramonjd
ramonjd enabled auto-merge (squash) November 11, 2026 06:08
@ramonjd
ramonjd merged commit 6035a74 into trunk Nov 11, 2026
81 of 83 checks passed
@ramonjd
ramonjd deleted the refactor/image-editor-edit-type-name branch November 11, 2026 06:26
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Image Editor Related to packages/image-editor [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants