diff --git a/crates/engine/src/script/dom/layers.rs b/crates/engine/src/script/dom/layers.rs index 341505da5f..0d6c200c27 100644 --- a/crates/engine/src/script/dom/layers.rs +++ b/crates/engine/src/script/dom/layers.rs @@ -1365,7 +1365,9 @@ fn board_set_safe_areas(h: &Host, a: &[Data]) -> Answer { fn board_remove(h: &Host, a: &[Data]) -> Answer { let (b, i) = this_board(h, a)?; + let active = active_board_id(h, b.doc); on_doc(h, b.doc.uid, true, |h| command(h, "artboard.delete", &json!({"index": i})))?; + keep_active_board(h, b.doc.uid, active); follow_active_board(h, b.doc.uid); Ok(Data::Undefined) } @@ -1467,6 +1469,7 @@ fn add_board(h: &Host, a: &[Data], at: Option<&Data>) -> Answer { if len >= max { return Err(HostError::Range(format!("a document has at most {max} artboards"))); } + let active = active_board_id(h, doc); let id = super::atomically(h, |h| { on_doc(h, doc.uid, true, |h| { let (_, made) = command_value(h, "artboard.new", &json!({"x": r.x0, "y": r.y0, "width": r.width(), "height": r.height(), "name": name}))?; @@ -1483,6 +1486,7 @@ fn add_board(h: &Host, a: &[Data], at: Option<&Data>) -> Answer { st.doc.artboards.get(to.unwrap_or(index)).map(|ab| ab.id).ok_or_else(|| HostError::Error("the artboard was not made".into())) }) })?; + keep_active_board(h, doc.uid, active); follow_active_board(h, doc.uid); Ok(board_ref(h, doc.uid, id)) } @@ -1505,11 +1509,45 @@ fn boards_remove(h: &Host, a: &[Data]) -> Answer { let doc = boards_doc(h, a)?; let len = with_doc(h, doc, |_, st| st.doc.artboards.len()); let i = board_index_arg(arg(a, 1), len, "index")?; + let active = active_board_id(h, doc); on_doc(h, doc.uid, true, |h| command(h, "artboard.delete", &json!({"index": i})))?; + keep_active_board(h, doc.uid, active); follow_active_board(h, doc.uid); Ok(Data::Undefined) } +/// The id and index of the artboard that is active in `doc` now, chosen or only the fallback. +fn active_board_id(h: &Host, doc: Doc) -> Option<(u32, usize)> { + with_doc(h, doc, |_, st| { + let i = active_artboard(st); + st.doc.artboards.get(i).map(|ab| (ab.id, i)) + }) +} + +/// Keeps artboard `id` active in document `uid` by its id when it is still there, as +/// `setActiveArtboardIndex` does, so an artboard added or removed before it moves its index +/// instead of making another artboard the active one. +/// A stored id of an artboard that is gone is replaced as well. In a document made during the run, +/// the index the artboard had before the change is recorded for the run's report, as the first +/// `setActiveArtboardIndex` records it. +fn keep_active_board(h: &Host, uid: u64, active: Option<(u32, usize)>) { + let Some((id, index)) = active else { return }; + { + let mut s = h.session.borrow_mut(); + let Some(st) = s.document_mut(uid) else { return }; + if !st.doc.artboards.iter().any(|ab| ab.id == id) { + return; + } + st.active_artboard = Some(id); + } + if !h.dom.boards_at_start.iter().any(|(u, _)| *u == uid) { + let mut made = h.dom.boards_chosen_in_new.borrow_mut(); + if !made.iter().any(|(u, _)| *u == uid) { + made.push((uid, index)); + } + } +} + /// After the script adds or removes an artboard in document `uid`, the steps `app.doScript` plays /// act on the document's active artboard by id, at the index the run's report gives it, as after /// `setActiveArtboardIndex`, until the document is reverted. diff --git a/crates/engine/src/tests/script_actions.rs b/crates/engine/src/tests/script_actions.rs index f7d78b4392..cf2010e7a1 100644 --- a/crates/engine/src/tests/script_actions.rs +++ b/crates/engine/src/tests/script_actions.rs @@ -340,7 +340,8 @@ fn steps_discard_the_chosen_artboard_after_revert() { /// An artboard the script removes or inserts moves the active artboard's index; its played steps act /// on the active artboard at the index `getActiveArtboardIndex()` gives, as the run's report does, -/// also after an undo puts the artboards back. +/// also after an undo puts the artboards back. The artboard that was active stays active, also when +/// none was chosen before the run. #[test] fn steps_follow_the_active_artboard_after_the_script_removes_or_inserts_one_before_it() { let setup = |active: Option| { @@ -365,12 +366,14 @@ fn steps_follow_the_active_artboard_after_the_script_removes_or_inserts_one_befo ]; for (active, edit) in cases { let (mut s, id) = setup(active); + let was = s.active().unwrap().doc.artboards[active.unwrap_or(0)].rect; let source = format!("var A = app.activeDocument.artboards; {edit} vc.log(A.getActiveArtboardIndex()); app.doScript('Align', 'Set');"); let out = log_with(&mut s, &source, &actions); let index = out[0].as_u64().unwrap() as usize; let board = s.active().unwrap().doc.artboards[index].rect; let left = s.active().unwrap().doc.bounds_of(&[vectorcraft_doc::NodeId(id)], false).unwrap().x0; assert_eq!(left, board.x0, "{active:?} {edit}"); + assert_eq!(board, was, "the artboard active before the edit stays active: {active:?} {edit}"); } } diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 020a49028c..7c7b07a101 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -4577,6 +4577,38 @@ fn a_new_documents_chosen_artboard_is_reported_after_an_insert_before_it() { assert!(list.iter().any(|e| e["artboard"] == json!(1)), "{r}"); } +/// Without a chosen artboard, an artboard inserted before the default active one moves it to the +/// next index, in an open document and in one the run makes, and the report gives that index. +#[test] +fn inserting_before_the_default_active_artboard_keeps_it() { + let (mut s, _, _) = doc(); + let first = s.active().unwrap().doc.artboards[0].id; + let r = run(&mut s, "var A = app.activeDocument.artboards; 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)); + let r = run(&mut s, "var d = app.documents.add(); var A = d.artboards; A.insert([800, 300, 900, 100], 0); vc.log(A.getActiveArtboardIndex());") + .unwrap(); + assert_eq!(r["log"], json!([1])); + let list = r["activeArtboards"].as_array().cloned().unwrap_or_default(); + assert!(list.iter().any(|e| e["artboard"] == json!(1)), "{r}"); +} + +/// After the chosen artboard is removed, the artboard active in its place stays active when one is +/// inserted before it. +#[test] +fn inserting_before_the_fallback_after_the_chosen_artboard_is_removed_keeps_it() { + let (mut s, _, _) = doc(); + let r = run( + &mut s, + "var A = app.activeDocument.artboards; A.add([800, 300, 900, 100]); A.setActiveArtboardIndex(1); A.remove(1);\ + var was = A[A.getActiveArtboardIndex()].name; A.insert([1000, 300, 1100, 100], 0);\ + vc.log(A[A.getActiveArtboardIndex()].name == was);", + ) + .unwrap(); + assert_eq!(r["log"], json!([true]), "{r}"); +} + /// `artboards.insert` gives nothing back; `artboards.add` gives the new artboard. #[test] fn artboards_insert_returns_nothing() { diff --git a/docs/mcp.md b/docs/mcp.md index c1f823f1a4..d5aa0d0835 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3362,8 +3362,10 @@ through `$`: change in `activeArtboards` (`[{document, artboard}]`, both indexes, with each document's active artboard as the run leaves it). The report also lists a document whose active artboard has another index when the run ends because artboards were inserted, removed or reordered before it, and a document the run opened in which an artboard was - chosen. The chosen artboard stays active when another is inserted before it, even if it was already the active - one. An index outside the list throws a + chosen, or whose active artboard ends at another index after artboards were added, inserted or removed before it. + The chosen artboard stays active when another is inserted before it, even if it was already the active + one. When the script adds, inserts or removes an artboard without having chosen one, the artboard active before + the change stays active. An index outside the list throws a `RangeError`. A layer that was deleted throws `Stale layer`, and an artboard that was deleted throws `Stale artboard`. `Layer.pathItems`, `blendingMode`, `artworkKnockout` and `sliced`, `GroupItem.pathItems` and `GroupItems.createFromFile` throw an error that says they are not available yet. `GroupItems.createFromFile` on a diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index bfd542a01c..0cca5398e4 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1114,7 +1114,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Variables: a type object holds one text variable. `variable.bind` and Make Text Dynamic refuse a second one and name the text variable the object has; a visibility variable can still share the object, and a document saved with two text variables on one object opens with both. - Synthetic input validates complete gestures before queue mutation: at most 256 drag steps, 16 clicks and 1,024 pending events; up to 64 control requests per frame, with later requests retained for the next frame (#1709). - The control server writes each reply through a 64 KiB buffer. On Windows 11 it sends the 17 MB `document.inspect` reply of a document with 50,000 objects on one layer to a client over loopback in 0.09 s instead of 34 s. - - Scripts: File › Scripts › Other Script… and `script.run` run a JavaScript file against the open documents as one transaction with one undo step for each changed document. A failed or stopped run puts every document back. `crates/engine/src/tests/script_run.rs`. + - Scripts: File › Scripts › Other Script… and `script.run` run a JavaScript file against the open documents as one transaction with one undo step for each changed document; showing or hiding a perspective grid and choosing its active plane change only the view and add no undo step. A failed or stopped run puts every document back. `crates/engine/src/tests/script_run.rs`. - Scripts: `Document.rasterize` replaces page items with an embedded image and `Document.imageCapture` writes a PNG of the art or of an area of the page, with the option classes the help pages https://ai-scripting.docsforadobe.dev/jsobjref/Document/, https://ai-scripting.docsforadobe.dev/jsobjref/RasterizeOptions/ and https://ai-scripting.docsforadobe.dev/jsobjref/ImageCaptureOptions/ describe; `object.rasterize` takes `ids` and `clipBounds` for them (`docs/mcp.md`). `Document.windowCapture` is not there yet. `crates/engine/src/tests/script_raster_capture.rs`. - Scripts: a page item's `translate`, `rotate`, `resize` and `transform` move, turn, scale and transform the object by offsets, an angle, percentages or a `Matrix` record, about its center or a `Transformation` point, with positions, patterns, gradients and line widths following their arguments (https://ai-scripting.docsforadobe.dev/jsobjref/PageItem/). Each call that moves or reshapes the object runs `object.transform` inside the run's transaction, a call that changes only a path's stroke weight runs `stroke.set`, and a call that changes nothing runs no command. `resize` and `transform` on a member of a compound path with `changeLineWidths` at their own scale set the compound's stroke weight; locked art, art on a locked artboard and bad arguments throw an error with nothing changed (`docs/mcp.md`). `crates/engine/src/tests/script_dom_transform.rs`. - Scripts: `Document.placedItems` and `Document.rasterItems` list a document's linked and embedded images; `placedItems.add()` with a `File` assigned to `file` places an image linked, and `PlacedItem.relink(file)` and `embed()` relink and embed it, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItems/, https://ai-scripting.docsforadobe.dev/jsobjref/PlacedItem/, https://ai-scripting.docsforadobe.dev/jsobjref/RasterItems/ and https://ai-scripting.docsforadobe.dev/jsobjref/RasterItem/ describe them. A raster item's bits per channel, channels, color space and transparency come from its encoded pixels' header. Only files the script may read, of at most 256 MiB and holding an image, are placed or relinked. `crates/engine/src/tests/script_dom_images.rs`. @@ -1138,7 +1138,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - Scripts: `app.redraw()` (https://ai-scripting.docsforadobe.dev/jsobjref/Application/) returns without changing the documents or adding an undo step; the app goes on drawing the documents as they were before the run until the run ends. VectorCraft's own rule. `crates/engine/src/tests/script_dom.rs`. - Scripts: a document's variables and data sets, the variables its page items are bound to, Lock Variables, and importing and exporting a variable library through a `File`, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/Variables/, https://ai-scripting.docsforadobe.dev/jsobjref/Variable/, https://ai-scripting.docsforadobe.dev/jsobjref/Datasets/ and https://ai-scripting.docsforadobe.dev/jsobjref/Dataset/ describe them; `variable.setBinding` binds one object for one slot, and `dataset.update {name}` updates a data set that is not the active one (`docs/mcp.md`). `crates/engine/src/tests/script_variables.rs`. - Scripts: `app.selectTool(name)` makes the named tool the current tool when the run ends without an error (https://community.adobe.com/questions-652/what-s-new-in-illustrator-scripting-cc2020-769942). `crates/engine/src/tests/script_dom.rs`. - - Scripts: `app.doScript(action, set, dialogs?)` plays an action of the Actions panel inside the run, as the `DoScript` method of the scripting reference https://web.archive.org/web/20170621180804/https://wwwimages.adobe.com/content/dam/Adobe/en/devnet/illustrator/sdk/CC2015_3/Illustrator%20VBScript%20Reference.pdf describes it: its steps join the run's one undo step and keep the script's file limits, and a stop or a step a script may not run, or that plays an action or runs a script, is refused before any step runs (`docs/mcp.md`). After the script adds, inserts or removes an artboard, playback acts on the active artboard at the index `getActiveArtboardIndex()` gives. After deletion of an artboard chosen by the script, playback uses the same fallback as script coordinates, `getActiveArtboardIndex()` and the view; once a script calls `setActiveArtboardIndex` on a document or adds, inserts or removes one of its artboards, untargeted paste and Fit to Artwork Bounds use the active artboard instead of the Artboard tool's selection at the start of the run; File > Revert discards that stored choice. `app.loadAction` and `app.unloadAction` are not there yet. `crates/engine/src/tests/script_actions.rs`. + - Scripts: `app.doScript(action, set, dialogs?)` plays an action of the Actions panel inside the run, as the `DoScript` method of the scripting reference https://web.archive.org/web/20170621180804/https://wwwimages.adobe.com/content/dam/Adobe/en/devnet/illustrator/sdk/CC2015_3/Illustrator%20VBScript%20Reference.pdf describes it: its steps join the run's one undo step and keep the script's file limits, and a stop or a step a script may not run, or that plays an action or runs a script, is refused before any step runs (`docs/mcp.md`). After the script adds, inserts or removes an artboard, the artboard active before the change stays active, also when the script never chose one, and playback acts on it at the index `getActiveArtboardIndex()` gives. After deletion of an artboard chosen by the script, playback uses the same fallback as script coordinates, `getActiveArtboardIndex()` and the view; once a script calls `setActiveArtboardIndex` on a document or adds, inserts or removes one of its artboards, untargeted paste and Fit to Artwork Bounds use the active artboard instead of the Artboard tool's selection at the start of the run; File > Revert discards that stored choice. `app.loadAction` and `app.unloadAction` are not there yet. `crates/engine/src/tests/script_actions.rs`. - Scripts: Window › Script Console shows each run's `$.write` and `$.writeln` output, `vc.log` entries, uncaught error with the script's name, line and column, and a report of its time, host calls and slowest commands, keeping at most 2,048 lines and counting the lines it dropped. `script.run`'s report carries the same data, and `script.console`, `script.console.clear` and `ui.copyScriptConsole` read, clear and copy it. A line's menu copies that line or the selected range. `crates/engine/src/tests/script_console.rs`. - Scripts: the status bar translates its own words around a failed run's error and the app's own error messages, and shows text the script threw itself (a value, an error it made, or an error whose name or message it changed) as it is, even when that text reads like one of the app's messages. `script.run`'s report gives `messageOrigin` (`script` or `built-in`), and the advice about objects other applications give their scripts follows only the app's own reference errors. VectorCraft's own rule. `crates/js/src/interp_tests.rs`, `crates/engine/src/tests/script_run.rs`, `crates/ui-egui/src/chrome/tests.rs`. - Scripts: the warning before File › Scripts › Other Script… runs a script lists what `script.check` finds the script reaches outside the document (files it writes, moves or deletes, other apps, the network, native code, environment variables, preferences, code the checker can't read), the files and dialogs it needs, features only the reference app has and its included files, read without running it. It says when the script is too large to check, compiled, not a script or not read, and when a list was cut short. VectorCraft's own rule. `crates/ui-egui/src/tests/script_run.rs`.