Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加css变量透传 #29

Open
wants to merge 1 commit into
base: feat/capi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/parse_style_properties.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

use lightningcss::{properties::{custom::TokenOrValue, Property}, stylesheet::PrinterOptions};
use lightningcss::{properties::{custom::TokenOrValue, Property}, stylesheet::PrinterOptions, traits::ToCss};
use swc_core::{common::DUMMY_SP, ecma::{ast::{self}, utils::quote_ident}};
use swc_core::ecma::ast::*;

use crate::style_propetries::style_value_type::{Variable};
use crate::{generate_expr_lit_str, style_parser::KeyFrameItem, style_propetries::{animation::Animation, animation_multi::AnimationMulti, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, expr::Expr, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, style_property_type::{string_to_css_property_type, CSSPropertyType}, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, transition::Transition, unit::{generate_expr_by_length_value, Platform}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak}, utils::lowercase_first};

pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<StyleValueType> {
Expand All @@ -25,6 +25,14 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<Style
}
});
},
Property::Custom(custom) => {
if (id.as_str().starts_with("--")) {
final_properties.push(StyleValueType::Variable(Variable {
id: custom.name.to_css_string(PrinterOptions::default()).unwrap(),
value: value.value_to_css_string(PrinterOptions::default()).unwrap(),
}));
}
}
_ => {}
};
if is_env {
Expand Down
11 changes: 9 additions & 2 deletions src/style_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,21 @@ impl<'i> StyleParser<'i> {
binding.iter_mut().for_each(|(media_index, selector, style_value)| {
let properties = style_value.declaration.declarations.iter().map(|property| {
(
to_camel_case(
if(property.property_id().to_css_string(PrinterOptions::default()).unwrap().as_str().starts_with("--")) {
property
.property_id()
.to_css_string(PrinterOptions::default())
.unwrap()
} else {
to_camel_case(
property
.property_id()
.to_css_string(PrinterOptions::default())
.unwrap()
.as_str(),
false,
),
)
},
property.clone(),
)
})
Expand Down
1 change: 1 addition & 0 deletions src/style_propetries/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl ToExpr for Background {
PropertyTuple::One(_, val) =>
props.push((CSSPropertyType::BackgroundPosition, val)),
PropertyTuple::Array(val) => props.extend(val),
_=> {}
}
}
if let Some(repeat) = &self.repeat {
Expand Down
17 changes: 16 additions & 1 deletion src/style_propetries/style_value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ use crate::generate_expr_based_on_platform;

use super::{animation::Animation, animation_multi::AnimationMulti, aspect_ratio::AspectRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, box_shadow::BoxShadow, color::ColorProperty, display::Display, expr::Expr, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, traits::{ToExpr, ToStyleValue}, transform::Transform, transform_origin::TransformOrigin, transition::Transition, unit::{Platform, PropertyTuple}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak};

#[derive(Debug, Clone)]
pub struct Variable {
pub id: String,
pub value: String
}

impl ToExpr for Variable {
fn to_expr(&self) -> PropertyTuple {
PropertyTuple::Variable{key: self.id.clone(),value: self.value.clone() }
}
}

#[derive(Debug, Clone)]
pub enum StyleValueType {
Expand Down Expand Up @@ -55,7 +66,8 @@ pub enum StyleValueType {
Visibility(Visibility),
Opacity(Opacity),
WordBreak(WordBreak),
WhiteSpace(WhiteSpace)
WhiteSpace(WhiteSpace),
Variable(Variable)
}

impl ToStyleValue for StyleValueType {
Expand Down Expand Up @@ -211,6 +223,9 @@ impl ToStyleValue for StyleValueType {
StyleValueType::WhiteSpace(value) => {
generate_expr_based_on_platform!(platform, value)
}
StyleValueType::Variable(value) => {
generate_expr_based_on_platform!(platform, value)
}
}
}
}
3 changes: 2 additions & 1 deletion src/style_propetries/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub enum PropertyTuple {
// 一对一属性:height: 100px 解析 => (height, "100px")
One(CSSPropertyType, Expr),
// 一对多属性:flex: 1 解析 => vec![(flexGrow, "1"), (flexShrink, "1"), (flexBasis, "0%")]
Array(Vec<(CSSPropertyType, Expr)>)
Array(Vec<(CSSPropertyType, Expr)>),
Variable{key: String, value: String}
}

// 根据长度单位生成对应的表达式
Expand Down
31 changes: 30 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub fn parse_style_values(value: Vec<StyleValueType>, platform: Platform) -> Vec

// 使用有序表
let mut index_map = IndexMap::new();

let mut variable_map = IndexMap::new();

value.into_iter().for_each(|style_value| {
let prop = style_value.to_expr(platform.clone());
match prop {
Expand All @@ -47,6 +48,9 @@ pub fn parse_style_values(value: Vec<StyleValueType>, platform: Platform) -> Vec
index_map.insert(id.clone(), Box::new(expr));
})
}
PropertyTuple::Variable { key, value} => {
variable_map.insert(key.clone(), value.clone());
}
}
});

Expand All @@ -71,6 +75,31 @@ pub fn parse_style_values(value: Vec<StyleValueType>, platform: Platform) -> Vec
}))
});

variable_map.into_iter().for_each(|(key, value)| {
let key_ = key.to_string();
prop_or_spread.push(Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: vec![
Some(ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: MyAtom::new(key_),
raw: None
})))
}),
Some(ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: MyAtom::new(value),
raw: None
})))
})
]
}))
});

prop_or_spread.into_iter().map(|expr| {
Some(ExprOrSpread {
spread: None,
Expand Down