diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index dd47eb00f3..6544309c8c 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -1373,7 +1373,27 @@ fn item_set_selected(h: &Host, a: &[Data]) -> Answer { } else { ids.retain(|i| *i != item.id); } - set_selection(h, item.doc, ids) + // The direct-selected anchors and segments of the other objects stay selected. + let partial = with_doc(h, item.doc, |_, st| { + let sel = &st.selection; + let plain = sel.target.is_none() && sel.symbol_part.is_none() && sel.on_path_member.is_none() && sel.slices.is_empty(); + (plain && ids.iter().any(|i| sel.anchors.contains_key(i) || sel.segments.contains_key(i))).then(|| { + let mut next = sel.clone(); + next.objects.clone_from(&ids); + next.anchors.retain(|i, _| ids.contains(i)); + next.segments.retain(|i, _| ids.contains(i)); + next.key = next.key.filter(|k| ids.contains(k)); + next.key_anchor = next.key_anchor.filter(|(k, _)| ids.contains(k)); + (st.doc.clone(), next) + }) + }); + match partial { + Some((doc, next)) => { + on_doc(h, item.doc.uid, false, |h| restore_selection(h, &doc, &next))?; + Ok(Data::Undefined) + } + None => set_selection(h, item.doc, ids), + } } /// The item's geometric bounds (`visual`: its visible bounds) in document coordinates and the diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index e1288b233c..36a06aa78e 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -536,6 +536,21 @@ fn an_edit_keeps_direct_selected_anchors() { assert!(s.journal.iter().any(|(id, _)| id == "select.anchorsMany"), "the journal holds the restore"); } +#[test] +fn selecting_an_item_keeps_the_direct_selected_anchors_of_others() { + let (mut s, a, b) = doc(); + s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]], "mode": "set"})).unwrap(); + s.execute("select.add", &json!({"ids": [b.0]})).unwrap(); + let anchors = s.active().unwrap().selection.anchors.clone(); + assert!(anchors.contains_key(&a)); + log(&mut s, "app.activeDocument.pageItems[1].selected = false;"); + let sel = &s.active().unwrap().selection; + assert_eq!((sel.objects.clone(), sel.anchors.clone()), (vec![a], anchors.clone())); + log(&mut s, "app.activeDocument.pageItems[1].selected = true;"); + let sel = &s.active().unwrap().selection; + assert_eq!((sel.objects.clone(), sel.anchors.get(&a)), (vec![a, b], anchors.get(&a))); +} + #[test] fn an_edit_keeps_the_order_of_a_mixed_selection() { let (mut s, a, b) = doc(); diff --git a/docs/mcp.md b/docs/mcp.md index 3b2ec0f6b1..e59fa68b9d 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2672,7 +2672,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot `Not found`; and `app.activeDocument` with no document open throws `No active document`. `Document.selection` takes one page item, a list of at most 10,000, `null` or `[]`, and an object that cannot be selected throws `Cannot select` with - nothing changed. `getIsFileOpen` compares the path with the open documents' files as text (in a Windows path, + nothing changed. Assigning a page item's `selected` leaves the direct-selected anchors and segments of the other + selected objects selected. `getIsFileOpen` compares the path with the open documents' files as text (in a Windows path, letter case is ignored and `\` matches `/`) and does not resolve the path it is given. A document counts only when the automation read roots allow its file, and that check can look the document's file up on disk. A path with `..`, a network path, a NUL character or more than 4,096 bytes throws an error. Some members the Application,