mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 05:35:52 +00:00
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.
This commit is contained in:
@@ -1379,7 +1379,7 @@ fn move_item(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
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<V
|
||||
}
|
||||
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))
|
||||
})?;
|
||||
let m = idx.len();
|
||||
|
||||
@@ -46,9 +46,9 @@ use generate::generate;
|
||||
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, label_rows, labels_in, paint_targets, series_group_marks,
|
||||
series_group_marks_where, series_marks_in, series_object_opacity_differs, shown_paint,
|
||||
capture_label_styles, capture_label_styles_given, 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, label_rows, labels_in, paint_targets,
|
||||
series_group_marks, series_group_marks_where, series_marks_in, series_object_opacity_differs, shown_paint,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -645,20 +645,31 @@ pub(crate) fn capture_label_anti_alias(doc: &mut Document, ids: &[NodeId], metho
|
||||
/// Keep a formatting change made to graph labels `ids` (Character or Appearance, on the whole graph or on labels
|
||||
/// picked with Group Selection) for every label of their kind: `change` is applied to the kind's kept style (or
|
||||
/// the default one), so the labels look the same when the graph is drawn again. Called inside the same edit, so
|
||||
/// they undo together. Returns whether a kept style changed.
|
||||
/// they undo together. A kind that keeps no style and that `change` leaves at the default style keeps none, unless
|
||||
/// `change` sets the legend fill. Returns whether a kept style changed.
|
||||
pub(crate) fn capture_label_styles(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);
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -1026,7 +1026,17 @@ fn set_style(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// 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<Value> {
|
||||
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))
|
||||
})?;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user