Repository navigation
Image Editor: Add mapImageEdit to apply an edit's parts in order #86704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63b22d3
Image Editor: Add mapImageEdit to turn an edit into ordered instructions
ramonjd 043ffb4
Image Editor: Add a changelog entry for mapImageEdit
ramonjd 53da5c5
Apply suggestion from @ramonjd
ramonjd 5344811
Update recipe to match usage, by passing in a type
andrewserong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { Flip, NormalizedRect } from './core/types'; | ||
| import type { ImageEdit } from './get-image-edit'; | ||
|
|
||
| /** | ||
| * Turn each part of an edit into the consumer's own format, in the order the | ||
| * parts apply: flip, then rotation, then crop. | ||
| * | ||
| * Use it to build instructions for something that applies them one at a | ||
| * time, such as the `modifiers` sent to the WordPress media REST endpoint. | ||
| * `rotation` is measured after the flip, and `crop` is a fraction of the | ||
| * image after both, so applying them in another order frames a different | ||
| * part of the image. | ||
| * | ||
| * @param edit The edit, as returned by `getImageEdit`. | ||
| * @param handlers One function for each part, called only when the | ||
| * edit has that part. | ||
| * @param handlers.flip Turns the flip into the consumer's format. | ||
| * @param handlers.rotation Turns the rotation, in degrees, into it. | ||
| * @param handlers.crop Turns the crop into it. | ||
| * @return What the handlers returned, in the order the parts apply. | ||
| */ | ||
| export function mapImageEdit< T >( | ||
| edit: ImageEdit, | ||
| handlers: { | ||
| flip: ( flip: Flip ) => T; | ||
| rotation: ( rotation: number ) => T; | ||
| crop: ( crop: NormalizedRect ) => T; | ||
| } | ||
| ): T[] { | ||
| const result: T[] = []; | ||
| if ( edit.flip ) { | ||
| result.push( handlers.flip( edit.flip ) ); | ||
| } | ||
| if ( edit.rotation !== undefined ) { | ||
| result.push( handlers.rotation( edit.rotation ) ); | ||
| } | ||
| if ( edit.crop ) { | ||
| result.push( handlers.crop( edit.crop ) ); | ||
| } | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { mapImageEdit } from '../map-image-edit'; | ||
|
|
||
| const handlers = { | ||
| flip: () => 'flip', | ||
| rotation: () => 'rotation', | ||
| crop: () => 'crop', | ||
| }; | ||
|
|
||
| describe( 'mapImageEdit', () => { | ||
| it( 'calls the handlers in the order the parts apply, whatever the key order', () => { | ||
| const edit = { | ||
| crop: { x: 0, y: 0, width: 0.5, height: 1 }, | ||
| rotation: 90, | ||
| flip: { horizontal: true, vertical: false }, | ||
| }; | ||
|
|
||
| expect( mapImageEdit( edit, handlers ) ).toEqual( [ | ||
| 'flip', | ||
| 'rotation', | ||
| 'crop', | ||
| ] ); | ||
| } ); | ||
|
|
||
| it( 'skips the parts the edit does not have', () => { | ||
| expect( mapImageEdit( { rotation: 180 }, handlers ) ).toEqual( [ | ||
| 'rotation', | ||
| ] ); | ||
| expect( mapImageEdit( {}, handlers ) ).toEqual( [] ); | ||
| } ); | ||
|
|
||
| it( 'passes each part to its handler', () => { | ||
| const flip = { horizontal: false, vertical: true }; | ||
| const crop = { x: 0.25, y: 0, width: 0.5, height: 1 }; | ||
|
|
||
| expect( | ||
| mapImageEdit< unknown >( | ||
| { flip, rotation: 270, crop }, | ||
| { | ||
| flip: ( value ) => value, | ||
| rotation: ( value ) => value, | ||
| crop: ( value ) => value, | ||
| } | ||
| ) | ||
| ).toEqual( [ flip, 270, crop ] ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes the handlers are required. i thought about making them optional, and so
mapImageEditwould just requireeditand return, by default, edit's values in the right order.But i'm not sure there’s anything useful to default to, and if a consumer passed one but not the other that could lead to a mistake in the final crop.