DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Insert Pages: an after past the last page appends again, as main pins; before stays strict
moppeeer89f44620d583
diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rsindex b88419538..0bbc5eab7 100644--- a/crates/engine/src/cmd/layout.rs+++ b/crates/engine/src/cmd/layout.rs@@ -161,7 +161,7 @@ pub fn specs() -> Vec<CommandSpec> {"Insert Pages…",["Layout", "Pages"],Some("Cmd+Shift+P"),- "{count?: 1 (1 to 9999), after?: 0-based page index (default: the last page), before?: 0-based page index (instead of `after`; 0: at the start), parent?: id|\"A-Parent\"|\"A\"|null}",+ "{count?: 1 (1 to 9999), after?: 0-based page index (default: the last page; past it: at the end), before?: 0-based page index (instead of `after`; 0: at the start), parent?: id|\"A-Parent\"|\"A\"|null}",has_doc,|s, p| {const ID: &str = "layout.pages.insert";@@ -181,15 +181,16 @@ pub fn specs() -> Vec<CommandSpec> {if p.get("after").is_some() && p.get("before").is_some() {return Err(bad("layout.pages.insert", "give `after` or `before`, not both"));}- // A page of the document: one past the last isn't, and would insert at the end.+ // `before` names a page of the document; an `after` past the last page appends+ // (`insert_pages_bounds.rs` pins that).let index = |k: &str| match p.get(k) {None => Ok(None),Some(v) => v.as_u64()- .and_then(|v| usize::try_from(v).ok())- .filter(|&i| i < n)- .map(Some)- .ok_or_else(|| bad("layout.pages.insert", format!("`{k}` must be the index of one of the {n} pages, not {v}"))),+ .map(|v| usize::try_from(v).unwrap_or(usize::MAX))+ .filter(|&i| k == "after" || i < n)+ .map(|i| Some(i.min(n.saturating_sub(1))))+ .ok_or_else(|| bad(ID, format!("`{k}` must be the index of one of the {n} pages, not {v}"))),};// The page the new ones follow; none: they go first.let after = match (index("after")?, index("before")?) {@@ -2220,7 +2221,6 @@ mod parent_margin_tests {json!({"before": null}),json!({"after": 1.5}),json!({"before": 6}),- json!({"after": 6}),json!({"before": 99}),] {assert!(s.execute("layout.pages.insert", &bad).is_err(), "{bad}");diff --git a/crates/engine/tests/it/insert_pages_bounds.rs b/crates/engine/tests/it/insert_pages_bounds.rsindex 340e2fc81..5f8856246 100644--- a/crates/engine/tests/it/insert_pages_bounds.rs+++ b/crates/engine/tests/it/insert_pages_bounds.rs@@ -1,21 +1,27 @@-//! `layout.pages.insert` refuses an out-of-range `after` or `before` (they must name a page)-//! instead of overflowing, and changes nothing.+//! `layout.pages.insert` clamps an out-of-range `after` to the end instead of overflowing; a+//! `before` must name a page, and one that doesn't is refused without a change.use designcraft_engine::Session;use serde_json::json;#[test]-fn insert_after_u64_max_is_refused_without_overflow() {+fn insert_after_u64_max_appends_at_the_end() {let mut s = Session::new();s.execute("file.new", &json!({"pages":2})).unwrap();let before: Vec<_> = s.doc().unwrap().doc.pages().map(|(_, _, p)| p.id).collect();- let undo = s.doc().unwrap().history.undo.len();- for p in [json!({"after": u64::MAX, "count": 2}), json!({"before": u64::MAX, "count": 2})] {- assert!(s.execute("layout.pages.insert", &p).is_err(), "{p}");- }+ let out = s.execute("layout.pages.insert", &json!({"after": u64::MAX, "count": 2})).unwrap();+ assert_eq!(out["total"], 4);let after: Vec<_> = s.doc().unwrap().doc.pages().map(|(_, _, p)| p.id).collect();- assert_eq!(after, before, "no pages added");+ assert_eq!(after.len(), 4);+ assert_eq!(after[..2], before[..], "the original pages must stay first; new pages go at the end");+ assert!(after[2..].iter().all(|id| !before.contains(id)));+}++#[test]+fn insert_before_u64_max_is_refused_without_overflow() {+ let mut s = Session::new();+ s.execute("file.new", &json!({"pages":2})).unwrap();+ let undo = s.doc().unwrap().history.undo.len();+ assert!(s.execute("layout.pages.insert", &json!({"before": u64::MAX, "count": 2})).is_err());+ assert_eq!(s.doc().unwrap().doc.page_count(), 2, "no pages added");assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");- // The last page is still a valid place: the new pages go at the end.- let out = s.execute("layout.pages.insert", &json!({"after": 1, "count": 2})).unwrap();- assert_eq!(out["total"], 4);}