mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 12:21:08 +01:00
Scripts: Fit Artboard to artwork bounds does nothing on a locked or empty artboard
app.executeMenuCommand('Fit Artboard to artwork bounds') threw the
command's error when the active artboard was locked or had no art on
it. The canvas's right-click menu dims Fit to Artwork Bounds then
(DocState::any_art_to_fit), so the script now does nothing and adds
no undo step, as for the other menu items that are not available. The docs state
that an available menu item runs its command and that an error that
command returns throws.
The host-versus-checker name table lists
Application.prototype.executeMenuCommand.
This commit is contained in:
1 parent
34abb11d0e
commit
0ea397db72
4 files changed
+29
-5
No files matched your search
@@ -795,7 +795,10 @@ const MENU_COMMANDS: &[(&str, &str)] = &[
|
||||
/// <https://www.indesignjs.de/indesignapi/illustrator/illustrator.d.ts> describes the method. A
|
||||
/// name it does not list throws an error that names it. A menu item that is not available, with
|
||||
/// no document open or nothing selected that it acts on, does nothing, as a dimmed menu item does
|
||||
/// (VectorCraft's own rule). `Fit Artboard to artwork bounds` fits the active artboard.
|
||||
/// (VectorCraft's own rule). A menu item the menus show as available runs its command, and an error
|
||||
/// that command returns throws. `Fit Artboard to artwork bounds` fits the active artboard, and does
|
||||
/// nothing when that artboard is locked or has no art, as the canvas's right-click menu dims
|
||||
/// the item then ([`DocState::any_art_to_fit`]).
|
||||
fn app_menu_command(h: &Host, a: &[Data]) -> Answer {
|
||||
let Data::String(name) = arg(a, 1) else { return Err(HostError::Type("executeMenuCommand needs a menu command, as a string".into())) };
|
||||
if name.len() > MAX_TEXT_BYTES {
|
||||
@@ -811,7 +814,13 @@ fn app_menu_command(h: &Host, a: &[Data]) -> Answer {
|
||||
return Ok(Data::Undefined);
|
||||
}
|
||||
match (id, s.active()) {
|
||||
("artboard.fitToArt", Some(st)) => json!({"index": active_artboard(st)}),
|
||||
("artboard.fitToArt", Some(st)) => {
|
||||
let index = active_artboard(st);
|
||||
if !st.any_art_to_fit(&[index]) {
|
||||
return Ok(Data::Undefined);
|
||||
}
|
||||
json!({"index": index})
|
||||
}
|
||||
_ => json!({}),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1394,6 +1394,19 @@ fn the_fit_to_artwork_menu_command_fits_the_artboard_to_the_art() {
|
||||
log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds');");
|
||||
let after = s.active().unwrap().doc.artboards[0].rect;
|
||||
assert!(after.width() < before.width() && after.height() < before.height(), "{before:?} -> {after:?}");
|
||||
// An artboard that is locked, or has no art on it, leaves the menu item dimmed: it does nothing.
|
||||
let (mut s, _, _) = doc();
|
||||
s.execute("artboard.setProps", &json!({"index": 0, "locked": true})).unwrap();
|
||||
let steps = undo_steps(&s);
|
||||
assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]);
|
||||
assert_eq!(s.active().unwrap().doc.artboards[0].rect, before);
|
||||
assert_eq!(undo_steps(&s), steps, "locked");
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let empty = s.active().unwrap().doc.artboards[0].rect;
|
||||
assert_eq!(log(&mut s, "app.executeMenuCommand('Fit Artboard to artwork bounds'); vc.log(1);"), vec![json!(1)]);
|
||||
assert_eq!(s.active().unwrap().doc.artboards[0].rect, empty);
|
||||
assert_eq!(undo_steps(&s), 0, "no art");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -82,8 +82,8 @@ const INSIDE: &[(&str, &str)] = &[
|
||||
("Application.", "prototype"),
|
||||
(
|
||||
"Application.prototype.",
|
||||
"activeDocument activeDocument= beep beep= constructor coordinateSystem coordinateSystem= documents getIsFileOpen \
|
||||
selection selection= toString typename",
|
||||
"activeDocument activeDocument= beep beep= constructor coordinateSystem coordinateSystem= documents executeMenuCommand \
|
||||
getIsFileOpen selection selection= toString typename",
|
||||
),
|
||||
("DocumentPreset.", "prototype"),
|
||||
(
|
||||
|
||||
+3
-1
@@ -2678,7 +2678,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot
|
||||
`Fit Artboard to artwork bounds` (https://gist.github.com/shalakolee/df3246659d4289eb99149b9ed00a00ee), which runs
|
||||
`artboard.fitToArt` on the active artboard. Names are matched exactly. Any other name throws an error that names
|
||||
it, and a name that is not a string throws a `TypeError`. VectorCraft's own rule: a menu item that is not
|
||||
available, with no document open or nothing selected that it acts on, does nothing and adds no undo step. A script may name the application's members without `app.`, as
|
||||
available, with no document open, nothing selected that it acts on, or a locked active artboard or one with no art
|
||||
for `Fit Artboard to artwork bounds`, does nothing and adds no undo step. An available menu item runs its command,
|
||||
and an error that command returns throws. A script may name the application's members without `app.`, as
|
||||
[Accessing and referencing objects](https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/)
|
||||
describes; in VectorCraft that holds for `activeDocument`, `documents` and `selection`. VectorCraft's own rules:
|
||||
assigning `activeDocument` or `selection` without `app.` assigns the application's member. A top-level
|
||||
|
||||
Reference in new issue
Block a user