Skip to content

Commit fb7f333

Browse files
author
Nir Maoz
authored
Add compass gravity model (#475)
1 parent e5196f0 commit fb7f333

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

__TESTS__/unit/toJson/resize.toJson.test.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ describe('resize.toJson()', () => {
6666
},
6767
x: 3,
6868
y: 4,
69-
gravity: 'north_east',
69+
gravity: {
70+
compass: "north_east",
71+
gravityType: "direction"
72+
},
7073
zoom: 10
7174
},
7275
{
@@ -76,7 +79,10 @@ describe('resize.toJson()', () => {
7679
},
7780
x: 3,
7881
y: 4,
79-
gravity: 'south'
82+
gravity: {
83+
compass: "south",
84+
gravityType: "direction"
85+
}
8086
},
8187
{
8288
"actionType": "limitFill",
@@ -85,22 +91,31 @@ describe('resize.toJson()', () => {
8591
},
8692
x: 3,
8793
y: 4,
88-
gravity: 'south'
94+
gravity: {
95+
compass: "south",
96+
gravityType: "direction"
97+
}
8998
},
9099
{
91100
"actionType": "thumbnail",
92101
"dimensions": {
93102
"width": 100
94103
},
95-
gravity: 'south',
104+
gravity: {
105+
compass: "south",
106+
gravityType: "direction"
107+
},
96108
zoom: 4
97109
},
98110
{
99111
"actionType": "pad",
100112
"dimensions": {
101113
"width": 100
102114
},
103-
gravity: 'south',
115+
gravity: {
116+
compass: "south",
117+
gravityType: "direction"
118+
},
104119
x: 3,
105120
y: 4
106121
},
@@ -109,7 +124,10 @@ describe('resize.toJson()', () => {
109124
"dimensions": {
110125
"width": 100
111126
},
112-
gravity: 'south',
127+
gravity: {
128+
compass: "south",
129+
gravityType: "direction"
130+
},
113131
x: 3,
114132
y: 4
115133
},
@@ -118,7 +136,10 @@ describe('resize.toJson()', () => {
118136
"dimensions": {
119137
"width": 100
120138
},
121-
gravity: 'south',
139+
gravity: {
140+
compass: "south",
141+
gravityType: "direction"
142+
},
122143
x: 3,
123144
y: 4
124145
}

public/progress/cloudinary-base-progress-report.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/actions/resize/ResizeAdvancedAction.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {ResizeSimpleAction} from "./ResizeSimpleAction.js";
2-
import {IGravity} from "../../qualifiers/gravity/GravityQualifier.js";
2+
import {IGravity, IShortenGravity} from "../../qualifiers/gravity/GravityQualifier.js";
33
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
44
import {IActionModel} from "../../internal/models/IActionModel.js";
5-
6-
export type IShortenGravity = 'auto' | 'north' | 'center' | 'east' | 'west' | 'south' | 'north_west' | 'south_east' | 'south_west' | 'north_east';
5+
import {createGravityModel} from "../../internal/models/createGravityModel.js";
76

87
/**
98
* @description Defines an advanced resize.
@@ -17,14 +16,10 @@ class ResizeAdvancedAction extends ResizeSimpleAction {
1716
* @param {Qualifiers.Gravity} gravity
1817
*/
1918
gravity(gravity: IGravity | IShortenGravity): this {
20-
if(typeof gravity === "string") {
21-
this._actionModel.gravity = gravity;
22-
return this.addQualifier(new Qualifier('g', gravity));
23-
}
24-
25-
this._actionModel.gravity = gravity.qualifierValue;
19+
this._actionModel.gravity = createGravityModel(gravity);
20+
const gravityQualifier = typeof gravity === "string" ? new Qualifier('g', gravity) : gravity;
2621

27-
return this.addQualifier(gravity);
22+
return this.addQualifier(gravityQualifier);
2823
}
2924

3025
static fromJson(actionModel: IActionModel): ResizeAdvancedAction {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {IGravity, IShortenGravity} from "../../qualifiers/gravity/GravityQualifier.js";
2+
import {ICompassGravity, isICompassGravity} from "../../qualifiers/gravity/compassGravity/CompassGravity.js";
3+
4+
export interface IGravityModel{
5+
gravityType: string;
6+
}
7+
8+
export interface ICompassGravityModel extends IGravityModel{
9+
compass: ICompassGravity
10+
}
11+
12+
/**
13+
* Creates a compassGravity model
14+
* @param compass
15+
*/
16+
function createCompassGravityModel(compass: ICompassGravity): ICompassGravityModel{
17+
return {
18+
compass,
19+
gravityType: 'direction'
20+
};
21+
}
22+
23+
/**
24+
* Create a model of given gravity
25+
* @param gravity
26+
*/
27+
export function createGravityModel(gravity: IGravity | IShortenGravity): ICompassGravityModel | string {
28+
const gravityString = (typeof gravity === "string" ? gravity : (gravity).qualifierValue) as string;
29+
30+
if (isICompassGravity(gravityString)) {
31+
return createCompassGravityModel(gravityString);
32+
}
33+
34+
return gravityString;
35+
}

src/qualifiers/gravity/GravityQualifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
22
import {QualifierValue} from "../../internal/qualifier/QualifierValue.js";
3-
import {CompassGravity} from "./compassGravity/CompassGravity.js";
3+
import {CompassGravity, ICompassGravity} from "./compassGravity/CompassGravity.js";
44
import {AutoGravity} from "./autoGravity/AutoGravity.js";
55
import {FocusOnGravity} from "./focusOnGravity/FocusOnGravity.js";
66
import {FocusOnValue} from "../focusOn.js";
@@ -10,7 +10,7 @@ import {XYCenterGravity} from "./xyCenterGravity/XYCenterGravity.js";
1010

1111
export type IGravity = CompassGravity | AutoGravity | FocusOnGravity | XYCenterGravity;
1212
export type IGravityValue = CompassQualifier | FocusOnValue | AutoFocus;
13-
13+
export type IShortenGravity = 'auto' | ICompassGravity;
1414

1515
/**
1616
* @memberOf Gravity.GravityQualifier

src/qualifiers/gravity/compassGravity/CompassGravity.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import {GravityQualifier} from "../GravityQualifier.js";
22
import {CompassQualifier} from "../qualifiers/compass/CompassQualifier.js";
33

4+
type ICompassGravity = 'north' | 'center' | 'east' | 'west' | 'south' | 'north_west' | 'south_east' | 'south_west' | 'north_east';
5+
6+
/**
7+
* Validate that given val is an ICompassGravity
8+
* @param val
9+
*/
10+
function isICompassGravity(val: unknown): val is ICompassGravity{
11+
return ['north', 'center', 'east', 'west', 'south', 'north_west', 'south_east', 'south_west', 'north_east'].includes(val as string);
12+
}
13+
414
/**
515
* @description The class for the CompassGravity builder
616
* @memberOf Qualifiers.Gravity
@@ -15,4 +25,4 @@ class CompassGravity extends GravityQualifier {
1525
}
1626
}
1727

18-
export {CompassGravity};
28+
export {CompassGravity, ICompassGravity, isICompassGravity};

0 commit comments

Comments
 (0)