From c75cee1942291e284540710b5e2f38d0c9da603e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 17:29:41 +0000 Subject: [PATCH] fix(fig): index native board shapes by source origin --- crates/format/src/fig/map.rs | 155 ++++++++++++++++++--------------- crates/ui-egui/locales/en.toml | 1 - 2 files changed, 84 insertions(+), 72 deletions(-) diff --git a/crates/format/src/fig/map.rs b/crates/format/src/fig/map.rs index 6d1cde23d..50cbf24fa 100644 --- a/crates/format/src/fig/map.rs +++ b/crates/format/src/fig/map.rs @@ -87,6 +87,7 @@ pub(super) fn to_rest_for(msg: &Kv, name: &str, max_nodes: usize, board: bool) - /// shape model, outline, and layout semantics. fn native_board_shapes(msg: &Kv, json: &mut Value, warnings: &mut Vec) { let Some(changes) = msg.get("nodeChanges") else { return }; + let mut shapes = std::collections::HashMap::new(); for node in changes.list() { if node.get("type").and_then(Kv::str) != Some("SHAPE_WITH_TEXT") { continue; @@ -110,70 +111,93 @@ fn native_board_shapes(msg: &Kv, json: &mut Value, warnings: &mut Vec) { if node.get("textData").and_then(|t| t.get("characters")).and_then(Kv::str).is_some_and(|s| !s.trim().is_empty()) { continue; } - let Some(rest_node) = find_origin_mut(json, &origin) else { continue }; - let Some(children) = rest_node.get("children").and_then(Value::as_array) else { continue }; - let empty_body = generated_empty_body(node); - let has_generated_outline = !children.is_empty() && children[0]["type"] == "VECTOR"; - let has_generated_label = children.len() == 2 && children[1]["type"] == "TEXT"; - if !has_generated_outline || !(has_generated_label || (children.len() == 1 && empty_body)) { - continue; - } - let (Some(width), Some(height)) = ( - rest_node.get("size").and_then(|s| s.get("x")).and_then(Value::as_f64), - rest_node.get("size").and_then(|s| s.get("y")).and_then(Value::as_f64), - ) else { - continue; - }; - if !(width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0) { - continue; - } - let (width, height) = (width.min(crate::rest::props::MAX_SIZE), height.min(crate::rest::props::MAX_SIZE)); - let Some(network) = serde_json::to_value(VectorNetwork::from_path(&shape.path(width, height))).ok() else { continue }; + shapes.insert(origin, (shape, generated_empty_body(node))); + } + apply_native_board_shapes(json, &shapes); +} - rest_node["board"] = serde_json::json!({"type": "shape", "shape": shape.id()}); - rest_node["fills"] = serde_json::json!([]); - rest_node["strokes"] = serde_json::json!([]); - rest_node["clipsContent"] = serde_json::json!(false); - rest_node["layoutMode"] = serde_json::json!("VERTICAL"); - rest_node["primaryAxisAlignItems"] = serde_json::json!("CENTER"); - rest_node["counterAxisAlignItems"] = serde_json::json!("CENTER"); - rest_node["primaryAxisSizingMode"] = serde_json::json!("FIXED"); - rest_node["counterAxisSizingMode"] = serde_json::json!("FIXED"); - let [top, right, bottom, left] = shape.text_insets(); - rest_node["paddingTop"] = serde_json::json!(top * height); - rest_node["paddingRight"] = serde_json::json!(right * width); - rest_node["paddingBottom"] = serde_json::json!(bottom * height); - rest_node["paddingLeft"] = serde_json::json!(left * width); - - let Some(children) = rest_node.get_mut("children").and_then(Value::as_array_mut) else { continue }; - if children.len() == 1 { - children.push(serde_json::json!({ - "id": format!("{origin};generated1"), - "type": "TEXT", - "name": "", - "characters": "", - "size": {"x": width, "y": height}, - "relativeTransform": [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], - "style": {"textAutoResize": "HEIGHT", "textAlignHorizontal": "CENTER"}, - "layoutSizingHorizontal": "FILL" - })); +/// Traverse only the mapped REST tree (already bounded by `max_nodes`) and adapt each source +/// origin at most once. The origin index avoids rescanning the tree for every source record. +fn apply_native_board_shapes(json: &mut Value, shapes: &std::collections::HashMap) { + fn visit(json: &mut Value, shapes: &std::collections::HashMap, visited: &mut std::collections::HashSet) { + if let Some(origin) = json.get(craft_fig::rest::FIG_SOURCE_FIELD).and_then(|s| s.get("origin")).and_then(Value::as_str).map(str::to_owned) { + if visited.insert(origin.clone()) { + if let Some((shape, empty_body)) = shapes.get(&origin).copied() { + apply_native_board_shape(json, &origin, shape, empty_body); + } + } } - let outline = &mut children[0]; - outline["name"] = serde_json::json!("Outline"); - outline["vectorNetwork"] = network; - outline["size"] = serde_json::json!({"x": width, "y": height}); - outline["relativeTransform"] = serde_json::json!([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]); - outline["layoutPositioning"] = serde_json::json!("ABSOLUTE"); - outline["constraints"] = serde_json::json!({"horizontal": "SCALE", "vertical": "SCALE"}); - outline.as_object_mut().map(|o| o.remove("cornerRadius")); - - let label = &mut children[1]; - label["layoutSizingHorizontal"] = serde_json::json!("FILL"); - if let Some(style) = label.get_mut("style").and_then(Value::as_object_mut) { - style.insert("textAutoResize".into(), serde_json::json!("HEIGHT")); - style.insert("textAlignHorizontal".into(), serde_json::json!("CENTER")); + match json { + Value::Object(fields) => fields.values_mut().for_each(|child| visit(child, shapes, visited)), + Value::Array(items) => items.iter_mut().for_each(|child| visit(child, shapes, visited)), + _ => {} } } + visit(json, shapes, &mut std::collections::HashSet::new()); +} + +fn apply_native_board_shape(rest_node: &mut Value, origin: &str, shape: BoardShape, empty_body: bool) { + let Some(children) = rest_node.get("children").and_then(Value::as_array) else { return }; + let has_generated_outline = !children.is_empty() && children[0]["type"] == "VECTOR"; + let has_generated_label = children.len() == 2 && children[1]["type"] == "TEXT"; + if !has_generated_outline || !(has_generated_label || (children.len() == 1 && empty_body)) { + return; + } + let (Some(width), Some(height)) = ( + rest_node.get("size").and_then(|s| s.get("x")).and_then(Value::as_f64), + rest_node.get("size").and_then(|s| s.get("y")).and_then(Value::as_f64), + ) else { + return; + }; + if !(width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0) { + return; + } + let (width, height) = (width.min(crate::rest::props::MAX_SIZE), height.min(crate::rest::props::MAX_SIZE)); + let Some(network) = serde_json::to_value(VectorNetwork::from_path(&shape.path(width, height))).ok() else { return }; + + rest_node["board"] = serde_json::json!({"type": "shape", "shape": shape.id()}); + rest_node["fills"] = serde_json::json!([]); + rest_node["strokes"] = serde_json::json!([]); + rest_node["clipsContent"] = serde_json::json!(false); + rest_node["layoutMode"] = serde_json::json!("VERTICAL"); + rest_node["primaryAxisAlignItems"] = serde_json::json!("CENTER"); + rest_node["counterAxisAlignItems"] = serde_json::json!("CENTER"); + rest_node["primaryAxisSizingMode"] = serde_json::json!("FIXED"); + rest_node["counterAxisSizingMode"] = serde_json::json!("FIXED"); + let [top, right, bottom, left] = shape.text_insets(); + rest_node["paddingTop"] = serde_json::json!(top * height); + rest_node["paddingRight"] = serde_json::json!(right * width); + rest_node["paddingBottom"] = serde_json::json!(bottom * height); + rest_node["paddingLeft"] = serde_json::json!(left * width); + + let Some(children) = rest_node.get_mut("children").and_then(Value::as_array_mut) else { return }; + if children.len() == 1 { + children.push(serde_json::json!({ + "id": format!("{origin};generated1"), + "type": "TEXT", + "name": "", + "characters": "", + "size": {"x": width, "y": height}, + "relativeTransform": [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], + "style": {"textAutoResize": "HEIGHT", "textAlignHorizontal": "CENTER"}, + "layoutSizingHorizontal": "FILL" + })); + } + let outline = &mut children[0]; + outline["name"] = serde_json::json!("Outline"); + outline["vectorNetwork"] = network; + outline["size"] = serde_json::json!({"x": width, "y": height}); + outline["relativeTransform"] = serde_json::json!([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]); + outline["layoutPositioning"] = serde_json::json!("ABSOLUTE"); + outline["constraints"] = serde_json::json!({"horizontal": "SCALE", "vertical": "SCALE"}); + outline.as_object_mut().map(|o| o.remove("cornerRadius")); + + let label = &mut children[1]; + label["layoutSizingHorizontal"] = serde_json::json!("FILL"); + if let Some(style) = label.get_mut("style").and_then(Value::as_object_mut) { + style.insert("textAutoResize".into(), serde_json::json!("HEIGHT")); + style.insert("textAlignHorizontal".into(), serde_json::json!("CENTER")); + } } /// The shared snapshot mapper omits a generated text child whose characters are empty. FigCraft @@ -200,17 +224,6 @@ fn generated_empty_body(node: &Kv) -> bool { style.get("textData").and_then(|v| v.get("characters")).and_then(Kv::str) == Some("") } -fn find_origin_mut<'a>(value: &'a mut Value, origin: &str) -> Option<&'a mut Value> { - if value.get(craft_fig::rest::FIG_SOURCE_FIELD).and_then(|s| s.get("origin")).and_then(Value::as_str) == Some(origin) { - return Some(value); - } - match value { - Value::Object(fields) => fields.values_mut().find_map(|child| find_origin_mut(child, origin)), - Value::Array(items) => items.iter_mut().find_map(|child| find_origin_mut(child, origin)), - _ => None, - } -} - /// FigJam's `shapeWithTextType` values that have a corresponding FigCraft board outline. pub(super) fn board_shape(kind: &str) -> Option { Some(match kind { diff --git a/crates/ui-egui/locales/en.toml b/crates/ui-egui/locales/en.toml index d4d4ce125..f73fcd980 100644 --- a/crates/ui-egui/locales/en.toml +++ b/crates/ui-egui/locales/en.toml @@ -1889,7 +1889,6 @@ name = "English" "set-variable actions without a value were skipped" = "set-variable actions without a value were skipped" "set-variable-mode actions on collections or modes that aren't in the variables JSON were skipped" = "set-variable-mode actions on collections or modes that aren't in the variables JSON were skipped" "shape generated outline is missing or damaged; using its rectangle" = "shape generated outline is missing or damaged; using its rectangle" -"shape with text layers import as frames with their text" = "shape with text layers import as frames with their text" "slot content assignment target is missing or is not slot content" = "slot content assignment target is missing or is not slot content" "slot content assignment uses the empty GUID sentinel and was skipped" = "slot content assignment uses the empty GUID sentinel and was skipped" "slot layers without a matching slot property were imported as frames" = "slot layers without a matching slot property were imported as frames"