Skip to content
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

[Feature] Add Image Crop Methods #271

Merged
merged 2 commits into from
May 9, 2023
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
38 changes: 38 additions & 0 deletions src/controllers/FrameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,42 @@ export class FrameController {
const res = await this.#editorAPI;
return res.setFrameBlendMode(frameId, blendMode).then((result) => getEditorResponseData<null>(result));
};

/**
* This method will make the specified image frame go into cropping mode.
* @param frameId The ID of a specific image frame
* @returns
*/
enterCropMode = async (frameId: Id) => {
const res = await this.#editorAPI;
return res.enterCropMode(frameId).then((result) => getEditorResponseData<null>(result));
};

/**
* This method will exit cropping mode while saving the applied crop.
* @returns
*/
applyCropMode = async () => {
const res = await this.#editorAPI;
return res.applyCropMode().then((result) => getEditorResponseData<null>(result));
};

/**
* This method will reset the currently applied crop mode and apply the last selected fit mode again.
* @param frameId The ID of a specific image frame
* @returns
*/
resetCropMode = async (frameId: Id) => {
const res = await this.#editorAPI;
return res.resetCropMode(frameId).then((result) => getEditorResponseData<null>(result));
};

/**
* This method will exit cropping mode without saving the applied crop.
* @returns
*/
cancelCropMode = async () => {
const res = await this.#editorAPI;
return res.cancelCropMode().then((result) => getEditorResponseData<null>(result));
};
}
30 changes: 30 additions & 0 deletions src/tests/controllers/FrameController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const mockedEditorApi: EditorAPI = {
setFrameBlendMode: async () => getEditorResponseData(castToEditorResponse(null)),
renameFrame: async () => getEditorResponseData(castToEditorResponse(null)),
setImageSource: async () => getEditorResponseData(castToEditorResponse(null)),
enterCropMode: async () => getEditorResponseData(castToEditorResponse(null)),
applyCropMode: async () => getEditorResponseData(castToEditorResponse(null)),
resetCropMode: async () => getEditorResponseData(castToEditorResponse(null)),
cancelCropMode: async () => getEditorResponseData(castToEditorResponse(null)),
};

beforeEach(() => {
Expand Down Expand Up @@ -111,6 +115,10 @@ beforeEach(() => {
jest.spyOn(mockedEditorApi, 'setFrameBlendMode');
jest.spyOn(mockedEditorApi, 'renameFrame');
jest.spyOn(mockedEditorApi, 'setImageSource');
jest.spyOn(mockedEditorApi, 'enterCropMode');
jest.spyOn(mockedEditorApi, 'applyCropMode');
jest.spyOn(mockedEditorApi, 'resetCropMode');
jest.spyOn(mockedEditorApi, 'cancelCropMode');

frameId = mockSelectFrame.frameId;
});
Expand Down Expand Up @@ -356,6 +364,28 @@ describe('FrameController', () => {
expect(mockedEditorApi.setFrameBlendMode).toHaveBeenCalledTimes(1);
expect(mockedEditorApi.setFrameBlendMode).toHaveBeenCalledWith(frameId, BlendMode.darken);
});

it('Should be possible to enter cropping mode on a specific frame', async () => {
await mockedFrameController.enterCropMode(frameId);
expect(mockedEditorApi.enterCropMode).toHaveBeenCalledTimes(1);
expect(mockedEditorApi.enterCropMode).toHaveBeenCalledWith(frameId);
});

it('Should be possible to apply the current image crop to the frame', async () => {
await mockedFrameController.applyCropMode();
expect(mockedEditorApi.applyCropMode).toHaveBeenCalledTimes(1);
});

it('Should be possible to reset cropping mode on a specific frame', async () => {
await mockedFrameController.resetCropMode(frameId);
expect(mockedEditorApi.resetCropMode).toHaveBeenCalledTimes(1);
expect(mockedEditorApi.resetCropMode).toHaveBeenCalledWith(frameId);
});

it('Should be possible to cancel the current image crop', async () => {
await mockedFrameController.cancelCropMode();
expect(mockedEditorApi.cancelCropMode).toHaveBeenCalledTimes(1);
});
});

describe('ImageFrameSource manipulations', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/types/FrameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type FrameLayoutType = {
scaleY: PropertyState<number>;
included: PropertyState<boolean>;
fitMode: PropertyState<FitMode>;
hasImageCrop: PropertyState<boolean>;
minCopyfitting: PropertyState<number>;
maxCopyfitting: PropertyState<number>;
enableCopyfitting: PropertyState<boolean>;
Expand Down Expand Up @@ -59,6 +60,7 @@ export type ImageFrame = {
src?: ImageFrameSource;
blendMode: BlendMode;
constrainProportions: boolean;
crop?: CropSettings | NoCropSettings;
};

export type ShapeFrame = {
Expand Down Expand Up @@ -102,6 +104,19 @@ export type TextFrame = {
constrainProportions: boolean;
};

export type CropSettings = {
left: number;
top: number;
width: number;
height: number;
rotationDegrees: number;
type: 'default';
};

export type NoCropSettings = {
type: 'noCrop';
};

export enum ImageSourceTypeEnum {
url = 'url',
variable = 'variable',
Expand Down