Scripts: zOrderPosition reuses the layer list; textFrames.removeAll releases masks

- Layer.zOrderPosition reads the sibling list through the cached layer
  lists, so reading it for every layer of a loop lists the layers once.
  Each read is still charged for the search through the list.
- TextFrames.removeAll releases the mask of a group or layer whose
  clipping path is one of the type objects it deletes, as
  PageItems.removeAll and remove() do, so the object above the type is
  not made the clipping path.
- GroupItems.createFromFile checks that its collection is still there
  first, so a collection whose group was deleted throws "Stale page item"
  instead of the not-available message. docs/mcp.md says so.
This commit is contained in:
dotCooCoo committed 2026-12-02 18:44:26 -08:00
1 parent e4e6dd517d
commit 0d8a56cfde
6 files changed
+82 -11

No files matched your search

+5 -1
View File
@@ -470,7 +470,8 @@ pub(super) fn returns(data_type: &str, arguments: &[(&str, &str)]) -> MemberInfo
}
/// A member the help pages list that is not available yet: reading or assigning it throws, after
/// the check that a document, layer, page item, path point or text range `this` is still there.
/// the check that a document, layer, page item, path point, text range or `GroupItems` collection
/// `this` is still there.
/// `hasOwnProperty` is false for it, so a script that checks for the member first skips it, and
/// `reflect` leaves it out.
pub(super) fn later(link: &Link, class: &'static str, name: &'static str) -> Described {
@@ -495,6 +496,9 @@ pub(super) fn later(link: &Link, class: &'static str, name: &'static str) -> Des
"TextRange" => {
text::check_range(&h, arg(&a, 0))?;
}
"GroupItems" => {
layers::check_groups(&h, arg(&a, 0))?;
}
_ => {}
}
Err(HostError::Error(message.clone()))
+8 -6
View File
@@ -295,14 +295,12 @@ fn layer_to_string(h: &Host, a: &[Data]) -> Answer {
}
/// The layer's place among the layers beside it, from 1 at the bottom. Art beside a sublayer is
/// not counted.
/// not counted. The list comes from [`layers_listed`], so reading it for each layer of a loop
/// lists the layers once. Each read is still charged for the search through the list.
fn layer_z_position(h: &Host, a: &[Data]) -> Answer {
let (doc, id) = this_layer(h, a)?;
let at = with_doc(h, doc, |_, st| {
let parent = st.doc.parent_of(id);
let count = parent.map_or(st.doc.layers.len(), |p| st.doc.children(Some(p)).map_or(0, Vec::len));
h.charge_call(count as u64)?;
let list = layer_list(&st.doc, parent);
let list = layers_listed(h, st, st.doc.parent_of(id))?;
h.charge_call(list.len() as u64)?;
Ok::<_, HostError>(list.iter().position(|l| *l == id).map(|i| list.len() - i))
})?;
@@ -1356,9 +1354,13 @@ fn layers_to_string(h: &Host, a: &[Data]) -> Answer {
Ok(text("[Layers]"))
}
fn groups_to_string(h: &Host, a: &[Data]) -> Answer {
art_scope(h, arg(a, 0), Kind::Group.class())?;
check_groups(h, arg(a, 0))?;
Ok(text("[GroupItems]"))
}
/// Fails when `d` is not a `GroupItems` collection or the document, layer or group it lists is gone.
pub(super) fn check_groups(h: &Host, d: &Data) -> Result<()> {
art_scope(h, d, Kind::Group.class()).map(|_| ())
}
fn compounds_to_string(h: &Host, a: &[Data]) -> Answer {
art_scope(h, arg(a, 0), Kind::Compound.class())?;
Ok(text("[CompoundPathItems]"))
+7 -3
View File
@@ -1480,13 +1480,16 @@ fn frames_path_text(h: &Host, a: &[Data]) -> Answer {
/// locked one, or one on a locked artboard, stops it first, as `PageItems.removeAll()` does.
fn frames_remove_all(h: &Host, a: &[Data]) -> Answer {
let (doc, c) = frames_scope(h, a)?;
let (ids, refused, on_locked) = with_doc(h, doc, |_, st| {
let (ids, refused, on_locked, released) = with_doc(h, doc, |_, st| {
let ids = listed(h, st, c)?;
let ok = |id: &NodeId| editable(&st.doc, *id) && st.doc.node(*id).is_some_and(all_editable);
let refused: Vec<u64> = ids.iter().filter(|i| !ok(i)).map(|i| i.0).take(20).collect();
let locks = st.doc.locked_artboards();
let on_locked: Vec<u64> = ids.iter().filter(|i| locks.holds_id(**i)).map(|i| i.0).take(20).collect();
Ok::<_, HostError>((ids, refused, on_locked))
// Type that clips a group or layer releases its mask, so the object above it is not made
// the clipping path.
let released = super::masks_cleared_by(&st.doc, &ids);
Ok::<_, HostError>((ids, refused, on_locked, released))
})?;
if !refused.is_empty() {
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
@@ -1497,7 +1500,8 @@ fn frames_remove_all(h: &Host, a: &[Data]) -> Answer {
return Err(HostError::Error(format!("{}: {list}", super::ON_LOCKED_ARTBOARD)));
}
if !ids.is_empty() {
on_doc(h, doc.uid, true, |h| command(h, "edit.clear", &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
let params = json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()});
super::atomically(h, |h| on_doc(h, doc.uid, true, |h| super::clear_releasing(h, &released, &params)))?;
}
Ok(Data::Undefined)
}