shape.connect: with one site given, the other end is the site closest to it

The facing rule is for when neither site is given; with exactly one given the other end
is the other shape's site closest to it, as it was before the default changed. Test extended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zach Berry
2026-11-05 21:53:02 -06:00
co-authored by Claude Opus 5.5
parent 39d8c5d7dd
commit f0c4bd6e6f
3 changed files with 28 additions and 3 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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
),
+18 -2
View File
@@ -51,8 +51,24 @@ pub(super) fn connect(s: &mut Session, p: &Value) -> Result<Value> {
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
+9
View File
@@ -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