22
33use std:: { cell:: RefCell , collections:: HashMap , rc:: Rc } ;
44
5- use lightningcss:: { printer:: PrinterOptions , properties:: { animation, Property } , traits:: ToCss , values:: time} ;
5+ use lightningcss:: { printer:: PrinterOptions , properties:: { animation, Property } , traits:: ToCss , values:: { easing :: EasingFunction , time} } ;
66
77use crate :: { generate_expr_lit_num, generate_invalid_expr, style_parser:: KeyFrameItem , visitor:: parse_style_values} ;
88use swc_core:: { common:: DUMMY_SP , ecma:: ast:: * } ;
@@ -15,7 +15,8 @@ pub struct Animation {
1515 pub animation_name : Option < String > ,
1616 pub animation_duration : f32 ,
1717 pub animation_delay : f32 ,
18- pub animation_iteration : f32
18+ pub animation_iteration : f32 ,
19+ pub animation_timeing_function : EasingFunction
1920 // pub value: Option<Vec<KeyFrameItem>>
2021}
2122
@@ -27,6 +28,7 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
2728 let mut animation_duration: f32 = 0.0 ;
2829 let mut animation_delay: f32 = 0.0 ;
2930 let mut animation_iteration: f32 = 1.0 ;
31+ let mut animation_timeing_function: EasingFunction = EasingFunction :: Ease ;
3032
3133 match value. 1 {
3234 // Property::AnimationName(_, _) => todo!(),
@@ -51,7 +53,9 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
5153 animation_iteration = match animation. iteration_count {
5254 animation:: AnimationIterationCount :: Number ( num) => num,
5355 animation:: AnimationIterationCount :: Infinite => -1.0 ,
54- }
56+ } ;
57+
58+ animation_timeing_function = animation. timing_function . clone ( ) ;
5559 } ) ;
5660 } ,
5761 _ => { }
@@ -63,7 +67,8 @@ impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>
6367 animation_name,
6468 animation_duration,
6569 animation_delay,
66- animation_iteration
70+ animation_iteration,
71+ animation_timeing_function
6772 }
6873
6974 }
@@ -76,7 +81,7 @@ impl ToExpr for Animation {
7681 let keyframe_map = self . keyframs . borrow ( ) ;
7782 if let Some ( keyframe_items) = keyframe_map. get ( name) {
7883
79-
84+ let mut mut_percentage = 0.0 ;
8085
8186 return PropertyTuple :: One (
8287 "animation" . to_string ( ) ,
@@ -108,7 +113,9 @@ impl ToExpr for Animation {
108113 value: Box :: new( Expr :: Array ( ArrayLit {
109114 span: DUMMY_SP ,
110115 elems: keyframe_items. into_iter( ) . map( |item| {
111- Some ( ExprOrSpread {
116+ let item_duration = ( item. percentage - mut_percentage) * self . animation_duration * 1000.0 ;
117+ mut_percentage = item. percentage;
118+ return Some ( ExprOrSpread {
112119 spread: None ,
113120 expr: Box :: new( Expr :: Object ( ObjectLit {
114121 span: DUMMY_SP ,
@@ -117,6 +124,10 @@ impl ToExpr for Animation {
117124 key: PropName :: Str ( "percentage" . into( ) ) ,
118125 value: Box :: new( generate_expr_lit_num!( item. percentage as f64 ) )
119126 } ) ) ) ,
127+ PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
128+ key: PropName :: Str ( "duration" . into( ) ) ,
129+ value: Box :: new( generate_expr_lit_num!( item_duration as f64 ) )
130+ } ) ) ) ,
120131 PropOrSpread :: Prop ( Box :: new( Prop :: KeyValue ( KeyValueProp {
121132 key: PropName :: Str ( "event" . into( ) ) ,
122133 value: Box :: new( Expr :: Object ( ObjectLit {
0 commit comments