diff --git a/crates/engine/src/cmd/shape.rs b/crates/engine/src/cmd/shape.rs index ee911363c..3c00ac4dd 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 sites facing each other along the dominant direction between the shapes, a tie to the lower index; 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}", + "{from: shape id, to: shape id, fromSite?, toSite?: site index (default: with neither given, the sites facing each other along the dominant direction between the shapes, a tie to the lower index; with one given, the closest site of the other shape; 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 54321c84c..da6fe408e 100644 --- a/crates/engine/src/cmd/shape/freeform.rs +++ b/crates/engine/src/cmd/shape/freeform.rs @@ -51,8 +51,24 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result { let best = cos.iter().copied().fold(f64::NEG_INFINITY, f64::max); cos.iter().position(|x| *x >= best - 1e-9).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)); + // With one site given, the other end is the site of the other shape closest to it, as before; + // the facing rule is for the case where neither is given. + let closest = |v: &[deckcraft_geom::Point], to: deckcraft_geom::Point| { + let mut best = (f64::MAX, 0); + for (i, q) in v.iter().enumerate() { + let d = (*q - to).hypot(); + if d < best.0 { + best = (d, i); + } + } + best.1 + }; + let (ia, ib) = match (pick(&a, "fromSite"), pick(&b, "toSite")) { + (Some(i), Some(j)) => (i, j), + (Some(i), None) => (i, closest(&b, a[i])), + (None, Some(j)) => (closest(&a, b[j]), j), + (None, None) => (facing(&a, ca, cb.x - ca.x, cb.y - ca.y), 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/tests.rs b/crates/engine/src/tests.rs index 64e48696c..5b89efe49 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6783,6 +6783,15 @@ fn connector_default_sites_follow_the_dominant_direction() { let big = s.execute("shape.insert", &json!({"preset": "rect", "rect": [600, 300, 200, 100]})).unwrap()["id"].as_u64().unwrap(); let small = s.execute("shape.insert", &json!({"preset": "rect", "rect": [650, 325, 100, 50]})).unwrap()["id"].as_u64().unwrap(); assert_eq!(sites(&mut s, big, small), (Some(0), Some(0))); + // One site given: the other end is the other shape's site closest to it, as before the facing + // rule (which is for when neither is given). From a's right side (100, 25) the diagonal box's + // top (200, 200) is nearest; to its left side (150, 225), a's bottom (50, 50). + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "fromSite": 3})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(3), Some(0)), "fromSite only: {r}"); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "toSite": 1})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(1)), "toSite only: {r}"); + let r = s.execute("shape.connect", &json!({"from": a, "to": diag, "fromSite": 2, "toSite": 3})).unwrap(); + assert_eq!((r["fromSite"].as_u64(), r["toSite"].as_u64()), (Some(2), Some(3)), "both given: {r}"); } /// A size (or colour, bold…) set on a shape before it has any text survives the text that is