mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Scripts: Views and View toString refuse a stale collection or view
A collection or view kept by a named engine from an earlier run now throws on toString() as typename does. The tests also check that `k in document.views` is false for an index or name with no view, and the error type of each bad view value. docs/mcp.md says a centerPoint outside the canvas throws a RangeError.
This commit is contained in:
1 parent
220c60b4a7
commit
165423a9c0
3 files changed
+34
-7
No files matched your search
@@ -1299,6 +1299,11 @@ fn views_parent(h: &Host, a: &[Data]) -> Answer {
|
||||
Ok(doc_ref(h, doc.uid))
|
||||
}
|
||||
|
||||
fn views_to_string(h: &Host, a: &[Data]) -> Answer {
|
||||
views_doc(h, a)?;
|
||||
Ok(text("[Views]"))
|
||||
}
|
||||
|
||||
fn views_at(h: &Host, a: &[Data]) -> Answer {
|
||||
let doc = views_doc(h, a)?;
|
||||
let first = matches!(arg(a, 1), Data::Number(n) if *n == 0.0);
|
||||
@@ -1352,6 +1357,11 @@ fn view_parent(h: &Host, a: &[Data]) -> Answer {
|
||||
Ok(doc_ref(h, doc.uid))
|
||||
}
|
||||
|
||||
fn view_to_string(h: &Host, a: &[Data]) -> Answer {
|
||||
this_view(h, a)?;
|
||||
Ok(text("[View]"))
|
||||
}
|
||||
|
||||
/// `bounds`: `[left, top, right, bottom]` of the part of the document the canvas shows.
|
||||
fn view_bounds(h: &Host, a: &[Data]) -> Answer {
|
||||
read_view(h, a, |f, c, v| f.bounds(c, v.visible()))
|
||||
@@ -2305,7 +2315,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
|
||||
("length", get(l, views_length)),
|
||||
("parent", get(l, views_parent)),
|
||||
("index", method(l, views_index)),
|
||||
("toString", class_text("Views")),
|
||||
("toString", method(l, views_to_string)),
|
||||
]),
|
||||
Some(function(l, views_at)),
|
||||
));
|
||||
@@ -2319,7 +2329,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
|
||||
("centerPoint", property(l, view_center, view_set_center)),
|
||||
("zoom", property(l, view_zoom, view_set_zoom)),
|
||||
("screenMode", property(l, view_screen_mode, view_set_screen_mode)),
|
||||
("toString", class_text("View")),
|
||||
("toString", method(l, view_to_string)),
|
||||
]),
|
||||
None,
|
||||
));
|
||||
|
||||
@@ -213,8 +213,8 @@ fn shown(s: &Session) -> Option<WindowView> {
|
||||
#[test]
|
||||
fn the_view_shows_the_part_of_the_document_its_window_shows() {
|
||||
let (mut s, ..) = doc();
|
||||
let none = "var v = app.activeDocument.views; vc.log(v.length); vc.log(v[0] === undefined);";
|
||||
assert_eq!(log(&mut s, none), vec![json!(0), json!(true)], "a document without a window has no view");
|
||||
let none = "var v = app.activeDocument.views; vc.log(v.length); vc.log(v[0] === undefined); vc.log(0 in v); vc.log('a' in v);";
|
||||
assert_eq!(log(&mut s, none), vec![json!(0), json!(true), json!(false), json!(false)], "a document without a window has no view");
|
||||
show(&mut s, window());
|
||||
// A rectangle drawn over the visible part of the document has the view's bounds.
|
||||
let r = window().visible();
|
||||
@@ -236,6 +236,13 @@ fn the_view_shows_the_part_of_the_document_its_window_shows() {
|
||||
assert!(width(&turned[0]) > width(&out[3]));
|
||||
let names = log(&mut s, "var d = app.activeDocument; vc.log(String(d.views) + String(d.views[0]));");
|
||||
assert_eq!(names, vec![json!("[Views][View]")]);
|
||||
assert_eq!(log(&mut s, "var v = app.activeDocument.views; vc.log(0 in v); vc.log(1 in v);"), vec![json!(true), json!(false)]);
|
||||
// A named engine keeps the views from an earlier run, but they are stale.
|
||||
run(&mut s, "#targetengine keep\nvar vs = app.activeDocument.views, v = vs[0];").unwrap();
|
||||
for member in ["String(vs)", "String(v)"] {
|
||||
let e = failure(&mut s, &format!("#targetengine keep\n{member};"));
|
||||
assert!(e.contains("Stale document"), "{member}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -259,8 +266,17 @@ fn setting_the_zoom_changes_the_view_without_an_undo_step() {
|
||||
assert_eq!(s.window_views[1].1.zoom, window().zoom, "the zoom is each window's own");
|
||||
assert_eq!(undo_steps(&s), steps, "a view change is not undoable");
|
||||
// A bad value throws and leaves the view as it is.
|
||||
for bad in ["v.zoom = 0;", "v.zoom = 1000;", "v.zoom = '2';", "v.centerPoint = [1];", "v.screenMode = 'FULLSCREEN';"] {
|
||||
failure(&mut s, &format!("var v = app.activeDocument.views[0]; {bad}"));
|
||||
let bad = [
|
||||
("v.zoom = 0;", "RangeError"),
|
||||
("v.zoom = 1000;", "RangeError"),
|
||||
("v.zoom = '2';", "TypeError"),
|
||||
("v.centerPoint = [1];", "TypeError"),
|
||||
("v.centerPoint = [1e12, 0];", "RangeError"),
|
||||
("v.screenMode = 'FULLSCREEN';", "TypeError"),
|
||||
];
|
||||
for (bad, name) in bad {
|
||||
let out = log(&mut s, &format!("var v = app.activeDocument.views[0]; try {{ {bad} vc.log('set'); }} catch (e) {{ vc.log(e.name); }}"));
|
||||
assert_eq!(out, vec![json!(name)], "{bad}");
|
||||
assert_eq!(shown(&s), Some(now), "{bad}");
|
||||
}
|
||||
// A run that fails after changing the view keeps the change.
|
||||
|
||||
+2
-1
@@ -2755,7 +2755,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot
|
||||
has none. `views` has `length`, `[i]` and `index(i)`. The view (https://ai-scripting.docsforadobe.dev/jsobjref/View/) has `typename`,
|
||||
`parent`, `bounds` (`[left, top, right, bottom]` of the part of the document the canvas shows, the box around a
|
||||
rotated canvas), `centerPoint`, `zoom` and `screenMode`, in the run's coordinate system. `zoom` is 1 at 100% and goes
|
||||
from 0.0313 to 640 (VectorCraft's own range); a zoom outside it throws a `RangeError`. `screenMode` reads Normal as
|
||||
from 0.0313 to 640 (VectorCraft's own range); a zoom outside it, or a `centerPoint` outside the canvas, throws a
|
||||
`RangeError`, and a `centerPoint` that is not two numbers throws a `TypeError`. `screenMode` reads Normal as
|
||||
`ScreenMode.MULTIWINDOW`, Full Screen Mode with Menu Bar as `ScreenMode.DESKTOP` and Full Screen Mode and
|
||||
Presentation Mode as `ScreenMode.FULLSCREEN` (VectorCraft's own mapping); the screen mode is the app's, so setting it
|
||||
on one view sets it on every view. Changing a view runs no command, adds
|
||||
|
||||
Reference in new issue
Block a user