Skip to content

Commit

Permalink
feat: added Slice shape (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
L2jLiga committed Jul 27, 2020
1 parent c155fd5 commit fcb880c
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 42 deletions.
5 changes: 1 addition & 4 deletions models/Bitmap/Bitmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class Bitmap extends Layer {
style: new Style(args.style),
layers: args.layers || [],
});
const fileHash = crypto
.createHash('sha1')
.update(args.filePath)
.digest('hex');
const fileHash = crypto.createHash('sha1').update(args.filePath).digest('hex');
this.image._ref = `images/${fileHash}.png`;
fs.ensureDirSync(STORAGE_IMG_DIR);
fs.copyFileSync(args.filePath, `${STORAGE_DIR}/${this.image._ref}`);
Expand Down
4 changes: 2 additions & 2 deletions models/Color/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class Color {
}
// Map all tinycolor's methods
// TODO: should check if this will cause performance issues
Object.keys(TinyColor.prototype).forEach(key => {
this[key] = arg => {
Object.keys(TinyColor.prototype).forEach((key) => {
this[key] = (arg) => {
const ret = this._getTinyColor()[key](arg);
// If the return value is a TinyColor class,
// it is modifying the value
Expand Down
6 changes: 3 additions & 3 deletions models/Document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class Document {
Object.assign(this, json);
// Create nested classes
this.layerTextStyles.objects = this.layerTextStyles.objects.map(
sharedStyle => new SharedStyle(null, sharedStyle)
(sharedStyle) => new SharedStyle(null, sharedStyle)
);
this.layerStyles.objects = this.layerStyles.objects.map(sharedStyle => new SharedStyle(null, sharedStyle));
this.layerStyles.objects = this.layerStyles.objects.map((sharedStyle) => new SharedStyle(null, sharedStyle));
} else {
const id = args.id || uuid().toUpperCase();

Expand All @@ -89,7 +89,7 @@ class Document {
* @returns {SharedStyle}
*/
getLayerStyle(name) {
return this.layerStyles.objects.find(style => style.name === name);
return this.layerStyles.objects.find((style) => style.name === name);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions models/Layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Layer {
// getGroup(id) {}

getGroups() {
return this.layers.filter(layer => layer._class === 'group');
return this.layers.filter((layer) => layer._class === 'group');
}

/**
Expand All @@ -112,7 +112,7 @@ class Layer {
}
const childLayers = current.getLayers();
if (childLayers !== undefined) {
childLayers.forEach(layer => {
childLayers.forEach((layer) => {
list.push(layer);
getRecursiveLayers(layer, list);
});
Expand Down
6 changes: 3 additions & 3 deletions models/Layer/Layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('getLayers', () => {
group.addLayer(text1);
group.addLayer(text2);

const layers = group.getLayers().map(l => l.name);
const layers = group.getLayers().map((l) => l.name);

expect(layers.sort()).toEqual(['text1', 'text2'].sort());
});
Expand All @@ -55,7 +55,7 @@ describe('getLayers', () => {
innerGroup.addLayer(text);
outerGroup.addLayer(innerGroup);

const layers = outerGroup.getLayers().map(l => l.name);
const layers = outerGroup.getLayers().map((l) => l.name);

expect(layers.sort()).toEqual(['inner group'].sort());
});
Expand All @@ -77,7 +77,7 @@ describe('getAllLayers', () => {
innerGroup.addLayer(text);
outerGroup.addLayer(innerGroup);

const layers = outerGroup.getAllLayers().map(l => l.name);
const layers = outerGroup.getAllLayers().map((l) => l.name);

expect(layers.sort()).toEqual(['inner group', 'text'].sort());
});
Expand Down
4 changes: 2 additions & 2 deletions models/Page/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Page extends Group {
* @returns {Artboard[]}
*/
getArtboards(predicate) {
const arr = this.layers.filter(layer => layer._class === 'artboard');
const arr = this.layers.filter((layer) => layer._class === 'artboard');
if (predicate) {
return arr.filter(predicate);
}
Expand All @@ -70,7 +70,7 @@ class Page extends Group {
* @returns {Artboard}
*/
getArtboard(name) {
return this.getArtboards().find(artboard => artboard.name === name);
return this.getArtboards().find((artboard) => artboard.name === name);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion models/Sketch/Sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Sketch {
})
.pipe(fs.createWriteStream(output))
.on('error', reject)
.on('finish', function () {
.on('finish', () => {
fs.removeSync(STORAGE_DIR);
resolve(output);
})
Expand Down
20 changes: 10 additions & 10 deletions models/Sketch/Sketch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,37 @@ describe('Sketch', () => {
});

describe('build', () => {
it('should work', async done => {
it('should work', async (done) => {
const sketch = await Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`);
expect(sketch).toBeDefined();
done();
});

it('should work when file is Buffer', async done => {
it('should work when file is Buffer', async (done) => {
const fileBuffer = fs.readFileSync(`${process.cwd()}/__tests__/__sketch-files/test.sketch`);
const sketch = await Sketch.fromFile(fileBuffer);
expect(sketch).toBeDefined();
done();
});

it('should have the same do_objectID', () =>
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`).then(sketch => {
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`).then((sketch) => {
const artboard = sketch.getPage('Page 1').getArtboard('Artboard');
expect(artboard.do_objectID).toBe('5863D14C-8C6E-4C73-801F-A5BF691CF415');
}));

it('should build', () =>
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`)
.then(sketch => sketch.build(`${process.cwd()}/__tests__/__output/test2.sketch`))
.then(output => {
.then((sketch) => sketch.build(`${process.cwd()}/__tests__/__output/test2.sketch`))
.then((output) => {
expect(output).toEqual(`${process.cwd()}/__tests__/__output/test2.sketch`);
}));
});

describe('add shared text style', () => {
it('should work', () =>
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`)
.then(sketch => {
.then((sketch) => {
const textStyle = SharedStyle.TextStyle({
name: 'foobar',
});
Expand All @@ -79,15 +79,15 @@ describe('Sketch', () => {
sketch.document.addLayerStyle(layerStyle);
return sketch.build(`${process.cwd()}/__tests__/__output/test3.sketch`);
})
.then(output => {
.then((output) => {
expect(output).toEqual(`${process.cwd()}/__tests__/__output/test3.sketch`);
}));
});

describe('add page', () => {
it('should work', () =>
Sketch.fromFile(`${process.cwd()}/__tests__/__sketch-files/test.sketch`)
.then(sketch => {
.then((sketch) => {
const page = new Page({
name: 'my page',
});
Expand All @@ -102,14 +102,14 @@ describe('Sketch', () => {
sketch.addArtboard(page.getID(), artboard);
return sketch.build(`${process.cwd()}/__tests__/__output/test4.sketch`);
})
.then(output => {
.then((output) => {
expect(output).toEqual(`${process.cwd()}/__tests__/__output/test4.sketch`);
}));
});

describe('from extracted file', () => {
it('should work', () => {
Sketch.fromExtractedFile(`${process.cwd()}/__tests__/__sketch-files/test-sketch`).then(sketch => {
Sketch.fromExtractedFile(`${process.cwd()}/__tests__/__sketch-files/test-sketch`).then((sketch) => {
expect(sketch).toBeDefined();
expect(sketch.getPages().length).toBe(2);
});
Expand Down
73 changes: 73 additions & 0 deletions models/Slice/Slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 Color = require('../Color');
const Style = require('../Style');
const Rect = require('../Rect');
const ExportOptions = require('../ExportOptions');

class Slice {
static get Model() {
return {
_class: 'slice',
booleanOperation: -1,
isFixedToViewport: false,
isFlippedHorizontal: false,
isFlippedVertical: false,
isLocked: false,
isVisible: true,
layerListExpandedType: 1,
nameIsFixed: false,
resizingConstraint: 63,
resizingType: 0,
rotation: 0,
shouldBreakMaskChain: false,
hasBackgroundColor: false,
};
}

/**
* @param {Object} args
* @param {Object} json
*/
constructor(args, json) {
if (json) {
Object.assign(this, json);
this.exportOptions = new ExportOptions(null, json.exportOptions);
this.frame = new Rect(null, json.frame);
this.style = new Style(null, json.style);
this.backgroundColor = new Color(null, json.backgroundColor);
}
if (args) {
const id = args.id || uuid().toUpperCase();

Object.assign(this, {
...Slice.Model,
...args,
name: args.name || id,
do_objectID: id,
exportOptions: new ExportOptions(args.exportOptions),
frame: new Rect(args.frame),
style: new Style(args.style),
backgroundColor: new Color(args.color),
});
}
}

static fromJSON(json) {
return new Slice(null, json);
}
}

module.exports = Slice;
37 changes: 37 additions & 0 deletions models/Slice/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Color from '../Color';
import ExportOptions from '../ExportOptions';
import Rect from '../Rect';
import Style from '../Style';

declare class Slice {
_class: 'slice';
do_objectID: string;
booleanOperation: number;
exportOptions: ExportOptions;
frame: Rect;
isFixedToViewport: boolean;
isFlippedHorizontal: boolean;
isFlippedVertical: boolean;
isLocked: boolean;
isVisible: boolean;
layerListExpandedType: number;
name: string;
nameIsFixed: boolean;
resizingConstraint: number;
resizingType: number;
rotation: number;
shouldBreakMaskChain: boolean;
style: Style;
hasBackgroundColor: boolean;
backgroundColor: Color;

constructor(args: Partial<Slice>)
constructor(args: any)
constructor(args: null | undefined, json: Slice)
constructor(args: null | undefined, json: any)

static fromJSON(json: Slice): Slice
static fromJSON(json: any): Slice
}

export = Slice;
14 changes: 14 additions & 0 deletions models/Slice/index.js
Original file line number Diff line number Diff line change
@@ -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('./Slice');
16 changes: 8 additions & 8 deletions models/Style/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class Style {
const id = args.id || uuid().toUpperCase();
return new Style({
id,
fills: (args.fills || []).map(fill => new Fill(fill)),
borders: (args.borders || []).map(border => new Border(border)),
fills: (args.fills || []).map((fill) => new Fill(fill)),
borders: (args.borders || []).map((border) => new Border(border)),
});
}

Expand All @@ -76,16 +76,16 @@ class Style {
if (json) {
Object.assign(this, json);
if (this.textStyle) this.textStyle = new TextStyle(null, this.textStyle);
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.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));
} else {
const id = args.id || uuid().toUpperCase();
Object.assign(this, Style.Model, {
do_objectID: id,
borders: (args.borders || []).map(border => new Border(border)),
fills: (args.fills || []).map(fill => new Fill(fill)),
shadows: (args.shadows || []).map(shadow => new Shadow(shadow)),
borders: (args.borders || []).map((border) => new Border(border)),
fills: (args.fills || []).map((fill) => new Fill(fill)),
shadows: (args.shadows || []).map((shadow) => new Shadow(shadow)),
textStyle: args.textStyle ? new TextStyle(args.textStyle) : undefined,
});
}
Expand Down

0 comments on commit fcb880c

Please sign in to comment.