Symbols: Symbol Options and Replace Symbol that change nothing add no undo step

symbol.options recorded an undo step and moved the revision whenever any
option was passed, unless only alignToPixelGrid was given with the value
the symbol already had. A newName equal to the current name, or a
registration, export type, 9-slice, type or pixel grid value the symbol
already has, now leaves the document and its undo history as they are.

symbol.replace with the symbol the instances already show recorded an
undo step too. It now records one only when an instance changes.

Both run through colorcmds::edit_if_changed, as the Symbol Shifter and
the other symbolism tools do.
This commit is contained in:
dotCooCoo committed 2026-11-29 02:43:57 -08:00
1 parent 5f92d8987f
commit 951e552a76
4 files changed
+69 -16

No files matched your search

+2 -2
View File
@@ -225,7 +225,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Symbol Options…",
[],
None,
"{name?, newName?, registration?: 0..8, exportType?: movieClip|graphic, nineSlice?: bool, nineSliceGuides?: array, type?: static|dynamic, alignToPixelGrid?: bool} rename a symbol (its instances and the graphics and Map Art maps on 3D objects follow the new name) and/or set Symbol Options › Registration, Export Type, 9-slice guides (nineSliceGuides [left, top, right, bottom] fractions in from each edge, each -16..16, with left + right < 1 and top + bottom < 1), Symbol Type (a dynamic symbol's instances may change the appearance of objects in its art and stay linked; making it static drops those changes) and Align to Pixel Grid (on: the horizontal and vertical straight segments of the paths in the symbol's art snap to whole points, or half points for an odd centered stroke, at the symbol's natural size, its stroke weights become whole points, and every instance placed at that size, reflected across one axis or not, moves to whole points and goes back to them after each move; a scaled, rotated (a half turn included) or sheared instance, and an instance in another symbol's art, is left where it is; the art snaps again after it is edited); alignToPixelGrid alone with the value the symbol already has changes nothing and adds no undo step; default name: current → {name, registration, exportType, nineSlice, type, alignToPixelGrid}",
"{name?, newName?, registration?: 0..8, exportType?: movieClip|graphic, nineSlice?: bool, nineSliceGuides?: array, type?: static|dynamic, alignToPixelGrid?: bool} rename a symbol (its instances and the graphics and Map Art maps on 3D objects follow the new name) and/or set Symbol Options › Registration, Export Type, 9-slice guides (nineSliceGuides [left, top, right, bottom] fractions in from each edge, each -16..16, with left + right < 1 and top + bottom < 1), Symbol Type (a dynamic symbol's instances may change the appearance of objects in its art and stay linked; making it static drops those changes) and Align to Pixel Grid (on: the horizontal and vertical straight segments of the paths in the symbol's art snap to whole points, or half points for an odd centered stroke, at the symbol's natural size, its stroke weights become whole points, and every instance placed at that size, reflected across one axis or not, moves to whole points and goes back to them after each move; a scaled, rotated (a half turn included) or sheared instance, and an instance in another symbol's art, is left where it is; the art snaps again after it is edited); values the symbol already has, newName included, change nothing and add no undo step; default name: current → {name, registration, exportType, nineSlice, type, alignToPixelGrid}",
has_doc,
symbol_options
),
@@ -289,7 +289,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Replace Symbol",
[],
None,
"{name, ids?|id?} swap the symbol of the selected instances (an instance given a different symbol drops its appearance changes; one given its own symbol keeps them) → {ids}",
"{name, ids?|id?} swap the symbol of the selected instances (an instance given a different symbol drops its appearance changes; one given its own symbol keeps them; when every instance already shows the symbol, nothing changes and no undo step is recorded) → {ids}",
has_selection,
symbol_replace
),
+8 -11
View File
@@ -347,13 +347,8 @@ pub(super) fn symbol_options(s: &mut Session, p: &Value) -> Result<Value> {
let dynamic = type_param(p, C)?;
let pixel = pixel_grid_param(p, C)?;
let new_name = str_param(p, "newName").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string);
if new_name.is_none() && export.is_none() && reg.is_none() && nine.is_none() && dynamic.is_none() {
match pixel {
None => return Err(bad(C, "pass newName, registration, exportType, nineSlice, type and/or alignToPixelGrid")),
// Align to Pixel Grid alone, already as asked: nothing changes and no undo step is added.
Some(on) if on == s.doc()?.doc.symbol_pixel_aligned(&name) => return Ok(options_json(&s.doc()?.doc, &name)),
Some(_) => {}
}
if new_name.is_none() && export.is_none() && reg.is_none() && nine.is_none() && dynamic.is_none() && pixel.is_none() {
return Err(bad(C, "pass newName, registration, exportType, nineSlice, type and/or alignToPixelGrid"));
}
if let Some(ref n) = new_name
&& n != &name
@@ -362,7 +357,8 @@ pub(super) fn symbol_options(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(C, format!("a symbol named `{n}` already exists")));
}
let final_name = new_name.clone().unwrap_or_else(|| name.clone());
s.edit("Symbol Options", |d, _| {
// Values the symbol already has leave the document as it is, and no undo step is added.
crate::cmd::colorcmds::edit_if_changed(s, "Symbol Options", |before, d| {
if let Some(ref n) = new_name
&& n != &name
{
@@ -439,7 +435,7 @@ pub(super) fn symbol_options(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(on) = pixel {
d.set_symbol_pixel_aligned(&final_name, on);
}
Ok(())
Ok(((), before != &*d))
})?;
Ok(options_json(&s.doc()?.doc, &final_name))
}
@@ -859,7 +855,8 @@ pub(super) fn symbol_replace(s: &mut Session, p: &Value) -> Result<Value> {
if ids.is_empty() {
return Err(EngineError::Other("no symbol instances selected".into()));
}
s.edit("Replace Symbol", |d, _| {
// Instances that already show the symbol leave the document as it is, and no undo step is added.
crate::cmd::colorcmds::edit_if_changed(s, "Replace Symbol", |before, d| {
let new = natural_size(d, &name);
for id in &ids {
let (old, other) = match d.node(*id).map(|n| &n.kind) {
@@ -877,7 +874,7 @@ pub(super) fn symbol_replace(s: &mut Session, p: &Value) -> Result<Value> {
}
}
fit_overrides(d, &ids);
Ok(())
Ok(((), ids.iter().any(|id| before.node(*id) != d.node(*id))))
})?;
Ok(json!({ "ids": ids_json(&ids) }))
}
+55
View File
@@ -3048,6 +3048,61 @@ fn symbol_export_type_defaults_to_movie_clip_and_round_trips() {
assert!(s.execute("symbol.options", &json!({"name": "Renamed"})).is_err());
}
/// `symbol.options` whose values are the ones the symbol already has changes neither the document
/// nor its revision, and records no undo step.
#[test]
fn symbol_options_that_change_nothing_record_no_undo_step() {
let mut s = session();
make_symbol(&mut s);
let state = |s: &Session| {
let st = s.doc().unwrap();
(st.doc.clone(), st.revision, st.history.undo.len())
};
let unchanged = [
json!({"name": "Box", "newName": "Box"}),
json!({"name": "Box", "newName": " Box "}),
json!({"name": "Box", "registration": 4}),
json!({"name": "Box", "exportType": "movieClip"}),
json!({"name": "Box", "nineSlice": false}),
json!({"name": "Box", "type": "static"}),
json!({"name": "Box", "alignToPixelGrid": false}),
json!({"name": "Box", "newName": "Box", "registration": 4, "exportType": "movieClip", "type": "static", "alignToPixelGrid": false}),
];
let before = state(&s);
for p in &unchanged {
assert_eq!(s.execute("symbol.options", p).unwrap()["name"], "Box");
assert_eq!(state(&s), before, "{p} changes nothing");
}
s.execute("symbol.options", &json!({"name": "Box", "registration": 0, "exportType": "graphic", "type": "dynamic"})).unwrap();
assert_eq!(undo_steps(&s), before.2 + 1, "changed options are one undo step");
let before = state(&s);
let r = s.execute("symbol.options", &json!({"name": "Box", "registration": 0, "exportType": "graphic", "type": "dynamic"})).unwrap();
assert_eq!((r["registration"].clone(), r["exportType"].clone(), r["type"].clone()), (json!(0), json!("graphic"), json!("dynamic")));
assert_eq!(state(&s), before, "setting the options again changes nothing");
s.execute("symbol.options", &json!({"name": "Box", "newName": "Logo"})).unwrap();
assert_eq!(undo_steps(&s), before.2 + 1, "a rename is one undo step");
}
/// `symbol.replace` with the symbol the instances already show changes neither the document nor
/// its revision, and records no undo step.
#[test]
fn symbol_replace_with_the_same_symbol_records_no_undo_step() {
let mut s = session();
let (inst, _, _) = make_symbol(&mut s);
rect(&mut s, 0.0, 0.0, 20.0, 20.0);
s.execute("symbol.new", &json!({"name": "Small"})).unwrap();
let state = |s: &Session| {
let st = s.doc().unwrap();
(st.doc.clone(), st.revision, st.history.undo.len())
};
let before = state(&s);
assert_eq!(s.execute("symbol.replace", &json!({"name": "Box", "ids": [inst.0]})).unwrap()["ids"], json!([inst.0]));
assert_eq!(state(&s), before, "replacing an instance with its own symbol changes nothing");
s.execute("symbol.replace", &json!({"name": "Small", "ids": [inst.0]})).unwrap();
assert!(matches!(&node(&s, inst).kind, NodeKind::SymbolInstance { symbol, .. } if symbol == "Small"));
assert_eq!(undo_steps(&s), before.2 + 1, "replacing it with another symbol is one undo step");
}
#[test]
fn sprayer_command_creates_a_symbol_set_and_alt_removes() {
let mut s = session();
+4 -3
View File
@@ -4968,8 +4968,8 @@ already walks Image Trace. Short agent entry points:
placed at that size, reflected across one axis or not, moves to whole points and goes back to them after every move;
VectorCraft leaves a scaled, rotated (a half turn included) or sheared instance, and an instance in another symbol's
art, where it is, snaps the art again after it is edited, and does not count
instances in `select.object.notAlignedToPixelGrid`; `alignToPixelGrid` alone with the value the symbol already has
adds no undo step),
instances in `select.object.notAlignedToPixelGrid`; values the symbol already has, `newName` included, do not change
the document or its revision and add no undo step),
`symbol.place {name?, x?, y?}` (on top of the current layer; in Draw Behind directly below the bottommost selected
object, beside the compound path that holds it, or at the bottom of the current layer with nothing selected, above
a clipping path; `symbol.placeFromDocument` and `symbol.addFromLibrary` with `place` stack the instance the same
@@ -4979,7 +4979,8 @@ already walks Image Trace. Short agent entry points:
undo step, and a symbol with the same name but other art or Symbol Options is added under a free name; an error
when that document is no longer open; plug-ins cannot call it), `symbol.breakLink {ids?}` (the art keeps the instance's opacity mask and its bounding box, square after
`object.resetBoundingBox`; a hidden or locked instance's
art stays hidden or locked and is not selected), `symbol.replace`,
art stays hidden or locked and is not selected), `symbol.replace` (instances that already show the symbol do not
change the document or its revision, and add no undo step),
`symbol.update {name, ids?|id?, keepArt?: false}` (Redefine Symbol: the selection, or `ids`, becomes the symbol's art and an instance
of it in one undo step; with `keepArt: true` the art stays ordinary art in its place, with its ids, and stays selected
unless it is hidden or locked, and the result's `id` is null; a `keepArt` other than true or false is an error; art that holds an instance of the symbol (opacity masks included), directly or through the art