DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Paragraph Styles: wire save and delete into the UI (#169)
zydvc26s67-webfde78978a492
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 8f2f39b0f..9eeb2ae03 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -537,3 +537,57 @@ fn commands_take_non_object_params_without_panicking() {assert!(r.is_ok(), "{p}: {r:?}");}}++#[test]+fn paragraph_style_edit_persists_attrs_and_rename() {+ let mut s = session();+ let name =+ s.execute("style.paragraph.create", &json!({"name": "Body Copy", "para": {"align": "left"}})).unwrap()["name"].as_str().unwrap().to_string();+ s.execute("style.paragraph.edit", &json!({"name": name, "para": {"align": "center"}})).unwrap();+ {+ let st = s.doc().unwrap();+ let style = st.doc.styles.para(&name).expect("style still exists");+ assert_eq!(style.para.align, Some(designcraft_doc::Align::Center), "edited attribute persisted");+ }+ // Renaming updates the style definition itself, and every place it's referenced.+ s.execute("style.paragraph.edit", &json!({"name": name, "rename": "Body Text"})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.para("Body Text").is_some(), "renamed style exists under the new name");+ assert!(st.doc.styles.para(&name).is_none(), "old name is gone");+}++#[test]+fn paragraph_style_edit_rejects_based_on_cycle() {+ let mut s = session();+ let a = s.execute("style.paragraph.create", &json!({"name": "A"})).unwrap()["name"].as_str().unwrap().to_string();+ let b = s.execute("style.paragraph.create", &json!({"name": "B", "basedOn": a})).unwrap()["name"].as_str().unwrap().to_string();+ let r = s.execute("style.paragraph.edit", &json!({"name": a, "basedOn": b}));+ assert!(r.is_err(), "A based on B based on A must be rejected");+}++#[test]+fn paragraph_style_delete_reassigns_affected_text() {+ let mut s = session();+ let name = s.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();+ let sid = s.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap()["story"].as_u64().unwrap();+ s.execute("text.insert", &json!({"text": "Hello, world"})).unwrap();+ s.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();+ s.execute("style.paragraph.apply", &json!({"name": name})).unwrap();+ {+ let st = s.doc().unwrap();+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.paras[0].style, name, "style applied before delete");+ }+ s.execute("style.paragraph.delete", &json!({"name": name, "replaceWith": designcraft_doc::BASIC_PARAGRAPH})).unwrap();+ let st = s.doc().unwrap();+ assert!(st.doc.styles.para(&name).is_none(), "deleted style is gone");+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).expect("story exists");+ assert_eq!(story.paras[0].style, designcraft_doc::BASIC_PARAGRAPH, "affected paragraph reassigned to the replacement style");+}++#[test]+fn paragraph_style_delete_rejects_builtin_styles() {+ let mut s = session();+ let r = s.execute("style.paragraph.delete", &json!({"name": designcraft_doc::BASIC_PARAGRAPH}));+ assert!(r.is_err(), "built-in styles can't be deleted");+}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 34832db14..b94c64445 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -813,6 +813,7 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {"documentSetup" => crate::i18n::tr(&app.ui.language, "Document Setup"),"findChange" => crate::i18n::tr(&app.ui.language, "Find/Change"),"paragraphStyleOptions" => crate::i18n::tr(&app.ui.language, "Paragraph Style Options"),+ "deleteParagraphStyle" => crate::i18n::tr(&app.ui.language, "Delete Paragraph Style"),"footnoteOptions" => crate::i18n::tr(&app.ui.language, "Footnote Options"),"insertXref" => crate::i18n::tr(&app.ui.language, "New Cross-Reference"),"findFont" => crate::i18n::tr(&app.ui.language, "Find/Replace Font"),@@ -1053,6 +1054,7 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {}}"paragraphStyleOptions" => paragraph_style_options(app, ui, &mut d),+ "deleteParagraphStyle" => delete_paragraph_style(app, ui, &mut d),"footnoteOptions" => footnote_options(app, ui, &mut d),"insertXref" => insert_xref(app, ui, &mut d),"findFont" => find_font(app, ui, &mut d),@@ -1482,6 +1484,14 @@ pub fn confirm(app: &mut DesignApp) -> Result<Value, String> {}Ok(r)}+ "deleteParagraphStyle" => {+ let name = d.s("name");+ let replace = d.s("replaceWith");+ app.run(+ "style.paragraph.delete",+ json!({"name": name, "replaceWith": if replace.is_empty() { json!(designcraft_doc::BASIC_PARAGRAPH) } else { json!(replace) }}),+ )+ }"colorPicker" => {let hex = d.s("hex");app.run("object.color", json!({"color": hex, "target": d.s("target")}))@@ -2170,6 +2180,51 @@ fn paragraph_style_options(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialo});}+fn delete_paragraph_style(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {+ let name = d.s("name");+ ui.set_min_width(380.0);+ crate::rtl::label(+ ui,+ egui::RichText::new(+ crate::i18n::tr(&app.ui.language, "Delete the paragraph style \"{name}\"?")+ .replace("{name}", crate::i18n::style_name(&app.ui.language, &name)),+ )+ .size(12.5),+ );+ ui.add_space(8.0);+ let Some(st) = app.session.active() else { return };+ let in_use = st.doc.stories.values().any(|s| s.paras.iter().any(|f| f.style == name));+ let names: Vec<String> = st.doc.styles.paragraph.iter().map(|p| p.name.clone()).filter(|n| *n != name).collect();+ if in_use {+ crate::rtl::label(+ ui,+ egui::RichText::new(crate::i18n::tr(&app.ui.language, "This style is in use. Text using it will be reassigned to:"))+ .size(11.0)+ .color(crate::theme::Tokens::get(ui.ctx()).text_dim),+ );+ } else {+ crate::rtl::label(+ ui,+ egui::RichText::new(crate::i18n::tr(&app.ui.language, "Replace with:")).size(11.0).color(crate::theme::Tokens::get(ui.ctx()).text_dim),+ );+ }+ ui.add_space(4.0);+ if !d.fields.contains_key("replaceWith") {+ d.fields.insert("replaceWith".into(), json!(designcraft_doc::BASIC_PARAGRAPH));+ }+ let replace = d.s("replaceWith");+ egui::ComboBox::from_id_salt("del_para_replace")+ .selected_text(crate::rtl::widget(ui, crate::i18n::style_name(&app.ui.language, &replace)))+ .width(260.0)+ .show_ui(ui, |ui| {+ for n in &names {+ if ui.selectable_label(*n == replace, crate::rtl::widget(ui, crate::i18n::style_name(&app.ui.language, n))).clicked() {+ d.fields.insert("replaceWith".into(), json!(n));+ }+ }+ });+}+fn combo(ui: &mut egui::Ui, d: &mut Dialog, key: &str, opts: &[(&str, &str)]) {let cur = d.s(key);let shown = opts.iter().find(|o| o.0 == cur).map_or(cur.as_str(), |o| o.1).to_string();@@ -2681,4 +2736,47 @@ mod tests {let _ = command_fields(c.params);}}++ fn test_app() -> crate::DesignApp {+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", &json!({"pages": 1})).unwrap();+ app+ }++ #[test]+ fn paragraph_style_options_dialog_saves_edits() {+ let mut app = test_app();+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Body Copy"})).unwrap()["name"].as_str().unwrap().to_string();+ app.ui.dialog = Some(Dialog::new("paragraphStyleOptions", json!({"name": name, "p.align": "center"})));+ confirm(&mut app).expect("OK saves the style edit");+ let st = app.session.active().unwrap();+ assert_eq!(st.doc.styles.para(&name).unwrap().para.align, Some(designcraft_doc::Align::Center));+ }++ #[test]+ fn delete_paragraph_style_dialog_removes_the_style_and_reassigns_text() {+ let mut app = test_app();+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();+ let sid = app.session.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap()["story"].as_u64().unwrap();+ app.session.execute("text.insert", &json!({"text": "Hello"})).unwrap();+ app.session.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();+ app.session.execute("style.paragraph.apply", &json!({"name": name})).unwrap();+ app.ui.dialog = Some(Dialog::new("deleteParagraphStyle", json!({"name": name, "replaceWith": designcraft_doc::BASIC_PARAGRAPH})));+ confirm(&mut app).expect("OK deletes the style");+ let st = app.session.active().unwrap();+ assert!(st.doc.styles.para(&name).is_none(), "style removed");+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).unwrap();+ assert_eq!(story.paras[0].style, designcraft_doc::BASIC_PARAGRAPH, "text reassigned to the replacement style");+ }++ #[test]+ fn delete_paragraph_style_dialog_defaults_replacement_to_basic_paragraph() {+ let mut app = test_app();+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();+ // No `replaceWith` set — the dialog's default field.+ app.ui.dialog = Some(Dialog::new("deleteParagraphStyle", json!({"name": name})));+ confirm(&mut app).expect("OK deletes the style with the default replacement");+ let st = app.session.active().unwrap();+ assert!(st.doc.styles.para(&name).is_none());+ }}diff --git a/crates/ui-egui/src/panels/styles.rs b/crates/ui-egui/src/panels/styles.rsindex c3412095e..2f41f7422 100644--- a/crates/ui-egui/src/panels/styles.rs+++ b/crates/ui-egui/src/panels/styles.rs@@ -152,5 +152,16 @@ fn style_row(app: &mut DesignApp, ui: &mut egui::Ui, para: bool, n: &str, currenui.data_mut(|d| d.insert_temp(id, name));});}+ if para && !n.starts_with('[') {+ ui.separator();+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Edit Paragraph Style…"))).clicked() {+ app.ui.dialog = Some(crate::dialogs::Dialog::new("paragraphStyleOptions", json!({"name": n})));+ ui.close();+ }+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Paragraph Style…"))).clicked() {+ app.ui.dialog = Some(crate::dialogs::Dialog::new("deleteParagraphStyle", json!({"name": n})));+ ui.close();+ }+ }});}