mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scope SVG event script drafts to their selected artwork
This commit is contained in:
1 parent
7d5d4039f6
commit
cc4542305a
1 file changed
+25
-9
@@ -42,6 +42,7 @@ pub(crate) struct SvgInteractivityPanel {
|
||||
pub(crate) struct Info {
|
||||
has: bool,
|
||||
events: Option<Vec<(String, String)>>,
|
||||
script_target: Option<egui::IdSalt>,
|
||||
}
|
||||
|
||||
/// What [`info`] was read for: (document uid, revision, selection size, first and last selected object).
|
||||
@@ -76,7 +77,12 @@ pub(crate) fn info(app: &mut VectorcraftApp) -> Info {
|
||||
let events = r["events"]
|
||||
.as_array()
|
||||
.map(|a| a.iter().filter_map(|e| Some((e["event"].as_str()?.to_string(), e["script"].as_str()?.to_string()))).collect::<Vec<_>>());
|
||||
let v = Info { has: r["ids"].as_array().is_some_and(|a| !a.is_empty()), events };
|
||||
let mut targets: Vec<u64> = r["ids"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect();
|
||||
targets.sort_unstable();
|
||||
let has = !targets.is_empty();
|
||||
// The editor's draft belongs to the actual targets, even if the selection is reordered.
|
||||
let script_target = has.then(|| egui::IdSalt::new((key.0, targets)));
|
||||
let v = Info { has, events, script_target };
|
||||
app.canvas.svg_info = Some((key, v.clone()));
|
||||
v
|
||||
}
|
||||
@@ -106,7 +112,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
.lines(3)
|
||||
.submits()
|
||||
.submit_key(SubmitKey::Enter)
|
||||
.id_salt(event)
|
||||
.id_salt((event, info.script_target))
|
||||
.enabled(info.has)
|
||||
.show(ui);
|
||||
typed = out.events.committed().filter(|js| js.as_str() != shown).cloned();
|
||||
@@ -192,23 +198,33 @@ mod tests {
|
||||
}
|
||||
let mut panel = Panel::new(app, show);
|
||||
let field = caption("JavaScript:");
|
||||
let multiline = craft::ui::contract::Role::MULTILINE_TEXT_INPUT;
|
||||
let doc = panel.app().session.active().unwrap().uid;
|
||||
panel.click_role(field, craft::ui::contract::Role::TEXT_INPUT);
|
||||
panel.click_role(field, multiline);
|
||||
panel.type_text("1;");
|
||||
assert_eq!(panel.value(field).as_deref(), Some("1;"));
|
||||
assert_eq!(panel.value_role(field, multiline).as_deref(), Some("1;"));
|
||||
|
||||
panel.app().run("window.nextDocument", json!({})).unwrap();
|
||||
panel.frame();
|
||||
assert_ne!(panel.app().session.active().unwrap().uid, doc);
|
||||
assert_eq!(panel.value(field).as_deref(), Some(""));
|
||||
assert_eq!(panel.value_role(field, multiline).as_deref(), Some(""));
|
||||
assert_eq!(run(&mut panel.app(), "svgInteractivity.info", json!({}))["events"], json!([]));
|
||||
|
||||
panel.click_role(field, craft::ui::contract::Role::TEXT_INPUT);
|
||||
panel.click_role(field, multiline);
|
||||
panel.type_text("2;");
|
||||
run(&mut panel.app(), "shape.ellipse", json!({ "x": 0, "y": 0, "width": 10, "height": 20 }));
|
||||
let first = panel.app().session.active().unwrap().selection.objects[0].0;
|
||||
let second = run(&mut panel.app(), "shape.ellipse", json!({ "x": 0, "y": 0, "width": 10, "height": 20 }))["id"].clone();
|
||||
panel.frame();
|
||||
assert_eq!(panel.value(field).as_deref(), Some(""));
|
||||
assert_eq!(panel.value_role(field, multiline).as_deref(), Some(""));
|
||||
assert_eq!(run(&mut panel.app(), "svgInteractivity.info", json!({}))["events"], json!([]));
|
||||
|
||||
run(&mut panel.app(), "select.set", json!({ "ids": [first, second] }));
|
||||
panel.frame();
|
||||
panel.click_role(field, multiline);
|
||||
panel.type_text("3;");
|
||||
run(&mut panel.app(), "select.set", json!({ "ids": [second, first] }));
|
||||
panel.frame();
|
||||
assert_eq!(panel.value_role(field, multiline).as_deref(), Some("3;"));
|
||||
}
|
||||
|
||||
fn run(app: &mut VectorcraftApp, id: &str, p: Value) -> Value {
|
||||
@@ -222,7 +238,7 @@ mod tests {
|
||||
run(&mut app, "shape.rectangle", json!({"x": 0, "y": 0, "width": 50, "height": 50}));
|
||||
let mut panel = Panel::new(app, show);
|
||||
|
||||
panel.click_role(caption("JavaScript:"), craft::ui::contract::Role::TEXT_INPUT);
|
||||
panel.click_role(caption("JavaScript:"), craft::ui::contract::Role::MULTILINE_TEXT_INPUT);
|
||||
panel.type_text("1;");
|
||||
panel.key(egui::Key::Enter, egui::Modifiers::SHIFT);
|
||||
panel.type_text("2;");
|
||||
|
||||
Reference in new issue
Block a user