Repository navigation
Image Editor: Describe the edit as one record, not a list of operations - #86630
Conversation
"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.
🤖 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: 0 B Total Size: 8.63 MB
⚡ 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. As a user I want to be able to create a navigation overlay for a specific navigation block in
|
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.
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.
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.
|
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 |
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for testing @andrewserong
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 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:
This PR gives reading and writing one type, The writing, |
| 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 ) { |
There was a problem hiding this comment.
Follow up - add a helper so consumers don't need to do this when creating the final cropped image instructions.
|
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.
What?
mapEditToOriginalbecomesgetImageEdit, returning one record instead of a list of tagged operations:Apply the parts in that order. Absent means nothing to do.
nullmeans no image has loaded.Why?
The list did not represent an edit "state", but a description of edits.
ImageEditorStateholds oneflip, onerotationand onecropRect; 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
/editendpoint'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 sameImageEdit, so changing one part of an edit is: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