diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index feb87f1a4c..73f00536a2 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -1588,6 +1588,9 @@ fn item_remove(h: &Host, a: &[Data]) -> Answer { /// The error of `remove()` and `removeAll()` for an object on a locked artboard. const ON_LOCKED_ARTBOARD: &str = "cannot delete objects on a locked artboard"; +/// The error of `move` for an object on a locked artboard. +const ON_LOCKED_ARTBOARD_MOVE: &str = "cannot move objects on a locked artboard"; + /// The error of `move` and `duplicate` given an object of another document. pub const CROSS_DOCUMENT: &str = "Cross-document placement is not available"; @@ -1670,6 +1673,9 @@ fn destination(h: &Host, item: Item, target: &Data, how: &Data, copy: bool) -> s let name = d.node(item.id).and_then(typename).unwrap_or("page item"); return Err(HostError::Error(format!("Cannot move {name} {}", item.id.0))); } + if !copy && d.on_locked_artboard(item.id) { + return Err(HostError::Error(format!("{ON_LOCKED_ARTBOARD_MOVE}: {}", item.id.0))); + } Ok(Destination { parent, beside, how }) }) } @@ -1696,7 +1702,12 @@ fn place(h: &Host, doc: Doc, id: NodeId, to: &Destination) -> std::result::Resul } (_, None) => count, }; - Ok::<_, HostError>(if clips { at.max(1) } else { at }) + let at = if clips { at.max(1) } else { at }; + // The index `node.move` puts the object at: art stays below the ruler guides. + let node = d.node(id).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?; + let rest: Vec> = + d.children(Some(to.parent)).map(|c| c.iter().filter(|n| n.id != id).cloned().collect()).unwrap_or_default(); + Ok::<_, HostError>(crate::doc::Node::insert_index(&rest, at, node)) })?; let now = with_doc(h, doc, |_, st| st.doc.position(id)); if now.is_some_and(|(p, i, _)| p == Some(to.parent) && i == index) { diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 6d64b4a123..a1ea190b57 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1051,6 +1051,37 @@ fn remove_and_remove_all_refuse_art_on_a_locked_artboard() { assert_eq!(out, vec![json!(0)]); } +/// `move` leaves art on a locked artboard where it is, and a move that leaves an object where it +/// is runs no command, also in a layer whose ruler guides are above its art. +#[test] +fn move_refuses_art_on_a_locked_artboard_and_skips_a_move_to_the_same_place() { + let (mut s, a, b) = doc(); + s.execute("guide.add", &json!({"vertical": true, "pos": 120})).unwrap(); + let steps = undo_steps(&s); + // The topmost art, moved to the top of its layer, stays where it is. + log( + &mut s, + "var d = app.activeDocument, p = d.pageItems, top = p[0];\ + for (var i = 1; i < p.length; i++) { if (p[i].zOrderPosition > top.zOrderPosition) top = p[i]; }\ + top.move(top.parent);", + ); + assert_eq!(undo_steps(&s), steps, "a move to the same place adds no step"); + s.edit("Lock Artboard", |d, _| { + d.artboards[0].locked = true; + Ok(()) + }) + .unwrap(); + let steps = undo_steps(&s); + let order = |s: &Session| format!("{:?}", (s.active().unwrap().doc.position(a), s.active().unwrap().doc.position(b))); + let before = order(&s); + let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[0].move(p[0].parent, ElementPlacement.PLACEATBEGINNING);"); + assert!(e.contains("cannot move objects on a locked artboard"), "{e}"); + let e = failure(&mut s, "var p = app.activeDocument.pageItems; p[1].move(p[1].parent, ElementPlacement.PLACEATEND);"); + assert!(e.contains("cannot move objects on a locked artboard"), "{e}"); + assert_eq!(order(&s), before); + assert_eq!(undo_steps(&s), steps); +} + #[test] fn get_is_file_open_cannot_probe_outside_script_scope() { let (mut s, _, _) = doc(); diff --git a/docs/mcp.md b/docs/mcp.md index 07ca7284f5..c480155de3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2662,7 +2662,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot `edit.duplicate` and then `node.move` on the copy. The relative object is a page item or a layer of the same document; `ElementPlacement.INSIDE` and `PLACEATBEGINNING` (the default) put the object on top of a layer's or a group's contents, `PLACEATEND` under them, `PLACEBEFORE` directly above the relative object and `PLACEAFTER` - directly below it. An object of another document throws `Cross-document placement is not available`, and when + directly below it. `move` on an object on a locked artboard throws `cannot move objects on a locked artboard`, and a + move that leaves the object where it is runs no command. An object of another document throws `Cross-document + placement is not available`, and when `node.move` refuses the place of a copy, `duplicate` throws that error and leaves the documents, the selection and the undo history as they were before the call. `duplicate` on a path inside a compound path throws `Duplicating a path inside a compound path is not available yet`, and `zOrder` on one throws