DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Paragraph styles: Based On and Next Style must name styles; the dialog resolves over the chosen Based On
paaspaas0003754e381a07
diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rsindex 903cd7d0f..699b01ea8 100644--- a/crates/engine/src/cmd/style.rs+++ b/crates/engine/src/cmd/style.rs@@ -875,6 +875,13 @@ fn create_para(s: &mut Session, p: &Value) -> Result<Value> {{return Err(bad("style.paragraph.create", format!("no style `{b}`")));}+ // Next Style: a style of the document, or this one.+ if let Some(n) = &next+ && *n != name+ && d.styles.para(n).is_none()+ {+ return Err(bad("style.paragraph.create", format!("no style `{n}`")));+ }d.styles_mut().paragraph.push(ParagraphStyle { name: name.clone(), based_on, next_style: next, para, chars, shortcut: String::new() });Ok(json!({"name": name}))})@@ -938,11 +945,19 @@ fn edit_para(s: &mut Session, p: &Value) -> Result<Value> {let based = p.get("basedOn").cloned();let next = p.get("nextStyle").cloned();let reset = p.get("resetToBase").and_then(Value::as_bool).unwrap_or(false);+ for (key, v) in [("basedOn", &based), ("nextStyle", &next)] {+ if v.as_ref().is_some_and(|v| !v.is_string() && !v.is_null()) {+ return Err(bad("style.paragraph.edit", format!("`{key}` must be a style name or null")));+ }+ }s.edit(|d, _| {- if let Some(Value::String(b)) = &based- && d.styles.para_based_on_cycles(&name, b)- {- return Err(bad("style.paragraph.edit", "based-on would create a cycle"));+ if let Some(Value::String(b)) = &based {+ if d.styles.para(b).is_none() {+ return Err(bad("style.paragraph.edit", format!("no style `{b}`")));+ }+ if d.styles.para_based_on_cycles(&name, b) {+ return Err(bad("style.paragraph.edit", "based-on would create a cycle"));+ }}if let Some(Value::String(n)) = &next&& d.styles.para(n).is_none()diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 4bc7e360c..33ae449e0 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -1665,3 +1665,20 @@ fn reset_to_base_with_a_new_parent_resolves_rule_edits_over_it() {let r = s.doc().unwrap().doc.styles.resolve_para_style("Child").0.rule_below;assert_eq!((r.weight, r.offset), (2.0, 9.0), "the offset is the new parent's");}++/// Paragraph style references are checked: a Based On or Next Style that names no style, or+/// that isn't a name or null, is refused and changes nothing.+#[test]+fn paragraph_style_references_must_name_styles() {+ let mut s = session();+ s.execute("style.paragraph.create", &json!({"name": "Body"})).unwrap();+ assert!(s.execute("style.paragraph.create", &json!({"name": "Lost", "nextStyle": "Nowhere"})).is_err());+ assert!(s.doc().unwrap().doc.styles.para("Lost").is_none());+ s.execute("style.paragraph.create", &json!({"name": "Self", "nextStyle": "Self"})).unwrap();+ let before = s.doc().unwrap().doc.clone();+ for p in [json!({"name": "Body", "basedOn": "Nowhere"}), json!({"name": "Body", "basedOn": 3}), json!({"name": "Body", "nextStyle": true})] {+ assert!(s.execute("style.paragraph.edit", &p).is_err(), "{p}");+ }+ assert!(std::sync::Arc::ptr_eq(&before, &s.doc().unwrap().doc), "nothing changed");+ s.execute("style.paragraph.edit", &json!({"name": "Body", "basedOn": null, "nextStyle": "Self"})).unwrap();+}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex d5341722e..491b82866 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -3645,6 +3645,21 @@ fn grep_error(p: &str) -> Option<String> {/// The Justification spacing check of Paragraph Style Options, against what the section showed:/// the style's own values, or a new style's Based On.+/// What paragraph style `style` resolves to when based on `based` (the Based On chosen in its+/// dialog, which may not be its own yet): `based`'s chain with the style's own settings over it.+fn style_over(+ styles: &designcraft_doc::Styles,+ based: &str,+ style: &designcraft_doc::ParagraphStyle,+) -> (designcraft_doc::ParaProps, designcraft_doc::CharProps) {+ styles.resolve_para(&designcraft_doc::ParaFormat {+ style: based.into(),+ para: style.para.clone(),+ chars: style.chars.clone(),+ ..Default::default()+ })+}+fn check_paragraph_style_justification(app: &DesignApp, d: &Dialog) -> Result<(), String> {// After Reset To Base the style starts from its Based On (the one chosen here, if any).let base_of = |st: &designcraft_engine::DocState| {@@ -3655,14 +3670,18 @@ fn check_paragraph_style_justification(app: &DesignApp, d: &Dialog) -> Result<().session.active().map(|st| {- let shown = if d.b("new") {- d.s("basedOn")+ let pp = if d.b("new") {+ st.doc.styles.resolve_para_style(&d.s("basedOn")).0} else if d.b("resetToBase") {- base_of(st)+ st.doc.styles.resolve_para_style(&base_of(st)).0} else {- d.s("name")+ // The style's own settings over the Based On chosen in the dialog.+ match st.doc.styles.para(&d.s("name")) {+ Some(style) => style_over(&st.doc.styles, &base_of(st), style).0,+ None => st.doc.styles.resolve_para_style(&d.s("name")).0,+ }};- serde_json::to_value(st.doc.styles.resolve_para_style(&shown).0).unwrap_or_default()+ serde_json::to_value(pp).unwrap_or_default()}).unwrap_or_default();check_justification(d, "p.", &base)@@ -3847,7 +3866,8 @@ fn paragraph_style_options(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialolet names: Vec<String> = doc.styles.paragraph.iter().map(|p| p.name.clone()).filter(|n| new || *n != name).collect();// A new style shows what it would inherit from its Based On (as does one reset to it).let based_name = d.fields.get("basedOn").and_then(Value::as_str).map(str::to_string).or(style.based_on.clone()).unwrap_or_default();- let (pp, cp) = doc.styles.resolve_para_style(if new || reset { &based_name } else { &name });+ // An existing style shows its own settings over the Based On chosen here.+ let (pp, cp) = if new || reset { doc.styles.resolve_para_style(&based_name) } else { style_over(&doc.styles, &based_name, &style) };let units = doc.settings.horizontal_units;let pv = serde_json::to_value(&pp).unwrap_or_default();let cv = serde_json::to_value(&cp).unwrap_or_default();@@ -7634,6 +7654,21 @@ mod tests {assert_eq!(s.based_on.as_deref(), Some(designcraft_doc::BASIC_PARAGRAPH));}+ /// A Based On changed in the dialog (without Reset To Base) is what the fields are checked+ /// against: the style's own settings over the new parent.+ #[test]+ fn a_changed_based_on_is_what_the_fields_are_checked_against() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("style.paragraph.create", json!({"name": "Loose", "para": {"wordSpaceMin": 1.2, "wordSpaceDesired": 1.3}})).unwrap();+ app.run("style.paragraph.create", json!({"name": "Child", "basedOn": "Loose"})).unwrap();+ let mut d = Dialog::new("paragraphStyleOptions", json!({"name": "Child"}));+ d.fields.insert("p.wordSpaceDesired".into(), json!(1.0));+ assert!(check_paragraph_style_justification(&app, &d).is_err(), "below Loose's minimum");+ d.fields.insert("basedOn".into(), json!(designcraft_doc::BASIC_PARAGRAPH));+ assert_eq!(check_paragraph_style_justification(&app, &d), Ok(()), "[Basic Paragraph]'s minimum is lower");+ }+/// With Reset To Base, the justification fields are checked against the base style (the/// style's own minimum no longer applies), and Preview shows the reset on the page.#[test]