From 094f59566c024a363abfead0916650e99843f0c4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 18:05:25 -0800 Subject: [PATCH] Arrange Tools: Group by Row refuses parts of live groups and graphs and leaves layers alone Group by Row now also refuses direct children of Objects on Path and Live Paint groups and objects inside a live graph, while a whole live group or graph can still be grouped with other art. Selected layers and sublayers stay selected and are not grouped. --- crates/engine/src/cmd/addons/arrange.rs | 33 +++-- crates/engine/src/cmd/addons/arrange/tests.rs | 129 ++++++++++++++++++ docs/mcp.md | 2 +- 3 files changed, 151 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/addons/arrange.rs b/crates/engine/src/cmd/addons/arrange.rs index 1d6d57b0cc..7768b4f62e 100644 --- a/crates/engine/src/cmd/addons/arrange.rs +++ b/crates/engine/src/cmd/addons/arrange.rs @@ -104,7 +104,7 @@ pub fn specs() -> Vec { "Group by Row", ["Object", "Plug-ins", "Arrange Tools"], None, - "{direction?: \"row\" (objects whose vertical extents overlap) | \"column\" (objects whose horizontal extents overlap) (\"row\" when omitted; null is invalid)} group the selected objects (the top-level ones, at most 20000) into one group for each row (column), as one undo step. Parameters must be an object. An object joins a row when its visual bounds overlap the row's extent across it. Measurement includes strokes and vector effects, measures a clipping group by its mask, and excludes raster effects such as shadows, glows, and blurs. Extents that only touch stay apart, and an object that overlaps two rows joins them into one. Only objects in the same layer or group and on the same artboard (the one holding the object's center, else the one it overlaps most) share a row. Objects outside all artboards, locked or hidden objects, and clipping masks stay in place. The command rejects selections of editable objects whose parent is neither a layer nor a group, including live blend keys, and direct children of Shaper groups or live compound shapes. An object alone in its row stays as it is, and each new group goes where its frontmost member was in the stacking order; the new groups (top to bottom, or left to right) and the objects left alone stay selected. A run with no row of two or more objects makes no step → {groups: [id…], grouped (how many objects went into groups)}", + "{direction?: \"row\" (objects whose vertical extents overlap) | \"column\" (objects whose horizontal extents overlap) (\"row\" when omitted; null is invalid)} group the selected objects (the top-level ones, at most 20000) into one group for each row (column), as one undo step. Parameters must be an object. An object joins a row when its visual bounds overlap the row's extent across it. Measurement includes strokes and vector effects, measures a clipping group by its mask, and excludes raster effects such as shadows, glows, and blurs. Extents that only touch stay apart, and an object that overlaps two rows joins them into one. Only objects in the same layer or group and on the same artboard (the one holding the object's center, else the one it overlaps most) share a row. Objects outside all artboards, locked or hidden objects, clipping masks, layers, and sublayers stay in place. Every grouping candidate must have a parent that is a layer or group. The command rejects selections containing editable live blend keys, direct children of Shaper groups, live compound shapes, Objects on Path groups, or Live Paint groups, and any descendants of live graphs. Complete live groups and graphs can be grouped with other artwork. An object alone in its row stays as it is, and each new group goes where its frontmost member was in the stacking order; the new groups (top to bottom, or left to right) and the objects left alone stay selected. A run with no row of two or more objects makes no step → {groups: [id…], grouped (how many objects went into groups)}", has_multi, group_rows ), @@ -471,26 +471,33 @@ fn group_rows(s: &mut Session, p: &Value) -> Result { let d = &s.doc()?.doc; let editable_roots: Vec<_> = { let editable = d.editable(); - roots.iter().copied().filter(|id| editable(*id)).collect() + roots.iter().copied().filter(|id| editable(*id) && d.node(*id).is_some_and(|n| !n.is_layer())).collect() }; for id in &editable_roots { - if d.parent_of(*id).and_then(|parent| d.node(parent)).is_some_and(|n| { - !matches!(n.kind, NodeKind::Layer { .. } | NodeKind::Group { .. }) || n.is_compound_shape() || n.extras().shaper.is_some() - }) { + let parent = d.parent_of(*id); + let valid_parent = parent.and_then(|parent| d.node(parent)).is_some_and(|n| { + matches!(n.kind, NodeKind::Layer { .. } | NodeKind::Group { .. }) + && !n.is_compound_shape() + && n.extras().shaper.is_none() + && n.extras().objects_on_path.is_none() + && !vectorcraft_tools::builder::is_live_paint(n) + }); + let in_graph = parent + .and_then(|parent| d.ancestry(parent)) + .is_some_and(|ancestors| ancestors.iter().any(|ancestor| d.node(*ancestor).is_some_and(|n| n.extras().graph.is_some()))); + if !valid_parent || in_graph { return Err(bad(C, "selected objects must be in a layer or group without live structural roles")); } } // Each object's row key (its parent and its artboard) and its extent across the rows. - type Keyed = ((Option, usize), (f64, f64)); + type Keyed = ((NodeId, usize), (f64, f64)); let symbols = d.symbol_boxes(); let items: Vec<(NodeId, Keyed)> = editable_roots .iter() .filter_map(|id| { - let parent = d.parent_of(*id); - if parent - .and_then(|parent| d.node(parent)) - .is_some_and(|n| n.clips() && n.children().and_then(|c| c.first()).is_some_and(|c| c.id == *id)) - { + let parent = d.parent_of(*id)?; + let container = d.node(parent)?; + if container.clips() && container.children().and_then(|c| c.first()).is_some_and(|c| c.id == *id) { return None; } let b = effects::preview::visual_bounds(&symbols.unmeasured(), d.node(*id)?)?; @@ -500,7 +507,9 @@ fn group_rows(s: &mut Session, p: &Value) -> Result { .collect(); let keyed: Vec<_> = items.iter().map(|(_, k)| *k).collect(); let sets: Vec> = rows_of(&keyed).into_iter().map(|set| set.iter().filter_map(|i| Some(items.get(*i)?.0)).collect()).collect(); - group_each(s, if rows { "Group by Row" } else { "Group by Column" }, &roots, sets) + let mut selection = roots; + selection.extend(s.doc()?.selection.objects.iter().copied().filter(|id| d.node(*id).is_some_and(|n| n.is_layer()))); + group_each(s, if rows { "Group by Row" } else { "Group by Column" }, &selection, sets) } /// Put each of `sets` (two or more of the selected top-level objects `roots`, each in stacking order) into a diff --git a/crates/engine/src/cmd/addons/arrange/tests.rs b/crates/engine/src/cmd/addons/arrange/tests.rs index 5416687473..6f50164c0c 100644 --- a/crates/engine/src/cmd/addons/arrange/tests.rs +++ b/crates/engine/src/cmd/addons/arrange/tests.rs @@ -616,6 +616,135 @@ fn group_rows_keeps_clipping_masks_first_in_groups_and_layers() { } } +#[test] +fn group_rows_leaves_selected_layers_and_sublayers_in_place() { + for sublayers in [false, true] { + let mut s = session(); + let parent = s.doc().unwrap().doc.layers[0].id; + let mut layers = vec![]; + for _ in 0..2 { + let command = if sublayers { "layer.newSublayer" } else { "layer.new" }; + let params = if sublayers { json!({"parent": parent.0}) } else { json!({"top": true}) }; + let layer = NodeId(s.execute(command, ¶ms).unwrap()["id"].as_u64().unwrap()); + rect(&mut s, 10.0, 10.0, 20.0, 20.0); + layers.push(layer); + } + s.execute("layer.setCurrent", &json!({"id": parent.0})).unwrap(); + let a = rect(&mut s, 10.0, 10.0, 20.0, 20.0); + let b = rect(&mut s, 15.0, 15.0, 20.0, 20.0); + for direction in ["row", "column"] { + select(&mut s, &layers); + let before = s.doc().unwrap().doc.clone(); + let n = steps(&s); + let r = s.execute("addon.arrange.groupRows", &json!({"direction": direction})).unwrap(); + assert_eq!(r["grouped"], json!(0), "layers must not become ordinary group members"); + assert_eq!(steps(&s), n); + assert!(*s.doc().unwrap().doc == *before); + select(&mut s, &[layers[0], layers[1], a, b]); + let r = s.execute("addon.arrange.groupRows", &json!({"direction": direction})).unwrap(); + assert_eq!(r["grouped"], json!(2)); + let groups = ids_of(&r["groups"]); + assert_eq!(children(&s, groups[0]), [a, b]); + for layer in &layers { + assert_eq!(s.doc().unwrap().doc.parent_of(*layer), sublayers.then_some(parent)); + assert!(s.doc().unwrap().selection.objects.contains(layer)); + } + s.execute("edit.undo", &json!({})).unwrap(); + assert!(*s.doc().unwrap().doc == *before); + } + } +} + +#[test] +fn group_rows_refuses_objects_without_a_parent() { + let mut s = session(); + let a = rect(&mut s, 10.0, 10.0, 20.0, 20.0); + let b = rect(&mut s, 15.0, 15.0, 20.0, 20.0); + s.edit("Set root objects", |d, _| { + for id in [a, b] { + let node = d.remove(id)?; + d.layers.push(node); + } + Ok(()) + }) + .unwrap(); + select(&mut s, &[a, b]); + refuses(&mut s, "addon.arrange.groupRows", &[json!({"direction": "row"}), json!({"direction": "column"})]); +} + +fn refuse_row_parts_and_group_whole(s: &mut Session, group: NodeId, parts: &[Vec]) { + let center = bounds(s, group).center(); + let a = rect(s, center.x - 10.0, center.y - 10.0, 20.0, 20.0); + let b = rect(s, center.x - 5.0, center.y - 5.0, 20.0, 20.0); + let members = children(s, group); + for direction in ["row", "column"] { + for selected in parts { + select(s, selected); + let selection = s.doc().unwrap().selection.clone(); + refuses(s, "addon.arrange.groupRows", &[json!({"direction": direction})]); + assert_eq!(s.doc().unwrap().selection, selection); + let mut mixed = selected.clone(); + mixed.extend([a, b]); + select(s, &mixed); + refuses(s, "addon.arrange.groupRows", &[json!({"direction": direction})]); + assert_eq!(children(s, group), members); + } + select(s, &[group, a, b]); + let before = s.doc().unwrap().doc.clone(); + let n = steps(s); + let r = s.execute("addon.arrange.groupRows", &json!({"direction": direction})).unwrap(); + assert_eq!(r["grouped"], json!(3)); + let groups = ids_of(&r["groups"]); + assert_eq!(children(s, groups[0]), [group, a, b]); + assert_eq!(children(s, group), members); + assert_eq!(steps(s), n + 1); + s.execute("edit.undo", &json!({})).unwrap(); + assert!(*s.doc().unwrap().doc == *before); + } +} + +#[test] +fn group_rows_refuses_objects_on_path_children_but_groups_the_whole_group() { + let mut s = session(); + let path = rect(&mut s, 20.0, 20.0, 100.0, 100.0); + let member = rect(&mut s, 30.0, 30.0, 20.0, 20.0); + let group = NodeId(s.execute("object.objectsOnPath.attach", &json!({"path": path.0, "ids": [member.0]})).unwrap()["id"].as_u64().unwrap()); + refuse_row_parts_and_group_whole(&mut s, group, &[vec![path, member]]); + assert!(s.doc().unwrap().doc.node(group).unwrap().extras().objects_on_path.is_some()); +} + +#[test] +fn group_rows_refuses_live_paint_faces_and_edges_but_groups_the_whole_group() { + let mut s = session(); + let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0); + let b = rect(&mut s, 50.0, 0.0, 100.0, 100.0); + select(&mut s, &[a, b]); + let group = NodeId(s.execute("livePaint.make", &json!({})).unwrap()["id"].as_u64().unwrap()); + let node = s.doc().unwrap().doc.node(group).unwrap(); + let faces: Vec<_> = vectorcraft_tools::builder::faces(node).iter().map(|n| n.id).collect(); + let edges: Vec<_> = vectorcraft_tools::builder::edges(node).iter().map(|n| n.id).collect(); + assert!(faces.len() >= 2); + assert!(edges.len() >= 2); + refuse_row_parts_and_group_whole(&mut s, group, &[faces.clone(), edges.clone()]); + let node = s.doc().unwrap().doc.node(group).unwrap(); + assert_eq!(vectorcraft_tools::builder::faces(node).iter().map(|n| n.id).collect::>(), faces); + assert_eq!(vectorcraft_tools::builder::edges(node).iter().map(|n| n.id).collect::>(), edges); +} + +#[test] +fn group_rows_refuses_live_graph_descendants_but_groups_the_whole_graph() { + let mut s = session(); + let params = json!({"type": "column", "x": 20, "y": 20, "width": 200, "height": 200, "csv": ",Series\nA,3\nB,5"}); + let graph = NodeId(s.execute("graph.create", ¶ms).unwrap()["id"].as_u64().unwrap()); + let node = s.doc().unwrap().doc.node(graph).unwrap(); + let series = node.children().unwrap().iter().find(|n| n.extras().series_index == Some(0)).unwrap(); + let marks: Vec<_> = series.children().unwrap().iter().take(2).map(|n| n.id).collect(); + assert_eq!(marks.len(), 2); + let direct = children(&s, graph); + refuse_row_parts_and_group_whole(&mut s, graph, &[marks, direct]); + assert!(s.doc().unwrap().doc.node(graph).unwrap().extras().graph.is_some()); +} + #[test] fn group_rows_refuses_explicitly_selected_live_blend_keys() { let mut s = session(); diff --git a/docs/mcp.md b/docs/mcp.md index 3543a70ff2..dfaf15c8b0 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -17582,7 +17582,7 @@ as `object.setBounds` measures them (Use Preview Bounds), except Arrange in Grid | `addon.arrange.replace` | `fit`: `keep`, `bounds` (the replaced object's width and height), `proportional` (fits inside them) (`keep`); `scaleStrokes`; `keep` (false) | Puts a copy of the topmost selected object centred on each of the other selected objects, in that object's place in the stacking order; the replaced objects are deleted unless `keep` (the copy then goes right above each). Needs two objects or more; the copies are selected. → `{ids}` | | `addon.arrange.size` | `width`, `height` pt, 0.001…1000000 (at least one); `proportional` (false: a side not given stays; true: it follows the given one in proportion); `reference` 0…8 (4, the centre); `scaleStrokes` | Gives every selected object that width and height, keeping its reference point in place (the 9-point grid row by row from the top-left). | | `addon.arrange.groupOverlapping` | none | Groups the selected objects (at most 20000) wherever their visual bounds overlap or touch, chains included: when A overlaps B and B overlaps C, all three go in one group. Only objects in the same layer or group are grouped together; objects that overlap nothing stay as they are. Each new group goes where its frontmost member was in the stacking order, and the groups and the objects left alone stay selected; with nothing overlapping it makes no step. Needs two objects or more. → `{groups: [id…], grouped}` (how many objects went into groups) | -| `addon.arrange.groupRows` | `direction`: `row` (vertical extents), `column` (horizontal extents) (`row` when omitted; `null` is invalid). Parameters must be an object. | Groups the selected objects (at most 20000) into one group for each row (column): an object joins a row when its visual bounds overlap the row's extent from top to bottom (left to right). Measurement includes strokes and vector effects, measures a clipping group by its mask, and excludes raster effects such as shadows, glows, and blurs. Extents that only touch stay apart, and an object that overlaps two rows joins them into one. Only objects in the same layer or group and on the same artboard (the one holding the object's center, else the one it overlaps most) share a row. Objects outside all artboards, locked or hidden objects, and clipping masks stay in place. The command rejects selections of editable objects whose parent is neither a layer nor a group, including live blend keys, and direct children of Shaper groups or live compound shapes. An object alone in its row stays as it is. Each new group goes where its frontmost member was in the stacking order, and the groups (top to bottom, or left to right) and the objects left alone stay selected; with no row of two objects or more it makes no step. Needs two objects or more. → `{groups: [id…], grouped}` (how many objects went into groups) | +| `addon.arrange.groupRows` | `direction`: `row` (vertical extents), `column` (horizontal extents) (`row` when omitted; `null` is invalid). Parameters must be an object. | Groups the selected objects (at most 20000) into one group for each row (column): an object joins a row when its visual bounds overlap the row's extent from top to bottom (left to right). Measurement includes strokes and vector effects, measures a clipping group by its mask, and excludes raster effects such as shadows, glows, and blurs. Extents that only touch stay apart, and an object that overlaps two rows joins them into one. Only objects in the same layer or group and on the same artboard (the one holding the object's center, else the one it overlaps most) share a row. Objects outside all artboards, locked or hidden objects, clipping masks, layers, and sublayers stay in place. Every grouping candidate must have a parent that is a layer or group. The command rejects selections containing editable live blend keys, direct children of Shaper groups, live compound shapes, Objects on Path groups, or Live Paint groups, and any descendants of live graphs. Complete live groups and graphs can be grouped with other artwork. An object alone in its row stays as it is. Each new group goes where its frontmost member was in the stacking order, and the groups (top to bottom, or left to right) and the objects left alone stay selected; with no row of two objects or more it makes no step. Needs two objects or more. → `{groups: [id…], grouped}` (how many objects went into groups) | | `addon.arrange.randomizeOpacity` | `min`, `max` % 0…100 (30, 100; `min` not more than `max`); `seed` whole number (0); `ids` (the selection) | Gives each selected object its own opacity (the object opacity the Transparency panel sets), picked at random and evenly from `min` to `max`; the same seed, objects and range give the same opacities. Groups take one opacity of their own; the objects inside keep theirs. A run that changes no opacity, such as `min` and `max` both at the opacity every object has, records no undo step. → `{count}` | | `addon.arrange.sort` | `by`, `apply`: `width`, `height`, `area` (width × height), `x` (left edge), `y` (top edge), `opacity` (%), `stacking` (stacking order from the back) (`area`, `stacking`); `order`: `ascending` (small to large), `descending` (large to small), `random` (`ascending`); `distribute` (false); `seed` whole number (0, for `random`); `scaleStrokes`; `ids` (the selection) | Sorts the selected objects (at most 20000) by `by` and gives them the values of `apply` in that order: the first gets the smallest value. Without `distribute` the objects trade the values they have; with it the values step evenly from the smallest to the largest. A width, height or area changes about each object's centre (an area in proportion), a place moves the top-left corner. Stacking order always trades places, and only among objects in the same layer or group. Needs two objects or more. → `{count}` |