Merge main into feat/scripts-api-d7b-tags-notes-uuid

This commit is contained in:
dotCooCoo committed 2026-11-25 23:07:28 -08:00
commit 562bec3f1e
695 files changed
+51015 -4743

No files matched your search

+217 -64
View File
@@ -13,9 +13,25 @@
//! changed document; a change that changes nothing runs no command. A change to a document that is
//! not the active one makes it active for the command and makes the other one active again after.
//!
//! VectorCraft's own rules, which the help pages do not state:
//! - Collections list documents in tab order and art in paint order (bottom first), counted from 0.
//! `pageItems` lists every object, with the objects inside groups and compound paths after the
//! Collections are counted from 0, and `pageItems` lists art topmost first, as
//! <https://ai-scripting.docsforadobe.dev/jsobjref/CMYKColor/> reads the frontmost path as
//! `pathItems[0]`. A collection read with a name, `pageItems["name"]`, gives the object `getByName`
//! gives, and a script may name the application's members without `app.`, as
//! <https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/> describes; here
//! that holds for `activeDocument`, `documents` and `selection`.
//!
//! VectorCraft's own rules:
//! - A document's selection lists the selected objects topmost first, as `pageItems` does, whatever
//! order they were selected or made in. The objectReferences page above says instead that the
//! first object is the one last added to the page; VectorCraft does not follow that sentence.
//! - A collection read with a name that nothing has throws `Not found`, and `"name" in pageItems` is
//! false then. Assigning to such a name throws the same error.
//! - Assigning `activeDocument` or `selection` without `app.` assigns `app.activeDocument` or the
//! active document's selection. A top-level `var selection = ...` does the same and does
//! not make a variable of the script. Assigning `documents` keeps its value.
//! - Collections list documents in tab order. The objectReferences page says `documents[0]` is
//! always the active document; VectorCraft does not follow that yet.
//! - `pageItems` lists every object, with the objects inside groups and compound paths after the
//! group or compound path, and leaves out layers and ruler guides. An index out of range gives
//! `undefined`; `index` and `getByName` throw `Not found` when nothing matches.
//! - Script coordinates are points with Y up from the bottom of the first artboard
@@ -26,6 +42,17 @@
//! - Assigning `Document.selection` takes one object, a list of them, `null` or `[]`; every object
//! must be one of that document's that can be selected, or nothing changes.
//! - Members the help pages list that are not here yet throw an error that says so.
//! - Assigning `hidden`, `locked`, `opacity`, `position`, `left`, `top`, `width` or `height` to an
//! object in a hidden or locked layer, and `zOrder()` on one, throw `Cannot edit` and change
//! nothing, even for the value the object has. The exception is `opacity` given the value the
//! object has when that changes nothing; on a graph's mark or label whose series or kind keeps
//! another opacity it would still change the graph, so it is refused too. Its `name` and `note`
//! still change. An object that is hidden or locked itself, or is in a hidden or locked group,
//! takes them.
//! - `name` and `note` take a number or a Boolean as the text `String(value)` gives.
//! - A compound path whose last path `move` or `remove()` takes out stays, empty.
//! - An object with nothing in it, such as an empty group or a compound path whose paths moved out,
//! reads its bounds, `position`, `width` and `height` as zeros, and assigning them does nothing.
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
@@ -208,7 +235,8 @@ fn method(link: &Link, f: Member) -> HostMember {
}
/// A member the help pages list that is not available yet: reading or assigning it throws, after
/// the check that a document or page item `this` is still there.
/// the check that a document or page item `this` is still there. `hasOwnProperty` is false for it,
/// so a script that checks for the member first skips it.
fn later(link: &Link, class: &'static str, name: &'static str) -> (String, HostMember) {
let message = format!("{class}.{name} is not available yet");
let l = link.clone();
@@ -226,7 +254,7 @@ fn later(link: &Link, class: &'static str, name: &'static str) -> (String, HostM
}
Err(HostError::Error(message.clone()))
});
(name.to_string(), HostMember::Property { get: f.clone(), set: f })
(name.to_string(), HostMember::Unavailable(f))
}
fn text(s: impl Into<String>) -> Data {
@@ -388,14 +416,14 @@ fn item_ref(h: &Host, st: &DocState, id: NodeId) -> Data {
}
}
/// The ids of a document's page items in paint order ([`State`] keeps the lists of the last
/// documents read).
/// The ids of a document's page items, topmost first, each group or compound path before the
/// objects in it ([`State`] keeps the lists of the last documents read).
fn page_items(h: &Host, st: &DocState) -> Rc<Vec<NodeId>> {
if let Some(ids) = kept_items(&h.dom.items.borrow(), st.uid, &st.doc) {
return ids;
}
fn walk(nodes: &[Arc<crate::doc::Node>], out: &mut Vec<NodeId>) {
for n in nodes {
for n in nodes.iter().rev() {
match &n.kind {
NodeKind::Layer { children, .. } => walk(children, out),
NodeKind::RulerGuide { .. } => {}
@@ -698,6 +726,18 @@ fn app_selection(h: &Host, _: &[Data]) -> Answer {
}
}
/// `app.selection = objects`: the active document's selection, as `selection` of that document is
/// assigned.
fn app_set_selection(h: &Host, a: &[Data]) -> Answer {
let doc = {
let s = h.session.borrow();
let active = s.active_index().and_then(|index| s.documents().get(index).map(|st| Doc { index, uid: st.uid }));
active.ok_or_else(|| HostError::Error(NO_ACTIVE_DOCUMENT.into()))?
};
let ids = selection_ids(h, doc, arg(a, 1))?;
set_selection(h, doc, ids)
}
fn app_coords(h: &Host, _: &[Data]) -> Answer {
Ok(text(if h.dom.coords.get() == Coords::Document { DOCUMENT_COORDS } else { ARTBOARD_COORDS }))
}
@@ -744,10 +784,15 @@ fn app_ref(_: &Host, _: &[Data]) -> Answer {
Ok(host_ref("Application", &[]))
}
/// `documents[i]`, and `documents["name"]` as `getByName` reads it.
fn docs_at(h: &Host, a: &[Data]) -> Answer {
let s = h.session.borrow();
let at = match arg(a, 1) {
Data::Number(n) => whole(*n).and_then(|i| usize::try_from(i).ok()).and_then(|i| s.documents().get(i)),
Data::String(name) => match s.documents().iter().find(|d| d.title() == *name) {
None if !probing(a) => return Err(HostError::Error(NOT_FOUND.into())),
found => found,
},
_ => None,
};
Ok(at.map_or(Data::Undefined, |st| doc_ref(h, st.uid)))
@@ -1216,19 +1261,37 @@ fn doc_selection(h: &Host, a: &[Data]) -> Answer {
selection_of(h, doc.uid)
}
/// The selection of the document `uid`: `null` when nothing is selected, otherwise a new list of
/// the selected objects.
/// The selection of the document `uid`: a new list of the selected objects, topmost first as
/// `pageItems` lists them, empty when nothing is selected.
fn selection_of(h: &Host, uid: u64) -> Answer {
let s = h.session.borrow();
let st = s.documents().iter().find(|d| d.uid == uid).ok_or_else(|| HostError::Error(STALE_DOCUMENT.into()))?;
let ids: Vec<NodeId> = st.selection.objects.iter().copied().filter(|i| st.doc.node(*i).and_then(typename).is_some()).collect();
if ids.is_empty() {
return Ok(Data::Null);
}
if ids.len() > MAX_LISTED {
let picked: HashSet<NodeId> = st.selection.objects.iter().copied().filter(|i| st.doc.node(*i).and_then(typename).is_some()).collect();
if picked.len() > MAX_LISTED {
return Err(HostError::Range(format!("the selection has more than {MAX_LISTED} objects")));
}
h.charge_call(16 * ids.len() as u64)?;
// Only the selected objects are ordered, by their place in the tree, so the cost follows the
// size of the selection and not of the document. `pageItems` walks each list of children
// topmost first and puts a group before the objects in it: a higher index comes first, and an
// ancestor's path, a prefix of its descendants' paths, sorts before them.
let mut placed: Vec<(Vec<std::cmp::Reverse<usize>>, NodeId)> = Vec::with_capacity(picked.len());
let mut unplaced: Vec<NodeId> = Vec::new();
let mut seen: HashSet<NodeId> = HashSet::with_capacity(picked.len());
let mut steps = 0u64;
for id in st.selection.objects.iter().copied().filter(|i| picked.contains(i) && seen.insert(*i)) {
match st.doc.index_path(id) {
Some(path) => {
steps += path.len() as u64;
placed.push((path.into_iter().map(std::cmp::Reverse).collect(), id));
}
// A selected object outside the tree's walk still comes last, in the order it was
// selected.
None => unplaced.push(id),
}
}
placed.sort();
let ids: Vec<NodeId> = placed.into_iter().map(|(_, id)| id).chain(unplaced).collect();
h.charge_call(steps + 16 * ids.len() as u64)?;
Ok(Data::Array(ids.iter().map(|i| item_ref(h, st, *i)).collect()))
}
@@ -1322,8 +1385,21 @@ fn items_parent(h: &Host, a: &[Data]) -> Answer {
Ok(doc_ref(h, doc.uid))
}
/// Whether an indexer was called for `name in collection` ([`vectorcraft_js::HostClass::index`]),
/// where a name nothing has gives `undefined` instead of the `Not found` error a read throws.
fn probing(a: &[Data]) -> bool {
matches!(arg(a, 2), Data::Bool(true))
}
/// `pageItems[i]`, and `pageItems["name"]` as `getByName` reads it.
fn items_at(h: &Host, a: &[Data]) -> Answer {
let doc = items_doc(h, a)?;
if let Data::String(_) = arg(a, 1) {
return match items_by_name(h, a) {
Err(HostError::Error(e)) if e == NOT_FOUND && probing(a) => Ok(Data::Undefined),
r => r,
};
}
let Data::Number(n) = arg(a, 1) else { return Ok(Data::Undefined) };
let Some(i) = whole(*n).and_then(|i| usize::try_from(i).ok()) else { return Ok(Data::Undefined) };
Ok(with_doc(h, doc, |_, st| page_items(h, st).get(i).map_or(Data::Undefined, |id| item_ref(h, st, *id))))
@@ -1420,8 +1496,20 @@ fn set_props(h: &Host, a: &[Data], unchanged: impl FnOnce(&crate::doc::Node) ->
Ok(Data::Undefined)
}
/// The text a string property such as `name` or `note` is given: a number or a Boolean becomes the
/// text `String(value)` gives, and any other value that is not a string is refused.
fn text_arg(v: &Data, what: &str) -> std::result::Result<String, HostError> {
match v {
Data::String(s) => Ok(s.clone()),
Data::Number(n) => Ok(vectorcraft_js::number_to_string(*n)),
Data::Bool(b) => Ok(b.to_string()),
_ => Err(HostError::Type(format!("{what} must be a string"))),
}
}
/// `name = value`: a number or a Boolean becomes the text `String(value)` gives.
fn item_set_name(h: &Host, a: &[Data]) -> Answer {
let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("name must be a string".into())) };
let name = &text_arg(arg(a, 1), "name")?;
if name.len() > MAX_TEXT_BYTES {
return Err(HostError::Range("the name is too long".into()));
}
@@ -1436,9 +1524,10 @@ fn item_note(h: &Host, a: &[Data]) -> Answer {
read_item(h, a, |_, n| Ok(text(note_of(n))))
}
/// `note = text`: the Attributes panel's note, set with `attributes.set`, which trims it.
/// `note = text`: the Attributes panel's note, set with `attributes.set`, which trims it. A number
/// or a Boolean becomes the text `String(value)` gives.
fn item_set_note(h: &Host, a: &[Data]) -> Answer {
let Data::String(note) = arg(a, 1) else { return Err(HostError::Type("note must be a string".into())) };
let note = &text_arg(arg(a, 1), "note")?;
if note.len() > MAX_TEXT_BYTES {
return Err(HostError::Range("the note is too long".into()));
}
@@ -1450,12 +1539,27 @@ fn item_set_note(h: &Host, a: &[Data]) -> Answer {
Ok(Data::Undefined)
}
/// Refuses a change to the item's appearance, place, stacking order or size while a layer it is in
/// is hidden or locked. Its name and note may still change.
fn refuse_in_closed_layer(h: &Host, item: Item) -> std::result::Result<(), HostError> {
let refused = with_doc(h, item.doc, |_, st| {
let d = &st.doc;
let closed = |i: &NodeId| d.node(*i).is_some_and(|n| matches!(n.kind, NodeKind::Layer { .. }) && (!n.visible || n.locked));
d.ancestry(item.id).is_some_and(|a| a.iter().any(closed)).then(|| d.node(item.id).and_then(typename).unwrap_or("page item"))
});
match refused {
Some(name) => Err(HostError::Error(format!("Cannot edit {name} {}: a layer it is in is hidden or locked", item.id.0))),
None => Ok(()),
}
}
fn item_hidden(h: &Host, a: &[Data]) -> Answer {
read_item(h, a, |_, n| Ok(Data::Bool(!n.visible)))
}
fn item_set_hidden(h: &Host, a: &[Data]) -> Answer {
let hidden = boolean(arg(a, 1), "hidden")?;
refuse_in_closed_layer(h, this_item(h, a)?)?;
set_props(h, a, |n| n.visible != hidden, json!({"visible": !hidden}))
}
@@ -1465,6 +1569,7 @@ fn item_locked(h: &Host, a: &[Data]) -> Answer {
fn item_set_locked(h: &Host, a: &[Data]) -> Answer {
let locked = boolean(arg(a, 1), "locked")?;
refuse_in_closed_layer(h, this_item(h, a)?)?;
set_props(h, a, |n| n.locked == locked, json!({"locked": locked}))
}
@@ -1480,10 +1585,21 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer {
// A value the getter reports for the item's opacity sends the opacity it has, and
// `object.setProps` finds the no-op itself: an opacity a graph mark or label already has still
// replaces the one the graph keeps for its series or kind.
// The opacity the item already has may be assigned while its layer is hidden or locked, unless
// it would still replace the opacity a graph keeps for the item's series or kind.
let item = this_item(h, a)?;
let now = with_doc(h, item.doc, |_, st| st.doc.node(item.id).map(|n| f64::from(n.opacity) * 100.0));
let v = now.filter(|now| (now - v).abs() < 1e-4).unwrap_or(v);
set_props(h, a, |_| false, json!({"opacity": v}))
let (now, keeps_other) = with_doc(h, item.doc, |_, st| {
let d = &st.doc;
// Only the exact number the getter reports counts as the same opacity.
let now = d.node(item.id).map(|n| f64::from(n.opacity) * 100.0).filter(|now| (now * 1e4).round() / 1e4 == v);
let keeps_other =
crate::cmd::graph::series_object_opacity_differs(d, &[item.id]) || crate::cmd::graph::label_object_opacity_differs(d, &[item.id]);
(now, keeps_other)
});
if now.is_none() || keeps_other {
refuse_in_closed_layer(h, item)?;
}
set_props(h, a, |_| false, json!({"opacity": now.unwrap_or(v)}))
}
fn item_selected(h: &Host, a: &[Data]) -> Answer {
@@ -1506,18 +1622,19 @@ fn item_set_selected(h: &Host, a: &[Data]) -> Answer {
}
/// The item's geometric bounds (`visual`: its visible bounds) in document coordinates and the
/// document's [`Frame`].
fn item_rect(h: &Host, item: Item, visual: bool) -> std::result::Result<(Rect, Frame), HostError> {
with_doc(h, item.doc, |_, st| {
let r = st.doc.bounds_of(&[item.id], visual).ok_or_else(|| HostError::Error("the object has no bounds".into()))?;
Ok((r, frame(st)?))
})
/// document's [`Frame`]. An object with nothing in it, such as an empty group or a compound path
/// whose paths moved out, has no bounds: its bounds read as zeros, and moving or resizing it does
/// nothing.
fn item_rect(h: &Host, item: Item, visual: bool) -> std::result::Result<(Option<Rect>, Frame), HostError> {
with_doc(h, item.doc, |_, st| Ok((st.doc.bounds_of(&[item.id], visual), frame(st)?)))
}
fn bounds(h: &Host, a: &[Data], visual: bool) -> Answer {
let item = this_item(h, a)?;
let (r, f) = item_rect(h, item, visual)?;
Ok(f.bounds(h.dom.coords.get(), r))
match item_rect(h, item, visual)? {
(Some(r), f) => Ok(f.bounds(h.dom.coords.get(), r)),
(None, _) => Ok(Data::Array(vec![num(0.0), num(0.0), num(0.0), num(0.0)])),
}
}
fn item_geometric(h: &Host, a: &[Data]) -> Answer {
@@ -1530,8 +1647,10 @@ fn item_visible(h: &Host, a: &[Data]) -> Answer {
fn item_position(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
let (r, f) = item_rect(h, item, false)?;
Ok(pair(f.to_script(h.dom.coords.get(), Point::new(r.x0, r.y0))))
match item_rect(h, item, false)? {
(Some(r), f) => Ok(pair(f.to_script(h.dom.coords.get(), Point::new(r.x0, r.y0)))),
(None, _) => Ok(pair([0.0, 0.0])),
}
}
fn item_left(h: &Host, a: &[Data]) -> Answer {
@@ -1550,19 +1669,20 @@ fn item_top(h: &Host, a: &[Data]) -> Answer {
fn item_width(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
Ok(num(item_rect(h, item, false)?.0.width()))
Ok(num(item_rect(h, item, false)?.0.map_or(0.0, |r| r.width())))
}
fn item_height(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
Ok(num(item_rect(h, item, false)?.0.height()))
Ok(num(item_rect(h, item, false)?.0.map_or(0.0, |r| r.height())))
}
/// Moves the item so its top-left corner is at `x` and `y` in script coordinates (`None`: that one
/// stays).
fn move_to(h: &Host, a: &[Data], x: Option<f64>, y: Option<f64>) -> Answer {
let item = this_item(h, a)?;
let (r, f) = item_rect(h, item, false)?;
refuse_in_closed_layer(h, item)?;
let (Some(r), f) = item_rect(h, item, false)? else { return Ok(Data::Undefined) };
let c = h.dom.coords.get();
let now = f.to_script(c, Point::new(r.x0, r.y0));
let to = f.to_document(c, [x.unwrap_or(now[0]), y.unwrap_or(now[1])]);
@@ -1598,7 +1718,8 @@ fn resize(h: &Host, a: &[Data], width: bool) -> Answer {
return Err(HostError::Range(format!("{what} must be more than 0 and at most 3999856 points")));
}
let item = this_item(h, a)?;
let (r, _) = item_rect(h, item, false)?;
refuse_in_closed_layer(h, item)?;
let (Some(r), _) = item_rect(h, item, false)? else { return Ok(Data::Undefined) };
let now = if width { r.width() } else { r.height() };
if now <= 0.0 {
return Err(HostError::Error(format!("the object has no {what} to scale")));
@@ -1637,7 +1758,7 @@ fn item_z_order_position(h: &Host, a: &[Data]) -> Answer {
/// `remove()`: deletes the object. A hidden or locked object, one in a hidden or locked layer or
/// group, a group holding one, or an object on a locked artboard is refused, as [`items_remove_all`]
/// refuses it.
/// refuses it. A compound path whose last path this is stays, empty.
fn item_remove(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
let (whole, on_locked) = with_doc(h, item.doc, |_, st| {
@@ -1649,7 +1770,8 @@ fn item_remove(h: &Host, a: &[Data]) -> Answer {
if on_locked {
return Err(HostError::Error(format!("{ON_LOCKED_ARTBOARD}: {}", item.id.0)));
}
edit_item(h, item, "edit.clear", &json!({"ids": [item.id.0]}))?;
// A compound path whose last path this is stays, empty, as it does after `move`.
edit_item(h, item, "edit.clear", &json!({"ids": [item.id.0], "keepEmpty": true}))?;
Ok(Data::Undefined)
}
@@ -1695,9 +1817,9 @@ struct Destination {
}
/// The object a relative object of `move` or `duplicate` names: a page item or a layer of the
/// same document as `item`. A copy (`copy`) may go directly above or below `item` itself, as
/// `layer.move` puts copies.
fn destination(h: &Host, item: Item, target: &Data, how: &Data, copy: bool) -> std::result::Result<Destination, HostError> {
/// same document as `item`. An object may go directly above or below itself: a copy goes there, as
/// `layer.move` puts copies, and a move there leaves the object where it is.
fn destination(h: &Host, item: Item, target: &Data, how: &Data) -> std::result::Result<Destination, HostError> {
let how = placement_of(how)?;
let beside = matches!(how, Placement::Above | Placement::Below);
let handle = handle_of(target, "PageItem").or_else(|| handle_of(target, "Layer"));
@@ -1716,7 +1838,7 @@ fn destination(h: &Host, item: Item, target: &Data, how: &Data, copy: bool) -> s
let d = &st.doc;
let t = d.node(target).ok_or_else(|| HostError::Error(STALE_ITEM.into()))?;
let inside = d.ancestry(target).is_some_and(|a| a.iter().any(|i| *i == item.id && *i != target));
if inside || (target == item.id && !(copy && beside)) {
if inside || (target == item.id && !beside) {
return Err(HostError::Error("an object cannot go into itself".into()));
}
let (parent, beside) = match how {
@@ -1778,27 +1900,16 @@ fn place(h: &Host, doc: Doc, id: NodeId, to: &Destination) -> std::result::Resul
/// `move(relativeObject, insertionLocation?)`: moves the item into or beside `relativeObject`, a
/// page item or a layer of its document (`insertionLocation` defaults to `PLACEATBEGINNING`). An
/// object that is hidden or locked, in a hidden or locked layer or group, or on a locked artboard
/// is refused.
/// is refused. Moving an object directly above or below itself leaves it where it is, and a compound
/// path whose last path moves out stays, empty.
fn item_move(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
if matches!(arg(a, 1), Data::Undefined) {
return Err(HostError::Type("move needs a relative object".into()));
}
let to = destination(h, item, arg(a, 1), arg(a, 2), false)?;
// When the last path leaves a compound path, the compound path is deleted in the same step
// rather than left empty.
let emptied = with_doc(h, item.doc, |_, st| {
let parent = st.doc.parent_of(item.id).and_then(|p| st.doc.node(p))?;
(matches!(parent.kind, NodeKind::Compound { .. }) && parent.children().is_some_and(|c| c.len() == 1) && parent.id != to.parent)
.then_some(parent.id)
});
match emptied {
None => place(h, item.doc, item.id, &to)?,
Some(compound) => atomically(h, |h| {
place(h, item.doc, item.id, &to)?;
on_doc(h, item.doc.uid, true, |h| command(h, "edit.clear", &json!({"ids": [compound.0]})))?;
Ok(())
})?,
let to = destination(h, item, arg(a, 1), arg(a, 2))?;
if to.beside != Some(item.id) {
place(h, item.doc, item.id, &to)?;
}
Ok(arg(a, 0).clone())
}
@@ -1837,7 +1948,7 @@ fn item_duplicate(h: &Host, a: &[Data]) -> Answer {
}
None
}
target => Some(destination(h, item, target, arg(a, 2), true)?),
target => Some(destination(h, item, target, arg(a, 2))?),
};
let id = atomically(h, |h| {
let (_, made) = on_doc(h, item.doc.uid, true, |h| command_value(h, "edit.duplicate", &json!({"ids": [item.id.0]})))?;
@@ -1853,7 +1964,7 @@ fn item_duplicate(h: &Host, a: &[Data]) -> Answer {
}
/// `zOrder(method)`: one of the four Object › Arrange commands for the item. A path inside a
/// compound path is refused.
/// compound path is refused, and so is an object in a hidden or locked layer.
fn item_z_order(h: &Host, a: &[Data]) -> Answer {
let id = match arg(a, 1) {
Data::String(s) if s == "ZOrderMethod.BRINGTOFRONT" => "object.arrange.bringToFront",
@@ -1866,6 +1977,7 @@ fn item_z_order(h: &Host, a: &[Data]) -> Answer {
if in_compound(h, item) {
return Err(HostError::Error(COMPOUND_MEMBER_ORDER.into()));
}
refuse_in_closed_layer(h, item)?;
let (before, after) = with_doc(h, item.doc, |_, st| {
let at = st.doc.position(item.id);
let count = at.and_then(|(p, ..)| st.doc.children(p).map(Vec::len)).unwrap_or(0);
@@ -1896,6 +2008,31 @@ fn layer_typename(h: &Host, a: &[Data]) -> Answer {
Ok(text("Layer"))
}
/// `toString()` of an object with a name: `[Class name]`, `[PathItem ]` for an object with no name.
fn named_text(class: &str, name: Answer) -> Answer {
match name? {
Data::String(n) => Ok(text(format!("[{class} {n}]"))),
_ => Ok(text(format!("[{class} ]"))),
}
}
fn item_to_string(h: &Host, a: &[Data]) -> Answer {
read_item(h, a, |_, n| Ok(text(format!("[{} {}]", typename(n).unwrap_or("PageItem"), n.name.as_deref().unwrap_or("")))))
}
fn doc_to_string(h: &Host, a: &[Data]) -> Answer {
named_text("Document", doc_name(h, a))
}
fn layer_to_string(h: &Host, a: &[Data]) -> Answer {
named_text("Layer", layer_name(h, a))
}
/// `toString()` of an object without a name: `[Class]`.
fn class_text(name: &'static str) -> HostMember {
HostMember::Method(Rc::new(move |_| Ok(text(format!("[{name}]")))))
}
fn typename_of(name: &'static str) -> HostMember {
HostMember::Get(Rc::new(move |_| Ok(text(name))))
}
@@ -1947,9 +2084,10 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("typename", typename_of("Application")),
("documents", get(l, app_documents)),
("activeDocument", property(l, app_active, app_set_active)),
("selection", get(l, app_selection)),
("selection", property(l, app_selection, app_set_selection)),
("coordinateSystem", property(l, app_coords, app_set_coords)),
("getIsFileOpen", method(l, app_is_open)),
("toString", class_text("Application")),
]);
application.push(later(l, "Application", "beep"));
let preset = {
@@ -1959,7 +2097,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
h.charge_call(64)?;
new_preset(&h)
});
let mut members = vec![("typename".to_string(), typename_of("DocumentPreset"))];
let mut members = vec![("typename".to_string(), typename_of("DocumentPreset")), ("toString".to_string(), class_text("DocumentPreset"))];
members.extend(PRESET_FIELDS.iter().map(|k| (k.to_string(), preset_field(link, k))));
HostClass { name: "DocumentPreset".into(), parent: None, members, index: None, construct: Some(f) }
};
@@ -1977,6 +2115,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("getByName", method(l, docs_by_name)),
("add", method(l, docs_add)),
("addDocument", method(l, docs_add_document)),
("toString", class_text("Documents")),
]),
Some(function(l, docs_at)),
),
@@ -1993,6 +2132,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("convertCoordinate", method(l, doc_convert)),
("pageItems", get(l, doc_page_items)),
("selection", property(l, doc_selection, doc_set_selection)),
("toString", method(l, doc_to_string)),
]);
document.extend(
["layers", "artboards", "pathItems", "groupItems", "textFrames", "swatches", "symbols", "views", "activeLayer", "saveAs", "close"]
@@ -2011,6 +2151,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("index", method(l, items_index)),
("getByName", method(l, items_by_name)),
("removeAll", method(l, items_remove_all)),
("toString", class_text("PageItems")),
]),
Some(function(l, items_at)),
));
@@ -2038,6 +2179,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("move", method(l, item_move)),
("remove", method(l, item_remove)),
("zOrder", method(l, item_z_order)),
("toString", method(l, item_to_string)),
]);
page_item.extend(
[
@@ -2064,9 +2206,20 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
);
classes.push(class("PageItem", None, page_item, None));
classes.extend(ITEM_CLASSES.iter().map(|c| class(c, Some("PageItem"), Vec::new(), None)));
classes.push(class("Layer", None, named(vec![("typename", get(l, layer_typename)), ("name", get(l, layer_name))]), None));
classes.push(class(
"Layer",
None,
named(vec![("typename", get(l, layer_typename)), ("name", get(l, layer_name)), ("toString", method(l, layer_to_string))]),
None,
));
classes.extend(tags::classes(l));
realm.define_host_classes(classes, vec![("app".into(), host_ref("Application", &[]))])
realm.define_host_classes(classes, vec![("app".into(), host_ref("Application", &[]))])?;
// The application's members a script may name without `app.`, assigned as `app.<member>` is.
realm.define_host_globals(vec![
("activeDocument".into(), function(l, app_active), Some(function(l, app_set_active))),
("documents".into(), function(l, app_documents), None),
("selection".into(), function(l, app_selection), Some(function(l, app_set_selection))),
])
}
#[cfg(test)]
+16 -8
View File
@@ -923,8 +923,15 @@ fn read_head(real: &Path, max: usize) -> std::io::Result<Vec<u8>> {
/// that is not well formed is left out. It is read under [`lock_store`]'s lock, so it is read
/// between two changes, and none are read when the lock cannot be taken.
fn read_store(store: &Path) -> Vec<serde_json::Value> {
let Ok(_lock) = lock_store(store) else { return Vec::new() };
load_store(store, false).unwrap_or_default()
list_store(store).unwrap_or_default()
}
/// The grants [`read_store`] reads; none when the grants file is missing, and `Err` when the lock
/// cannot be taken or the file is there but cannot be read, is too large, is not a grants file of
/// this version or holds no list of grants.
fn list_store(store: &Path) -> std::io::Result<Vec<serde_json::Value>> {
let _lock = lock_store(store)?;
load_store(store, false)
}
/// The grants the grants file at `store` holds, to change and write back; none when it is missing,
@@ -940,7 +947,8 @@ fn try_read_store(store: &Path) -> std::io::Result<Vec<serde_json::Value>> {
fn load_store(store: &Path, strict: bool) -> std::io::Result<Vec<serde_json::Value>> {
let bytes = match read_head(store, MAX_STORE_BYTES) {
Ok(bytes) => bytes,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
// A link whose target is missing is there but cannot be read.
Err(e) if e.kind() == std::io::ErrorKind::NotFound && std::fs::symlink_metadata(store).is_err() => return Ok(Vec::new()),
Err(e) => return Err(e),
};
if bytes.len() > MAX_STORE_BYTES {
@@ -958,14 +966,14 @@ fn load_store(store: &Path, strict: bool) -> std::io::Result<Vec<serde_json::Val
&& g["write"].is_boolean()
&& g["folder"].is_boolean()
};
let grants = v["grants"].as_array().ok_or_else(|| std::io::Error::other("the grants file holds no list of grants"))?;
if strict {
let grants = v["grants"].as_array().ok_or_else(|| std::io::Error::other("the grants file holds no list of grants"))?;
if grants.len() > MAX_STORED_GRANTS || !grants.iter().all(valid) {
return Err(std::io::Error::other("the grants file holds a grant that cannot be read"));
}
return Ok(grants.clone());
}
Ok(v["grants"].as_array().map(|a| a.iter().filter(|g| valid(g)).take(MAX_STORED_GRANTS).cloned().collect()).unwrap_or_default())
Ok(grants.iter().filter(|g| valid(g)).take(MAX_STORED_GRANTS).cloned().collect())
}
/// The number in the name of the next file [`write_store`] writes before it takes the grants file's
@@ -1065,9 +1073,9 @@ fn lock_store(_: &Path) -> std::io::Result<std::fs::File> {
}
/// The grants the grants file beside the data folder `data_dir` keeps: each script, the SHA-256 of
/// its text, the path and whether it may be written (`script.grants`).
pub(crate) fn stored_grants(data_dir: &Path) -> Vec<serde_json::Value> {
read_store(&data_dir.with_file_name(GRANTS_FILE))
/// its text, the path and whether it may be written (`script.grants`); as [`list_store`] reads them.
pub(crate) fn stored_grants(data_dir: &Path) -> std::io::Result<Vec<serde_json::Value>> {
list_store(&data_dir.with_file_name(GRANTS_FILE))
}
/// Removes from the grants file beside `data_dir` the grants of the script `script` and of the path
+1 -1
View File
@@ -326,7 +326,7 @@ impl Files {
/// The options a file of this run is parsed with, preprocessor lines aside.
pub(crate) fn options(&self) -> Options {
Options { max_source_units: MAX_TOTAL_BYTES, es2015: self.es2015, ..Options::default() }
Options { max_source_units: MAX_TOTAL_BYTES, es2015: self.es2015, e4x: true, ..Options::default() }
}
/// The `JSINCLUDE` folders of this process: absolute folders separated by `;`.
+2 -8
View File
@@ -1622,16 +1622,10 @@ fn kind_id(kind: &RunErrorKind) -> &'static str {
/// program. A message the script supplied itself ([`RunError::authored`]) is given as it is.
fn explained(e: &RunError) -> String {
const HOST_OBJECTS: &[&str] = &["Window", "BridgeTalk"];
const APP_MEMBERS: &[&str] = &["activeDocument", "documents", "selection"];
let message = &e.message;
let first = message.strip_prefix("ReferenceError: ").and_then(|m| m.split_whitespace().next());
let names_host_object = first.is_some_and(|f| HOST_OBJECTS.contains(&f));
if e.kind == RunErrorKind::Thrown
&& !e.authored
&& let Some(member) = first.filter(|f| APP_MEMBERS.contains(f))
{
format!("{message} (use `app.{member}`)")
} else if e.kind == RunErrorKind::Thrown && !e.authored && names_host_object {
if e.kind == RunErrorKind::Thrown && !e.authored && names_host_object {
format!("{message} (scripts here use the `vc` object; objects other applications give their scripts are not available)")
} else {
message.to_string()
@@ -2033,7 +2027,7 @@ impl Session {
// A `.js` script reads ECMAScript 2015; `eval`, `Function`, includes and `$.evalFile` read
// code the way the script does.
let es2015 = include::es2015_script(request.file.as_deref());
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), es2015, ..Options::default() };
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), es2015, e4x: true, ..Options::default() };
let strict = options.strict;
// A kept engine reads each run's code the way that run's script does.
let mut realm = match realm {