From 9f8e166f8d62ad6c0051d077da2bfc2907e0fd42 Mon Sep 17 00:00:00 2001 From: jayzed80 <121670721+jayzed80@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:23:32 +0000 Subject: [PATCH] Pages: a new page takes the parent of the page it follows (#2788) (#2817) --- crates/engine/src/cmd/layout/registry.rs | 10 ++++- crates/engine/tests/it/insert_pages_parent.rs | 42 +++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 crates/engine/tests/it/insert_pages_parent.rs diff --git a/crates/engine/src/cmd/layout/registry.rs b/crates/engine/src/cmd/layout/registry.rs index f120c80f6..71cccb384 100644 --- a/crates/engine/src/cmd/layout/registry.rs +++ b/crates/engine/src/cmd/layout/registry.rs @@ -14,7 +14,7 @@ pub fn specs() -> Vec { "Insert Pages…", ["Layout", "Pages"], Some("Cmd+Shift+P"), - "{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}", + "{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 (default: the parent of the page they follow, or of the first page)}", has_doc, |s, p| { const ID: &str = "layout.pages.insert"; @@ -50,7 +50,13 @@ pub fn specs() -> Vec { (_, Some(b)) => b.checked_sub(1), (a, None) => Some(a.unwrap_or(n.saturating_sub(1))), }; - let parent = parent_ref(ID, &st.doc, p.get("parent"))?.unwrap_or_else(|| st.doc.parents.first().map(|x| x.id)); + // No parent given: the new pages continue the page they follow (the first page when + // they go at the start), [None] included (#2788); in a document without pages, the + // first parent. + let parent = parent_ref(ID, &st.doc, p.get("parent"))?.unwrap_or_else(|| match st.doc.page(after.unwrap_or(0)) { + Some(pg) => pg.parent, + None => st.doc.parents.first().map(|x| x.id), + }); s.edit(|d, _| { let ids = d.insert_pages(after, count, parent)?; Ok(json!({"pages": ids.len(), "total": d.page_count()})) diff --git a/crates/engine/tests/it/insert_pages_parent.rs b/crates/engine/tests/it/insert_pages_parent.rs new file mode 100644 index 000000000..e513aeb2b --- /dev/null +++ b/crates/engine/tests/it/insert_pages_parent.rs @@ -0,0 +1,42 @@ +//! `layout.pages.insert` without a `parent` gives the new pages the parent of the page they +//! follow, [None] included, instead of always the first parent (#2788). +use designcraft_engine::Session; +use serde_json::json; + +fn parents(s: &Session) -> Vec> { + s.doc().unwrap().doc.pages().map(|(_, _, p)| p.parent.map(|id| id.0)).collect() +} + +#[test] +fn a_page_added_after_a_page_without_a_parent_has_none() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 2})).unwrap(); + let a = s.doc().unwrap().doc.parents.first().map(|p| p.id.0).unwrap(); + s.execute("layout.pages.applyParent", &json!({"pages": [1], "parent": null})).unwrap(); + assert_eq!(parents(&s), [Some(a), None]); + // As the Pages panel's Create new page button does: after the current page, no parent. + s.execute("layout.pages.insert", &json!({"after": 1, "count": 1})).unwrap(); + assert_eq!(parents(&s), [Some(a), None, None], "the new page continues the page it follows"); + s.execute("layout.pages.insert", &json!({"after": 0, "count": 1})).unwrap(); + assert_eq!(parents(&s), [Some(a), Some(a), None, None]); +} + +#[test] +fn pages_inserted_at_the_start_take_the_first_page_s_parent() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 2})).unwrap(); + let a = s.doc().unwrap().doc.parents.first().map(|p| p.id.0).unwrap(); + s.execute("layout.pages.applyParent", &json!({"pages": [0], "parent": null})).unwrap(); + s.execute("layout.pages.insert", &json!({"before": 0, "count": 1})).unwrap(); + assert_eq!(parents(&s), [None, None, Some(a)]); +} + +#[test] +fn a_parent_given_still_wins() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 1})).unwrap(); + s.execute("layout.pages.applyParent", &json!({"pages": [0], "parent": null})).unwrap(); + let a = s.doc().unwrap().doc.parents.first().map(|p| p.id.0).unwrap(); + s.execute("layout.pages.insert", &json!({"after": 0, "count": 1, "parent": a})).unwrap(); + assert_eq!(parents(&s), [None, Some(a)]); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 14555d3ee..5891c5d5d 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -10,6 +10,7 @@ mod combined_close_glyph; mod distribute_locks; mod export_page_input; mod insert_pages_bounds; +mod insert_pages_parent; mod param_keys; mod pdf_backend; mod preflight_resolution;