Skip to content

Commit 32d81d8

Browse files
committed
switch TextStyles.create args
1 parent 2a4235d commit 32d81d8

File tree

8 files changed

+75
-69
lines changed

8 files changed

+75
-69
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). Every release
1818
- Port to TypeScript and publish TypeScript definitions
1919
- `TextStyles.get(name)` now returns text styles that are part of the document (even if they haven't been defined with `react-sketchapp`) (#407)
2020
- `getSymbolComponentByName` now returns Symbols that are part of the document (even if they haven't been defined with `react-sketchapp`) (#177)
21+
- Switch the order of the `TextStyles.create` arguments to `TextStyles.create(styles, options)`
2122

2223
## Version 3.0.0-beta.9
2324

__tests__/jest/sharedStyles/TextStyles.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable global-require */
22
let TextStyles;
3-
let ctx;
3+
let doc;
44
let sharedTextStyles;
55

66
beforeEach(() => {
@@ -12,9 +12,9 @@ beforeEach(() => {
1212

1313
TextStyles = require('../../../src/sharedStyles/TextStyles');
1414

15-
sharedTextStyles = require('../../../src/wrappers/sharedTextStyles');
15+
sharedTextStyles = require('../../../src/utils/sharedTextStyles');
1616

17-
jest.mock('../../../src/wrappers/sharedTextStyles');
17+
jest.mock('../../../src/utils/sharedTextStyles');
1818

1919
jest.mock('../../../src/jsonUtils/sketchImpl/createStringMeasurer');
2020
jest.mock('../../../src/jsonUtils/sketchImpl/findFontName');
@@ -24,46 +24,48 @@ beforeEach(() => {
2424
TextStyles = TextStyles.default;
2525
sharedTextStyles = sharedTextStyles.default;
2626

27-
sharedTextStyles.setContext = jest.fn(ctx => {
28-
if (!ctx) {
29-
throw new Error('Please provide a context');
27+
sharedTextStyles.setDocument = jest.fn(doc => {
28+
if (!doc) {
29+
throw new Error('Please provide a sketch document reference');
3030
}
3131
});
3232
sharedTextStyles.addStyle = jest.fn(() => 'styleId');
3333
sharedTextStyles.setStyles = jest.fn(() => sharedTextStyles);
3434

35-
ctx = jest.fn();
35+
doc = jest.fn();
3636
});
3737

3838
describe('create', () => {
3939
describe('without a context', () => {
4040
it('it errors', () => {
4141
const styles = {};
4242

43-
expect(() => TextStyles.create({}, styles)).toThrowError(/Please provide a context/);
43+
expect(() => TextStyles.create({}, styles)).toThrowError(
44+
/Please provide a sketch document reference/,
45+
);
4446
});
4547
});
4648

4749
describe('with a context', () => {
4850
it('clears clearExistingStyles when true', () => {
4951
TextStyles.create(
52+
{},
5053
{
5154
clearExistingStyles: true,
52-
context: ctx,
55+
document: doc,
5356
},
54-
{},
5557
);
5658

5759
expect(sharedTextStyles.setStyles).toHaveBeenCalled();
5860
});
5961

6062
it('doesn’t clearExistingStyles when false', () => {
6163
TextStyles.create(
64+
{},
6265
{
6366
clearExistingStyles: false,
64-
context: ctx,
67+
document: doc,
6568
},
66-
{},
6769
);
6870
expect(sharedTextStyles.setStyles).not.toHaveBeenCalled();
6971
});
@@ -75,7 +77,7 @@ describe('create', () => {
7577
},
7678
};
7779

78-
const res = TextStyles.create({ context: ctx }, styles);
80+
const res = TextStyles.create(styles, { document: doc });
7981

8082
expect(Object.keys(res).length).toBe(1);
8183
});
@@ -90,7 +92,7 @@ describe('create', () => {
9092
},
9193
};
9294

93-
const res = TextStyles.create({ context: ctx }, styles);
95+
const res = TextStyles.create(styles, { document: doc });
9496

9597
expect(Object.keys(res).length).toBe(2);
9698
expect(sharedTextStyles.addStyle).toHaveBeenCalledTimes(2);
@@ -106,7 +108,7 @@ describe('create', () => {
106108
},
107109
};
108110

109-
const res = TextStyles.create({ context: ctx }, styles);
111+
const res = TextStyles.create(styles, { document: doc });
110112

111113
expect(Object.keys(res).length).toBe(1);
112114
expect(sharedTextStyles.addStyle).toHaveBeenCalledTimes(2);
@@ -139,12 +141,7 @@ describe('create', () => {
139141
{},
140142
);
141143

142-
const res = TextStyles.create(
143-
{
144-
context: ctx,
145-
},
146-
{ foo: input },
147-
);
144+
const res = TextStyles.create({ foo: input }, { document: doc });
148145

149146
const firstStoredStyle = res[Object.keys(res)[0]].cssStyle;
150147

@@ -161,12 +158,7 @@ describe('create', () => {
161158

162159
describe('resolve', () => {
163160
beforeEach(() => {
164-
TextStyles.create(
165-
{
166-
context: ctx,
167-
},
168-
{},
169-
);
161+
TextStyles.create({}, { document: doc });
170162
});
171163

172164
it('retrieves a matching style', () => {
@@ -175,7 +167,7 @@ describe('resolve', () => {
175167
[key]: { fontSize: 'bar' },
176168
};
177169

178-
TextStyles.create({ context: ctx }, styles);
170+
TextStyles.create(styles, { document: doc });
179171

180172
expect(TextStyles.resolve(styles[key])).toBeDefined();
181173
expect(sharedTextStyles.addStyle).toHaveBeenCalledTimes(1);
@@ -192,7 +184,7 @@ describe('resolve', () => {
192184
fontSize: 'qux',
193185
};
194186

195-
TextStyles.create({ context: ctx }, styles);
187+
TextStyles.create(styles, { document: doc });
196188

197189
expect(TextStyles.resolve(style2)).not.toBeDefined();
198190
expect(sharedTextStyles.addStyle).toHaveBeenCalledTimes(1);
@@ -210,7 +202,7 @@ describe('get', () => {
210202
},
211203
};
212204

213-
TextStyles.create({ context: ctx }, styles);
205+
TextStyles.create(styles, { document: doc });
214206

215207
expect(TextStyles.get('foo')).toEqual(styles.foo);
216208
expect(TextStyles.get('baz')).toEqual(undefined);
@@ -223,7 +215,7 @@ describe('get', () => {
223215
},
224216
};
225217

226-
TextStyles.create({ context: ctx }, styles);
218+
TextStyles.create(styles, { document: doc });
227219

228220
expect(TextStyles.get('baz')).toEqual(undefined);
229221
});
@@ -240,7 +232,7 @@ describe('clear', () => {
240232
},
241233
};
242234

243-
TextStyles.create({ context: ctx }, styles);
235+
TextStyles.create(styles, { document: doc });
244236
TextStyles.clear();
245237

246238
expect(TextStyles.styles()).toEqual({});

docs/API.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,16 @@ StyleSheet.resolve(styles.foo);
495495

496496
An interface to Sketch's shared text styles. Create styles with or without rendering them to the document canvas.
497497

498-
### `create(options, styles)`
498+
### `create(styles, options)`
499499

500500
The primary interface to TextStyles. **Call this before rendering**.
501501

502502
#### Parameters
503503

504+
##### `styles` **(required)**
505+
506+
An object of JavaScript styles. The keys will be used as Sketch's Text Style names.
507+
504508
##### `options: { document, clearExistingStyles }`
505509

506510
###### `document`
@@ -511,10 +515,6 @@ The Sketch Document currently being rendered into.
511515

512516
Clear any styles already registered in the document.
513517

514-
##### `styles` **(required)**
515-
516-
An object of JavaScript styles. The keys will be used as Sketch's Text Style names.
517-
518518
#### Example
519519

520520
```js
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FileFormat1 as FileFormat } from '@sketch-hq/sketch-file-format-ts';
2+
import { SketchDocument, TextStyle } from '../../types';
3+
import { generateID } from '../models';
4+
5+
class TextStyles {
6+
setDocument(_doc: SketchDocument) {
7+
return this;
8+
}
9+
10+
setStyles(_styles: Array<any>) {
11+
return this;
12+
}
13+
14+
addStyle(name: string, _style: FileFormat.Style): string {
15+
return generateID(`sharedStyle:${name}`, !!name);
16+
}
17+
18+
getStyle(_name: string, _document?: SketchDocument): TextStyle | undefined {
19+
return undefined;
20+
}
21+
}
22+
23+
export default TextStyles;

src/wrappers/sharedTextStyles.ts renamed to src/jsonUtils/sketchImpl/sharedTextStyles.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import invariant from 'invariant';
2-
import { fromSJSON } from '../jsonUtils/sketchImpl/json-to-sketch';
3-
import { toSJSON } from '../jsonUtils/sketchImpl/sketch-to-json';
2+
import { fromSJSON } from './json-to-sketch';
3+
import { toSJSON } from './sketch-to-json';
44
import { FileFormat1 as FileFormat } from '@sketch-hq/sketch-file-format-ts';
5-
import { SketchDocument, TextStyle } from '../types';
6-
import { generateID } from '../jsonUtils/models';
7-
import { parseTextStyle } from '../jsonUtils/textLayers';
5+
import { SketchDocument, TextStyle } from '../../types';
6+
import { generateID } from '../models';
7+
import { parseTextStyle } from '../textLayers';
88

99
class TextStyles {
1010
_document?: SketchDocument;
@@ -92,4 +92,4 @@ class TextStyles {
9292
}
9393
}
9494

95-
export default new TextStyles();
95+
export default TextStyles;

src/sharedStyles/TextStyles.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { SketchDocumentData, SketchDocument, WrappedSketchDocument, TextStyle }
33
import { getSketchVersion } from '../utils/getSketchVersion';
44
import hashStyle from '../utils/hashStyle';
55
import { getDocument } from '../utils/getDocument';
6-
import sharedTextStyles from '../wrappers/sharedTextStyles';
6+
import sharedTextStyles from '../utils/sharedTextStyles';
77
import { makeTextStyle } from '../jsonUtils/textLayers';
88
import pick from '../utils/pick';
99
import { INHERITABLE_FONT_STYLES } from '../utils/constants';
10-
import { generateID } from '../jsonUtils/models';
1110

1211
type MurmurHash = string;
1312

@@ -29,13 +28,7 @@ const registerStyle = (name: string, style: TextStyle): void => {
2928
const safeStyle = pick(style, INHERITABLE_FONT_STYLES);
3029
const hash = hashStyle(safeStyle);
3130
const sketchStyle = makeTextStyle(safeStyle);
32-
let sharedObjectID: string;
33-
34-
if (sketchVersion !== 'NodeJS') {
35-
sharedObjectID = sharedTextStyles.addStyle(name, sketchStyle);
36-
} else {
37-
sharedObjectID = generateID(`sharedStyle:${name}`, !!name);
38-
}
31+
const sharedObjectID = sharedTextStyles.addStyle(name, sketchStyle);
3932

4033
// FIXME(gold): side effect :'(
4134
_byName[name] = hash;
@@ -53,7 +46,7 @@ type Options = {
5346
document?: SketchDocumentData | SketchDocument | WrappedSketchDocument;
5447
};
5548

56-
const create = (options: Options, styles: { [key: string]: TextStyle }): StyleHash => {
49+
const create = (styles: { [key: string]: TextStyle }, options: Options = {}): StyleHash => {
5750
const { clearExistingStyles, document } = options;
5851

5952
const doc = getDocument(document);
@@ -63,13 +56,11 @@ const create = (options: Options, styles: { [key: string]: TextStyle }): StyleHa
6356
return {};
6457
}
6558

66-
if (sketchVersion !== 'NodeJS' && doc) {
67-
sharedTextStyles.setDocument(doc);
59+
sharedTextStyles.setDocument(doc);
6860

69-
if (clearExistingStyles) {
70-
_styles = {};
71-
sharedTextStyles.setStyles([]);
72-
}
61+
if (clearExistingStyles) {
62+
_styles = {};
63+
sharedTextStyles.setStyles([]);
7364
}
7465

7566
Object.keys(styles).forEach(name => registerStyle(name, styles[name]));
@@ -95,18 +86,12 @@ const get = (
9586
return style.cssStyle;
9687
}
9788

98-
if (sketchVersion !== 'NodeJS') {
99-
return sharedTextStyles.getStyle(name, document ? getDocument(document) : undefined);
100-
}
101-
102-
return undefined;
89+
return sharedTextStyles.getStyle(name, document ? getDocument(document) : undefined);
10390
};
10491

10592
const clear = () => {
10693
_styles = {};
107-
if (sketchVersion !== 'NodeJS') {
108-
sharedTextStyles.setStyles([]);
109-
}
94+
sharedTextStyles.setStyles([]);
11095
};
11196

11297
const toJSON = (): FileFormat.SharedStyle[] =>

src/utils/getDocument.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const getDocument = (
1919
document?: SketchDocumentData | SketchDocument | WrappedSketchDocument,
2020
): SketchDocument | undefined => {
2121
const documentData = getDocumentData(document);
22-
if (!documentData) {
23-
return undefined;
22+
if (typeof documentData === 'undefined' || !('delegate' in documentData)) {
23+
return documentData;
2424
}
2525

2626
return documentData.delegate();

src/utils/sharedTextStyles.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getSketchVersion } from './getSketchVersion';
2+
import SketchStyles from '../jsonUtils/sketchImpl/sharedTextStyles';
3+
import NodeStyles from '../jsonUtils/nodeImpl/sharedTextStyles';
4+
5+
export default getSketchVersion() === 'NodeJS' ? new NodeStyles() : new SketchStyles();

0 commit comments

Comments
 (0)