|
| 1 | + |
| 2 | + |
| 3 | +use std::{cell::RefCell, collections::HashMap, rc::Rc}; |
| 4 | + |
| 5 | +use lightningcss::{printer::PrinterOptions, properties::{animation, Property}, traits::ToCss, values::time}; |
| 6 | + |
| 7 | +use crate::{generate_expr_lit_num, generate_invalid_expr, style_parser::KeyFrameItem, visitor::parse_style_values}; |
| 8 | +use swc_core::{common::DUMMY_SP, ecma::ast::*}; |
| 9 | +use super::{traits::ToExpr, unit::{Platform, PropertyTuple}}; |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct Animation { |
| 13 | + pub id: String, |
| 14 | + pub keyframs: Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>, |
| 15 | + pub animation_name: Option<String>, |
| 16 | + pub animation_duration: f32, |
| 17 | + pub animation_delay: f32, |
| 18 | + pub animation_iteration: f32 |
| 19 | + // pub value: Option<Vec<KeyFrameItem>> |
| 20 | +} |
| 21 | + |
| 22 | +impl From<(String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>)> for Animation { |
| 23 | + fn from(value: (String, &Property<'_>, Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>)) -> Self { |
| 24 | + |
| 25 | + |
| 26 | + let mut animation_name = None; |
| 27 | + let mut animation_duration: f32 = 0.0; |
| 28 | + let mut animation_delay: f32 = 0.0; |
| 29 | + let mut animation_iteration: f32 = 1.0; |
| 30 | + |
| 31 | + match value.1 { |
| 32 | + // Property::AnimationName(_, _) => todo!(), |
| 33 | + // Property::AnimationDuration(_, _) => todo!(), |
| 34 | + // Property::AnimationTimingFunction(_, _) => todo!(), |
| 35 | + // Property::AnimationIterationCount(_, _) => todo!(), |
| 36 | + // Property::AnimationDirection(_, _) => todo!(), |
| 37 | + // Property::AnimationPlayState(_, _) => todo!(), |
| 38 | + // Property::AnimationDelay(_, _) => todo!(), |
| 39 | + // Property::AnimationFillMode(_, _) => todo!(), |
| 40 | + Property::Animation(animation_list, _) => { |
| 41 | + animation_list.into_iter().for_each(|animation| { |
| 42 | + animation_name = Some(animation.name.to_css_string(PrinterOptions::default()).unwrap()); |
| 43 | + animation_duration = match animation.duration { |
| 44 | + time::Time::Seconds(s) => s, |
| 45 | + time::Time::Milliseconds(m) => m * 60.0, |
| 46 | + }; |
| 47 | + animation_delay = match animation.delay { |
| 48 | + time::Time::Seconds(s) => s, |
| 49 | + time::Time::Milliseconds(m) => m * 60.0, |
| 50 | + }; |
| 51 | + animation_iteration = match animation.iteration_count { |
| 52 | + animation::AnimationIterationCount::Number(num) => num, |
| 53 | + animation::AnimationIterationCount::Infinite => -1.0, |
| 54 | + } |
| 55 | + }); |
| 56 | + }, |
| 57 | + _ => {} |
| 58 | + } |
| 59 | + |
| 60 | + Animation { |
| 61 | + id: value.0, |
| 62 | + keyframs: value.2.clone(), |
| 63 | + animation_name, |
| 64 | + animation_duration, |
| 65 | + animation_delay, |
| 66 | + animation_iteration |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +impl ToExpr for Animation { |
| 74 | + fn to_expr(&self) -> PropertyTuple { |
| 75 | + if let Some(name) = &self.animation_name { |
| 76 | + let keyframe_map = self.keyframs.borrow(); |
| 77 | + if let Some(keyframe_items) = keyframe_map.get(name) { |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + return PropertyTuple::One( |
| 82 | + "animation".to_string(), |
| 83 | + Expr::Object(ObjectLit { |
| 84 | + span: DUMMY_SP, |
| 85 | + props: vec![ |
| 86 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 87 | + key: PropName::Str("params".into()), |
| 88 | + value: Box::new(Expr::Object(ObjectLit { |
| 89 | + span: DUMMY_SP, |
| 90 | + props: vec![ |
| 91 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 92 | + key: PropName::Str("delay".into()), |
| 93 | + value: Box::new(generate_expr_lit_num!((self.animation_delay * 1000.0) as f64)) |
| 94 | + }))), |
| 95 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 96 | + key: PropName::Str("iterations".into()), |
| 97 | + value: Box::new(generate_expr_lit_num!(self.animation_iteration as f64)) |
| 98 | + }))), |
| 99 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 100 | + key: PropName::Str("duration".into()), |
| 101 | + value: Box::new(generate_expr_lit_num!((self.animation_duration * 1000.0) as f64)) |
| 102 | + }))), |
| 103 | + ] |
| 104 | + })) |
| 105 | + }))), |
| 106 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 107 | + key: PropName::Str("keyframes".into()), |
| 108 | + value: Box::new(Expr::Array(ArrayLit { |
| 109 | + span: DUMMY_SP, |
| 110 | + elems: keyframe_items.into_iter().map(|item| { |
| 111 | + Some(ExprOrSpread { |
| 112 | + spread: None, |
| 113 | + expr: Box::new(Expr::Object(ObjectLit { |
| 114 | + span: DUMMY_SP, |
| 115 | + props: vec![ |
| 116 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 117 | + key: PropName::Str("percentage".into()), |
| 118 | + value: Box::new(generate_expr_lit_num!(item.percentage as f64)) |
| 119 | + }))), |
| 120 | + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { |
| 121 | + key: PropName::Str("event".into()), |
| 122 | + value: Box::new(Expr::Object(ObjectLit { |
| 123 | + span: DUMMY_SP, |
| 124 | + props: parse_style_values(item.declarations.clone(), Platform::Harmony) |
| 125 | + })) |
| 126 | + }))) |
| 127 | + ] |
| 128 | + })) |
| 129 | + }) |
| 130 | + }).collect::<Vec<Option<ExprOrSpread>>>() |
| 131 | + })) |
| 132 | + }))) |
| 133 | + ] |
| 134 | + }) |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + PropertyTuple::One( |
| 139 | + self.id.to_string(), |
| 140 | + generate_invalid_expr!() |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + fn to_rn_expr(&self) -> PropertyTuple { |
| 145 | + PropertyTuple::One( |
| 146 | + self.id.to_string(), |
| 147 | + generate_invalid_expr!() |
| 148 | + ) |
| 149 | + } |
| 150 | +} |
| 151 | + |
0 commit comments