From 36005d9a02b1f92677427da1a3b4aa5b2a7157e9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 21:41:47 -0800 Subject: [PATCH] Appearance: aligned copies go to `to` when the stacks are the same `appearance.duplicateItem` with `aligned: true` and `to` was refused for every selection of two or more objects, so copying a row of a graph's Legend group to `to` failed even when every label had the same rows. It now refuses `to` with aligned rows only when the stacks differ, as the params text says, and the copies land at `to` in each object as one undo step. --- crates/engine/src/cmd/appearance.rs | 24 ++++++------------------ crates/engine/src/cmd/graph/tests.rs | 23 +++++++++++++++++++++++ crates/engine/src/tests/appearance.rs | 24 ++++++++++++++++++++++++ docs/mcp.md | 5 ++--- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index 3b23d62634..6fede6aceb 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -398,15 +398,6 @@ pub(crate) fn aligned(p: &Value, cmd: &str) -> Result { switch_or(p, "aligned", false, cmd) } -/// What the appearances of `ids` share, when there are several of them and they all exist. -pub(crate) fn shared_stack(d: &vectorcraft_doc::Document, ids: &[NodeId]) -> Option { - if ids.len() < 2 { - return None; - } - let aps: Vec<&Appearance> = ids.iter().map(|id| d.node(*id).map(|n| &*n.appearance)).collect::>()?; - Some(SharedStack::of(&aps)) -} - /// Do the appearances of `ids` differ (several objects whose stacks aren't all equal)? pub(crate) fn appearances_differ(d: &vectorcraft_doc::Document, ids: &[NodeId]) -> bool { let mut aps = ids.iter().filter_map(|id| d.node(*id)).map(|n| &n.appearance); @@ -1502,19 +1493,16 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { let mut first_at = None; let active = s.active_appearance_item.as_ref().and_then(|a| a.objects.first().copied()); let mut active_at = None; - // Aligned rows of a mixed selection are copied right above themselves in each object. Rows of a graph design's art, - // which is left out, go to `to` in an object whose stack is the same as the art's. + // Aligned rows of a mixed selection are copied right above themselves in each object. Aligned rows of objects whose + // stacks are the same, and rows of a graph design's art, which is left out, go to `to` as in one stack. let of = design_reference(s, p, C)?; - if aligned(p, C)? - && to.is_some() - && (shared_stack(&s.doc()?.doc, &ids).is_some() || (of.is_some() && appearances_differ(&s.doc()?.doc, &with_reference(of, &ids)))) - { + if aligned(p, C)? && to.is_some() && appearances_differ(&s.doc()?.doc, &with_reference(of, &ids)) { return Err(bad(C, "`to` takes the rows of one object's stack, not aligned rows")); } let before = s.doc()?.doc.clone(); - // With `to`, the art's stack is the same as the object's, so its rows are the object's, each of which must exist. - let per = - if to.is_some() { items_per_target(&before, &ids, &idx, aligned(p, C)?) } else { rows_per_target(&before, of, &ids, &idx, aligned(p, C)?) }; + // With `to`, aligned rows are rows of stacks that are all the same (the art's too), so each target takes the same + // indices, each of which must exist. + let per = if to.is_some() { vec![idx.clone(); ids.len()] } else { rows_per_target(&before, of, &ids, &idx, aligned(p, C)?) }; s.edit("Duplicate Item", |d, _| { let mut any = false; // Copies that became the topmost visible row of their kind: the series stores their paint even when the object diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 13394fc8d0..6e0d90bf65 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -9472,6 +9472,29 @@ fn appearance_commands_given_the_axes_or_legend_group_edit_its_lines_and_labels( } } +/// Aligned rows copied to `to` given the Legend group, whose labels have the same rows, go to `to` in every label. +#[test] +fn aligned_copies_given_the_legend_group_go_to_to_when_its_labels_have_the_same_rows() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let labels = |s: &Session| -> Vec { + let n = s.doc().unwrap().doc.node(id).unwrap().clone(); + group(&n, "Legend").children().unwrap().iter().map(|c| (*c.appearance).clone()).collect() + }; + let legend = group(s.doc().unwrap().doc.node(id).unwrap(), "Legend").id; + s.execute("appearance.addFill", &json!({"ids": [legend.0]})).unwrap(); + s.execute("appearance.addStroke", &json!({"ids": [legend.0]})).unwrap(); + let was = labels(&s); + assert!(was.len() >= 2 && was.iter().all(|a| *a == was[0]) && was[0].items.len() >= 2, "{was:?}"); + let top = was[0].items.len() - 1; + s.execute("appearance.duplicateItem", &json!({"ids": [legend.0], "index": top, "to": 0, "aligned": true})).unwrap(); + for a in labels(&s) { + assert_eq!(a.items.len(), was[0].items.len() + 1); + assert_eq!(a.items[0], was[0].items[top], "the copy is at the bottom"); + } +} + /// A row index given the Axes group, whose lines and labels have different rows, is refused unless it is `aligned`, a row /// of the backmost member's stack: then the members that share that row change, the labels keep theirs, and the graph /// keeps the change when it is drawn again. diff --git a/crates/engine/src/tests/appearance.rs b/crates/engine/src/tests/appearance.rs index a04bb89b4a..9a29f9e026 100644 --- a/crates/engine/src/tests/appearance.rs +++ b/crates/engine/src/tests/appearance.rs @@ -1129,6 +1129,30 @@ fn shared_effects_and_new_fills_reach_every_selected_object() { assert!(s.execute("appearance.moveItem", &json!({"from": 1, "to": 0, "aligned": true})).is_err()); } +/// Aligned rows copied to `to`: refused when the selected stacks differ, and placed at `to` in each object when the +/// stacks are the same, as an aligned move is. +#[test] +fn aligned_copies_go_to_to_when_the_stacks_are_the_same() { + let mut s = session(); + let (a, b) = mixed_circles(&mut s, true); + let was = (node(&s, a).appearance.clone(), node(&s, b).appearance.clone()); + assert!(s.execute("appearance.duplicateItem", &json!({"index": 1, "to": 0, "aligned": true})).is_err()); + assert_eq!((node(&s, a).appearance.clone(), node(&s, b).appearance.clone()), was, "nothing changed"); + + let mut s = session(); + let (a, b) = mixed_circles(&mut s, false); + run(&mut s, "paint.setFill", json!({"ids": [b.0], "color": "#ffff00"})); + assert_eq!(node(&s, a).appearance, node(&s, b).appearance); + let steps = s.doc().unwrap().history.undo.len(); + // The stroke (row 1) copied to the bottom of each stack: [Stroke, Fill, Stroke]. + run(&mut s, "appearance.duplicateItem", json!({"index": 1, "to": 0, "aligned": true})); + for id in [a, b] { + assert_eq!(node(&s, id).appearance.items.iter().map(AppearanceItem::kind_name).collect::>(), ["stroke", "fill", "stroke"]); + assert_eq!(stroke(&node(&s, id), 0).width, 11.0); + } + assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "one undo step"); +} + /// An effect applied to the first object's stroke row goes on the equal stroke of the other /// object, which sits lower in a longer stack there. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index e7abf476a9..ab34928297 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1018,9 +1018,8 @@ as it is. When no object has one, `appearance.setItem` and `appearance.removeIte do for a row no object has, and the other commands are an error. The paint, stroke and gradient commands are also an error when the art's row is not the kind of row they edit, and `paint.editGradient`, `paint.setGradientGeom` and the `paint.freeform.*` commands without `stroke` edit the kind of the art's row. `appearance.moveItem`, `effect.move` and -`appearance.duplicateItem` with `to` are an error when the art's stack differs from the other objects', and -`appearance.duplicateItem` with `to` is also an error when two or more other objects are given, as it is without the -art. To change the art, edit the design with `graph.design`. +`appearance.duplicateItem` with `to` are an error when the art's stack differs from the other objects'. To change the +art, edit the design with `graph.design`. The graph also stores a paint or stroke weight that one of these commands changes on the topmost visible fill or stroke of a path directly in a series: a weight set with `stroke.set`, `stroke.weightUp` or `stroke.weightDown`; a gradient