diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index a132dbe..412ae98 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -4948,6 +4948,10 @@ impl NoteCraftApp { /// Puts a snippet where the caret is, or at the end of the page. fn insert_in_page(&mut self, text: &str) { let Some(ed) = self.editor.as_mut() else { return }; + // An insert is an edit: it is recorded for undo before the page changes. + if ed.caret.is_some() { + Self::remember_step(ed); + } match ed.caret.as_mut() { Some(caret) => { // Where the caret is, not at the end of the block. diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index dd44b9c..5e0b6fd 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -2803,6 +2803,28 @@ fn a_column_border_is_dragged_and_the_page_keeps_the_width() { /// Ctrl+Z takes back what was just done to the page, and Ctrl+Y puts it back: typing, and /// a row taken out of a table. +/// Inserting the date is an edit like any other: Ctrl+Z takes it back (owner, 2026-11-12). +#[test] +fn undo_takes_back_an_inserted_date() { + let (_t, app, s) = sample_app("ui-undo-insert-date"); + let mut h = sized_harness(app, LIGHT, egui::vec2(1400.0, 900.0)); + notecraft_store::page::save_body(&s.md_page, "plain text").expect("fixture"); + h.state_mut().open_path(&s.md_page.to_string_lossy()); + settle(&mut h); + ask(&mut h, "ui.caret", json!({"block": 0})).expect("caret"); + settle(&mut h); + let before = h.state().body().unwrap_or_default().to_string(); + h.get_by_label("Insert").click(); + h.run(); + h.get_by_label("Date").click(); + h.run(); + settle(&mut h); + assert_ne!(h.state().body().unwrap_or_default().to_string(), before, "the date was inserted"); + ask(&mut h, "ui.key", json!({"key": "Z", "ctrl": true})).expect("undo"); + settle(&mut h); + assert_eq!(h.state().body().unwrap_or_default().to_string(), before, "undo takes the inserted date back"); +} + #[test] fn undo_takes_back_a_change_to_the_page_and_redo_puts_it_back() { let (_t, app, s) = sample_app("ui-undo");