Skip to content

Commit

Permalink
fix: use getters for Models (#52)
Browse files Browse the repository at this point in the history
fixes #44
Previous behavior was to assign Model once per class which may produce mutations, now each class has own Model getter which returns new Object every time it called
  • Loading branch information
L2jLiga authored and dbanksdesign committed Sep 5, 2019
1 parent 80c9e29 commit 84293db
Show file tree
Hide file tree
Showing 30 changed files with 612 additions and 547 deletions.
69 changes: 42 additions & 27 deletions __tests__/building.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
const {Sketch, Text, Page, Artboard} = require('../index');
const {fileExists, clearOutput} = require('./__helpers');
const { Sketch, Text, Page, Artboard } = require('../index');
const { fileExists, clearOutput } = require('./__helpers');

describe('Building from scratch', () => {
beforeEach(() => {
clearOutput();
});

it('should write a valid file', () => {
let sketch = new Sketch();
const page = new Page({
name: 'My Page'
});
const artboard = new Artboard({
frame: {
width: 1024,
height: 768
},
name: 'My Artboard'
});
const text = new Text({
name: 'My Text',
string: 'Test',
fontSize: 24
});
artboard.addLayer( text );
sketch.addPage( page );
sketch.addArtboard(page.getID(), artboard);
const path = `${process.cwd()}/__tests__/__output/output2.sketch`;
sketch.build(path).then((path) => {
expect(fileExists(path)).toBeTruthy();
});
it('should write a valid file', async () => {
await (async () => {
const sketch = new Sketch();
const page = new Page({
name: 'My Page',
});
const artboard = new Artboard({
frame: {
width: 1024,
height: 768,
},
name: 'My Artboard',
});
const text = new Text({
name: 'My Text',
string: 'Test',
fontSize: 24,
});
artboard.addLayer(text);
sketch.addPage(page);
sketch.addArtboard(page.getID(), artboard);
const path = `${process.cwd()}/__tests__/__output/output2.sketch`;
const outputPath = await sketch.build(path);
expect(fileExists(outputPath)).toBeTruthy();

const { meta } = sketch;
expect(Object.keys(meta.pagesAndArtboards || {}).length).toEqual(1);
const metaPage = Object.values(meta.pagesAndArtboards)[0];
expect(metaPage.name).toEqual('My Page');
expect(Object.keys(metaPage.artboards).length).toEqual(1);
const metaArtboard = Object.values(metaPage.artboards)[0];
expect(metaArtboard.name).toEqual('My Artboard');
})();

// another sketch should have correct meta
await (async () => {
const { meta } = new Sketch();
expect(Object.keys(meta.pagesAndArtboards || {}).length).toEqual(0);
})();
});
});
});
54 changes: 28 additions & 26 deletions models/Artboard/Artboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ const RulerData = require('../RulerData');
* An artboard in Sketch
*/
class Artboard extends Group {
/**
* The underlying JSON object structure in a Sketch document.
* @mixes Group.Model
* @property {boolean} shouldBreakMaskChain
* @property {RulerData.Model} horizontalRulerData
* @property {RulerData.Model} verticalRulerData
* @property {boolean} includeBackgroundColorInExport
* @property {boolean} includeInCloudUpload
* @property {boolean} isFlowHome
* @property {boolean} resizesContent
* @property {boolean} hasClickThrough
* @property {Color.Model} backgroundColor
*/
static get Model() {
return Object.assign({}, Group.Model, {
_class: 'artboard',
shouldBreakMaskChain: true,
backgroundColor: {},
hasBackgroundColor: false,
horizontalRulerData: RulerData.Model,
verticalRulerData: RulerData.Model,
includeBackgroundColorInExport: true,
includeInCloudUpload: true,
isFlowHome: false,
resizesContent: false,
});
}

/**
*
* @param {Object} args Use this when creating an artboard programmatically
Expand Down Expand Up @@ -56,30 +84,4 @@ class Artboard extends Group {
}
}

/**
* The underlying JSON object structure in a Sketch document.
* @mixes Group.Model
* @property {boolean} shouldBreakMaskChain
* @property {RulerData.Model} horizontalRulerData
* @property {RulerData.Model} verticalRulerData
* @property {boolean} includeBackgroundColorInExport
* @property {boolean} includeInCloudUpload
* @property {boolean} isFlowHome
* @property {boolean} resizesContent
* @property {boolean} hasClickThrough
* @property {Color.Model} backgroundColor
*/
Artboard.Model = Object.assign({}, Group.Model, {
_class: 'artboard',
shouldBreakMaskChain: true,
backgroundColor: {},
hasBackgroundColor: false,
horizontalRulerData: RulerData.Model,
verticalRulerData: RulerData.Model,
includeBackgroundColorInExport: true,
includeInCloudUpload: true,
isFlowHome: false,
resizesContent: false,
});

module.exports = Artboard;
22 changes: 12 additions & 10 deletions models/AttributedString/AttributedString.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ const StringAttribute = require('../StringAttribute');
* An AttributedString is a string of text that has certain parts of it with different styles.
*/
class AttributedString {
/**
* @property {String} string
* @property {StringAttribute[]} attributes
*/
static get Model() {
return {
_class: 'attributedString',
string: '',
attributes: [],
};
}

/**
*
* @param {Object} args
Expand Down Expand Up @@ -44,14 +56,4 @@ class AttributedString {
}
}

/**
* @property {String} string
* @property {StringAttribute[]} attributes
*/
AttributedString.Model = {
_class: 'attributedString',
string: '',
attributes: [],
};

module.exports = AttributedString;
35 changes: 18 additions & 17 deletions models/Border/Border.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ const Fill = require('../Fill');
* Border class
*/
class Border {
/**
* @property {boolean} isEnabled
* @property {Color.Model} color
* @property {Fill.FillType} fillType
* @property {Border.position} position
* @property {integer} thickness
*/
static get Model() {
return {
_class: 'border',
isEnabled: false,
color: Color.Model,
fillType: 0,
position: 0,
thickness: 0,
};
}

/**
*
* @param {Object} args
Expand Down Expand Up @@ -46,7 +64,6 @@ class Border {
}

/**
*
* @enum {integer}
*/
Border.Position = {
Expand All @@ -55,20 +72,4 @@ Border.Position = {
Outside: 2,
};

/**
* @property {boolean} isEnabled
* @property {Color.Model} color
* @property {Fill.FillType} fillType
* @property {Border.position} position
* @property {integer} thickness
*/
Border.Model = {
_class: 'border',
isEnabled: false,
color: Color.Model,
fillType: 0,
position: 0,
thickness: 0,
};

module.exports = Border;
30 changes: 16 additions & 14 deletions models/BorderOptions/BorderOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
* Border options
*/
class BorderOptions {
/**
* @property {boolean} isEnabled
* @property {integer[]} dashPattern
* @property {BorderOptions.LineCapStyle} lineCapStyle
* @property {BorderOptions.LineJoinStyle} lineJoinStyle
*/
static get Model() {
return {
_class: 'borderOptions',
isEnabled: true,
dashPattern: [],
lineCapStyle: 1,
lineJoinStyle: 1,
};
}

/**
*
* @param {Object} [args]
Expand Down Expand Up @@ -55,18 +71,4 @@ BorderOptions.LineJoinStyle = {
bevel: 2,
};

/**
* @property {boolean} isEnabled
* @property {integer[]} dashPattern
* @property {BorderOptions.LineCapStyle} lineCapStyle
* @property {BorderOptions.LineJoinStyle} lineJoinStyle
*/
BorderOptions.Model = {
_class: 'borderOptions',
isEnabled: true,
dashPattern: [],
lineCapStyle: 1,
lineJoinStyle: 1,
};

module.exports = BorderOptions;
30 changes: 16 additions & 14 deletions models/Color/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const TinyColor = require('tinycolor2');
* @mixes TinyColor
*/
class Color {
/**
* @property {float} alpha 0-1
* @property {float} blue 0-1
* @property {float} green 0-1
* @property {float} red 0-1
*/
static get Model() {
return {
_class: 'color',
alpha: 1,
blue: 0,
green: 0,
red: 0,
};
}

/**
*
* @param {String|Object} args This is passed to tinycolor 2. As long as tinycolor2 can understand this argument, the color will work.
Expand Down Expand Up @@ -85,18 +101,4 @@ class Color {
}
}

/**
* @property {float} alpha 0-1
* @property {float} blue 0-1
* @property {float} green 0-1
* @property {float} red 0-1
*/
Color.Model = {
_class: 'color',
alpha: 1,
blue: 0,
green: 0,
red: 0,
};

module.exports = Color;
42 changes: 22 additions & 20 deletions models/CurvePoint/CurvePoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
* CurvePoints are used to create Shapes
*/
class CurvePoint {
/**
* @property {integer} cornerRadius
* @property {String} curveFrom "{0,0}"
* @property {String} curveTo "{0,0}"
* @property {String} point "{0,0}"
* @property {integer} curveMode
* @property {boolean} hasCurveFrom
* @property {boolean} hasCurveTo
*/
static get Model() {
return {
_class: 'curvePoint',
cornerRadius: 0,
curveFrom: '{0, 0}',
curveMode: 1,
curveTo: '{0, 0}',
hasCurveFrom: false,
hasCurveTo: false,
point: '{0, 0}',
};
}

/**
*
* @param {CurvePoint.Model} args
Expand All @@ -29,24 +51,4 @@ class CurvePoint {
}
}

/**
* @property {integer} cornerRadius
* @property {String} curveFrom "{0,0}"
* @property {String} curveTo "{0,0}"
* @property {String} point "{0,0}"
* @property {integer} curveMode
* @property {boolean} hasCurveFrom
* @property {boolean} hasCurveTo
*/
CurvePoint.Model = {
_class: 'curvePoint',
cornerRadius: 0,
curveFrom: '{0, 0}',
curveMode: 1,
curveTo: '{0, 0}',
hasCurveFrom: false,
hasCurveTo: false,
point: '{0, 0}',
};

module.exports = CurvePoint;
Loading

0 comments on commit 84293db

Please sign in to comment.