Skip to content

Commit 82a7308

Browse files
committed
feat: 生成样式中增加 design_width 设计稿宽度
1 parent a3c45c2 commit 82a7308

File tree

8 files changed

+534
-322
lines changed

8 files changed

+534
-322
lines changed

flatbuffers/stylesheet.fbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ table StyleSheet {
8585
keyframes:[string];
8686
medias:[string];
8787
styles:[Style];
88+
design_width:uint16;
8889
}
8990

9091
root_type StyleSheet;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
],
2525
"license": "MIT",
2626
"devDependencies": {
27-
"@napi-rs/cli": "^2.16.3",
28-
"ava": "^5.1.1",
27+
"@napi-rs/cli": "^2.18.4",
28+
"ava": "^5.3.1",
2929
"conventional-changelog-cli": "2.2.2",
3030
"npm-run-all": "4.1.5",
3131
"prettier": "2.8.8",

pnpm-lock.yaml

Lines changed: 499 additions & 319 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/json_writer.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct JsonWriter {
1717
keyframes: IndexMap<(u32, String), Vec<KeyFrameItem>>,
1818
medias: Vec<StyleMedia>,
1919
fonts: Vec<FontFaceItem>,
20+
design_width: Option<i32>,
2021
}
2122

2223
impl JsonWriter {
@@ -25,12 +26,14 @@ impl JsonWriter {
2526
keyframes: IndexMap<(u32, String), Vec<KeyFrameItem>>,
2627
medias: Vec<StyleMedia>,
2728
fonts: Vec<FontFaceItem>,
29+
design_width: Option<i32>,
2830
) -> Self {
2931
Self {
3032
styles,
3133
keyframes,
3234
medias,
3335
fonts,
36+
design_width,
3437
}
3538
}
3639

@@ -208,7 +211,7 @@ impl JsonWriter {
208211

209212
// fonts
210213

211-
let json_value = expr_to_json(&Expr::Object(ObjectLit {
214+
let mut json_value = expr_to_json(&Expr::Object(ObjectLit {
212215
span: DUMMY_SP,
213216
props: vec![
214217
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
@@ -281,6 +284,11 @@ impl JsonWriter {
281284
}))),
282285
],
283286
}));
287+
// 如果 design_width 存在,则添加 design_width 到 json_value
288+
if let Some(design_width) = self.design_width {
289+
let map = json_value.as_object_mut().unwrap();
290+
map.insert("design_width".to_string(), Value::Number(serde_json::Number::from(design_width)));
291+
}
284292

285293
// 打印 JSON 值
286294

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct OutputOptions {
3535
#[derive(Deserialize)]
3636
pub struct ParseOptions {
3737
pub platform_string: String,
38+
pub design_width: Option<i32>,
3839
pub output: Option<OutputOptions>,
3940
}
4041

@@ -51,6 +52,7 @@ pub fn parse(styles: Vec<String>, options: ParseOptions) -> ParseResult {
5152
"Harmony" => Platform::Harmony,
5253
_ => Platform::Harmony,
5354
};
55+
let design_width = options.design_width;
5456
let output = options.output.unwrap_or(OutputOptions {
5557
is_bin: Some(false)
5658
});
@@ -70,6 +72,7 @@ pub fn parse(styles: Vec<String>, options: ParseOptions) -> ParseResult {
7072
style_data.all_keyframes.borrow().clone(),
7173
style_data.all_medias.borrow().clone(),
7274
style_data.all_fonts.borrow().clone(),
75+
design_width,
7376
);
7477

7578
let style_json = style_map.to_json();

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn main() {
3131
style_data.all_keyframes.borrow().clone(),
3232
style_data.all_medias.borrow().clone(),
3333
style_data.all_fonts.borrow().clone(),
34+
None
3435
);
3536

3637
let style_json = style_map.to_json();

src/stylesheet_generated.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,7 @@ impl<'a> StyleSheet<'a> {
22782278
pub const VT_KEYFRAMES: flatbuffers::VOffsetT = 6;
22792279
pub const VT_MEDIAS: flatbuffers::VOffsetT = 8;
22802280
pub const VT_STYLES: flatbuffers::VOffsetT = 10;
2281+
pub const VT_DESIGN_WIDTH: flatbuffers::VOffsetT = 12;
22812282

22822283
#[inline]
22832284
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
@@ -2293,6 +2294,7 @@ impl<'a> StyleSheet<'a> {
22932294
if let Some(x) = args.medias { builder.add_medias(x); }
22942295
if let Some(x) = args.keyframes { builder.add_keyframes(x); }
22952296
if let Some(x) = args.fonts { builder.add_fonts(x); }
2297+
builder.add_design_width(args.design_width);
22962298
builder.finish()
22972299
}
22982300

@@ -2325,6 +2327,13 @@ impl<'a> StyleSheet<'a> {
23252327
// which contains a valid value in this slot
23262328
unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Style>>>>(StyleSheet::VT_STYLES, None)}
23272329
}
2330+
#[inline]
2331+
pub fn design_width(&self) -> u16 {
2332+
// Safety:
2333+
// Created from valid Table for this object
2334+
// which contains a valid value in this slot
2335+
unsafe { self._tab.get::<u16>(StyleSheet::VT_DESIGN_WIDTH, Some(0)).unwrap()}
2336+
}
23282337
}
23292338

23302339
impl flatbuffers::Verifiable for StyleSheet<'_> {
@@ -2338,6 +2347,7 @@ impl flatbuffers::Verifiable for StyleSheet<'_> {
23382347
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<&'_ str>>>>("keyframes", Self::VT_KEYFRAMES, false)?
23392348
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<&'_ str>>>>("medias", Self::VT_MEDIAS, false)?
23402349
.visit_field::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'_, flatbuffers::ForwardsUOffset<Style>>>>("styles", Self::VT_STYLES, false)?
2350+
.visit_field::<u16>("design_width", Self::VT_DESIGN_WIDTH, false)?
23412351
.finish();
23422352
Ok(())
23432353
}
@@ -2347,6 +2357,7 @@ pub struct StyleSheetArgs<'a> {
23472357
pub keyframes: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<&'a str>>>>,
23482358
pub medias: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<&'a str>>>>,
23492359
pub styles: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a, flatbuffers::ForwardsUOffset<Style<'a>>>>>,
2360+
pub design_width: u16,
23502361
}
23512362
impl<'a> Default for StyleSheetArgs<'a> {
23522363
#[inline]
@@ -2356,6 +2367,7 @@ impl<'a> Default for StyleSheetArgs<'a> {
23562367
keyframes: None,
23572368
medias: None,
23582369
styles: None,
2370+
design_width: 0,
23592371
}
23602372
}
23612373
}
@@ -2382,6 +2394,10 @@ impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> StyleSheetBuilder<'a, 'b, A> {
23822394
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(StyleSheet::VT_STYLES, styles);
23832395
}
23842396
#[inline]
2397+
pub fn add_design_width(&mut self, design_width: u16) {
2398+
self.fbb_.push_slot::<u16>(StyleSheet::VT_DESIGN_WIDTH, design_width, 0);
2399+
}
2400+
#[inline]
23852401
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a, A>) -> StyleSheetBuilder<'a, 'b, A> {
23862402
let start = _fbb.start_table();
23872403
StyleSheetBuilder {
@@ -2403,6 +2419,7 @@ impl core::fmt::Debug for StyleSheet<'_> {
24032419
ds.field("keyframes", &self.keyframes());
24042420
ds.field("medias", &self.medias());
24052421
ds.field("styles", &self.styles());
2422+
ds.field("design_width", &self.design_width());
24062423
ds.finish()
24072424
}
24082425
}

src/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,13 @@ pub fn convert_json_to_flatbuffer(json_str: &str) -> Result<Vec<u8>, serde_json:
442442
})
443443
}).collect();
444444
let styles = builder.create_vector(&styles);
445+
let design_width = json["design_width"].as_u64().unwrap_or(0) as u16;
445446
let stylesheet = styles::StyleSheet::create(&mut builder, &styles::StyleSheetArgs {
446447
fonts: None,
447448
keyframes: None,
448449
medias: None,
449450
styles: Some(styles),
451+
design_width: design_width,
450452
});
451453

452454
builder.finish(stylesheet, None);

0 commit comments

Comments
 (0)