layout.section: keep the settings the command does not name

The command removed the section at a nonzero start with retain() and only then
looked it up to keep its properties, so the lookup always fell back to the
defaults: editing one field (a marker, a start number) silently reset the
style, prefix, include-prefix flag and start number. The existing section is
now read before the list is rebuilt.

Regression test: a Roman-numbered page-2 section with prefix A- and marker
First, then marker Second keeps startNumber, style, prefix and includePrefix.
This commit is contained in:
midasmatterhorn
2026-10-14 23:47:55 +03:00
parent d07cbbe94c
commit 7a1ba45d18
+26 -1
View File
@@ -476,11 +476,14 @@ pub fn specs() -> Vec<CommandSpec> {
let start = page - 1;
let p = p.clone();
s.edit(|d, _| {
// The settings the command does not name stay as the section was: read it
// before the list is rebuilt (#717).
let existing = d.sections.iter().find(|x| x.start == start).cloned();
d.sections.retain(|x| x.start != start || start == 0);
if p.get("remove").and_then(Value::as_bool).unwrap_or(false) && start != 0 {
return ok();
}
let mut sec = d.sections.iter().find(|x| x.start == start).cloned().unwrap_or(designcraft_doc::Section {
let mut sec = existing.unwrap_or(designcraft_doc::Section {
start,
start_number: None,
style: Default::default(),
@@ -1034,6 +1037,28 @@ mod page_numbering_view_tests {
assert_eq!(s.resolve_page("1"), Some(0));
assert_eq!(s.resolve_page("9"), None);
}
/// `layout.section` edits one named setting; the others stay as the section was (#717).
#[test]
fn updating_a_section_keeps_settings_the_command_does_not_name() {
let mut s = Session::new();
s.execute("file.new", &json!({"pages": 4})).unwrap();
s.execute(
"layout.section",
&json!({"page": 2, "startNumber": 1, "style": "lowerRoman", "prefix": "A-", "includePrefix": true, "marker": "First"}),
)
.unwrap();
assert_eq!(s.page_label(1), "A-i");
s.execute("layout.section", &json!({"page": 2, "marker": "Second"})).unwrap();
let d = s.doc().unwrap().doc.clone();
let sec = d.sections.iter().find(|x| x.start == 1).expect("the page-2 section is still the only one there");
assert_eq!(sec.marker, "Second");
assert_eq!(sec.start_number, Some(1), "startNumber is kept");
assert_eq!(sec.style, designcraft_doc::NumberStyle::LowerRoman, "style is kept");
assert_eq!(sec.prefix, "A-", "prefix is kept");
assert!(sec.include_prefix, "includePrefix is kept");
assert_eq!(s.page_label(1), "A-i");
}
}
#[cfg(test)]