Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Selected points follow their path: Toggle Selection and Add Anchor at the start

moppeeer9442f5d9c83a
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index d0ed5cacc..1d2a9d1df 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -246,6 +246,8 @@ pub fn specs() -> Vec<CommandSpec> {
st.selection.cells = None;
if let Some(i) = st.selection.items.iter().position(|x| *x == id) {
st.selection.items.remove(i);
+ // Its points go with it: arrow keys and Delete act on the selected points.
+ st.selection.anchors.retain(|a| a.0 != id);
} else {
st.selection.items.push(id);
}
diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rs
index 498dd5674..7e1693f86 100644
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@ -3208,22 +3208,25 @@ fn path_append(s: &mut Session, p: &Value) -> Result<Value> {
Some(v) if v.as_str() == Some("start") => true,
Some(_) => return Err(bad("path.appendAnchor", "`at` must be `start` or `end`")),
};
- s.edit(|d, _| {
+ s.edit(|d, sel| {
path_editable(d, id, "path.appendAnchor")?;
let loc = d.find(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
let inv = (d.parent_xf(&loc) * d.item_at(&loc).map(|i| i.xf).unwrap_or_default()).inverse();
let it = d.item_mut(id).ok_or(designcraft_doc::DocError::NoItem(id))?;
- let sp = match subpath {
- Some(i) => {
- usize::try_from(i).ok().and_then(|i| it.path.subpaths.get_mut(i)).ok_or_else(|| bad("path.appendAnchor", "no such subpath"))?
- }
- None => it.path.subpaths.last_mut().ok_or_else(|| bad("path.appendAnchor", "empty path"))?,
+ let si = match subpath {
+ Some(i) => usize::try_from(i).ok().filter(|i| *i < it.path.subpaths.len()).ok_or_else(|| bad("path.appendAnchor", "no such subpath"))?,
+ None => it.path.subpaths.len().checked_sub(1).ok_or_else(|| bad("path.appendAnchor", "empty path"))?,
};
+ let sp = &mut it.path.subpaths[si];
if sp.closed {
return Err(bad("path.appendAnchor", "the subpath is closed"));
}
if at_start {
sp.anchors.insert(0, a.transform(inv));
+ // The selected points of this sub-path move up one place with the others.
+ for a in sel.anchors.iter_mut().filter(|a| a.0 == id && a.1 == si) {
+ a.2 += 1;
+ }
} else {
sp.anchors.push(a.transform(inv));
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 634e22cb8..4d3af2f81 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1687,6 +1687,27 @@ fn direct_selection_moves_the_points_of_several_frames_at_once() {
assert_eq!(corners(&s, b), vec![(200.0, 300.0), (300.0, 300.0), (300.0, 400.0), (200.0, 400.0)]);
}
+#[test]
+fn selected_points_follow_their_path() {
+ let mut s = session();
+ let a = s.execute("frame.create", &json!({"rect": [100, 300, 200, 400]})).unwrap()["id"].as_u64().unwrap();
+ let b = s.execute("frame.create", &json!({"rect": [200, 300, 300, 400]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"anchors": [[a, 0, 1], [b, 0, 2]]})).unwrap();
+ // Shift-clicking a frame off the selection drops its points too.
+ s.execute("selection.toggle", &json!({"id": a})).unwrap();
+ let id = |n| designcraft_doc::ItemId(n);
+ assert_eq!(s.doc().unwrap().selection.anchors, vec![(id(b), 0, 2)]);
+ // A point added before the first one: the selected point keeps pointing at the same anchor.
+ let l = s.execute("path.create", &json!({"anchors": [{"p": [100, 100]}, {"p": [200, 100]}, {"p": [300, 150]}]})).unwrap()["id"].as_u64().unwrap();
+ s.execute("selection.set", &json!({"anchors": [[l, 0, 1]]})).unwrap();
+ s.execute("path.appendAnchor", &json!({"id": l, "anchor": {"p": [50, 120]}, "at": "start"})).unwrap();
+ let st = s.doc().unwrap();
+ let (_, si, ai) = st.selection.anchors[0];
+ let it = st.doc.item(id(l)).unwrap();
+ assert_eq!((it.xf * it.path.subpaths[si].anchors[ai].p).round(), designcraft_geom::Point::new(200.0, 100.0));
+ assert!(s.execute("path.appendAnchor", &json!({"id": l, "anchor": {"p": [0, 0]}, "subpath": 3})).is_err(), "no such subpath");
+}
+
#[test]
fn direct_selection_pulls_one_corner_of_an_unselected_frame() {
// Chris Nielsen's "The Adobe InDesign Toolbox" (10:10): with the White Arrow, click outside,