Pages: a new page takes the parent of the page it follows (#2788) (#2817)

This commit is contained in:
jayzed80 authored and GitHub committed 2026-11-15 06:23:32 +00:00
1 parent bcebb83a93
commit 9f8e166f8d
3 files changed
+51 -2

No files matched your search

+8 -2
View File
@@ -14,7 +14,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; 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<CommandSpec> {
(_, 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()}))
@@ -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<Option<u64>> {
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)]);
}
+1
View File
@@ -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;