mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-19 00:27:49 +00:00
REST import: variables bound to stroke sides bind each side's weight (#2441)
boundVariables.individualStrokeWeights was skipped with a warning, so a 1 px bottom divider bound to a variable came across as a plain number. Each side now binds strokeTopWeight ... strokeLeftWeight, read from the live API's BORDER_TOP_WEIGHT ... keys or the published spec's top ... keys.
This commit is contained in:
1 parent
55413ffc89
commit
d332c18a2c
2 files changed
+51
No files matched your search
@@ -188,6 +188,44 @@ fn bound_variables_and_explicit_modes() {
|
||||
assert!(has(&warnings, "pass the local variables JSON"), "{warnings:?}");
|
||||
}
|
||||
|
||||
/// #2441: a variable bound to one or more stroke sides (`individualStrokeWeights`) binds that
|
||||
/// side's weight. The live API names the sides `BORDER_TOP_WEIGHT` …, the published spec `top` …;
|
||||
/// both are read, and neither is reported as skipped.
|
||||
#[test]
|
||||
fn variables_bound_to_stroke_sides_bind_each_sides_weight() {
|
||||
let rect = |id: &str, name: &str, sides: Value| {
|
||||
json!({"id": id, "name": name, "type": "RECTANGLE", "relativeTransform": at(0.0, 0.0), "size": {"x": 40, "y": 20},
|
||||
"strokes": [{"type": "SOLID", "color": rgba(0.0, 0.0, 0.0)}], "strokeWeight": 1,
|
||||
"individualStrokeWeights": {"top": 1, "right": 1, "bottom": 1, "left": 1},
|
||||
"boundVariables": {"individualStrokeWeights": sides}})
|
||||
};
|
||||
let file = json!({"name": "Sides", "document": {"id": "0:0", "type": "DOCUMENT", "children": [
|
||||
{"id": "0:1", "name": "Page", "type": "CANVAS", "children": [
|
||||
rect("1:1", "Row", json!({"BORDER_BOTTOM_WEIGHT": alias(SPACE)})),
|
||||
rect("1:2", "Card", json!({"BORDER_TOP_WEIGHT": alias(SPACE), "BORDER_RIGHT_WEIGHT": alias(SPACE),
|
||||
"BORDER_BOTTOM_WEIGHT": alias(SPACE), "BORDER_LEFT_WEIGHT": alias(SPACE)})),
|
||||
rect("1:3", "Spec", json!({"top": alias(SPACE), "left": alias(SPACE)})),
|
||||
rect("1:4", "Colour", json!({"BORDER_LEFT_WEIGHT": alias(BRAND)}))
|
||||
]}
|
||||
]}});
|
||||
let (doc, warnings) = import_with(&file, Some(&variables_json()), &no_images).unwrap();
|
||||
let sides = |name: &str| -> Vec<(String, String)> {
|
||||
let n = named(&doc, name);
|
||||
["strokeTopWeight", "strokeRightWeight", "strokeBottomWeight", "strokeLeftWeight"]
|
||||
.iter()
|
||||
.filter_map(|f| n.bound.get(*f).map(|v| (f.to_string(), v.clone())))
|
||||
.collect()
|
||||
};
|
||||
let b = |f: &str| (f.to_string(), SPACE.to_string());
|
||||
assert_eq!(sides("Row"), [b("strokeBottomWeight")], "one side bound");
|
||||
assert_eq!(sides("Card"), [b("strokeTopWeight"), b("strokeRightWeight"), b("strokeBottomWeight"), b("strokeLeftWeight")]);
|
||||
assert_eq!(sides("Spec"), [b("strokeTopWeight"), b("strokeLeftWeight")], "the spec's names");
|
||||
assert_eq!(sides("Colour"), [], "a colour variable can't be a weight");
|
||||
assert!(!has(&warnings, "`individualStrokeWeights`"), "{warnings:?}");
|
||||
assert!(has(&warnings, "type doesn't fit"), "{warnings:?}");
|
||||
assert!(doc.validate().is_empty(), "{:?}", doc.validate());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_variables_match_separate_ones() {
|
||||
let mut embedded = file_json();
|
||||
|
||||
@@ -427,6 +427,19 @@ fn node_binding<'v>(k: &str, v: &'v Value, n: &Node, out: &mut Vec<(String, &'v
|
||||
push(f, v.get(key).and_then(alias));
|
||||
}
|
||||
}
|
||||
// Each stroke side's weight (#2441). The live `/nodes` response names the sides
|
||||
// `BORDER_TOP_WEIGHT` and so on, as `rectangleCornerRadii` names its corners; Figma's
|
||||
// published OpenAPI spec names them `top` … `left`. Either is read.
|
||||
"individualStrokeWeights" => {
|
||||
for (keys, f) in [
|
||||
(["BORDER_TOP_WEIGHT", "top"], "strokeTopWeight"),
|
||||
(["BORDER_RIGHT_WEIGHT", "right"], "strokeRightWeight"),
|
||||
(["BORDER_BOTTOM_WEIGHT", "bottom"], "strokeBottomWeight"),
|
||||
(["BORDER_LEFT_WEIGHT", "left"], "strokeLeftWeight"),
|
||||
] {
|
||||
push(f, keys.iter().find_map(|key| v.get(*key)).and_then(alias));
|
||||
}
|
||||
}
|
||||
"fills" | "strokes" => {
|
||||
let field = if k == "fills" { "fill" } else { "stroke" };
|
||||
match v.as_array().and_then(|a| a.first()).and_then(alias) {
|
||||
|
||||
Reference in new issue
Block a user