From 9d1091331c404a7ce05394335ecaab40a210fd48 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 15:07:45 -0800 Subject: [PATCH] Graphs: a reorder that changes no row keeps the line rows its kind keeps A no-op appearance.moveItem on a graph axis line or label replaced the rows its kind keeps with the line's own. The reorder now keeps rows only for the lines and labels whose rows it changed. text.setStyle keeps the default label style for a kind only when the command gives a character attribute; justify-only and empty featureChanges requests record nothing. --- crates/engine/src/cmd/appearance.rs | 4 +- crates/engine/src/cmd/graph.rs | 6 +-- crates/engine/src/cmd/graph/series.rs | 41 +++++++++++++++------ crates/engine/src/cmd/graph/tests.rs | 18 +++++++++ crates/engine/src/cmd/typecmd.rs | 20 +++++++++- crates/engine/src/cmd/typecmd/noop_tests.rs | 28 ++++++++++++++ 6 files changed, 98 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index e5bc8fa97f..796b1ae405 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -1379,7 +1379,7 @@ fn move_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, &ids); + let labels = super::graph::capture_label_changes(&before, d, &ids, true); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; // The moved row stays the active one; the rows it passed shift by one. @@ -1425,7 +1425,7 @@ fn move_items(s: &mut Session, p: &Value, ids: &[NodeId], to: usize) -> Result bool { let mut changed = false; for (graph, role) in label_kinds(doc, ids) { - changed |= store_label_style(doc, graph, role, change); + changed |= store_label_style(doc, graph, role, change, true); + } + changed +} + +/// [`capture_label_styles`], except that a kind that keeps no style keeps the default style when `change` leaves it +/// unchanged, for a command that gives character attributes (a size equal to the labels' own, for example). +pub(crate) fn capture_label_styles_given(doc: &mut Document, ids: &[NodeId], change: &dyn Fn(&mut CharStyle)) -> bool { + let mut changed = false; + for (graph, role) in label_kinds(doc, ids) { + changed |= store_label_style(doc, graph, role, change, false); } changed } /// Apply `change` to the style graph `graph` keeps for its labels of kind `role` (or the default one). A `change` that /// sets the fill of legend labels marks it as set on them, so a black one stays black on dark pie wedges. Returns whether -/// the kept style or that mark changed. A kind that keeps no style and that `change` leaves at the default style keeps -/// none, unless `change` sets the legend fill. -fn store_label_style(doc: &mut Document, graph: NodeId, role: Role, change: &dyn Fn(&mut CharStyle)) -> bool { +/// the kept style or that mark changed. With `unless_default`, a kind that keeps no style and that `change` leaves at +/// the default style keeps none, unless `change` sets the legend fill. +fn store_label_style(doc: &mut Document, graph: NodeId, role: Role, change: &dyn Fn(&mut CharStyle), unless_default: bool) -> bool { let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { return false }; let was = label_style(&spec.label_styles, role); let mut style = was.clone(); @@ -677,7 +688,7 @@ fn store_label_style(doc: &mut Document, graph: NodeId, role: Role, change: &dyn Role::Category => &mut styles.categories, Role::Legend => &mut styles.legend, }; - if slot.is_none() && style == was && !(sets_fill && !styles.legend_fill_set) { + if unless_default && slot.is_none() && style == was && !(sets_fill && !styles.legend_fill_set) { return false; } let mut changed = slot.as_ref() != Some(&style); @@ -1099,13 +1110,19 @@ pub(crate) fn capture_label_changes(before: &Document, doc: &mut Document, ids: } let mut changed = capture_label_rows(doc, &changed_rows) | lines; for ((graph, role), (style, entries)) in &characters { - changed |= store_label_style(doc, *graph, *role, &|st| { - for i in entries { - if let Some(copy) = RUN_PAINT.get(*i) { - copy(style, st); + changed |= store_label_style( + doc, + *graph, + *role, + &|st| { + for i in entries { + if let Some(copy) = RUN_PAINT.get(*i) { + copy(style, st); + } } - } - }); + }, + true, + ); } changed } diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index c800fb8269..51f78f70fb 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -6537,6 +6537,24 @@ fn an_object_opacity_on_an_axis_line_keeps_the_stroke_its_kind_keeps() { } } +/// A reorder that leaves an axis line's rows as they were keeps what its kind keeps from another line and is no edit. +#[test] +fn a_reorder_that_changes_nothing_on_an_axis_line_keeps_the_stroke_its_kind_keeps() { + 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); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "color": "#ff0000"})).unwrap(); + assert_eq!(line_look(&line_nodes(&s, id)[1]).0, "#000000", "the other lines show black until the graph is drawn again"); + let stroke = top_row(&s, lines[1].id, false); + let undo = s.doc().unwrap().history.undo.len(); + s.execute("appearance.moveItem", &json!({"ids": [lines[1].id.0], "from": stroke, "to": stroke})).unwrap(); + s.execute("appearance.moveItem", &json!({"ids": [lines[1].id.0], "indices": [stroke], "to": stroke})).unwrap(); + assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing changed"); + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#ff0000"), "{looks:?}"); +} + /// An opacity and a stroke weight given to a radar graph's ring are kept for its rings, and its spokes keep their own. #[test] fn transparency_and_stroke_weight_on_a_radar_ring_are_kept_for_its_rings() { diff --git a/crates/engine/src/cmd/typecmd.rs b/crates/engine/src/cmd/typecmd.rs index 05b45cc051..d6c45bb2f0 100644 --- a/crates/engine/src/cmd/typecmd.rs +++ b/crates/engine/src/cmd/typecmd.rs @@ -1026,7 +1026,17 @@ fn set_style(s: &mut Session, p: &Value) -> Result { // not in a scaled label's own units). let mut labels_changed = false; if range.is_none() { - labels_changed = super::graph::capture_label_styles(d, &ids, &|st| { + let gives_character = font.is_some() + || style.is_some() + || size.is_some() + || leading.is_some() + || tracking.is_some() + || fill.is_some() + || stroke.is_some() + || !stroke_weight.is_empty() + || features.is_some() + || feature_changes.as_ref().is_some_and(|c| !c.is_empty()); + let change = |st: &mut vectorcraft_doc::CharStyle| { if let Some(f) = &font { st.font_family = f.clone(); st.font_version = None; @@ -1067,7 +1077,13 @@ fn set_style(s: &mut Session, p: &Value) -> Result { if let Some(c) = &feature_changes { st.features = super::textedit::change_features(&st.features, c); } - }); + }; + // A command that gives character attributes keeps the style even when they equal the labels' own. + labels_changed = if gives_character { + super::graph::capture_label_styles_given(d, &ids, &change) + } else { + super::graph::capture_label_styles(d, &ids, &change) + }; } Ok(((), labels_changed)) })?; diff --git a/crates/engine/src/cmd/typecmd/noop_tests.rs b/crates/engine/src/cmd/typecmd/noop_tests.rs index 02481d8bee..b4f5cb2099 100644 --- a/crates/engine/src/cmd/typecmd/noop_tests.rs +++ b/crates/engine/src/cmd/typecmd/noop_tests.rs @@ -102,6 +102,34 @@ fn a_kept_graph_label_style_change_alone_records_one_undo_step() { records_nothing(&mut s, "text.setStyle", p); } +/// `text.setStyle` on a graph label with only `justify`, which the label already has, records no undo step and keeps no +/// character style for its kind of label. +#[test] +fn a_justify_only_style_change_on_a_graph_label_records_nothing() { + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap(); + let csv = ",2024,2025\nQ1,3,2\nQ2,5,4"; + let graph = NodeId( + s.execute("graph.create", &json!({"type": "column", "x": 100, "y": 100, "width": 300, "height": 200, "csv": csv})).unwrap()["id"] + .as_u64() + .unwrap(), + ); + let doc = s.doc().unwrap().doc.clone(); + let legend = doc.node(graph).unwrap().children().unwrap().iter().find(|c| c.name.as_deref() == Some("Legend")).unwrap(); + let label = legend + .children() + .unwrap() + .iter() + .find_map(|c| match &c.kind { + NodeKind::Text(t) if t.plain_text() == "2025" => Some(c.id), + _ => None, + }) + .unwrap(); + records_nothing(&mut s, "text.setStyle", json!({"ids": [label.0], "justify": "left"})); + let kept = s.doc().unwrap().doc.node(graph).unwrap().extras().graph.as_deref().unwrap().label_styles.legend.clone(); + assert!(kept.is_none(), "the legend keeps a style"); +} + /// `text.setText` with the text the type already has records no undo step; new text records one. #[test] fn the_same_text_records_no_undo_step() {