@@ -8,8 +8,7 @@ use std::collections::HashSet;
88use std:: sync:: Mutex ;
99use wasm_bindgen:: prelude:: * ;
1010
11- static GLOBAL_STYLE_SHEET : Lazy < Mutex < StyleSheet > > =
12- Lazy :: new ( || Mutex :: new ( StyleSheet :: default ( ) ) ) ;
11+ static GLOBAL_STYLE_SHEET : Lazy < Mutex < StyleSheet > > = Lazy :: new ( || Mutex :: new ( StyleSheet :: default ( ) ) ) ;
1312
1413#[ wasm_bindgen]
1514pub struct Output {
@@ -19,48 +18,25 @@ pub struct Output {
1918 updated_base_style : bool ,
2019 css : Option < String > ,
2120}
22- #[ wasm_bindgen]
23- extern "C" {
24- #[ wasm_bindgen( js_namespace = console) ]
25- fn log ( s : & JsValue ) ;
26- #[ wasm_bindgen( js_namespace = console, js_name = log) ]
27- fn log_str ( s : & str ) ;
28- #[ wasm_bindgen( js_namespace = console, js_name = time) ]
29- fn time ( s : & str ) ;
30- #[ wasm_bindgen( js_namespace = console, js_name = timeEnd) ]
31- fn time_end ( s : & str ) ;
32- }
21+ // #[wasm_bindgen]
22+ // extern "C" {
23+ // #[wasm_bindgen(js_namespace = console)]
24+ // fn log(s: &JsValue);
25+ // #[wasm_bindgen(js_namespace = console, js_name = log)]
26+ // fn log_str(s: &str);
27+ // #[wasm_bindgen(js_namespace = console, js_name = time)]
28+ // fn time(s: &str);
29+ // #[wasm_bindgen(js_namespace = console, js_name = timeEnd)]
30+ // fn time_end(s: &str);
31+ // }
3332
3433#[ wasm_bindgen]
3534impl Output {
36- fn new (
37- code : String ,
38- styles : HashSet < ExtractStyleValue > ,
39- map : Option < String > ,
40- single_css : bool ,
41- filename : String ,
42- css_file : Option < String > ,
43- import_main_css : bool ,
44- ) -> Self {
35+ fn new ( code : String , styles : HashSet < ExtractStyleValue > , map : Option < String > , single_css : bool , filename : String , css_file : Option < String > , import_main_css : bool ) -> Self {
4536 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
4637 let default_collected = sheet. rm_global_css ( & filename, single_css) ;
4738 let ( collected, updated_base_style) = sheet. update_styles ( & styles, & filename, single_css) ;
48- Self {
49- code,
50- map,
51- css_file,
52- updated_base_style : updated_base_style || default_collected,
53- css : {
54- if !collected && !default_collected {
55- None
56- } else {
57- Some ( sheet. create_css (
58- if !single_css { Some ( & filename) } else { None } ,
59- import_main_css,
60- ) )
61- }
62- } ,
63- }
39+ Self { code, map, css_file, updated_base_style : updated_base_style || default_collected, css : { if !collected && !default_collected { None } else { Some ( sheet. create_css ( if !single_css { Some ( & filename) } else { None } , import_main_css) ) } } }
6440 }
6541
6642 /// Get the code
@@ -103,23 +79,18 @@ pub fn is_debug() -> bool {
10379
10480#[ wasm_bindgen( js_name = "importSheet" ) ]
10581pub fn import_sheet ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
106- * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = serde_wasm_bindgen:: from_value ( sheet_object)
107- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
82+ * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?;
10883 Ok ( ( ) )
10984}
11085
11186#[ wasm_bindgen( js_name = "exportSheet" ) ]
11287pub fn export_sheet ( ) -> Result < String , JsValue > {
113- serde_json:: to_string ( & * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) )
114- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) )
88+ serde_json:: to_string ( & * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) )
11589}
11690
11791#[ wasm_bindgen( js_name = "importClassMap" ) ]
11892pub fn import_class_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
119- set_class_map (
120- serde_wasm_bindgen:: from_value ( sheet_object)
121- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
122- ) ;
93+ set_class_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?) ;
12394 Ok ( ( ) )
12495}
12596
@@ -130,10 +101,7 @@ pub fn export_class_map() -> Result<String, JsValue> {
130101
131102#[ wasm_bindgen( js_name = "importFileMap" ) ]
132103pub fn import_file_map ( sheet_object : JsValue ) -> Result < ( ) , JsValue > {
133- set_file_map (
134- serde_wasm_bindgen:: from_value ( sheet_object)
135- . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?,
136- ) ;
104+ set_file_map ( serde_wasm_bindgen:: from_value ( sheet_object) . map_err ( |e| JsValue :: from_str ( & e. to_string ( ) ) ) ?) ;
137105 Ok ( ( ) )
138106}
139107
@@ -143,44 +111,16 @@ pub fn export_file_map() -> Result<String, JsValue> {
143111}
144112
145113#[ wasm_bindgen( js_name = "codeExtract" ) ]
146- pub fn code_extract (
147- filename : & str ,
148- code : & str ,
149- package : & str ,
150- css_dir : String ,
151- single_css : bool ,
152- import_main_css_in_code : bool ,
153- import_main_css_in_css : bool ,
154- ) -> Result < Output , JsValue > {
155- match extract (
156- filename,
157- code,
158- ExtractOption {
159- package : package. to_string ( ) ,
160- css_dir,
161- single_css,
162- import_main_css : import_main_css_in_code,
163- } ,
164- ) {
165- Ok ( output) => Ok ( Output :: new (
166- output. code ,
167- output. styles ,
168- output. map ,
169- single_css,
170- filename. to_string ( ) ,
171- output. css_file ,
172- import_main_css_in_css,
173- ) ) ,
114+ pub fn code_extract ( filename : & str , code : & str , package : & str , css_dir : String , single_css : bool , import_main_css_in_code : bool , import_main_css_in_css : bool ) -> Result < Output , JsValue > {
115+ match extract ( filename, code, ExtractOption { package : package. to_string ( ) , css_dir, single_css, import_main_css : import_main_css_in_code } ) {
116+ Ok ( output) => Ok ( Output :: new ( output. code , output. styles , output. map , single_css, filename. to_string ( ) , output. css_file , import_main_css_in_css) ) ,
174117 Err ( error) => Err ( JsValue :: from_str ( error. to_string ( ) . as_str ( ) ) ) ,
175118 }
176119}
177120
178121#[ wasm_bindgen( js_name = "registerTheme" ) ]
179122pub fn register_theme ( theme_object : JsValue ) -> Result < ( ) , JsValue > {
180- GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme (
181- serde_wasm_bindgen:: from_value ( theme_object)
182- . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?,
183- ) ;
123+ GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme ( serde_wasm_bindgen:: from_value ( theme_object) . map_err ( |e| JsValue :: from_str ( e. to_string ( ) . as_str ( ) ) ) ?) ;
184124 Ok ( ( ) )
185125}
186126
@@ -193,26 +133,13 @@ pub fn get_default_theme() -> Result<Option<String>, JsValue> {
193133#[ wasm_bindgen( js_name = "getCss" ) ]
194134pub fn get_css ( file_num : Option < usize > , import_main_css : bool ) -> Result < String , JsValue > {
195135 let sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
196- Ok ( sheet. create_css (
197- file_num. map ( get_filename_by_file_num) . as_deref ( ) ,
198- import_main_css,
199- ) )
136+ Ok ( sheet. create_css ( file_num. map ( get_filename_by_file_num) . as_deref ( ) , import_main_css) )
200137}
201138
202139#[ wasm_bindgen( js_name = "getThemeInterface" ) ]
203- pub fn get_theme_interface (
204- package_name : & str ,
205- color_interface_name : & str ,
206- typography_interface_name : & str ,
207- theme_interface_name : & str ,
208- ) -> String {
140+ pub fn get_theme_interface ( package_name : & str , color_interface_name : & str , typography_interface_name : & str , theme_interface_name : & str ) -> String {
209141 let sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
210- sheet. create_interface (
211- package_name,
212- color_interface_name,
213- typography_interface_name,
214- theme_interface_name,
215- )
142+ sheet. create_interface ( package_name, color_interface_name, typography_interface_name, theme_interface_name)
216143}
217144#[ cfg( test) ]
218145mod tests {
@@ -231,10 +158,7 @@ mod tests {
231158 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
232159 * sheet = StyleSheet :: default ( ) ;
233160 }
234- assert_eq ! (
235- get_css( None , false ) . unwrap( ) . split( "*/" ) . nth( 1 ) . unwrap( ) ,
236- ""
237- ) ;
161+ assert_eq ! ( get_css( None , false ) . unwrap( ) . split( "*/" ) . nth( 1 ) . unwrap( ) , "" ) ;
238162
239163 {
240164 let mut sheet = GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) ;
@@ -347,48 +271,15 @@ mod tests {
347271 let mut color_theme = ColorTheme :: default ( ) ;
348272 color_theme. add_color ( "primary" , "#fff" ) ;
349273 theme. add_color_theme ( "dark" , color_theme) ;
350- theme. add_typography (
351- "default" ,
352- vec ! [
353- Some ( Typography :: new(
354- Some ( "Arial" . to_string( ) ) ,
355- Some ( "16px" . to_string( ) ) ,
356- Some ( "400" . to_string( ) ) ,
357- Some ( "1.5" . to_string( ) ) ,
358- Some ( "0.5" . to_string( ) ) ,
359- ) ) ,
360- Some ( Typography :: new(
361- Some ( "Arial" . to_string( ) ) ,
362- Some ( "24px" . to_string( ) ) ,
363- Some ( "400" . to_string( ) ) ,
364- Some ( "1.5" . to_string( ) ) ,
365- Some ( "0.5" . to_string( ) ) ,
366- ) ) ,
367- ] ,
368- ) ;
274+ theme. add_typography ( "default" , vec ! [ Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "16px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) , Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "24px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
369275
370- theme. add_typography (
371- "default1" ,
372- vec ! [
373- None ,
374- Some ( Typography :: new(
375- Some ( "Arial" . to_string( ) ) ,
376- Some ( "24px" . to_string( ) ) ,
377- Some ( "400" . to_string( ) ) ,
378- Some ( "1.5" . to_string( ) ) ,
379- Some ( "0.5" . to_string( ) ) ,
380- ) ) ,
381- ] ,
382- ) ;
276+ theme. add_typography ( "default1" , vec ! [ None , Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "24px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
383277 let css = theme. to_css ( ) ;
384278 assert_debug_snapshot ! ( css) ;
385279
386280 assert_eq ! ( Theme :: default ( ) . to_css( ) , "" ) ;
387281 let mut theme = Theme :: default ( ) ;
388- theme. add_typography (
389- "default" ,
390- vec ! [ Some ( Typography :: new( None , None , None , None , None ) ) ] ,
391- ) ;
282+ theme. add_typography ( "default" , vec ! [ Some ( Typography :: new( None , None , None , None , None ) ) ] ) ;
392283 assert_eq ! ( theme. to_css( ) , "" ) ;
393284
394285 let mut theme = Theme :: default ( ) ;
@@ -562,58 +453,25 @@ mod tests {
562453 #[ serial]
563454 fn test_get_theme_interface ( ) {
564455 let sheet = StyleSheet :: default ( ) ;
565- assert_eq ! (
566- sheet. create_interface(
567- "package" ,
568- "ColorInterface" ,
569- "TypographyInterface" ,
570- "ThemeInterface"
571- ) ,
572- ""
573- ) ;
456+ assert_eq ! ( sheet. create_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "" ) ;
574457
575458 let mut theme = Theme :: default ( ) ;
576459 let mut color_theme = ColorTheme :: default ( ) ;
577460 color_theme. add_color ( "primary" , "#000" ) ;
578461 theme. add_color_theme ( "dark" , color_theme) ;
579462 GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) . set_theme ( theme) ;
580- assert_eq ! (
581- get_theme_interface(
582- "package" ,
583- "ColorInterface" ,
584- "TypographyInterface" ,
585- "ThemeInterface"
586- ) ,
587- "import \" package\" ;declare module \" package\" {interface ColorInterface{$primary:null;}interface TypographyInterface{}interface ThemeInterface{dark:null;}}"
588- ) ;
463+ assert_eq ! ( get_theme_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "import \" package\" ;declare module \" package\" {interface ColorInterface{$primary:null;}interface TypographyInterface{}interface ThemeInterface{dark:null;}}" ) ;
589464
590465 // test wrong case
591466 let mut sheet = StyleSheet :: default ( ) ;
592467 let mut theme = Theme :: default ( ) ;
593468 let mut color_theme = ColorTheme :: default ( ) ;
594469 color_theme. add_color ( "(primary)" , "#000" ) ;
595470 theme. add_color_theme ( "dark" , color_theme) ;
596- theme. add_typography (
597- "prim``ary" ,
598- vec ! [ Some ( Typography :: new(
599- Some ( "Arial" . to_string( ) ) ,
600- Some ( "16px" . to_string( ) ) ,
601- Some ( "400" . to_string( ) ) ,
602- Some ( "1.5" . to_string( ) ) ,
603- Some ( "0.5" . to_string( ) ) ,
604- ) ) ] ,
605- ) ;
471+ theme. add_typography ( "prim``ary" , vec ! [ Some ( Typography :: new( Some ( "Arial" . to_string( ) ) , Some ( "16px" . to_string( ) ) , Some ( "400" . to_string( ) ) , Some ( "1.5" . to_string( ) ) , Some ( "0.5" . to_string( ) ) ) ) ] ) ;
606472 sheet. set_theme ( theme) ;
607473 * GLOBAL_STYLE_SHEET . lock ( ) . unwrap ( ) = sheet;
608- assert_eq ! (
609- get_theme_interface(
610- "package" ,
611- "ColorInterface" ,
612- "TypographyInterface" ,
613- "ThemeInterface"
614- ) ,
615- "import \" package\" ;declare module \" package\" {interface ColorInterface{[`$(primary)`]:null;}interface TypographyInterface{[`prim\\ `\\ `ary`]:null;}interface ThemeInterface{dark:null;}}"
616- ) ;
474+ assert_eq ! ( get_theme_interface( "package" , "ColorInterface" , "TypographyInterface" , "ThemeInterface" ) , "import \" package\" ;declare module \" package\" {interface ColorInterface{[`$(primary)`]:null;}interface TypographyInterface{[`prim\\ `\\ `ary`]:null;}interface ThemeInterface{dark:null;}}" ) ;
617475 }
618476
619477 #[ test]
0 commit comments