Retain the shared-set conditional command shape

This commit is contained in:
yoriswind committed 2026-11-04 09:29:52 -08:00
1 parent 73e625882c
commit 3ed156b67d
3 files changed
+28 -4

No files matched your search

+1 -1
View File
@@ -859,7 +859,7 @@ pub fn moved_index(index: usize, from: usize, to: usize) -> usize {
}
}
/// Remove the resolved action while retaining the identities of unaffected definitions and calls.
/// Delete the selected set, action or step with the appropriate playback and recording guards.
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
not_playing(s, "actions.delete")?;
if p.get("set").is_some() {
+14 -3
View File
@@ -113,9 +113,20 @@ pub(super) fn conditional_from(d: &Descriptor) -> Mapped {
_ => None,
};
let mut params = json!({"if": condition, "then": branch("then"), "else": branch("else")});
for key in ["then", "else"] {
if let Some(set) = get_ref(d, key).and_then(|reference| super::steps_layer::action_set(reference)) {
params[format!("{key}Set")] = json!(set);
let scopes = ["then", "else"].map(|key| (key, get_ref(d, key).and_then(|reference| super::steps_layer::action_set(reference))));
let shared = scopes
.iter()
.filter(|(key, _)| params[*key].is_string())
.find_map(|(_, set)| set.as_deref())
.filter(|set| scopes.iter().all(|(key, scope)| !params[*key].is_string() || scope.as_deref() == Some(*set)));
if let Some(set) = shared {
// Preserve the existing command shape when every active branch has the same scope.
params["set"] = json!(set);
} else {
for (key, set) in scopes {
if let Some(set) = set {
params[format!("{key}Set")] = json!(set);
}
}
}
step("actions.conditional", params)
+13
View File
@@ -122,3 +122,16 @@ fn portable_conditional_branches_retain_independent_set_scopes() {
assert_eq!(session.active().unwrap().doc.layers.last().unwrap().name, expected);
}
}
/// Retaining set references must not invent per-branch fields for the existing shared-set API.
#[test]
fn portable_conditional_shared_scope_keeps_the_existing_command_shape() {
use photocraft_engine::atn_cmds;
for other in [serde_json::Value::Null, json!("Else")] {
let params = json!({"if":"always","then":"Then","else":other,"set":"S"});
let action = Action::new("Caller", vec![("actions.conditional".into(), params.clone())]);
let bytes = atn_cmds::export("S", &[&action], &[], false).unwrap().0;
let imported = atn_cmds::import(&bytes).unwrap();
assert_eq!(imported.actions[0].steps[0].1, params);
}
}