Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration (#662 x next): a null spread is absent (spread 0), as Paste in Place forwards its optional spread and file.place documents null as absent

aiongg13f4c37153ba
diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs
index 5c0cdacd5..de75419ab 100644
--- a/crates/engine/src/cmd/mod.rs
+++ b/crates/engine/src/cmd/mod.rs
@@ -273,13 +273,13 @@ pub(crate) fn rect_param(p: &Value, key: &str) -> Option<Rect> {
Some(Rect::new(f(0)?, f(1)?, f(2)?, f(3)?))
}
/// The `spread` param: a body spread index (`0`), a spread reference (`{"kind":"doc"|"parent","index":0}`)
-/// or absent (= spread 0). Anything else is an error: read as spread 0, a script aiming at a parent
+/// or absent or null (= spread 0; commands that forward an optional `spread` pass null). Anything else is an error: read as spread 0, a script aiming at a parent
/// spread got its items on the first page without a word.
pub(crate) fn spread_param(p: &Value, command: &str) -> Result<SpreadRef> {
let index = |v: &Value, key: &str| {
v.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad(command, format!("`{key}` must be a non-negative integer")))
};
- let Some(value) = p.get("spread") else { return Ok(SpreadRef::Doc(0)) };
+ let Some(value) = p.get("spread").filter(|v| !v.is_null()) else { return Ok(SpreadRef::Doc(0)) };
if value.is_number() {
return Ok(SpreadRef::Doc(index(value, "spread")?));
}
diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rs
index 4ca8e4373..b915c228b 100644
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@ -4486,6 +4486,12 @@ mod spread_target_tests {
assert!(s.execute("line.create", &json!({"spread": bad, "a": [0, 0], "b": [50, 50]})).is_err(), "line.create spread {bad}");
}
assert!(s.doc().unwrap().doc.spreads[0].items.is_empty());
+ // Null is absent: spread 0 (Paste in Place forwards its optional `spread`).
+ let r = s.execute("frame.create", &json!({"spread": null, "rect": [0, 0, 100, 100]})).unwrap();
+ assert_eq!(s.doc().unwrap().doc.find(ItemId(r["id"].as_u64().unwrap())).unwrap().spread, SpreadRef::Doc(0));
+ s.execute("frame.create", &json!({"rect": [0, 0, 100, 100]})).unwrap();
+ s.execute("edit.copy", &json!({})).unwrap();
+ s.execute("edit.pasteInPlace", &json!({})).unwrap();
}
}