Type: Discard Empty Type drops unfinished panel values for the text it removes

When the Type tool leaves an empty text object, Discard Empty Type
takes back the step that made it, which also frees the text's node ID.
It did not advance the document's rollback count, so a value typed in
a panel field for the text and not yet committed (Opacity, for example)
stayed in the field, and the next Enter applied it to a new object that
took the same ID. Discard Empty Type now advances the count.

A command that panics puts back every document it changed. That restore
now advances the rollback count of each document whose snapshot it puts
back, for the same reason.

Tests: an engine test for each restore, and a Transparency panel test
that types 20+ in Opacity for an empty text, discards it, selects a new
rectangle with the same ID and types 1 and Enter.
This commit is contained in:
dotCooCoo committed 2026-12-05 00:29:22 -08:00
1 parent 75ed49ed34
commit 9ac0fe0fae
5 files changed
+67 -2

No files matched your search

+1
View File
@@ -2249,6 +2249,7 @@ fn discard_empty(s: &mut Session, p: &Value) -> Result<Value> {
st.selection.prune(&st.doc);
super::globaledit::follow_history(st);
st.revision += 1;
st.rollbacks += 1;
return Ok(json!({"removed": true}));
}
}
+3 -2
View File
@@ -121,8 +121,9 @@ pub struct DocState {
/// Advances when Undo or Redo restores an older document snapshot. The same node ID can name different objects
/// across that boundary.
pub history_restores: u64,
/// Advances when a canceled undo group, undo scope or interaction, or Exit Symbol Editing Mode taking
/// back the entering step, puts back an older document snapshot. A canceled plug-in run advances it
/// Advances when a canceled undo group, undo scope or interaction, Exit Symbol Editing Mode taking
/// back the entering step, Discard Empty Type taking back the step that made the text, or a command
/// that panicked puts back an older document snapshot. A canceled plug-in run advances it
/// when its calls changed the document. A preview advances it when it discards an earlier preview
/// that allocated node IDs, or fails after discarding an earlier preview's document change.
/// A command run during a tool interaction advances it when it sets aside a preview that allocated
+3
View File
@@ -473,6 +473,9 @@ impl Session {
for (uid, doc, selection, interaction, history) in snapshot {
// A document the command closed stays closed.
let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) else { continue };
if !std::sync::Arc::ptr_eq(&st.doc, &doc) {
st.rollbacks += 1;
}
st.doc = doc;
st.selection = selection;
st.interaction = interaction;
+43
View File
@@ -877,6 +877,49 @@ fn leaving_symbol_editing_advances_rollbacks_only_when_taking_back_the_entering_
}
}
/// Discard Empty Type advances `rollbacks` when it takes back the step that made the empty text, which frees the
/// text's node ID; when it removes the text as a new step instead, it leaves the count as it is.
#[test]
fn discard_empty_type_advances_rollbacks_only_when_taking_back_the_step_that_made_the_text() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let made = s.execute("text.create", &json!({"x": 10, "y": 40, "text": ""})).unwrap();
let rollbacks = s.doc().unwrap().rollbacks;
s.execute("text.discardEmpty", &json!({"id": made["id"]})).unwrap();
assert!(s.doc().unwrap().rollbacks > rollbacks, "taking back the step that made the text advances the rollback count");
let kept = s.execute("text.create", &json!({"x": 10, "y": 80, "text": ""})).unwrap();
s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap();
let rollbacks = s.doc().unwrap().rollbacks;
s.execute("text.discardEmpty", &json!({"id": kept["id"]})).unwrap();
assert!(s.doc().unwrap().doc.node(NodeId(kept["id"].as_u64().unwrap())).is_none(), "the empty text is removed");
assert_eq!(s.doc().unwrap().rollbacks, rollbacks, "removing the text as a new step keeps the rollback count");
}
/// A command that panics after changing a document advances that document's `rollbacks` when the document is put
/// back; one that panics before changing it leaves the count as it is.
#[test]
fn a_panicking_command_advances_rollbacks_only_when_it_changed_the_document() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
let a = NodeId(s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap()["id"].as_u64().unwrap());
for changed in [false, true] {
let rollbacks = s.doc().unwrap().rollbacks;
let r = s.run_guarded("test.panic", |s| {
if changed {
let st = s.doc_mut()?;
std::sync::Arc::make_mut(&mut st.doc).remove(a)?;
}
s.run_nested(|_| panic!("boom"))
});
assert!(matches!(r, Err(EngineError::Internal { .. })), "the panic is returned as an internal error");
if changed {
assert!(s.doc().unwrap().rollbacks > rollbacks, "putting back the changed document advances the rollback count");
} else {
assert_eq!(s.doc().unwrap().rollbacks, rollbacks, "a panic before any change keeps the rollback count");
}
}
}
/// Canceling an interaction whose preview changed the document advances `rollbacks`; canceling one that changed
/// nothing leaves it as it is.
#[test]
+17
View File
@@ -762,6 +762,23 @@ mod tests {
assert_eq!(opacity_of(&panel, &b), 0.01, "the canceled group drops the unfinished value: 1%, not 20+1");
}
/// Discard Empty Type drops an unfinished Opacity expression when it takes back the step that made the empty
/// text, though the object made and selected after it has the same id and opacity.
#[test]
fn unfinished_opacity_is_dropped_by_discard_empty_type() {
let mut app = new_app();
let a = run_cmd(&mut app, "text.create", json!({"x": 10, "y": 40, "text": ""}))["id"].clone();
run_cmd(&mut app, "select.set", json!({"ids": [a]}));
let mut panel = Panel::new(app, show);
unfinished_opacity(&mut panel);
run_cmd(&mut panel.app(), "text.discardEmpty", json!({"id": a}));
let b = run_cmd(&mut panel.app(), "shape.rectangle", json!({"x": 50, "y": 0, "width": 30, "height": 30}))["id"].clone();
assert_eq!(b, a, "the new rectangle has the discarded text's id");
run_cmd(&mut panel.app(), "select.set", json!({"ids": [b]}));
finish_opacity(&mut panel);
assert_eq!(opacity_of(&panel, &b), 0.01, "Discard Empty Type drops the unfinished value: 1%, not 20+1");
}
/// The same objects selected in another order keep an unfinished Opacity expression.
#[test]
fn unfinished_opacity_stays_when_the_same_objects_are_selected_in_another_order() {