mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Validate generator parameters and include mask art in opening warnings
This commit is contained in:
4 files changed
+50
-3
No files matched your search
@@ -83,7 +83,7 @@ fn changed(g: &GeneratedArt) -> String {
|
||||
/// damaged, and those whose art doesn't match its file's hash. Opening runs no plug-in.
|
||||
pub fn open_notes(doc: &Document) -> Vec<String> {
|
||||
let mut notes: Vec<String> = vec![];
|
||||
doc.walk(|n| {
|
||||
let mut visit = |n: &Node| {
|
||||
let Some(g) = n.extras().generated.as_deref() else { return };
|
||||
let note = match g.state {
|
||||
State::Damaged => DAMAGED.to_string(),
|
||||
@@ -97,7 +97,10 @@ pub fn open_notes(doc: &Document) -> Vec<String> {
|
||||
if !notes.contains(¬e) {
|
||||
notes.push(note);
|
||||
}
|
||||
});
|
||||
};
|
||||
for layer in &doc.layers {
|
||||
vectorcraft_doc::swatches::walk_with_masks(layer, &mut visit);
|
||||
}
|
||||
notes
|
||||
}
|
||||
|
||||
|
||||
@@ -273,6 +273,21 @@ fn a_missing_plug_in_keeps_the_art_and_is_named() {
|
||||
assert_eq!(art(&s, id), ["Big", "Line"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_generator_in_an_opacity_mask_is_named_on_open() {
|
||||
let mut s = Session::new();
|
||||
sizes(&mut s, "test.gen.mask.gone");
|
||||
s.execute("file.new", &json!({"width": 300, "height": 300})).unwrap();
|
||||
let target = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap()["id"].clone();
|
||||
let mask = s.execute("plugin.generate", &json!({"id": "test.gen.mask.gone"})).unwrap()["id"].clone();
|
||||
s.execute("transparency.makeOpacityMask", &json!({"ids": [target, mask]})).unwrap();
|
||||
let bytes = vectorcraft_format::save(&s.doc().unwrap().doc, true);
|
||||
s.execute("plugin.remove", &json!({"id": "test.gen.mask.gone"})).unwrap();
|
||||
let warnings = reopen_bytes(&mut s, &bytes);
|
||||
assert_eq!(warnings.len(), 1, "{warnings:?}");
|
||||
assert!(warnings[0].contains("test.gen.mask.gone") && warnings[0].contains("isn't installed"), "{warnings:?}");
|
||||
}
|
||||
|
||||
/// The saved native text `bytes` with the generated art object's JSON changed by `f`.
|
||||
fn tampered(bytes: &[u8], f: impl FnOnce(&mut Value)) -> Vec<u8> {
|
||||
fn find(v: &mut Value) -> Option<&mut Value> {
|
||||
|
||||
@@ -562,7 +562,10 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
match m.kind {
|
||||
Kind::Filter => {}
|
||||
Kind::Generator => {
|
||||
let given = p.get("params").filter(|v| v.as_object().is_some_and(|o| !o.is_empty()));
|
||||
let given = p.get("params");
|
||||
if given.is_some_and(|value| !value.is_object()) {
|
||||
return Err(vectorcraft_plugins::Error::Params("plug-in parameters must be a JSON object".into()).to_string());
|
||||
}
|
||||
if m.params.is_empty() || given.is_some() {
|
||||
return app.run(GENERATE, json!({"id": id, "params": given.cloned().unwrap_or(Value::Null)}));
|
||||
}
|
||||
|
||||
@@ -95,6 +95,32 @@ fn object_plug_ins_draws_and_edits_generated_art() {
|
||||
assert_eq!(art(&app, id), ["Big"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generator_dialog_validates_supplied_parameters_and_runs_an_empty_object() {
|
||||
for (id, with_parameters) in [("test.ui.generator.params", true), ("test.ui.generator.no_params", false)] {
|
||||
let mut app = app(id);
|
||||
if !with_parameters {
|
||||
let wasm = wat::parse_str(tpl::returning(&tpl::generator(id, "{}"), SMALL)).unwrap();
|
||||
app.run("plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "replace": true})).unwrap();
|
||||
}
|
||||
app.run("file.new", json!({"width": 300, "height": 300})).unwrap();
|
||||
let before = app.session.doc().unwrap().doc.clone();
|
||||
let undo = steps(&app);
|
||||
for params in [json!([]), json!("bad"), json!(false), json!(7), serde_json::Value::Null] {
|
||||
assert!(app.run("plugin.dialog", json!({"id": id, "params": params})).is_err(), "{params}");
|
||||
assert_eq!(app.session.doc().unwrap().doc, before);
|
||||
assert_eq!(steps(&app), undo);
|
||||
assert!(kind(&app).is_none());
|
||||
}
|
||||
app.run("plugin.dialog", json!({"id": id, "params": {}})).unwrap();
|
||||
assert!(kind(&app).is_none());
|
||||
assert_eq!(steps(&app), undo + 1);
|
||||
let selected = app.session.doc().unwrap().selection.objects[0].0;
|
||||
assert_eq!(art(&app, selected), ["Small"]);
|
||||
app.run("plugin.remove", json!({"id": id})).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Edit Generated Art… is for one selected object: a generated art object selected with another
|
||||
/// object is not. OK in its dialog draws the object of the document it was opened in: after another
|
||||
/// document comes to the front, OK draws nothing there, closes the dialog and says why.
|
||||
|
||||
Reference in new issue
Block a user