From aec18abac7182337f496bed6acad43d22f4950f2 Mon Sep 17 00:00:00 2001 From: Daniel Worthington Date: Thu, 9 Sep 2021 15:15:32 -0700 Subject: [PATCH] feat(models): Add support for inner shadows (#160) --- models/InnerShadow/InnerShadow.js | 42 ++++++++++++++++++++++++++ models/InnerShadow/InnerShadow.test.js | 23 ++++++++++++++ models/InnerShadow/__InnerShadow.json | 20 ++++++++++++ models/InnerShadow/index.d.ts | 17 +++++++++++ models/InnerShadow/index.js | 14 +++++++++ models/SharedStyle/SharedStyle.js | 2 ++ models/Style/Style.js | 7 +++++ models/Style/index.d.ts | 4 ++- 8 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 models/InnerShadow/InnerShadow.js create mode 100644 models/InnerShadow/InnerShadow.test.js create mode 100644 models/InnerShadow/__InnerShadow.json create mode 100644 models/InnerShadow/index.d.ts create mode 100644 models/InnerShadow/index.js diff --git a/models/InnerShadow/InnerShadow.js b/models/InnerShadow/InnerShadow.js new file mode 100644 index 0000000..8525d1e --- /dev/null +++ b/models/InnerShadow/InnerShadow.js @@ -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; diff --git a/models/InnerShadow/InnerShadow.test.js b/models/InnerShadow/InnerShadow.test.js new file mode 100644 index 0000000..8eb90dd --- /dev/null +++ b/models/InnerShadow/InnerShadow.test.js @@ -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)); + }); +}); diff --git a/models/InnerShadow/__InnerShadow.json b/models/InnerShadow/__InnerShadow.json new file mode 100644 index 0000000..bb8c9fd --- /dev/null +++ b/models/InnerShadow/__InnerShadow.json @@ -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 + } +} diff --git a/models/InnerShadow/index.d.ts b/models/InnerShadow/index.d.ts new file mode 100644 index 0000000..2880aac --- /dev/null +++ b/models/InnerShadow/index.d.ts @@ -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; diff --git a/models/InnerShadow/index.js b/models/InnerShadow/index.js new file mode 100644 index 0000000..79688ed --- /dev/null +++ b/models/InnerShadow/index.js @@ -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'); diff --git a/models/SharedStyle/SharedStyle.js b/models/SharedStyle/SharedStyle.js index cdaaa60..5686bc5 100644 --- a/models/SharedStyle/SharedStyle.js +++ b/models/SharedStyle/SharedStyle.js @@ -23,6 +23,7 @@ class SharedStyle { fills: args.fills, borders: args.borders, shadows: args.shadows, + innerShadows: args.innerShadows, }); } @@ -52,6 +53,7 @@ class SharedStyle { borders: args.borders, fills: args.fills, shadows: args.shadows, + innerShadows: args.innerShadows, }), }); } diff --git a/models/Style/Style.js b/models/Style/Style.js index f5fdd57..11a8cbd 100644 --- a/models/Style/Style.js +++ b/models/Style/Style.js @@ -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'); /** @@ -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 */ @@ -43,6 +45,7 @@ class Style { borders: [], fills: [], shadows: [], + innerShadows: [], contextSettings: GraphicsContextSettings.Model, }; } @@ -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 @@ -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(); @@ -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) } : {}), }); diff --git a/models/Style/index.d.ts b/models/Style/index.d.ts index 8220b4e..9481eb4 100644 --- a/models/Style/index.d.ts +++ b/models/Style/index.d.ts @@ -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 { @@ -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, @@ -56,6 +57,7 @@ declare class Style { borders: Border[]; fills: Fill[]; shadows: Shadow[]; + innerShadows: InnerShadow[]; textStyle: TextStyle; contextSettings: GraphicsContextSettings;