Merge pull request #8958 from dotCooCoo/fix/duplicate-item-aligned-to-identical

Appearance: aligned copies go to `to` when the stacks are the same
This commit is contained in:
BFlat authored and GitHub committed 2026-11-22 10:49:15 +07:00
commit c2e1e28f87
4 files changed
+55 -21

No files matched your search

+6 -18
View File
@@ -398,15 +398,6 @@ pub(crate) fn aligned(p: &Value, cmd: &str) -> Result<bool> {
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<SharedStack> {
if ids.len() < 2 {
return None;
}
let aps: Vec<&Appearance> = ids.iter().map(|id| d.node(*id).map(|n| &*n.appearance)).collect::<Option<_>>()?;
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);
@@ -1530,19 +1521,16 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result<Value> {
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
+23
View File
@@ -9611,6 +9611,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<vectorcraft_doc::Appearance> {
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.
+24
View File
@@ -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::<Vec<_>>(), ["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]
+2 -3
View File
@@ -1019,9 +1019,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