@@ -14,7 +14,6 @@ import sharedTextStyles from '../utils/sharedTextStyles';
1414import { makeTextStyle } from '../jsonUtils/textLayers' ;
1515import pick from '../utils/pick' ;
1616import { INHERITABLE_FONT_STYLES } from '../utils/constants' ;
17- import getDefaultBridge from '../platformBridges/getDefaultBridge' ;
1817
1918type MurmurHash = string ;
2019
@@ -27,104 +26,100 @@ type RegisteredStyle = {
2726
2827type StyleHash = { [ key : string ] : RegisteredStyle } ;
2928
30- let _styles : StyleHash = { } ;
31- const _byName : { [ key : string ] : MurmurHash } = { } ;
32-
33- const registerStyle = (
34- name : string ,
35- style : TextStyle ,
36- platformBridge : PlatformBridge = getDefaultBridge ( ) ,
37- ) : void => {
38- const safeStyle = pick ( style , INHERITABLE_FONT_STYLES ) ;
39- const hash = hashStyle ( safeStyle ) ;
40- const sketchStyle = makeTextStyle ( safeStyle , null , platformBridge ) ;
41- const sharedObjectID = sharedTextStyles . addStyle ( name , sketchStyle ) ;
42-
43- // FIXME(gold): side effect :'(
44- _byName [ name ] = hash ;
45-
46- _styles [ hash ] = {
47- cssStyle : safeStyle ,
48- name,
49- sketchStyle,
50- sharedObjectID,
51- } ;
52- } ;
53-
5429type Options = {
5530 clearExistingStyles ?: boolean ;
5631 document ?: SketchDocumentData | SketchDocument | WrappedSketchDocument ;
5732} ;
5833
59- const create = (
60- styles : { [ key : string ] : TextStyle } ,
61- options : Options = { } ,
62- platformBridge : PlatformBridge = getDefaultBridge ( ) ,
63- ) : StyleHash => {
64- const { clearExistingStyles, document } = options ;
65-
66- const doc = getDocument ( document ) ;
67-
68- if ( isRunningInSketch ( ) && parseInt ( getSketchVersion ( ) ) < 50 ) {
69- doc . showMessage ( '💎 Requires Sketch 50+ 💎' ) ;
70- return { } ;
71- }
72-
73- sharedTextStyles . setDocument ( doc ) ;
34+ let _styles : StyleHash = { } ;
35+ const _byName : { [ key : string ] : MurmurHash } = { } ;
7436
75- if ( clearExistingStyles ) {
37+ const TextStyles = ( getDefaultBridge : ( ) => PlatformBridge ) => ( {
38+ registerStyle (
39+ name : string ,
40+ style : TextStyle ,
41+ platformBridge : PlatformBridge = getDefaultBridge ( ) ,
42+ ) {
43+ const safeStyle = pick ( style , INHERITABLE_FONT_STYLES ) ;
44+ const hash = hashStyle ( safeStyle ) ;
45+ const sketchStyle = makeTextStyle ( safeStyle , null , platformBridge ) ;
46+ const sharedObjectID = sharedTextStyles . addStyle ( name , sketchStyle ) ;
47+
48+ // FIXME(gold): side effect :'(
49+ _byName [ name ] = hash ;
50+
51+ _styles [ hash ] = {
52+ cssStyle : safeStyle ,
53+ name,
54+ sketchStyle,
55+ sharedObjectID,
56+ } ;
57+ } ,
58+
59+ create (
60+ styles : { [ key : string ] : TextStyle } ,
61+ options : Options = { } ,
62+ platformBridge : PlatformBridge = getDefaultBridge ( ) ,
63+ ) {
64+ const { clearExistingStyles, document } = options ;
65+
66+ const doc = getDocument ( document ) ;
67+
68+ if ( isRunningInSketch ( ) && parseInt ( getSketchVersion ( ) ) < 50 ) {
69+ doc . showMessage ( '💎 Requires Sketch 50+ 💎' ) ;
70+ return { } ;
71+ }
72+
73+ sharedTextStyles . setDocument ( doc ) ;
74+
75+ if ( clearExistingStyles ) {
76+ _styles = { } ;
77+ sharedTextStyles . setStyles ( [ ] ) ;
78+ }
79+
80+ Object . keys ( styles ) . forEach ( name => this . registerStyle ( name , styles [ name ] , platformBridge ) ) ;
81+
82+ return _styles ;
83+ } ,
84+
85+ resolve ( style : TextStyle ) : RegisteredStyle | undefined {
86+ const safeStyle = pick ( style , INHERITABLE_FONT_STYLES ) ;
87+ const hash = hashStyle ( safeStyle ) ;
88+
89+ return _styles [ hash ] ;
90+ } ,
91+
92+ get (
93+ name : string ,
94+ document ?: SketchDocumentData | SketchDocument | WrappedSketchDocument ,
95+ ) : TextStyle | undefined {
96+ const hash = _byName [ name ] ;
97+ const style = _styles [ hash ] ;
98+
99+ if ( style ) {
100+ return style . cssStyle ;
101+ }
102+
103+ return sharedTextStyles . getStyle ( name , document ? getDocument ( document ) : undefined ) ;
104+ } ,
105+
106+ clear ( ) : void {
76107 _styles = { } ;
77108 sharedTextStyles . setStyles ( [ ] ) ;
78- }
79-
80- Object . keys ( styles ) . forEach ( name => registerStyle ( name , styles [ name ] , platformBridge ) ) ;
81-
82- return _styles ;
83- } ;
84-
85- const resolve = ( style : TextStyle ) : RegisteredStyle | undefined => {
86- const safeStyle = pick ( style , INHERITABLE_FONT_STYLES ) ;
87- const hash = hashStyle ( safeStyle ) ;
88-
89- return _styles [ hash ] ;
90- } ;
91-
92- const get = (
93- name : string ,
94- document ?: SketchDocumentData | SketchDocument | WrappedSketchDocument ,
95- ) : TextStyle | undefined => {
96- const hash = _byName [ name ] ;
97- const style = _styles [ hash ] ;
98-
99- if ( style ) {
100- return style . cssStyle ;
101- }
102-
103- return sharedTextStyles . getStyle ( name , document ? getDocument ( document ) : undefined ) ;
104- } ;
105-
106- const clear = ( ) => {
107- _styles = { } ;
108- sharedTextStyles . setStyles ( [ ] ) ;
109- } ;
110-
111- const toJSON = ( ) : FileFormat . SharedStyle [ ] =>
112- Object . keys ( _styles ) . map ( k => ( {
113- _class : 'sharedStyle' ,
114- do_objectID : _styles [ k ] . sharedObjectID ,
115- name : _styles [ k ] . name ,
116- value : _styles [ k ] . sketchStyle ,
117- } ) ) ;
118-
119- const styles = ( ) => _styles ;
120-
121- const TextStyles = {
122- create,
123- resolve,
124- get,
125- styles,
126- clear,
127- toJSON,
128- } ;
109+ } ,
110+
111+ toJSON ( ) : FileFormat . SharedStyle [ ] {
112+ return Object . keys ( _styles ) . map ( k => ( {
113+ _class : 'sharedStyle' ,
114+ do_objectID : _styles [ k ] . sharedObjectID ,
115+ name : _styles [ k ] . name ,
116+ value : _styles [ k ] . sketchStyle ,
117+ } ) ) ;
118+ } ,
119+
120+ styles ( ) {
121+ return _styles ;
122+ } ,
123+ } ) ;
129124
130125export default TextStyles ;
0 commit comments