Scripts: setActiveArtboardIndex keeps the artboard when one is inserted before it

An assignment that chooses the artboard already active by default now records it, so a later insert moves it to the next index. hasSelectedArtwork = true counts only the art it would add toward the 10,000-object limit.
This commit is contained in:
dotCooCoo
2026-11-25 23:11:44 -08:00
parent 7365371e18
commit f5090591bc
2 changed files with 50 additions and 13 deletions
+17 -13
View File
@@ -297,29 +297,30 @@ fn layer_has_selected(h: &Host, a: &[Data]) -> Answer {
fn layer_set_has_selected(h: &Host, a: &[Data]) -> Answer {
let on = boolean(arg(a, 1), "hasSelectedArtwork")?;
let (doc, id) = this_layer(h, a)?;
let (ids, total) = with_doc(h, doc, |_, st| {
let (ids, over) = with_doc(h, doc, |_, st| {
let inside = |o: &NodeId| st.doc.ancestry(*o).is_some_and(|a| a.contains(&id));
let selected = &st.selection.objects;
if !on {
return (selected.iter().copied().filter(inside).collect::<Vec<_>>(), 0);
return (selected.iter().copied().filter(inside).collect::<Vec<_>>(), false);
}
let locks = st.doc.locked_artboards();
let art = st.doc.node(id).map(|n| n.layer_art(true)).unwrap_or_default();
let mut have: std::collections::HashSet<NodeId> = selected.iter().copied().collect();
let mut ids = Vec::new();
for o in art {
if selected.len() + ids.len() > MAX_LISTED {
break;
if have.contains(&o) || !crate::cmd::select::is_selectable(&st.doc, st.isolation, &locks, o) {
continue;
}
if !have.contains(&o) && crate::cmd::select::is_selectable(&st.doc, st.isolation, &locks, o) {
have.insert(o);
ids.push(o);
// Only art that would be added counts toward the limit.
if selected.len() + ids.len() >= MAX_LISTED {
return (ids, true);
}
have.insert(o);
ids.push(o);
}
let total = selected.len() + ids.len();
(ids, total)
(ids, false)
});
if total > MAX_LISTED {
if over {
return Err(HostError::Range(format!("a selection takes at most {MAX_LISTED} objects")));
}
if ids.is_empty() {
@@ -1291,16 +1292,19 @@ fn boards_active(h: &Host, a: &[Data]) -> Answer {
}
/// `setActiveArtboardIndex(index)`: the artboard becomes the view's active one. Nothing in the
/// document changes, and no command runs.
/// document changes, and no command runs. The artboard is kept by its id even when it is already
/// active only as the fallback (no artboard chosen yet), so an artboard inserted before it later
/// in the run moves it to the next index instead of taking its place.
fn boards_set_active(h: &Host, a: &[Data]) -> Answer {
let doc = boards_doc(h, a)?;
let (len, now) = with_doc(h, doc, |_, st| (st.doc.artboards.len(), active_artboard(st)));
let i = board_index_arg(arg(a, 1), len, "index")?;
if i != now {
{
let mut s = h.session.borrow_mut();
let st = s.document_mut(doc.uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
st.active_artboard = st.doc.artboards.get(i).map(|ab| ab.id);
drop(s);
}
if i != now {
let mut moved = h.dom.active_artboards.borrow_mut();
if !moved.contains(&doc.uid) {
moved.push(doc.uid);
+33
View File
@@ -2109,6 +2109,24 @@ fn set_active_artboard_index_is_reported_without_an_undo_step() {
assert!(r.get("activeArtboards").is_none());
}
/// Choosing the artboard that is active only by default keeps that artboard: one inserted before it
/// moves it to the next index, and the report gives that index.
#[test]
fn choosing_the_default_active_artboard_keeps_it_when_one_is_inserted_before() {
let (mut s, _, _) = doc();
assert_eq!(s.active().unwrap().active_artboard, None);
let first = s.active().unwrap().doc.artboards[0].id;
let r = run(
&mut s,
"var A = app.activeDocument.artboards; A.setActiveArtboardIndex(0); A.insert([800, 300, 900, 100], 0);\
vc.log(A.getActiveArtboardIndex());",
)
.unwrap();
assert_eq!(r["log"], json!([1]));
assert_eq!(r["activeArtboards"], json!([{"document": 0, "artboard": 1}]));
assert_eq!(s.active().unwrap().active_artboard, Some(first));
}
#[test]
fn the_report_gives_the_active_artboard_the_run_leaves() {
let (mut s, _, _) = doc();
@@ -2193,6 +2211,21 @@ fn has_selected_artwork_keeps_the_rest_of_the_selection() {
assert_eq!(sel.guides.len(), 1);
}
/// The selection limit counts only the art an assignment would add: a layer with nothing to add
/// changes nothing beside a selection already at the limit, and one with art to add throws.
#[test]
fn has_selected_artwork_checks_the_limit_only_for_art_it_adds() {
let (mut s, a, _) = doc();
run(&mut s, "app.activeDocument.layers.add();").unwrap();
// A selection of 10,001 entries, as the limit counts them.
s.active_mut().unwrap().selection.objects = vec![a; 10_001];
run(&mut s, "var l = app.activeDocument.layers[0]; l.hasSelectedArtwork = true;").unwrap_or_else(|e| panic!("{e}"));
assert_eq!(s.active().unwrap().selection.objects.len(), 10_001);
s.active_mut().unwrap().selection.objects = vec![a; 10_000];
let e = failure(&mut s, "app.activeDocument.layers.getByName('Layer 1').hasSelectedArtwork = true;");
assert!(e.contains("at most 10000"), "{e}");
}
#[test]
fn artboards_stop_at_one_hundred() {
let (mut s, _, _) = doc();