mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-29 01:34:37 +01:00
A text layer's name counts as unique only over all of a variant's text layers
The name fallback counted names after path matching had taken its layers, so with a/Label and b/Label switching to a/Label and c/Label, a/Label matched by path and the two leftovers looked unique: text typed into b/Label landed in c/Label, although Label names two layers on each side. Names are now counted over every text layer outside nested instances, matched ones too, and only then limited to the unmatched ones, as the rule says: exactly one layer of that name on each side.
This commit is contained in:
1 parent
8056e20dcf
commit
16976d07c3
2 files changed
+27
-10
No files matched your search
@@ -115,15 +115,23 @@ fn match_layers(
|
||||
}
|
||||
m.map.insert(*s, nid);
|
||||
}
|
||||
match_text_by_name(doc, inst, component, old, &mut m);
|
||||
match_text_by_name(doc, inst, component, src, old, &mut m);
|
||||
m
|
||||
}
|
||||
|
||||
/// Main text layers nothing matched take the unmatched old text layer of the same name, when
|
||||
/// exactly one of each has it: text typed into an instance stays when it switches to a variant that
|
||||
/// nests the same text layer in other frames (its name path differs). Only layers outside nested
|
||||
/// instances, which rebuild from their own mains.
|
||||
fn match_text_by_name(doc: &Document, inst: NodeId, component: NodeId, old: &BTreeMap<NodeId, std::sync::Arc<Node>>, m: &mut Matched) {
|
||||
/// instances, which rebuild from their own mains. Names are counted over all those text layers,
|
||||
/// matched ones too, so a name two layers share on either side never pairs by elimination.
|
||||
fn match_text_by_name(
|
||||
doc: &Document,
|
||||
inst: NodeId,
|
||||
component: NodeId,
|
||||
src: &[NodeId],
|
||||
old: &BTreeMap<NodeId, std::sync::Arc<Node>>,
|
||||
m: &mut Matched,
|
||||
) {
|
||||
fn unique<K: Ord>(pairs: impl Iterator<Item = (K, NodeId)>) -> BTreeMap<K, NodeId> {
|
||||
let mut seen: BTreeMap<K, Option<NodeId>> = BTreeMap::new();
|
||||
for (k, id) in pairs {
|
||||
@@ -132,19 +140,19 @@ fn match_text_by_name(doc: &Document, inst: NodeId, component: NodeId, old: &BTr
|
||||
seen.into_iter().filter_map(|(k, id)| Some((k, id?))).collect()
|
||||
}
|
||||
let text = |n: &Node| matches!(n.kind, NodeKind::Text(_));
|
||||
let mains = unique(
|
||||
m.fresh.iter().filter_map(|s| doc.node(*s)).filter(|s| text(s) && outside_nested(doc, s.id, component)).map(|s| (s.name.clone(), s.id)),
|
||||
);
|
||||
let mut mains =
|
||||
unique(src.iter().filter_map(|s| doc.node(*s)).filter(|s| text(s) && outside_nested(doc, s.id, component)).map(|s| (s.name.clone(), s.id)));
|
||||
mains.retain(|_, s| m.fresh.contains(s));
|
||||
if mains.is_empty() {
|
||||
return;
|
||||
}
|
||||
let olds = unique(
|
||||
old.iter()
|
||||
.filter(|(id, o)| text(o) && !m.used.contains(id) && outside_nested(doc, **id, inst))
|
||||
.filter(|(id, o)| text(o) && outside_nested(doc, **id, inst))
|
||||
.map(|(id, o)| (o.source.and_then(|s| doc.node(s)).map_or(&o.name, |s| &s.name).clone(), *id)),
|
||||
);
|
||||
for (name, s) in mains {
|
||||
if let Some(o) = olds.get(&name) {
|
||||
if let Some(o) = olds.get(&name).filter(|o| !m.used.contains(*o)) {
|
||||
m.used.insert(*o);
|
||||
m.map.insert(s, *o);
|
||||
m.fresh.retain(|f| *f != s);
|
||||
|
||||
@@ -286,7 +286,8 @@ fn variant_switch_keeps_compatible_overrides() {
|
||||
fn typed_text_stays_across_variants_that_nest_it_differently() {
|
||||
let mut s = Session::new();
|
||||
// Each variant's Label layers, by the frames holding them.
|
||||
let shapes: [(&str, &[&[&str]]); 3] = [("Kind=Plain", &[&["text"]]), ("Kind=Boxed", &[&["div", "text"]]), ("Kind=Twice", &[&["a"], &["b"]])];
|
||||
let shapes: [(&str, &[&[&str]]); 4] =
|
||||
[("Kind=Plain", &[&["text"]]), ("Kind=Boxed", &[&["div", "text"]]), ("Kind=Twice", &[&["a"], &["b"]]), ("Kind=Moved", &[&["a"], &["c"]])];
|
||||
let mut labels = Vec::new();
|
||||
let mut variants = Vec::new();
|
||||
for (k, (name, paths)) in shapes.into_iter().enumerate() {
|
||||
@@ -313,8 +314,16 @@ fn typed_text_stays_across_variants_that_nest_it_differently() {
|
||||
|
||||
ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Kind": "Twice"}}));
|
||||
healthy(&s);
|
||||
let texts: Vec<String> = labels[2..].iter().map(|l| chars(&s, copy_of(&s, i, *l))).collect();
|
||||
let texts: Vec<String> = labels[2..4].iter().map(|l| chars(&s, copy_of(&s, i, *l))).collect();
|
||||
assert_eq!(texts, ["I agree", "I agree"], "two layers of that name: neither is the one typed into");
|
||||
|
||||
// `a › Label` matches by path; the leftover `b › Label` and `c › Label` still share a name with it.
|
||||
let typed = copy_of(&s, i, labels[3]);
|
||||
ex(&mut s, "text.set", json!({"id": typed, "characters": "Architect"}));
|
||||
ex(&mut s, "instance.setProperties", json!({"ids": [i], "properties": {"Kind": "Moved"}}));
|
||||
healthy(&s);
|
||||
let texts: Vec<String> = labels[4..].iter().map(|l| chars(&s, copy_of(&s, i, *l))).collect();
|
||||
assert_eq!(texts, ["I agree", "I agree"], "a name two layers share doesn't pair what path matching left over");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user