DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into insert-pages-dialog
moppeeerccc6f16e7242
diff --cc crates/engine/src/cmd/layout.rsindex 4362ae339,d67f469f0..b88419538--- a/crates/engine/src/cmd/layout.rs+++ b/crates/engine/src/cmd/layout.rs@@@ -161,35 -161,27 +161,44 @@@ pub fn specs() -> Vec<CommandSpec>"Insert Pages…",["Layout", "Pages"],Some("Cmd+Shift+P"),- "{count?: 1, after?: page index (default: last), before?: 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), parent?: id|\"A-Parent\"|\"A\"|null}",++ "{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}",has_doc,|s, p| {- let count = p.get("count").and_then(Value::as_u64).unwrap_or(1).clamp(1, 9999) as usize;+ const ID: &str = "layout.pages.insert";+ // A count that is given but is not a number of pages used to insert one page:+ // `0`, `-1` and `1.5` all came out as 1, and 10000 was quietly capped.+ let count = match p.get("count").filter(|v| !v.is_null()) {+ None => 1,+ Some(v) => match v.as_u64().filter(|n| (1..=9999).contains(n)).map(|n| n as usize) {+ Some(n) => n,+ None => return Err(bad(ID, format!("count {v}: 1 to 9999 pages"))),+ },+ };let st = s.doc()?;let n = st.doc.page_count();- // An `after` past the last page appends, which `insert_after_u64_max_appends_at_the_end`- // pins deliberately.- let after = p.get("after").and_then(Value::as_u64).map(|v| usize::try_from(v).unwrap_or(usize::MAX)).unwrap_or(n.saturating_sub(1));+ // A placement given (null included) must be a page index; only when neither is+ // given do the pages go after the last.+ 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.+ 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}"))),+ };+ // The page the new ones follow; none: they go first.+ let after = match (index("after")?, index("before")?) {+ (_, Some(b)) => b.checked_sub(1),+ (a, None) => Some(a.unwrap_or(n.saturating_sub(1))),+ };- let parent = parent_ref("layout.pages.insert", &st.doc, p.get("parent"))?.unwrap_or_else(|| st.doc.parents.first().map(|x| x.id));+ let parent = parent_ref(ID, &st.doc, p.get("parent"))?.unwrap_or_else(|| st.doc.parents.first().map(|x| x.id));s.edit(|d, _| {- let ids = d.insert_pages(Some(after), count, parent)?;+ let ids = d.insert_pages(after, count, parent)?;Ok(json!({"pages": ids.len(), "total": d.page_count()}))})}