From 9954aec6f1a16a49bbc4813338b42400bc2f502a Mon Sep 17 00:00:00 2001 From: Kevin Goedecke Date: Tue, 23 Jul 2019 22:46:19 +0200 Subject: [PATCH] feat(models): add ShapePath model with convenience constructor in ShapeGroup (#17) --- models/ShapeGroup/ShapeGroup.js | 24 ++++++++++ models/ShapePath/ShapePath.js | 70 ++++++++++++++++++++++++++++++ models/ShapePath/ShapePath.test.js | 22 ++++++++++ models/ShapePath/index.js | 14 ++++++ models/index.js | 1 + 5 files changed, 131 insertions(+) create mode 100644 models/ShapePath/ShapePath.js create mode 100644 models/ShapePath/ShapePath.test.js create mode 100644 models/ShapePath/index.js diff --git a/models/ShapeGroup/ShapeGroup.js b/models/ShapeGroup/ShapeGroup.js index 6a61c00..4a1e1dc 100644 --- a/models/ShapeGroup/ShapeGroup.js +++ b/models/ShapeGroup/ShapeGroup.js @@ -14,6 +14,7 @@ const uuid = require('uuid-v4'); const Layer = require('../Layer'); const Rectangle = require('../Rectangle'); +const ShapePath = require('../ShapePath'); const Oval = require('../Oval'); const Rect = require('../Rect'); const Style = require('../Style'); @@ -76,6 +77,29 @@ class ShapeGroup extends Layer { ], }); } + + static ShapePath(args) { + return new this({ + name: args.name, + id: args.id, + style: args.style, + frame: { + x: args.x, + y: args.y, + width: args.width, + height: args.height, + }, + layers: [ + new ShapePath({ + x: 0, + y: 0, + width: args.width, + height: args.height, + points: args.points, + }), + ], + }); + } } ShapeGroup.Model = Object.assign({}, Layer.Model, { diff --git a/models/ShapePath/ShapePath.js b/models/ShapePath/ShapePath.js new file mode 100644 index 0000000..b35eed1 --- /dev/null +++ b/models/ShapePath/ShapePath.js @@ -0,0 +1,70 @@ +/* + * Copyright 2017 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 uuid = require('uuid-v4'); +const Layer = require('../Layer'); +const Rect = require('../Rect'); +const Style = require('../Style'); + +/** + * + */ +class ShapePath extends Layer { + /** + * + * @param {Object} args + * @param {String} args.name + * @param {integer} args.x + * @param {integer} args.y + * @param {integer} args.height + * @param {integer} args.width + * @param {Array.} args.points + * @param {Object} args.style Passed to {@link LayerStyle} + * @param {ShapePath.Model} json + */ + constructor(args = {}, json) { + super(args, json); + if (!json) { + const id = args.id || uuid().toUpperCase(); + Object.assign(this, ShapePath.Model, { + points: args.points || [], + do_objectID: id, + frame: new Rect({ + x: args.x, + y: args.y, + width: args.width, + height: args.height, + }), + style: new Style(args.style), + name: args.name || id, + }); + } + } +} + +/** + * @mixes Layer.Model + * @property {boolean} edited + * @property {boolean} isClosed + * @property {integer} pointRadiusBehaviour + * @property {String[]} points + */ +ShapePath.Model = Object.assign({}, Layer.Model, { + _class: 'shapePath', + edited: true, + isClosed: false, + pointRadiusBehaviour: 1, + points: [], +}); + +module.exports = ShapePath; diff --git a/models/ShapePath/ShapePath.test.js b/models/ShapePath/ShapePath.test.js new file mode 100644 index 0000000..7b409d1 --- /dev/null +++ b/models/ShapePath/ShapePath.test.js @@ -0,0 +1,22 @@ +/* + * Copyright 2017 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 ShapePath = require('./index'); + +const json = {}; + +describe('ShapePath', () => { + it('should work from raw JSON', () => { + expect(true).toBeTruthy(); + }); +}); diff --git a/models/ShapePath/index.js b/models/ShapePath/index.js new file mode 100644 index 0000000..25f3235 --- /dev/null +++ b/models/ShapePath/index.js @@ -0,0 +1,14 @@ +/* + * Copyright 2017 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('./ShapePath'); diff --git a/models/index.js b/models/index.js index aa390ad..0bce568 100644 --- a/models/index.js +++ b/models/index.js @@ -32,6 +32,7 @@ module.exports = { RulerData: require('./RulerData'), Shadow: require('./Shadow'), ShapeGroup: require('./ShapeGroup'), + ShapePath: require('./ShapePath'), SharedStyle: require('./SharedStyle'), Sketch: require('./Sketch'), Style: require('./Style'),