Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/image-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Initial release ([#86223](https://github.com/WordPress/gutenberg/pull/86223)).
- Allow consumers to theme the canvas through CSS custom properties ([#86340](https://github.com/WordPress/gutenberg/pull/86340)).
- Add `exportImage( state, options? )`, which exports the edited image as a `Blob` in the browser ([#86405](https://github.com/WordPress/gutenberg/pull/86405)).
- Describe the edit to apply when saving as one `ImageEdit` record, returned by `getImageEdit( state )` ([#86630](https://github.com/WordPress/gutenberg/pull/86630)).
- Add `mapImageEdit( edit, handlers )`, which turns each part of an `ImageEdit` into the consumer's own format in the order the parts apply ([#86704](https://github.com/WordPress/gutenberg/pull/86704)).

### Bug Fixes

Expand Down
4 changes: 4 additions & 0 deletions packages/image-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ Use it when saving. The editor stores where the image sits, how far it is zoomed

The edit has an optional `flip`, `rotation` and `crop`. Apply them in that order; anything absent needs nothing done. With exactly one axis flipped, `rotation` is 360 minus `state.rotation`, because the flip comes first. The crop is given as fractions of the image after rotation. An edit with no parts means there is nothing to apply, and `null` means no image has loaded. A locked ratio can produce a crop before the person edits anything.

### `mapImageEdit( edit, handlers )`

Use it to turn an edit into a list of instructions for something that applies them one at a time, such as the WordPress media REST endpoint. `handlers` has one function for each part, `flip`, `rotation` and `crop`; each is called only when the edit has that part, and the results come back in the order the parts apply. The order matters because `rotation` is measured after the flip and `crop` after both, so instructions applied in another order frame a different part of the image.

### `exportImage( state, options? )`

Use it when the edited image is uploaded as a new file, rather than applied to the original by a server. It loads the image in `state.image`, draws the region the crop frames with the flip, rotation and zoom applied, and resolves with a `Blob` whose dimensions are the ones `getOutputSize( state )` reports. `options.type` is the MIME type, `image/png` by default, and `options.quality` is the quality for lossy types, from 0 to 1.
Expand Down
1 change: 1 addition & 0 deletions packages/image-editor/docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Viewport pan and zoom let the person look closer at the crop area without changi
| Task | API |
| --- | --- |
| Describe the edit to apply to the loaded image when saving | `getImageEdit()`. |
| Turn the edit into instructions in the order they apply | `mapImageEdit()`. |
| Keep the edit in your own undo history | `useImageEditor( { state, onChange } )`, `createImageEditorState()` and `areImageEditorStatesEqual()`. |
| Build zoom and rotation controls | `controller.minZoom`, `controller.maxZoom` and `controller.maxRotationOffset`. |

Expand Down
47 changes: 26 additions & 21 deletions packages/image-editor/docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,32 +172,37 @@ function ImageEditorWithHistory( { src }: { src: string } ) {

`getImageEdit( state )` describes the edit against the full-size image loaded in `state.image`: an optional `flip`, `rotation` and `crop`, to apply in that order. The [README](/packages/image-editor/README.md) says what each part measures.

The WordPress media REST endpoint (`/wp/v2/media/<id>/edit`) applies `modifiers` in the same order, with the crop in percentages:
For example, the WordPress media REST endpoint (`/wp/v2/media/<id>/edit`) applies `modifiers` in the order they arrive, with the crop in percentages. `mapImageEdit( edit, handlers )` calls one handler for each part the edit has, in the order the parts apply, so the list comes out in the right order:

```ts
import apiFetch from '@wordpress/api-fetch';
import { getImageEdit } from '@wordpress/image-editor';
import { getImageEdit, mapImageEdit } from '@wordpress/image-editor';
import type { Flip } from '@wordpress/image-editor';

const edit = getImageEdit( controller.state );
const modifiers = [];
type Modifier =
| { type: 'flip'; args: { flip: Flip } }
| { type: 'rotate'; args: { angle: number } }
| {
type: 'crop';
args: { left: number; top: number; width: number; height: number };
};

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 ) {
modifiers.push( {
type: 'crop',
args: {
left: edit.crop.x * 100,
top: edit.crop.y * 100,
width: edit.crop.width * 100,
height: edit.crop.height * 100,
},
} );
}
const edit = getImageEdit( controller.state );
const modifiers = edit
? mapImageEdit< Modifier >( 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,
},
} ),
} )
: [];

if ( modifiers.length ) {
await apiFetch( {
Expand Down
1 change: 1 addition & 0 deletions packages/image-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { areImageEditorStatesEqual } from './core/state';
export { getOutputSize } from './get-output-size';
export { getImageEdit } from './get-image-edit';
export type { ImageEdit } from './get-image-edit';
export { mapImageEdit } from './map-image-edit';

// Export the edited image as a file, in the browser.
export { exportImage } from './export-image';
Expand Down
41 changes: 41 additions & 0 deletions packages/image-editor/src/map-image-edit.ts
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: {

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.

yes the handlers are required. i thought about making them optional, and so mapImageEdit would just require edit and 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.

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;
}
47 changes: 47 additions & 0 deletions packages/image-editor/src/test/map-image-edit.test.ts
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 ] );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { store as coreStore } from '@wordpress/core-data';
import { useCallback, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { getImageEdit } from '@wordpress/image-editor';
import { getImageEdit, mapImageEdit } from '@wordpress/image-editor';
import type { Media } from '../media-editor-provider';
import type { MediaEditorSession } from '../../state';

Expand Down Expand Up @@ -94,30 +94,20 @@ function getCropModifiers( session: MediaEditorSession ): Modifier[] {
return [];
}

// Core applies modifiers in the order they arrive, so send them in the
// edit's order: flip, then rotate, then crop.
const modifiers: Modifier[] = [];
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 ) {
modifiers.push( {
// Core applies modifiers in the order they arrive.
return mapImageEdit< Modifier >( edit, {
flip: ( flip ) => ( { type: 'flip', args: { flip } } ),
rotation: ( angle ) => ( { type: 'rotate', args: { angle } } ),
crop: ( { x, y, width, height } ) => ( {
type: 'crop',
args: {
left: edit.crop.x * 100,
top: edit.crop.y * 100,
width: edit.crop.width * 100,
height: edit.crop.height * 100,
left: x * 100,
top: y * 100,
width: width * 100,
height: height * 100,
},
} );
}
return modifiers;
} ),
} );
}

function getMetadataEdits(
Expand Down
Loading