diff --git a/crates/engine/src/cmd/shape.rs b/crates/engine/src/cmd/shape.rs index a447a20b9..db91a48ed 100644 --- a/crates/engine/src/cmd/shape.rs +++ b/crates/engine/src/cmd/shape.rs @@ -34,7 +34,7 @@ pub fn specs() -> Vec { "Connect Shapes", [], None, - "{from: shape id, to: shape id, fromSite?, toSite?: site index (default: the closest pair), id?: existing line to glue (else a new connector), preset?: straightConnector1|bentConnector3|curvedConnector3} → {id}", + "{from: shape id, to: shape id, fromSite?, toSite?: site index (default: the sites facing each other along the dominant direction between the shapes; a box has 0 top, 1 left, 2 bottom, 3 right; shape.sites lists them), id?: existing line to glue (else a new connector), preset?: straightConnector1|bentConnector3|curvedConnector3} → {id}", has_slide, connect ), diff --git a/crates/engine/src/cmd/shape/freeform.rs b/crates/engine/src/cmd/shape/freeform.rs index 5fd299c9e..b068f86ac 100644 --- a/crates/engine/src/cmd/shape/freeform.rs +++ b/crates/engine/src/cmd/shape/freeform.rs @@ -24,21 +24,29 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result { return Err(bad("shape.connect", "both shapes need connection sites (lines and groups have none)")); } let pick = |v: &[deckcraft_geom::Point], key: &str| usize_param(p, key).filter(|i| *i < v.len()); - let (ia, ib) = match (pick(&a, "fromSite"), pick(&b, "toSite")) { - (Some(i), Some(j)) => (i, j), - (fi, fj) => { - let mut best = (f64::MAX, 0, 0); - for (i, pa) in a.iter().enumerate().filter(|(i, _)| fi.is_none_or(|f| f == *i)) { - for (j, pb) in b.iter().enumerate().filter(|(j, _)| fj.is_none_or(|f| f == *j)) { - let d = (*pa - *pb).hypot(); - if d < best.0 { - best = (d, i, j); - } - } - } - (best.1, best.2) - } + // A site left unsaid is the one facing the other shape along the dominant direction between + // the two (a box mostly below is left from the bottom), which is where a person glues an + // arrow; "the closest pair" chose a side site for a diagonal neighbour and crossed boxes. + let centre = |v: &[deckcraft_geom::Point]| { + let n = v.len().max(1) as f64; + deckcraft_geom::Point::new(v.iter().map(|q| q.x).sum::() / n, v.iter().map(|q| q.y).sum::() / n) }; + let (ca, cb) = (centre(&a), centre(&b)); + let facing = |v: &[deckcraft_geom::Point], c: deckcraft_geom::Point, dx: f64, dy: f64| { + let len = dx.hypot(dy).max(1e-9); + v.iter() + .enumerate() + .map(|(i, q)| { + let (sx, sy) = (q.x - c.x, q.y - c.y); + let sl = sx.hypot(sy).max(1e-9); + (i, (sx * dx + sy * dy) / (sl * len)) + }) + .max_by(|p, q| p.1.total_cmp(&q.1)) + .map(|(i, _)| i) + .unwrap_or(0) + }; + let ia = pick(&a, "fromSite").unwrap_or_else(|| facing(&a, ca, cb.x - ca.x, cb.y - ca.y)); + let ib = pick(&b, "toSite").unwrap_or_else(|| facing(&b, cb, ca.x - cb.x, ca.y - cb.y)); let (p0, p1) = (a[ia], b[ib]); let id = match id_param(p, "id", "shape.connect")? { // The ends are in slide space and rerouting follows the slide's own connectors, so a line diff --git a/crates/engine/src/cmd/text/caret.rs b/crates/engine/src/cmd/text/caret.rs index c7305d62e..845e72d60 100644 --- a/crates/engine/src/cmd/text/caret.rs +++ b/crates/engine/src/cmd/text/caret.rs @@ -338,7 +338,10 @@ pub(super) fn set(s: &mut Session, p: &Value) -> Result { _ => Some(sh.text.get_or_insert_with(TextBody::default)), }; let Some(body) = target else { return Err(bad("text.set", "no text here (tables need `cell`)")) }; - let keep = body.paragraphs.first().and_then(|p| p.runs.first()).map(|r| r.props.clone()).unwrap_or_default(); + // The first run's formatting, or for text that is still empty the formatting its end + // mark holds (format.* before any text lands there), so a size set before text.set survives. + let keep = + body.paragraphs.first().map(|p| p.runs.first().map(|r| r.props.clone()).unwrap_or_else(|| p.end_props.clone())).unwrap_or_default(); let levels: Vec<(u8, deckcraft_model::text::ParaProps)> = body.paragraphs.iter().map(|p| (p.level, p.props.clone())).collect(); let mut nb = TextBody::from_text(&text); for (i, para) in nb.paragraphs.iter_mut().enumerate() { @@ -353,6 +356,9 @@ pub(super) fn set(s: &mut Session, p: &Value) -> Result { for r in &mut para.runs { r.props = keep.clone(); } + // The end mark keeps it too, so an empty `text` (no runs) carries the formatting to + // the text set next. + para.end_props = keep.clone(); } body.paragraphs = nb.paragraphs; Ok(()) diff --git a/crates/engine/src/cmd/text/formatting.rs b/crates/engine/src/cmd/text/formatting.rs index d942ee282..60923c9c2 100644 --- a/crates/engine/src/cmd/text/formatting.rs +++ b/crates/engine/src/cmd/text/formatting.rs @@ -72,6 +72,11 @@ pub(crate) fn format_run_levels( } _ => { let body = sh.text.get_or_insert_with(TextBody::default); + // Text that is still empty keeps the formatting on an end mark, so a size set + // before the text lands survives it; a body with no paragraph has none. + if body.paragraphs.is_empty() { + body.paragraphs.push(Default::default()); + } let eff = effective.iter().find(|(i, _)| *i == id).map(|(_, e)| e); match eff { Some(eff) => { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ca1f93d26..af36218e0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6750,6 +6750,41 @@ fn invalid_slide_index_never_falls_back_to_the_selection_in_export_commands() { let _ = std::fs::remove_dir_all(&dir); } +/// G6: with no sites given, shape.connect joins the sites that face each other along the +/// dominant direction between the two shapes (a box mostly below leaves from the bottom), not +/// "the closest pair", which for a diagonal neighbour picked a side site and crossed boxes. +#[test] +fn connector_default_sites_follow_the_dominant_direction() { + let mut s = session(); + let a = s.execute("shape.insert", &json!({"preset": "rect", "rect": [0, 0, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let diag = s.execute("shape.insert", &json!({"preset": "rect", "rect": [150, 200, 100, 50]})).unwrap()["id"].as_u64().unwrap(); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(0)), "mostly below: bottom → top, not right → left: {r}"); + let r = s.execute("shape.connect", &json!({"from": diag, "to": a})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(0), Some(2)), "and back: top → bottom: {r}"); +} + +/// A size (or colour, bold…) set on a shape before it has any text survives the text that is +/// then set: text.set keeps the first run's properties, and an empty paragraph keeps them on its +/// end mark, which is what the agent's format.size then set_text sequence relies on. +#[test] +fn text_set_keeps_the_formatting_of_an_empty_shape() { + let mut s = session(); + let id = s.execute("shape.insert", &json!({"preset": "rect", "rect": [10, 10, 300, 60]})).unwrap()["id"].as_u64().unwrap(); + s.execute("format.size", &json!({"size": 30})).unwrap(); + s.execute("format.bold", &json!({"on": true})).unwrap(); + s.execute("text.set", &json!({"id": id, "text": "Hello"})).unwrap(); + let props = + |s: &Session| s.doc().unwrap().shape(deckcraft_model::ShapeId(id as u32)).unwrap().text.as_ref().unwrap().paragraphs[0].runs[0].props.clone(); + let run = props(&s); + assert_eq!((run.size, run.bold), (Some(30.0), Some(true)), "{run:?}"); + // Emptied and filled again: the empty paragraph's end mark carried the formatting. + s.execute("text.set", &json!({"id": id, "text": ""})).unwrap(); + s.execute("text.set", &json!({"id": id, "text": "Again"})).unwrap(); + let run = props(&s); + assert_eq!((run.size, run.bold), (Some(30.0), Some(true)), "after an empty text.set: {run:?}"); +} + #[test] fn highlight_with_an_invalid_colour_is_rejected_and_keeps_the_highlight() { let mut s = session();