diff --git a/crates/engine/src/cmd/blocks/attedit.rs b/crates/engine/src/cmd/blocks/attedit.rs index 16919f500..2ee9358cc 100644 --- a/crates/engine/src/cmd/blocks/attedit.rs +++ b/crates/engine/src/cmd/blocks/attedit.rs @@ -154,6 +154,19 @@ fn set_value(text: &mut Text, value: String) { } } +/// Move `text` so the point it is placed by lands on `to`: the alignment point of justified text, +/// else the insertion point. Both points move by the same offset, so aligned and fit text keep the +/// baseline between them. +fn move_text(text: &mut Text, to: cadcraft_geom::Vec2) { + let anchor = match text.align_pt { + Some(ap) if !matches!((text.halign, text.valign), (HAlign::Left, VAlign::Baseline) | (HAlign::Aligned | HAlign::Fit, _)) => ap, + _ => text.insert, + }; + let d = Vec3::new(to.x - anchor.x, to.y - anchor.y, 0.0); + text.insert = text.insert + d; + text.align_pt = text.align_pt.map(|ap| ap + d); +} + /// Change the properties set on attribute `k` of block reference `h`. fn set_props(s: &mut Session, (h, k): (Handle, usize), f: impl FnOnce(&mut AttribProps)) -> Result<()> { s.doc_mut()?.modify_entity(h, |e| { @@ -542,12 +555,7 @@ impl Interactive for DashAtteditM { _ => return Ok(Step::Continue), }; if let Some(it) = self.items.get(self.cur).copied() { - modify_attrib(s, it, |a| { - a.text.insert = Vec3::new(p.x, p.y, a.text.insert.z); - if a.text.align_pt.is_some() { - a.text.align_pt = Some(Vec3::new(p.x, p.y, a.text.insert.z)); - } - })?; + modify_attrib(s, it, |a| move_text(&mut a.text, p))?; } self.stage = Stage::Option; } diff --git a/crates/engine/tests/it/attedit_prompts.rs b/crates/engine/tests/it/attedit_prompts.rs index 27bf556b9..15fd5e7aa 100644 --- a/crates/engine/tests/it/attedit_prompts.rs +++ b/crates/engine/tests/it/attedit_prompts.rs @@ -117,3 +117,34 @@ fn attribute_tags_match_in_any_case_and_unknown_tags_are_reported() { let r = s.execute("attedit", &json!({"handle": h.hex(), "values": {"T": "four"}})).unwrap(); assert_eq!(r, json!({"changed": 1})); } + +/// -ATTEDIT Position moves an aligned or fit attribute without losing its baseline: both of its +/// points move by the same offset (setting both to the picked point would leave no direction or +/// length to fit the text between). +#[test] +fn dash_attedit_position_keeps_an_aligned_attributes_baseline() { + use cadcraft_doc::HAlign; + use cadcraft_geom::Vec3; + for halign in [HAlign::Aligned, HAlign::Fit] { + let mut s = with_block(); + let h = handle(&insert(&mut s, 100.0, json!({"T": "abc"}))); + s.doc_mut() + .unwrap() + .modify_entity(h, |e| { + if let EntityKind::Insert(i) = &mut e.kind { + for a in i.attribs.iter_mut().filter(|a| a.tag == "T") { + a.text.halign = halign; + a.text.insert = Vec3::new(100.0, 1.0, 0.0); + a.text.align_pt = Some(Vec3::new(104.0, 1.0, 0.0)); + } + } + }) + .unwrap(); + type_lines(&mut s, &["-attedit", "y", "", "T", "", "105,0", "", "p", "10,20", "n"]); + assert!(s.running.is_none(), "{}", s.prompt_text()); + let EntityKind::Insert(ins) = &s.doc().unwrap().entity(h).unwrap().kind else { panic!("not an insert") }; + let t = &ins.attribs.iter().find(|a| a.tag == "T").unwrap().text; + assert!(t.insert.xy().near(cadcraft_geom::Vec2::new(10.0, 20.0), 1e-9), "{halign:?}: {:?}", t.insert); + assert!(t.align_pt.is_some_and(|p| p.xy().near(cadcraft_geom::Vec2::new(14.0, 20.0), 1e-9)), "{halign:?}: {:?}", t.align_pt); + } +}