DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
fix(editing): Story Editor stays with its document; Undo restores the selection from before the command (#729, #716)
aiongg2c7e3e53f08b
diff --git a/ROADMAP.md b/ROADMAP.mdindex 8a0f82071..8de8ef1e7 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -129,6 +129,7 @@ list) · [roadmap.md](docs/roadmap.md) (milestones, plan) · [parity-checklist.mNewest first.+- **2026-10-13** · The Story Editor stays with the document its story belongs to: it hides while another document is active and closes with its document, so its edits never reach another document's story with the same id (#729). Undo restores the selection from before the command and Redo the one after it (#716). No readiness percentage change.- **2026-10-12** · macOS Dock Quit uses the same unsaved-document prompts as window and menu Quit. Repeated requests keep the current answers; Cancel retains every tab and unsaved recovery checkpoint, and completed Save/Don't Save answers exit through the ordinary event loop. No readiness percentage change.- **2026-10-12** · Orderly native exit writes the latest recovery state after finalizing current quit approvals; incomplete or stale plans keep their documents, and Undo to the saved state clears stale recovery. Unavailable or failed recovery is reported. Cancelable macOS Dock Quit remains open (#544). No readiness percentage change.- **2026-10-12** · Recovered documents keep a checkpoint until saved, closed or reverted, including a second crash before autosave. Restarted document IDs and explicit recovery folders preserve other snapshots; failed republishing retains the original. No readiness percentage change.diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex caeeb80ba..c76aa5671 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -418,7 +418,7 @@ impl Session {if id != "data.preview" && (spec.undoable || spec.journal) {self.stop_data_preview();}- let before = self.active().map(|d| (d.uid, d.doc.clone()));+ let before = self.active().map(|d| (d.uid, d.doc.clone(), d.selection.clone()));let r = (spec.run)(self, params)?;// Undo, redo or deleting layers can take the active layer away: new objects would land on// a layer that isn't there (invisible, not exported). Fall back to the top layer.@@ -443,13 +443,13 @@ impl Session {}}// Record undo if the document changed (and we're not previewing an interaction).- if let (Some((uid, old)), Some(st)) = (before, self.active_mut())+ if let (Some((uid, old, selection)), Some(st)) = (before, self.active_mut())&& st.uid == uid&& !Arc::ptr_eq(&old, &st.doc)&& st.interaction.is_none()&& spec.undoable{- let entry = HistoryEntry { label: spec.label.to_string(), doc: old, selection: st.selection.clone() };+ let entry = HistoryEntry { label: spec.label.to_string(), doc: old, selection };push_undo(st, entry);}if spec.journal {diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 17d1ed098..165c48811 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -25,6 +25,20 @@ fn create_frame_type_and_undo() {assert!(s.doc().unwrap().doc.stories.is_empty());}+#[test]+fn undo_restores_the_selection_from_before_the_command() {+ let mut s = session();+ let a = s.execute("frame.create", &json!({"rect": [36, 36, 100, 100]})).unwrap()["id"].as_u64().unwrap();+ s.execute("selection.set", &json!({"ids": [a]})).unwrap();+ let b = s.execute("frame.create", &json!({"rect": [200, 36, 300, 100]})).unwrap()["id"].as_u64().unwrap();+ let sel = |s: &Session| s.doc().unwrap().selection.items.iter().map(|i| i.0).collect::<Vec<_>>();+ assert_eq!(sel(&s), vec![b]);+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(sel(&s), vec![a]);+ s.execute("edit.redo", &json!({})).unwrap();+ assert_eq!(sel(&s), vec![b]);+}+#[test]fn smart_quotes_and_formatting() {let mut s = session();diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex fc7a3e634..76d9c6628 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -497,7 +497,7 @@ pub struct DesignApp {pub perf: Perf,pub synthetic: Vec<egui::Event>,/// Story open in the Story Editor.- pub story_editor: Option<designcraft_doc::StoryId>,+ pub story_editor: Option<story_editor::Target>,control_rx: Option<Receiver<ControlRequest>>,pending_shots: Vec<(u64, Option<String>, Sender<ControlResponse>, f64)>,queued_shots: Vec<(u64, f64, u32)>,diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 872cb81d0..9470da6be 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -944,13 +944,18 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,}}"app.storyEditor" => {- let sid = p.get("story").and_then(Value::as_u64).map(designcraft_doc::StoryId).or_else(|| {- let st = app.session.active()?;- st.selection.text.map(|t| t.story).or_else(|| st.selection.items.iter().find_map(|i| st.doc.item(*i)?.text_frame().map(|t| t.story)))+ let target = app.session.active().and_then(|st| {+ let story = p.get("story").and_then(Value::as_u64).map(designcraft_doc::StoryId).or_else(|| {+ st.selection+ .text+ .map(|t| t.story)+ .or_else(|| st.selection.items.iter().find_map(|i| st.doc.item(*i)?.text_frame().map(|t| t.story)))+ })?;+ Some(crate::story_editor::Target { doc: st.uid, story })});- match sid {- Some(s) => {- app.story_editor = if app.story_editor == Some(s) { None } else { Some(s) };+ match target {+ Some(t) => {+ app.story_editor = if app.story_editor == Some(t) { None } else { Some(t) };Ok(Value::Null)}None => Err("select a text frame or place the cursor in text".into()),diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rsindex 05fc1150c..6f9afea2c 100644--- a/crates/ui-egui/src/panel_docking.rs+++ b/crates/ui-egui/src/panel_docking.rs@@ -311,7 +311,7 @@ pub(crate) fn show(app: &mut DesignApp, ui: &mut egui::Ui) -> bool {let mut blocked = Vec::new();if app.ui.dialog.is_some() || app.ui.about || app.ui.palette.is_some() || egui::Popup::is_any_open(ui.ctx()) {blocked.push(ui.ctx().content_rect());- } else if app.story_editor.is_some() {+ } else if crate::story_editor::visible(app) {// The editor is rendered after the dock; block its unmeasured first frame conservatively.let rect = ui.ctx().memory(|m| m.area_rect(egui::Id::new("story_editor"))).unwrap_or_else(|| ui.ctx().content_rect());blocked.push(rect);diff --git a/crates/ui-egui/src/story_editor.rs b/crates/ui-egui/src/story_editor.rsindex 5b093ab41..cf9fd0d2d 100644--- a/crates/ui-egui/src/story_editor.rs+++ b/crates/ui-egui/src/story_editor.rs@@ -5,15 +5,35 @@use egui::vec2;use serde_json::json;+use designcraft_doc::StoryId;+use crate::DesignApp;use crate::theme::Tokens;+/// The story open in the Story Editor. Story ids are counted per document, so the editor is bound+/// to the document the story belongs to.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub struct Target {+ /// [`designcraft_engine::DocState::uid`] of the story's document.+ pub doc: u64,+ pub story: StoryId,+}++/// The editor is open and its document is the active one.+pub fn visible(app: &DesignApp) -> bool {+ app.story_editor.is_some_and(|t| app.session.active().is_some_and(|st| st.uid == t.doc))+}++/// Draws the editor while its document is active; it hides while another document is active and+/// closes with its document.pub fn show(app: &mut DesignApp, ctx: &egui::Context) {- let Some(sid) = app.story_editor else { return };- let Some(st) = app.session.active() else {+ let Some(target) = app.story_editor else { return };+ if !app.session.documents().iter().any(|d| d.uid == target.doc) {app.story_editor = None;return;- };+ }+ let Some(st) = app.session.active().filter(|st| st.uid == target.doc) else { return };+ let sid = target.story;let Some(story) = st.doc.story(sid).cloned() else {app.story_editor = None;return;@@ -89,3 +109,51 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {app.story_editor = None;}}++#[cfg(test)]+mod tests {+ use egui_kittest::kittest::Queryable;+ use serde_json::json;++ use crate::DesignApp;++ fn story_text(app: &DesignApp, doc: usize) -> String {+ let st = &app.session.documents()[doc];+ st.doc.stories.values().next().unwrap().text.clone()+ }++ /// Story ids are counted per document: typing in the editor after switching documents edited+ /// the other document's story with the same id.+ #[test]+ fn editor_stays_with_its_document() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), Default::default());+ let mut stories = Vec::new();+ for text in ["Alpha story", "Bravo story"] {+ app.run("file.new", json!({})).unwrap();+ let r = app.run("frame.create", json!({"rect": [72, 72, 300, 200], "content": "text", "text": text, "caret": false})).unwrap();+ stories.push(r["story"].as_u64().unwrap());+ }+ assert_eq!(stories[0], stories[1], "the fixture needs the same story id in both documents");+ app.run("file.activate", json!({"index": 0})).unwrap();+ app.run("app.storyEditor", json!({"story": stories[0]})).unwrap();+ let mut h = crate::test_window::open(app, egui::vec2(1440.0, 900.0));+ h.get_by_value("Alpha story").focus();+ h.run_steps(2);+ h.event(egui::Event::Text("!".into()));+ h.run_steps(2);+ let a = story_text(&h.state().app, 0);+ assert!(a.contains('!'), "typing reaches the open story: {a:?}");++ h.state_mut().app.run("file.activate", json!({"index": 1})).unwrap();+ h.run_steps(2);+ h.event(egui::Event::Text("?".into()));+ h.run_steps(2);+ assert_eq!(story_text(&h.state().app, 1), "Bravo story");+ assert_eq!(story_text(&h.state().app, 0), a);+ assert!(h.state().app.story_editor.is_some(), "the editor waits for its document");++ h.state_mut().app.run("file.close", json!({"index": 0})).unwrap();+ h.run_steps(2);+ assert_eq!(h.state().app.story_editor, None);+ }+}