diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index 1ce0228b9d..35cd93ce94 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -551,6 +551,20 @@ pub(crate) fn check_part_rows(s: &Session, p: &Value, aligned: bool, cmd: &str) Ok(()) } +/// Whether row `index` of `n` is the topmost visible row of its kind, the row a graph keeps for a label or line. +fn is_top_shown(n: &Node, index: usize) -> bool { + let items = &n.appearance.items; + items.get(index).is_some_and(|it| items.iter().rposition(|o| o.is_fill() == it.is_fill() && o.visible()) == Some(index)) +} + +/// `ids` without the objects whose topmost visible fill and stroke rows ([`super::graph::label_rows`]) an edit left as +/// they were (`before`: the document before it), unless `wrote` says the edit wrote one of those rows. An edit of a +/// hidden row of a graph label or line then leaves what the graph keeps for its kind as it is. +fn shown_rows_edited(before: &Document, d: &Document, ids: Vec, wrote: impl Fn(NodeId) -> bool) -> Vec { + let shown = |doc: &Document, id: NodeId| doc.node(id).map(super::graph::label_rows); + ids.into_iter().filter(|id| wrote(*id) || shown(before, *id) != shown(d, *id)).collect() +} + /// One undo step (`label`) running `f` on each of `ids` with the item a fill (`fill`) or stroke /// edit aimed at `item` changes there (`None`: the topmost one; text then edits its characters). /// What the edit changes on the topmost visible fill and stroke of graph series marks, and on the @@ -600,6 +614,8 @@ pub(crate) fn edit_items_then( let mut edited = Vec::new(); // The objects whose rows `f` edited: on type, no row means its characters. let mut rows = Vec::new(); + // The objects where `f` wrote the topmost visible row of its kind. + let mut wrote = Vec::new(); for (id, item) in ids.iter().zip(&per) { let Some(item) = item else { continue }; let Some(n) = d.node_mut(*id) else { continue }; @@ -609,11 +625,14 @@ pub(crate) fn edit_items_then( if index.is_some() { rows.push(*id); } + if index.is_none_or(|i| is_top_shown(n, i)) { + wrote.push(*id); + } } let more = after(&before, d, &edited)?; - let labels = super::graph::capture_label_rows(d, &rows); + let labels = super::graph::capture_label_rows(d, &shown_rows_edited(&before, d, rows, |id| wrote.contains(&id))); // A graph line keeps the rows the edit wrote, also when the line already showed them. - let lines = super::graph::capture_line_rows(d, &edited); + let lines = super::graph::capture_line_rows(d, &shown_rows_edited(&before, d, edited.clone(), |id| wrote.contains(&id))); Ok(count_changed(d, &before, &edited) + usize::from(more || labels || lines)) })?; Ok(()) @@ -1125,6 +1144,7 @@ fn set_item(s: &mut Session, p: &Value) -> Result { let mut rows = top_paint_changed(&before, d, &ids); rows.extend(painted_rows(d, written)); let painted = super::graph::capture_series_top_paints(d, &rows); + let reached = shown_rows_edited(&before, d, reached, |id| edited.iter().any(|(e, _)| *e == id)); let labels = super::graph::capture_label_rows(d, &reached); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; @@ -1178,7 +1198,7 @@ fn remove_item(s: &mut Session, p: &Value) -> Result { } let kept = super::graph::capture_series_opacity(d, &top_opacity_changed(&before, d, &ids)); let painted = super::graph::capture_series_top_paints(d, &top_paint_changed(&before, d, &ids)); - let labels = super::graph::capture_label_rows(d, &reached); + let labels = super::graph::capture_label_rows(d, &shown_rows_edited(&before, d, reached, |_| false)); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; // The rows removed from the active row's object, which need not come first in `ids`. @@ -1268,8 +1288,10 @@ fn duplicate_item(s: &mut Session, p: &Value) -> Result { } super::graph::capture_series_opacity(d, &top_opacity_changed(&before, d, &ids)); let mut rows = top_paint_changed(&before, d, &ids); + let top_copies: Vec = added.iter().map(|(id, _)| *id).collect(); rows.extend(painted_rows(d, added)); super::graph::capture_series_top_paints(d, &rows); + let reached = shown_rows_edited(&before, d, reached, |id| top_copies.contains(&id)); super::graph::capture_label_rows(d, &reached); Ok(()) })?; diff --git a/crates/engine/src/cmd/graph.rs b/crates/engine/src/cmd/graph.rs index 1654940e87..85ed559ae5 100644 --- a/crates/engine/src/cmd/graph.rs +++ b/crates/engine/src/cmd/graph.rs @@ -47,8 +47,8 @@ use series::default_series_color; pub(crate) use series::{ LabelTextChange, capture_label_anti_alias, capture_label_changes, capture_label_object_opacity, capture_label_paragraphs, capture_label_rows, capture_label_styles, capture_label_text, capture_line_rows, capture_series_object_opacity, capture_series_opacity, capture_series_top_paints, - graph_part_members, is_design_art, label_object_opacity_differs, labels_in, paint_targets, series_group_marks, series_group_marks_where, - series_marks_in, series_object_opacity_differs, shown_paint, + graph_part_members, is_design_art, label_object_opacity_differs, label_rows, labels_in, paint_targets, series_group_marks, + series_group_marks_where, series_marks_in, series_object_opacity_differs, shown_paint, }; #[cfg(test)] diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 5e4d99f7ad..99c05fb2a7 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -6319,6 +6319,36 @@ fn an_aligned_row_edit_given_the_axes_group_keeps_the_rows_of_labels_it_did_not_ }); } +/// An edit of a hidden row of an axis line leaves what its kind keeps from another line: the line's visible rows did not +/// change. +#[test] +fn an_edit_of_a_hidden_row_of_an_axis_line_keeps_what_its_kind_keeps() { + let cases = [ + ("appearance.setItem", json!({"color": "#0000ff"})), + ("appearance.duplicateItem", json!({})), + ("appearance.removeItem", json!({})), + ("transparency.set", json!({"opacity": 40})), + ("paint.setStroke", json!({"color": "#0000ff"})), + ]; + every_case(cases, |(cmd, params)| { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let id = graph(&mut s, "column"); + let lines = line_nodes(&s, id); + let line = lines[1].id; + s.execute("appearance.addStroke", &json!({"ids": [line.0]})).unwrap(); + let hidden = top_row(&s, line, false); + s.execute("appearance.setItem", &json!({"ids": [line.0], "index": hidden, "visible": false})).unwrap(); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "color": "#ff0000"})).unwrap(); + let kept = spec(&s, id).label_styles.axis_rows; + let mut p = params.clone(); + p["ids"] = json!([line.0]); + p[if cmd.starts_with("appearance.") { "index" } else { "item" }] = json!(hidden); + s.execute(cmd, &p).unwrap(); + assert_eq!(spec(&s, id).label_styles.axis_rows, kept, "{cmd}"); + }); +} + /// An Eyedropper pick of only the object's opacity on an axis line keeps the stroke its kind keeps from another line, /// though the line it is given still shows the default stroke. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index dbfe3f4a2a..56d8b484a6 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -871,7 +871,7 @@ The `appearance.*` commands, `graphicStyle.apply` and `graphicStyle.redefine`, a transparency commands given an `item` (or the Appearance panel's active row) store a graph label's object rows for its kind of label: the paint, weight, opacity and blend mode of its topmost visible fill and stroke row, their order and their place relative to the Characters row, and the label's object opacity and blend mode. They store them even when the -label already showed them. `transparency.set` without an `item`, `object.setProps` with an opacity or blend mode, and +label already showed them, but an edit of a hidden row that leaves the topmost visible rows as they were stores nothing. `transparency.set` without an `item`, `object.setProps` with an opacity or blend mode, and an `appearance.copyFrom` (Eyedropper) pick of transparency store only the label's object opacity and blend mode; `appearance.copyFrom` with `append` stores the rows. When the graph is drawn again, every label of that kind gets what is stored, with a placed gradient fitted to each label, and the graph saves it as `valuesRows`, `categoriesRows` and @@ -896,7 +896,8 @@ The other commands store them when given the line itself, and the `appearance.*` picks up a fill or stroke attribute or adds rows with `append`; a pick of only the object's transparency stores only the object opacity and blend mode. The recolor commands and `paint.swap` store a line's rows when they change them. The other commands store them even when the line already showed them, except that rows equal to a kind's default stroke -(0.5 pt black for axis lines and tick marks, 0.25 pt gray for rings) are not saved. When one command reaches several lines of one kind, the frontmost line in +(0.5 pt black for axis lines and tick marks, 0.25 pt gray for rings) are not saved, and an edit of a hidden row that +leaves the topmost visible rows as they were stores nothing. When one command reaches several lines of one kind, the frontmost line in paint order sets what the kind keeps, whatever order the `ids` are in. A gradient is stored without its placement, and each line fits it when the graph is drawn again. The graph saves the rows as `axisRows` and `ringRows` in its label styles. Other stroke options (caps, joins, miter limit, alignment, dashes and arrowheads), rows below the topmost one of