mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-19 00:27:49 +00:00
M8: design context keeps its assets to the layers returned and says when a join stops
From review: the asset list follows the layers placed in the tree, so a smaller tree (maxNodes, or the MCP tool's retry for maxBytes) means a smaller list, and `truncated.assetsLeftOut` counts the rest; following aliases reports `truncated.variables` when it stops at its budget with variables still to read; and a font is checked with figcraft_text::has_family by its trimmed name, as the missing-fonts list does, so a family stored with outer spaces isn't taken for installed. A regression test for each fails without its fix.
This commit is contained in:
1 parent
ae592a0d71
commit
8dffbf87a1
3 files changed
+99
-32
No files matched your search
@@ -350,3 +350,60 @@ fn a_malformed_target_is_refused_rather_than_read_as_the_selection() {
|
||||
}
|
||||
assert_eq!(context(&mut s, json!({"ids": []}))["target"]["layers"], 0, "an empty list reads nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_font_stored_with_outer_spaces_isnt_taken_for_installed() {
|
||||
let mut s = session();
|
||||
let t = node(&mut s, json!({"type": "text", "text": "Hi", "props": {"fontFamily": " Missing Display Test "}}));
|
||||
let v = context(&mut s, json!({"ids": [t]}));
|
||||
let font = &v["fonts"][0];
|
||||
assert_eq!(font["family"], " Missing Display Test ", "the spelling stays as stored");
|
||||
assert_eq!(font["installed"], false, "{font}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assets_follow_the_layers_returned_and_say_what_was_left_out() {
|
||||
let mut s = session();
|
||||
let frame = node(&mut s, json!({"type": "frame", "width": 400, "height": 400}));
|
||||
for seed in 0..20u8 {
|
||||
let img = image::RgbImage::from_fn(4, 4, |x, y| image::Rgb([x as u8, y as u8, seed]));
|
||||
let mut bytes = Vec::new();
|
||||
img.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
|
||||
let data = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &bytes);
|
||||
s.execute("image.place", &json!({"data": data, "parent": frame})).unwrap();
|
||||
}
|
||||
let all = context(&mut s, json!({"ids": [frame]}));
|
||||
assert_eq!(all["assets"].as_array().unwrap().len(), 20);
|
||||
let few = context(&mut s, json!({"ids": [frame], "maxNodes": 5}));
|
||||
let shown = few["assets"].as_array().unwrap().len();
|
||||
assert_eq!(shown, 4, "only the images among the 5 layers returned");
|
||||
assert_eq!(few["truncated"]["assetsLeftOut"], 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_join_stopped_at_its_budget_says_so() {
|
||||
let mut s = session();
|
||||
let (c, first) = variable(&mut s, "v0", "#000000", "#000000");
|
||||
let mut prev = first.clone();
|
||||
for k in 1..6 {
|
||||
let next = s
|
||||
.execute(
|
||||
"variable.create",
|
||||
&json!({"collection": c, "name": format!("v{k}"), "type": "color", "values": {"Light": "#111111", "Dark": "#111111"}}),
|
||||
)
|
||||
.unwrap()["id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
for mode in ["Light", "Dark"] {
|
||||
s.execute("variable.set", &json!({"variable": prev, "mode": mode, "value": {"alias": next}})).unwrap();
|
||||
}
|
||||
prev = next;
|
||||
}
|
||||
let start = std::collections::BTreeSet::from([first]);
|
||||
let none = std::collections::BTreeSet::new();
|
||||
let (all, complete) = crate::design_context::referenced(s.doc().unwrap(), &start, &none, 100);
|
||||
assert_eq!((all.len(), complete), (6, true));
|
||||
let (some, complete) = crate::design_context::referenced(s.doc().unwrap(), &start, &none, 3);
|
||||
assert_eq!((some.len(), complete), (3, false), "{some:?}");
|
||||
}
|
||||
@@ -45,6 +45,8 @@ struct Used {
|
||||
left_out: usize,
|
||||
/// Reached `depth` with children left.
|
||||
cut_by_depth: bool,
|
||||
/// The layers placed.
|
||||
placed: BTreeSet<NodeId>,
|
||||
/// Font families of the text layers placed, with their styles.
|
||||
fonts: BTreeMap<String, BTreeSet<String>>,
|
||||
}
|
||||
@@ -185,6 +187,7 @@ fn code(doc: &Document, id: NodeId) -> Vec<Value> {
|
||||
fn layer(doc: &Document, id: NodeId, depth: usize, cap: usize, used: &mut Used) -> Option<Value> {
|
||||
let n = doc.node(id)?;
|
||||
used.nodes += 1;
|
||||
used.placed.insert(id);
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
for (_, _, style) in t.spans() {
|
||||
used.fonts.entry(style.font_family.clone()).or_default().insert(style.font_style.clone());
|
||||
@@ -297,32 +300,33 @@ fn variables_json(doc: &Document, ids: &BTreeSet<String>, missing: &mut Vec<Valu
|
||||
/// Most variables followed through aliases.
|
||||
const MAX_VARIABLES: usize = 10_000;
|
||||
|
||||
/// The variables the tree uses: those its layers bind, those its shared styles bind, and those any
|
||||
/// of them reads as an alias or in a colour calculation, however deep.
|
||||
fn referenced(doc: &Document, used: &Used) -> BTreeSet<String> {
|
||||
let mut out: BTreeSet<String> = used.variables.clone();
|
||||
out.extend(doc.styles.iter().filter(|st| used.styles.contains(&st.id)).flat_map(|st| st.bound.values().cloned()));
|
||||
let mut todo: Vec<String> = out.iter().cloned().collect();
|
||||
/// The variables a tree uses: those its layers bind (`bound`), those its shared `styles` bind, and
|
||||
/// those any of them reads as an alias or in a colour calculation, however deep, up to `max`. The
|
||||
/// flag is false when `max` stopped it with variables still to follow.
|
||||
pub(crate) fn referenced(doc: &Document, bound: &BTreeSet<String>, styles: &BTreeSet<String>, max: usize) -> (BTreeSet<String>, bool) {
|
||||
let mut out: BTreeSet<String> = BTreeSet::new();
|
||||
let mut todo: Vec<String> = bound.iter().cloned().collect();
|
||||
todo.extend(doc.styles.iter().filter(|st| styles.contains(&st.id)).flat_map(|st| st.bound.values().cloned()));
|
||||
todo.reverse();
|
||||
while let Some(id) = todo.pop() {
|
||||
if out.len() >= MAX_VARIABLES {
|
||||
break;
|
||||
if out.contains(&id) {
|
||||
continue;
|
||||
}
|
||||
if out.len() >= max {
|
||||
return (out, false);
|
||||
}
|
||||
out.insert(id.clone());
|
||||
let Some(var) = doc.variables.variables.iter().find(|v| v.id == id) else { continue };
|
||||
for value in var.values.values() {
|
||||
let reads: Vec<&String> = match value {
|
||||
figcraft_doc::VariableValue::Alias(other) => vec![other],
|
||||
figcraft_doc::VariableValue::ColorAlpha { variable, .. } => vec![variable],
|
||||
figcraft_doc::VariableValue::ColorMix { from, to, .. } => from.variable().into_iter().chain(to.variable()).collect(),
|
||||
_ => vec![],
|
||||
};
|
||||
for other in reads {
|
||||
if out.insert(other.clone()) {
|
||||
todo.push(other.clone());
|
||||
}
|
||||
match value {
|
||||
figcraft_doc::VariableValue::Alias(other) => todo.push(other.clone()),
|
||||
figcraft_doc::VariableValue::ColorAlpha { variable, .. } => todo.push(variable.clone()),
|
||||
figcraft_doc::VariableValue::ColorMix { from, to, .. } => todo.extend(from.variable().into_iter().chain(to.variable()).cloned()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
(out, true)
|
||||
}
|
||||
|
||||
/// The shared styles the tree uses, with what each sets.
|
||||
@@ -358,15 +362,12 @@ fn components_json(doc: &Document, ids: &BTreeSet<NodeId>) -> Value {
|
||||
Value::Object(out)
|
||||
}
|
||||
|
||||
/// The font families the tree's text layers use, with their styles, and whether each is installed.
|
||||
fn fonts(doc: &Document, used: &BTreeMap<String, BTreeSet<String>>) -> Value {
|
||||
let gone = crate::fonts::missing(doc);
|
||||
/// The font families the tree's text layers use, with their styles, and whether each is installed
|
||||
/// (the family name trimmed, as the missing-fonts list reads it). The spelling stays as stored.
|
||||
fn fonts(used: &BTreeMap<String, BTreeSet<String>>) -> Value {
|
||||
Value::Array(
|
||||
used.iter()
|
||||
.map(|(family, styles)| {
|
||||
let installed = !gone.iter().any(|m| m.family.eq_ignore_ascii_case(family));
|
||||
json!({"family": family, "styles": styles, "installed": installed})
|
||||
})
|
||||
.map(|(family, styles)| json!({"family": family, "styles": styles, "installed": figcraft_text::has_family(family.trim())}))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
@@ -429,11 +430,16 @@ fn design_context(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut missing = vec![
|
||||
json!({"what": "responsive behaviour", "why": "the file stores no breakpoints: sizes are as designed here, and constraints, sizing and annotations are all it says about other widths"}),
|
||||
];
|
||||
let variables = variables_json(doc, &referenced(doc, &used), &mut missing);
|
||||
let (joined, complete) = referenced(doc, &used.variables, &used.styles, MAX_VARIABLES);
|
||||
let variables = variables_json(doc, &joined, &mut missing);
|
||||
let styles = styles_json(doc, &used.styles, &mut missing);
|
||||
let components = components_json(doc, &used.components);
|
||||
let fonts = fonts(doc, &used.fonts);
|
||||
let fonts = fonts(&used.fonts);
|
||||
let page = doc.node(s.page()?).map(|n| n.name.clone());
|
||||
// The assets of the layers returned: a smaller tree means a smaller list.
|
||||
let found = crate::devinspect::assets(doc, &ids);
|
||||
let assets: Vec<&crate::devinspect::Asset> = found.iter().filter(|a| used.placed.contains(&a.id)).collect();
|
||||
let assets_left_out = found.len() - assets.len();
|
||||
let mut out = json!({
|
||||
"version": VERSION,
|
||||
"document": {"name": doc.name, "page": {"id": s.page()?, "name": page}, "colorProfile": doc.color_profile.id()},
|
||||
@@ -443,11 +449,15 @@ fn design_context(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"styles": styles,
|
||||
"components": components,
|
||||
"fonts": fonts,
|
||||
"assets": crate::devinspect::assets(doc, &ids),
|
||||
"assets": assets,
|
||||
"missing": missing,
|
||||
});
|
||||
if used.left_out > 0 || used.cut_by_depth {
|
||||
out["truncated"] = json!({"layersLeftOut": used.left_out, "depth": used.cut_by_depth, "maxNodes": cap, "hint": "call again with the ids of the parts you need"});
|
||||
out["truncated"] = json!({"layersLeftOut": used.left_out, "assetsLeftOut": assets_left_out, "depth": used.cut_by_depth, "maxNodes": cap, "hint": "call again with the ids of the parts you need"});
|
||||
}
|
||||
if !complete {
|
||||
out["truncated"]["variables"] =
|
||||
json!({"maxVariables": MAX_VARIABLES, "why": "aliases go deeper than this: some variables they read aren't defined here"});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
+2
-2
@@ -435,9 +435,9 @@ a later step can use `{"$step": 0, "field": "ids/0"}` to target the first match.
|
||||
| `styles` | By id: the shared styles the tree uses |
|
||||
| `components` | By id: `name`, `type`, `properties` with variants, `codeLinks` |
|
||||
| `fonts` | `family`, `styles`, and whether it's `installed` |
|
||||
| `assets` | What `inspect.assets` lists for the same layers |
|
||||
| `assets` | What `inspect.assets` lists, for the layers in the tree |
|
||||
| `missing` | `{what, why}`: what the design doesn't specify. Always says that the file stores no breakpoints |
|
||||
| `truncated` | When `depth` or `maxNodes` cut the tree, or `maxBytes` made the tool ask for fewer layers |
|
||||
| `truncated` | When `depth` or `maxNodes` cut the tree, or `maxBytes` made the tool ask for fewer layers: `layersLeftOut`, `assetsLeftOut`, `depth`, `maxNodes`; and `variables` when aliases went deeper than the 10,000 variables followed |
|
||||
|
||||
Each layer in `nodes` has `id`, `name`, `type` and:
|
||||
|
||||
|
||||
Reference in new issue
Block a user