From c3d54c0d08f3ebe8ffb1c48e9be0393725820f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Tue, 5 Mar 2024 12:55:24 +0100 Subject: [PATCH 1/2] Add fromJson/toJson for resize actions --- .../unit/fromJson/rotate.fromJson.test.ts | 21 ++++++++++++------- __TESTS__/unit/toJson/rotate.toJson.test.ts | 12 +++++++++++ src/actions/rotate/RotateAction.ts | 9 +++++--- src/internal/fromJson.ts | 1 + src/internal/models/IRotateActionModel.ts | 5 ++++- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/__TESTS__/unit/fromJson/rotate.fromJson.test.ts b/__TESTS__/unit/fromJson/rotate.fromJson.test.ts index 993617f..2024701 100644 --- a/__TESTS__/unit/fromJson/rotate.fromJson.test.ts +++ b/__TESTS__/unit/fromJson/rotate.fromJson.test.ts @@ -2,15 +2,20 @@ import {fromJson} from "../../../src/internal/fromJson"; describe('rotate.fromJson', () => { it('should generate a url with rotate actions from array of models', function () { - const transformation = fromJson({actions:[ - { - actionType: 'rotateByAngle', - angle: 4 - } - ]} - ); + const transformation = fromJson({ + actions: [ + { + actionType: 'rotateByAngle', + angle: 4 + }, + { + actionType: 'rotateByMode', + mode: 'vflip' + } + ] + }); expect(transformation.toString()).toStrictEqual( - 'a_4' + 'a_4/a_vflip' ); }); }); diff --git a/__TESTS__/unit/toJson/rotate.toJson.test.ts b/__TESTS__/unit/toJson/rotate.toJson.test.ts index cb190a6..4f1b0d0 100644 --- a/__TESTS__/unit/toJson/rotate.toJson.test.ts +++ b/__TESTS__/unit/toJson/rotate.toJson.test.ts @@ -14,4 +14,16 @@ describe('Rotate toJson()', () => { ] }); }); + it('rotateByMode', () => { + const transformation = new Transformation() + .addAction(Rotate.mode('hflip')); + expect(transformation.toJson()).toStrictEqual({ + actions: [ + { + actionType: 'rotateByMode', + mode: 'hflip' + } + ] + }); + }); }); diff --git a/src/actions/rotate/RotateAction.ts b/src/actions/rotate/RotateAction.ts index dd7c2b4..522e2c8 100644 --- a/src/actions/rotate/RotateAction.ts +++ b/src/actions/rotate/RotateAction.ts @@ -22,9 +22,9 @@ class RotateAction extends Action { constructor(angle?: number) { super(); this.addQualifier(new Qualifier(QUALIFIER_KEY, angle)); - this._actionModel.actionType = 'rotateByAngle'; if (angle) { + this._actionModel.actionType = 'rotateByAngle'; this._actionModel.angle = angle; } } @@ -37,6 +37,8 @@ class RotateAction extends Action { * @return {this} */ mode(rotationMode: RotationModeQualifierValue | RotationModeType | string):this { + this._actionModel.actionType = 'rotateByMode'; + this._actionModel.mode = rotationMode; return this.addValueToQualifier(QUALIFIER_KEY, rotationMode); } @@ -46,16 +48,17 @@ class RotateAction extends Action { * @return {this} */ angle(degrees: number): this { + this._actionModel.actionType = 'rotateByAngle'; this._actionModel.angle = degrees; return this.addValueToQualifier(QUALIFIER_KEY, degrees); } static fromJson(actionModel: IActionModel): RotateAction { - const {angle} = (actionModel as IRotateByAngleActionModel); + const {angle, mode} = (actionModel as IRotateByAngleActionModel); // We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel]) // This allows the inheriting classes to determine the class to be created - const result = new this(angle); + const result = mode ? new this().mode(mode) : new this(angle); return result; } } diff --git a/src/internal/fromJson.ts b/src/internal/fromJson.ts index c43b6e8..9aae355 100644 --- a/src/internal/fromJson.ts +++ b/src/internal/fromJson.ts @@ -142,6 +142,7 @@ const ActionModelMap: Record = { videoCodec: VideoCodecAction, ifCondition: ConditionalAction, rotateByAngle: RotateAction, + rotateByMode: RotateAction, backgroundRemoval: BackgroundRemoval, dropshadow: DropShadow, roundCorners: RoundCornersAction, diff --git a/src/internal/models/IRotateActionModel.ts b/src/internal/models/IRotateActionModel.ts index c2f6990..ae45f10 100644 --- a/src/internal/models/IRotateActionModel.ts +++ b/src/internal/models/IRotateActionModel.ts @@ -1,7 +1,10 @@ import {IActionModel} from "./IActionModel.js"; +import {RotationModeType} from "../../types/types.js"; +import {RotationModeQualifierValue} from "../../qualifiers/rotate/RotationModeQualifierValue"; interface IRotateByAngleActionModel extends IActionModel { - angle?: number + angle?: number; + mode?: RotationModeQualifierValue | RotationModeType | string; } export {IRotateByAngleActionModel}; From 3181f5c8c1a85eee20fcc3a8e4b443d239707dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Wed, 6 Mar 2024 10:40:59 +0100 Subject: [PATCH 2/2] Add extension --- src/internal/models/IRotateActionModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/models/IRotateActionModel.ts b/src/internal/models/IRotateActionModel.ts index ae45f10..0520d24 100644 --- a/src/internal/models/IRotateActionModel.ts +++ b/src/internal/models/IRotateActionModel.ts @@ -1,6 +1,6 @@ import {IActionModel} from "./IActionModel.js"; import {RotationModeType} from "../../types/types.js"; -import {RotationModeQualifierValue} from "../../qualifiers/rotate/RotationModeQualifierValue"; +import {RotationModeQualifierValue} from "../../qualifiers/rotate/RotationModeQualifierValue.js"; interface IRotateByAngleActionModel extends IActionModel { angle?: number;