Merge main into feat/scripts-api-d6-save-export-open

This commit is contained in:
dotCooCoo committed 2026-11-30 10:55:42 -08:00
commit 8a5f206e3c
158 files changed
+5292 -1645

No files matched your search

+93 -6
View File
@@ -49,6 +49,9 @@
//! `No active document`.
//! - 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. `undefined` throws.
//! - `app.selectTool(name)` takes a tool's id, its label, or a name of the list cited at
//! [`TOOL_NAME_PREFIX`], and the tool becomes the current tool when the run ends without an
//! error. A run that fails leaves the tool as it was.
//! - `controlBounds` is the visible bounds, widened to take in the direction points of every path
//! in the object.
//! - `Document.views` holds the document's drawn windows ([`crate::Session::window_views`]),
@@ -173,7 +176,8 @@ const ARTBOARD_COORDS: &str = "CoordinateSystem.ARTBOARDCOORDINATESYSTEM";
/// written yet, how many times each path's points went stale and each path as the run last saw its
/// points, whether a call's own savepoint ([`atomically`]) is open, the settings of each
/// `DocumentPreset` the run made, with the size of the values they hold now and the most they may
/// hold (the run's heap), and the `Variable` and `Dataset` objects it gave out.
/// hold (the run's heap), the `Variable` and `Dataset` objects it gave out, and the tool
/// `app.selectTool` chose last.
#[derive(Default)]
pub(super) struct State {
coords: Cell<Coords>,
@@ -210,6 +214,7 @@ pub(super) struct State {
preset_bytes: Cell<u64>,
preset_limit: u64,
vars: variables::Handles,
tool: Cell<Option<&'static str>>,
}
impl State {
@@ -222,6 +227,12 @@ impl State {
pub(super) fn saving(&self) -> bool {
self.saving.get()
}
/// The id of the tool `app.selectTool` chose last in the run, which becomes the current tool
/// when the run ends without an error.
pub(super) fn chosen_tool(&self) -> Option<&'static str> {
self.tool.get()
}
}
/// Runs `f`, whose commands are kept together or put back together: when `f` fails, every
@@ -847,12 +858,24 @@ fn restore_layer(h: &Host, uid: u64, layer: Option<NodeId>, rows: &[NodeId]) ->
/// commands changed it (commands given ids select them first), direct-selected anchors, segments,
/// ruler guides, slices, the key, a Layers panel target and an object chosen inside a dynamic
/// symbol instance or an Objects on Path group included, less the objects that are now deleted,
/// hidden or locked.
/// hidden or locked. `f`'s commands and the steps that put things back succeed or fail together
/// ([`atomically`]): when putting back the selection, the layer or the active document fails, `f`'s
/// changes are undone as well.
fn on_doc<T>(
h: &Host,
uid: u64,
keep_selection: bool,
f: impl FnOnce(&Host) -> std::result::Result<T, HostError>,
) -> std::result::Result<T, HostError> {
atomically(h, |h| on_doc_steps(h, uid, keep_selection, f))
}
/// The steps of [`on_doc`].
fn on_doc_steps<T>(
h: &Host,
uid: u64,
keep_selection: bool,
f: impl FnOnce(&Host) -> std::result::Result<T, HostError>,
) -> std::result::Result<T, HostError> {
let (index, active, before, layer, activated) = {
let s = h.session.borrow();
@@ -1024,6 +1047,69 @@ fn app_is_open(h: &Host, a: &[Data]) -> Answer {
Ok(Data::Bool(open))
}
/// The start of the tool names that scripts pass to `app.selectTool`, as the list at
/// <https://community.adobe.com/questions-652/what-s-new-in-illustrator-scripting-cc2020-769942> gives them.
const TOOL_NAME_PREFIX: &str = "Adobe "; // brand-ok: the start of the tool names scripts pass
/// The names on that list, without [`TOOL_NAME_PREFIX`], whose words differ from the label of the
/// VectorCraft tool they choose, with that tool's id. Names whose words are a VectorCraft tool's
/// label (`Pen Tool`) choose that tool without a row here. The pairs are VectorCraft's own.
const TOOL_NAMES: &[(&str, &str)] = &[
("Select Tool", "selection"),
("Direct Select Tool", "directSelection"),
("Direct Object Select Tool", "groupSelection"),
("Direct Lasso Tool", "lasso"),
("Path Type Tool", "typeOnPath"),
("Vertical Path Type Tool", "verticalTypeOnPath"),
("Line Tool", "lineSegment"),
("Shape Construction Regular Polygon Tool", "polygon"),
("Shape Construction Star Tool", "star"),
("Shape Construction Spiral Tool", "spiral"),
("Brush Tool", "paintbrush"),
("Freehand Tool", "pencil"),
("Freehand Smooth Tool", "smooth"),
("Freehand Erase Tool", "pathEraser"),
("Corner Join Tool", "join"),
("Knife Tool", "knife"),
("New Twirl Tool", "twirl"),
("Cyrstallize Tool", "crystallize"),
("Planar Paintbucket Tool", "livePaintBucket"),
("Planar Face Select Tool", "livePaintSelection"),
("Mesh Editing Tool", "mesh"),
("Gradient Vector Tool", "gradient"),
("Crop Tool", "artboard"),
("Slice Select Tool", "sliceSelection"),
("Scroll Tool", "hand"),
("Page Tool", "printTiling"),
];
/// The id of the tool `name` names: a tool's id (`pen`), its label (`Pen Tool`), or a name of the
/// list [`TOOL_NAME_PREFIX`] cites.
fn tool_named(name: &str) -> Option<&'static str> {
let tools = || vectorcraft_tools::catalog::all_tools();
if let Some(t) = tools().find(|t| t.id == name || t.label == name) {
return Some(t.id);
}
let rest = name.strip_prefix(TOOL_NAME_PREFIX)?;
if let Some((_, id)) = TOOL_NAMES.iter().find(|(n, _)| *n == rest) {
return vectorcraft_tools::tool_info(id).map(|t| t.id);
}
tools().find(|t| t.label == rest).map(|t| t.id)
}
/// `app.selectTool(name)`: chooses the tool `name` names ([`tool_named`]) and gives `true`. The
/// tool becomes the current tool when the run ends without an error, as a choice on the toolbar
/// does; the tool chosen last in a run is the one that counts. A name no tool has throws.
fn app_select_tool(h: &Host, a: &[Data]) -> Answer {
let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("selectTool needs a tool name, as a string".into())) };
if name.len() > MAX_TEXT_BYTES {
return Err(HostError::Range("the tool name is too long".into()));
}
let id = tool_named(name).ok_or_else(|| HostError::Error(format!("No tool is named \"{name}\"")))?;
h.dom.tool.set(Some(id));
Ok(Data::Bool(true))
}
/// The menu command strings `app.executeMenuCommand` takes, each with the command it runs. The
/// strings are the ones
/// <https://community.adobe.com/questions-652/a-list-of-illustrator-menu-commands-we-can-call-from-javascript-769510>
@@ -2227,9 +2313,9 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer {
if !(0.0..=100.0).contains(&v) {
return Err(HostError::Range("opacity must be from 0 to 100".into()));
}
// 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.
// A value the getter reports for the item's opacity runs no command, unless the item is a graph
// mark or label whose graph keeps another opacity for its series or kind: then the opacity the
// item has is sent, and replaces the one the graph keeps.
// 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 (now, keeps_other) = with_doc(h, item.doc, |_, st| {
@@ -2243,7 +2329,7 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer {
if now.is_none() || keeps_other {
refuse_protected_edit(h, item)?;
}
set_props(h, a, |_| false, json!({"opacity": now.unwrap_or(v)}))
set_props(h, a, |_| now.is_some() && !keeps_other, json!({"opacity": now.unwrap_or(v)}))
}
/// The members of `BlendModes` with the blend mode each one names.
@@ -2845,6 +2931,7 @@ fn classes(link: &Link) -> Vec<HostClass> {
("coordinateSystem", property(l, app_coords, app_set_coords), typed("CoordinateSystem")),
("getIsFileOpen", method(l, app_is_open), returns("boolean", &[("filePath", "string")])),
("redraw", method(l, app_redraw), returns("undefined", &[])),
("selectTool", method(l, app_select_tool), returns("boolean", &[("toolName", "string")])),
("undo", method(l, app_undo), returns("undefined", &[])),
("redo", method(l, app_redo), returns("undefined", &[])),
("executeMenuCommand", method(l, app_menu_command), returns("undefined", &[("menuCommandString", "string")])),
+8
View File
@@ -2647,7 +2647,15 @@ impl Session {
o.insert("unhandledRejectionCount".into(), json!(job.realm.unhandled_rejection_count()));
}
close_run(self, host, &mut report, console::Ending::Finished);
let tool = host.dom.chosen_tool();
self.end_job(job, false);
// The tool `app.selectTool` chose last becomes the current tool, as a choice on the
// toolbar makes it; the run's changes are kept when the tool cannot be chosen.
if let Some(id) = tool
&& let Err(e) = self.select_tool(id, self.last_view)
{
report["toolError"] = json!(e.to_string());
}
self.script_report = Some(report.clone());
Ok(report)
}