Skip to content

Commit

Permalink
feat(models): Add support for inner shadows (#160) (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Worthington authored Oct 15, 2021
1 parent 53381c5 commit 34a3870
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 1 deletion.
42 changes: 42 additions & 0 deletions models/InnerShadow/InnerShadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const Color = require('../Color');
const GraphicsContextSettings = require('../GraphicsContextSettings');

class InnerShadow {
static get Model() {
return {
_class: 'innerShadow',
isEnabled: true,
blurRadius: 3,
color: Color.Model,
contextSettings: GraphicsContextSettings.Model,
offsetX: 0,
offsetY: 1,
spread: 0,
};
}

constructor(args, json) {
if (json) {
Object.assign(this, json);
} else {
Object.assign(this, InnerShadow.Model, args, {
color: new Color(args.color),
});
}
}
}

module.exports = InnerShadow;
23 changes: 23 additions & 0 deletions models/InnerShadow/InnerShadow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const InnerShadow = require('./index');

const json = require('./__InnerShadow.json');

describe('InnerShadow', () => {
it('should work from raw JSON', () => {
const innerShadow = new InnerShadow(null, json);
expect(JSON.stringify(json, null, 2)).toEqual(JSON.stringify(innerShadow, null, 2));
});
});
20 changes: 20 additions & 0 deletions models/InnerShadow/__InnerShadow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"_class": "innerShadow",
"isEnabled": true,
"blurRadius": 3,
"offsetX": 0,
"offsetY": 1,
"spread": 0,
"color": {
"_class": "color",
"alpha": 0.5,
"blue": 0,
"green": 0,
"red": 0
},
"contextSettings": {
"_class": "graphicsContextSettings",
"blendMode": 0,
"opacity": 1
}
}
17 changes: 17 additions & 0 deletions models/InnerShadow/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Color from '../Color';
import GraphicsContextSettings from '../GraphicsContextSettings';

declare class InnerShadow {
_class: 'innerShadow';
isEnabled: boolean;
blurRadius: number;
color: Color;
contextSettings: GraphicsContextSettings;
offsetX: number;
offsetY: number;
spread: number;

constructor(args?: any, json?: any);
}

export = InnerShadow;
14 changes: 14 additions & 0 deletions models/InnerShadow/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

module.exports = require('./InnerShadow');
2 changes: 2 additions & 0 deletions models/SharedStyle/SharedStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SharedStyle {
fills: args.fills,
borders: args.borders,
shadows: args.shadows,
innerShadows: args.innerShadows,
});
}

Expand Down Expand Up @@ -52,6 +53,7 @@ class SharedStyle {
borders: args.borders,
fills: args.fills,
shadows: args.shadows,
innerShadows: args.innerShadows,
}),
});
}
Expand Down
7 changes: 7 additions & 0 deletions models/Style/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Fill = require('../Fill');
const Blur = require('../Blur');
const Border = require('../Border');
const Shadow = require('../Shadow');
const InnerShadow = require('../InnerShadow');
const GraphicsContextSettings = require('../GraphicsContextSettings');

/**
Expand All @@ -29,6 +30,7 @@ class Style {
* @property {Border.Model[]} border
* @property {Fill.Model[]} fills
* @property {Shadow.Model[]} shadows
* @property {InnerShadow.Model[]} innerShadows
* @property {TextStyle.Model} textStyle
* @property {GraphicsContextSettings.Model} contextSettings
*/
Expand All @@ -43,6 +45,7 @@ class Style {
borders: [],
fills: [],
shadows: [],
innerShadows: [],
contextSettings: GraphicsContextSettings.Model,
};
}
Expand Down Expand Up @@ -70,6 +73,7 @@ class Style {
* @param {Object[]} args.fills Sent to {@link Fill}
* @param {Object[]} args.borders Sent to {@link Border}
* @param {Object[]} args.shadows Sent to {@link Shadow}
* @param {Object[]} args.innerShadows Sent to {@link InnerShadow}
* @param {Object} args.textStyle Sent to {@link TextStyle}
* @param {Blur} args.blur Sent to {@link Blur}
* @param {Style.Model} json
Expand All @@ -81,6 +85,8 @@ class Style {
if (this.fills) this.fills = this.fills.map((fill) => new Fill(null, fill));
if (this.borders) this.borders = this.borders.map((border) => new Border(null, border));
if (this.shadows) this.shadows = this.shadows.map((shadow) => new Shadow(null, shadow));
if (this.innerShadows)
this.innerShadows = this.innerShadows.map((innerShadow) => new InnerShadow(null, innerShadow));
if (this.blur) this.blur = new Blur(this.blur);
} else {
const id = args.id || uuid().toUpperCase();
Expand All @@ -89,6 +95,7 @@ class Style {
borders: (args.borders || []).map((border) => new Border(border)),
fills: (args.fills || []).map((fill) => new Fill(fill)),
shadows: (args.shadows || []).map((shadow) => new Shadow(shadow)),
innerShadows: (args.innerShadows || []).map((innerShadow) => new InnerShadow(innerShadow)),
...(args.textStyle ? { textStyle: new TextStyle(args.textStyle) } : {}),
...(args.blur ? { blur: new Blur(args.blur) } : {}),
});
Expand Down
4 changes: 3 additions & 1 deletion models/Style/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Border from '../Border';
import Fill from '../Fill';
import GraphicsContextSettings from '../GraphicsContextSettings';
import Shadow from '../Shadow';
import InnerShadow from '../InnerShadow';
import TextStyle from '../TextStyle';

declare const enum MarkerType {
Expand All @@ -15,7 +16,7 @@ declare const enum MarkerType {
filledSquare = 7
}

declare const enum WindingRule {} // TODO
declare const enum WindingRule { } // TODO

declare const enum BlendMode {
Normal = 0,
Expand Down Expand Up @@ -56,6 +57,7 @@ declare class Style {
borders: Border[];
fills: Fill[];
shadows: Shadow[];
innerShadows: InnerShadow[];
textStyle: TextStyle;
contextSettings: GraphicsContextSettings;

Expand Down

0 comments on commit 34a3870

Please sign in to comment.