diff --git a/crates/engine/src/assets/components/sync.rs b/crates/engine/src/assets/components/sync.rs index d0e7a6eac..7ef3fed8c 100644 --- a/crates/engine/src/assets/components/sync.rs +++ b/crates/engine/src/assets/components/sync.rs @@ -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>, 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>, + m: &mut Matched, +) { fn unique(pairs: impl Iterator) -> BTreeMap { let mut seen: BTreeMap> = 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); diff --git a/crates/engine/src/assets/components/tests.rs b/crates/engine/src/assets/components/tests.rs index 6bc8e83c5..ea5542c9d 100644 --- a/crates/engine/src/assets/components/tests.rs +++ b/crates/engine/src/assets/components/tests.rs @@ -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 = labels[2..].iter().map(|l| chars(&s, copy_of(&s, i, *l))).collect(); + let texts: Vec = 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 = 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]