-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 增加LineHeight\LineSpacing\TextAlign\TextOverflow\FontWeight解析
- Loading branch information
Showing
11 changed files
with
409 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use lightningcss::properties::{Property, font}; | ||
use swc_common::DUMMY_SP; | ||
use swc_ecma_ast::{Expr, Lit, Number, Ident, MemberProp, MemberExpr}; | ||
|
||
use crate::style_transform::traits::ToExpr; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum FontWeight { | ||
Number(f32), | ||
Bold, | ||
Bolder, | ||
Lighter, | ||
Normal | ||
} | ||
|
||
impl ToExpr for FontWeight { | ||
fn to_expr(&self) -> Expr { | ||
let font_weight: Expr; | ||
match self { | ||
FontWeight::Number(num) => { | ||
font_weight = Expr::Lit(Lit::Num(Number::from(*num as f64))); | ||
} | ||
FontWeight::Bold | FontWeight::Bolder | FontWeight::Lighter | FontWeight::Normal => { | ||
font_weight = Expr::Member(MemberExpr { | ||
span: DUMMY_SP, | ||
obj: Box::new(Expr::Ident(Ident::new("FontWeight".into(), DUMMY_SP))), | ||
prop: MemberProp::Ident(Ident { | ||
span: DUMMY_SP, | ||
sym: match self { | ||
FontWeight::Bold => "Bold", | ||
FontWeight::Bolder => "Bolder", | ||
FontWeight::Lighter => "Lighter", | ||
FontWeight::Normal => "Normal", | ||
FontWeight::Number(_) => "", | ||
} | ||
.into(), | ||
optional: false, | ||
}), | ||
}) | ||
.into() | ||
}, | ||
} | ||
font_weight | ||
} | ||
} | ||
|
||
impl From<&Property<'_>> for FontWeight { | ||
fn from(value: &Property<'_>) -> Self { | ||
let mut font_weight = FontWeight::Normal; | ||
match value { | ||
Property::FontWeight(value) => { | ||
match value { | ||
font::FontWeight::Bolder => { | ||
font_weight = FontWeight::Bolder | ||
}, | ||
font::FontWeight::Lighter => { | ||
font_weight = FontWeight::Lighter | ||
}, | ||
font::FontWeight::Absolute(val) => { | ||
match val { | ||
font::AbsoluteFontWeight::Bold => { | ||
font_weight = FontWeight::Bold | ||
}, | ||
font::AbsoluteFontWeight::Weight(num) => { | ||
font_weight = FontWeight::Number(*num) | ||
}, | ||
font::AbsoluteFontWeight::Normal => { | ||
font_weight = FontWeight::Normal | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
_ => {} | ||
} | ||
font_weight | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use lightningcss::{ | ||
properties::{Property,text::Spacing}, | ||
values | ||
}; | ||
use swc_common::DUMMY_SP; | ||
use swc_ecma_ast::{Expr, Lit, Number, Callee, Ident, CallExpr}; | ||
|
||
use crate::style_transform::traits::ToExpr; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum LetterSpacing { | ||
Px(f32), | ||
Number(f32) | ||
} | ||
|
||
impl ToExpr for LetterSpacing { | ||
fn to_expr(&self) -> Expr { | ||
let mut letter_spacing = Expr::Lit(Lit::Null(swc_ecma_ast::Null { span: DUMMY_SP })); | ||
match self { | ||
LetterSpacing::Number(_) => { | ||
// 暂不支持数值类型 | ||
// Expr::Call(CallExpr { | ||
// span: DUMMY_SP, | ||
// callee: Callee::Expr(Box::new(Expr::Ident(Ident::new( | ||
// "convertLetterSpacing".into(), | ||
// DUMMY_SP, | ||
// )))), | ||
// args: vec![ | ||
// Expr::Lit(Lit::Num(Number::from(*value as f64))).into() | ||
// ], | ||
// type_args: None, | ||
// }) | ||
}, | ||
LetterSpacing::Px(value) => { | ||
letter_spacing = Expr::Call(CallExpr { | ||
span: DUMMY_SP, | ||
callee: Callee::Expr(Box::new(Expr::Ident(Ident::new( | ||
"convertPx".into(), | ||
DUMMY_SP, | ||
)))), | ||
args: vec![ | ||
Expr::Lit(Lit::Num(Number::from(*value as f64))).into() | ||
], | ||
type_args: None, | ||
}) | ||
} | ||
} | ||
letter_spacing | ||
} | ||
} | ||
|
||
impl From<&Property<'_>> for LetterSpacing { | ||
fn from(value: &Property<'_>) -> Self { | ||
let mut letter_spacing = LetterSpacing::Number(1.0); | ||
match value { | ||
Property::LetterSpacing(value) => { | ||
match value { | ||
Spacing::Length(val) => { | ||
match val { | ||
values::length::Length::Value(value) => { | ||
match value { | ||
values::length::LengthValue::Px(value) => { | ||
// 匹配px单位 | ||
letter_spacing = LetterSpacing::Px(*value) | ||
}, | ||
_ => {} | ||
} | ||
}, | ||
_ => {} | ||
} | ||
} | ||
Spacing::Normal => {}, | ||
} | ||
} | ||
_ => {} | ||
} | ||
letter_spacing | ||
} | ||
} |
Oops, something went wrong.