Recolor Artwork: Save Changes keeps colors added and removed on the Edit tab

Save Changes now gives the group the same colors OK would: a color removed
with the Remove Color tool removes its swatch, and a color added with the
Add Color tool joins the group after its other colors, also when the art
holds colors of the group. `swatch.editGroup` takes `null` in `colors` to
remove the swatch at that place.

After the save, the rows follow the saved group, so a later edit or OK does
not remove or add a color a second time.
This commit is contained in:
dotCooCoo
2026-11-16 00:21:37 -08:00
parent 982c99da88
commit 8bc2d29d45
4 changed files with 206 additions and 19 deletions
+14 -7
View File
@@ -129,7 +129,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Edit Color Group",
["Window", "Swatches"],
None,
"{group: colour group, colors: [colour] (hex, key, {c,m,y,k} or {l,a,b}; #rrggbbaa and #rgba are refused; the group's swatches take them in order, extra colours become new swatches, swatches past the end are removed; built-in swatches can't change; a color the swatch already has keeps its model), keepModel?: bool (as paint.setFill), rename?: new group name} rewrite a colour group as one undo step; art linked to its global swatches takes their new colours (and keeps its colour, unlinked, when its swatch goes) → {name, swatches: [names], relinked}",
"{group: colour group, colors: [colour] (hex, key, {c,m,y,k} or {l,a,b}; #rrggbbaa and #rgba are refused; the group's swatches take them in order, extra colours become new swatches, swatches past the end are removed; a null removes the swatch at that place; built-in swatches can't change; a color the swatch already has keeps its model), keepModel?: bool (as paint.setFill), rename?: new group name} rewrite a colour group as one undo step; art linked to its global swatches takes their new colours (and keeps its colour, unlinked, when its swatch goes) → {name, swatches: [names], relinked}",
has_doc,
swatch_edit_group
),
@@ -1317,29 +1317,36 @@ pub(super) fn rewrite_group(d: &mut Document, group: &str, colors: &[Option<Colo
fn swatch_edit_group(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "swatch.editGroup";
let group = str_param(p, "group").ok_or_else(|| bad(C, "missing `group`"))?;
let colors: Vec<Color> = p
// A null removes the swatch at that place.
let colors: Vec<Option<Color>> = p
.get("colors")
.and_then(Value::as_array)
.ok_or_else(|| bad(C, "missing `colors`"))?
.iter()
.map(|v| color_value(v).ok_or_else(|| bad(C, format!("bad colour {v}"))))
.map(|v| if v.is_null() { Ok(None) } else { color_value(v).map(Some).ok_or_else(|| bad(C, format!("bad colour {v}"))) })
.collect::<Result<_>>()?;
super::paint::check_keep_model(p, C)?;
// A color its swatch already has keeps its model. The others take the document's.
let old: Vec<Option<Color>> =
s.doc()?.doc.swatch_groups.iter().find(|g| g.name == group).map(|g| g.swatches.iter().map(|w| w.paint.color()).collect()).unwrap_or_default();
let colors: Vec<Color> =
colors.into_iter().enumerate().map(|(i, c)| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) }).collect();
let mut colors: Vec<Option<Color>> = colors
.into_iter()
.enumerate()
.map(|(i, c)| c.map(|c| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) }))
.collect();
// A null past the group's swatches removes nothing.
while colors.len() > old.len() && colors.last() == Some(&None) {
colors.pop();
}
let rename = str_param(p, "rename");
// The group's own colors, in order, under its own name change nothing: no undo step.
if let Some(g) = s.doc()?.doc.swatch_groups.iter().find(|g| g.name == group)
&& rename.map(str::trim).is_none_or(|n| n.is_empty() || n == group)
&& old.len() == colors.len()
&& old.iter().zip(&colors).all(|(o, c)| *o == Some(*c))
&& old.iter().zip(&colors).all(|(o, c)| c.is_some() && o == c)
{
return Ok(json!({"name": group, "swatches": g.swatches.iter().map(|w| w.name.clone()).collect::<Vec<_>>(), "relinked": 0}));
}
let colors: Vec<Option<Color>> = colors.into_iter().map(Some).collect();
let e = s.edit("Edit Color Group", |d, _| rewrite_group(d, group, &colors, rename, C))?;
e.defaults(s)?;
Ok(json!({"name": e.name, "swatches": e.swatches, "relinked": e.relinked}))
+14
View File
@@ -445,6 +445,20 @@ fn edit_color_group_with_its_own_colors_records_no_undo_step() {
assert_eq!(undo_len(&s), undo + 2, "a new name is a step");
}
#[test]
fn edit_color_group_with_a_null_removes_that_swatch() {
let mut s = session();
run(&mut s, "swatch.newGroup", json!({"name": "Mine", "colors": ["#ff0000", "#00ff00", "#0000ff"], "keepModel": true}));
let undo = undo_len(&s);
let r = run(&mut s, "swatch.editGroup", json!({"group": "Mine", "colors": ["#ff0000", null, "#0000ff"]}));
assert_eq!(r["swatches"], json!(["R=255 G=0 B=0", "R=0 G=0 B=255"]));
assert_eq!(names(&s, Some("Mine")), ["R=255 G=0 B=0", "R=0 G=0 B=255"]);
assert_eq!(undo_len(&s), undo + 1, "a removed swatch is a step");
run(&mut s, "swatch.editGroup", json!({"group": "Mine", "colors": ["#ff0000", "#0000ff", null]}));
assert_eq!(undo_len(&s), undo + 1, "a null past the swatches removes nothing and records no step");
assert!(s.execute("swatch.editGroup", &json!({"group": "Mine", "colors": ["#ff0000", "nope"]})).is_err());
}
#[test]
fn unused_leaves_out_linked_colours_gradients_and_patterns_in_use() {
let mut s = session();