Merge pull request #2423 from Silvertree2010/fix/2416-bug-report-figcraft-library-acce

Keep library variables that styles bind when accepting updates
This commit is contained in:
BFlat authored and GitHub committed 2026-11-17 18:20:55 +07:00
commit 2eb14ab0af
2 files changed
+55 -1

No files matched your search

+3 -1
View File
@@ -1158,10 +1158,12 @@ pub fn prune(doc: &mut Document, library: &str) -> Result<()> {
doc.styles.retain(|s| !unused.contains(&s.id));
doc.libraries_mut().styles.retain(|r| !unused.contains(&r.local));
}
// Variables layers bind or set, local variables alias or extend (with what those alias).
// Variables layers bind or set, styles bind, local variables alias or extend (with what those
// alias).
let seeds = doc
.nodes()
.flat_map(|n| n.bound.values().map(String::as_str).chain(action_variables(n)))
.chain(doc.styles.iter().flat_map(|s| s.bound.values().map(String::as_str)))
.chain(doc.variables.variables.iter().filter_map(|v| v.extends.as_deref()))
.map(str::to_string)
.chain(
@@ -550,3 +550,55 @@ fn stored_variable_ids_keep_library_alias_dependencies() {
assert!(copy.values.values().all(|v| *v == VariableValue::Alias(map.variables[target].clone())));
assert!(dest.variables.collections.iter().any(|c| c.name == "Base"));
}
/// A library with collection `coll` (Main/Alt), a colour variable `var` and a paint style `var`
/// bound to it, published to `path`; returns (the library session, its id).
fn styled_library(path: &str, coll: &str, var: &str, main: &str, alt: &str) -> (Session, String) {
let mut lib = Session::new();
ex(&mut lib, "variable.createCollection", json!({"name": coll, "modes": ["Main", "Alt"]}));
let v = ex(&mut lib, "variable.create", json!({"collection": coll, "name": var, "type": "color", "values": {"Main": main, "Alt": alt}}))["id"]
.clone();
let style = ex(&mut lib, "style.create", json!({"type": "paint", "name": var, "value": main}))["id"].clone();
ex(&mut lib, "style.bind", json!({"style": style, "field": "fill", "variable": v}));
let lid = publish_to(&mut lib, path);
(lib, lid)
}
#[test]
fn accepting_updates_keeps_variables_only_library_styles_use() {
let d = dir("accept-style-vars");
let (pa, pb) = (path_str(&d.join("a.figcraft")), path_str(&d.join("b.figcraft")));
let (mut a, la) = styled_library(&pa, "TokensA", "accent", "#1565c0", "#2e7d32");
let (_b, lb) = styled_library(&pb, "TokensB", "primary", "#7b1fa2", "#00838f");
let mut s = Session::new();
let frame = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 200, "height": 200})));
let ra = idv(&ex(&mut s, "shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20, "parent": frame})));
let rb = idv(&ex(&mut s, "shape.rectangle", json!({"x": 50, "y": 10, "width": 20, "height": 20, "parent": frame})));
ex(&mut s, "library.add", json!({"path": pa}));
ex(&mut s, "library.add", json!({"path": pb}));
ex(&mut s, "library.applyStyle", json!({"library": la, "style": "accent", "id": ra}));
ex(&mut s, "library.applyStyle", json!({"library": lb, "style": "primary", "id": rb}));
ex(&mut s, "library.setVariableMode", json!({"library": la, "collection": "TokensA", "mode": "Alt", "ids": [frame]}));
ex(&mut s, "library.setVariableMode", json!({"library": lb, "collection": "TokensB", "mode": "Alt", "ids": [frame]}));
assert_eq!(fill(&s, ra), "2E7D32");
assert_eq!(fill(&s, rb), "00838F");
healthy(&s);
ex(&mut a, "variable.set", json!({"variable": "accent", "mode": "Alt", "value": "#ef6c00"}));
republish(&mut a);
let r = ex(&mut s, "library.acceptUpdates", json!({}));
assert_eq!(r["variables"], 1, "{r}");
let doc = s.doc().unwrap();
for name in ["accent", "primary"] {
let v = doc.variables.variables.iter().find(|v| v.name == name && is_remote_variable(doc, &v.id));
assert!(v.is_some(), "library variable copy `{name}` was deleted");
let id = &v.unwrap().id;
assert!(doc.libraries.variables.iter().any(|r| &r.local == id), "record of `{name}` was deleted");
}
assert_eq!(fill(&s, ra), "EF6C00");
assert_eq!(fill(&s, rb), "00838F");
healthy(&s);
let _ = std::fs::remove_dir_all(&d);
}