Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Edit Contents switches to the already-open contents document (#1037)

jason4short7e40b40fb071
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index 8d6e31ffd..ca097122c 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -646,6 +646,12 @@ fn edit_contents(s: &mut Session, p: &Value) -> Result<Value> {
let id = layer_param(s, p)?;
let st = s.active().ok_or(EngineError::NoDocument)?;
let parent = st.doc.id;
+ // Already open for editing: switch to that document rather than opening another copy.
+ if let Some(index) = s.smart_links.iter().find(|l| l.parent == parent && l.layer == id).and_then(|l| s.documents().iter().position(|d| d.doc.id == l.child))
+ {
+ s.set_active(index);
+ return Ok(json!({"document": index, "parentLayer": id.0}));
+ }
let (name, bytes) = source_bytes(&st.doc.metadata, &smart(&st.doc, id)?.source).ok_or_else(|| other("the smart object's contents are unavailable"))?;
let mut child = decode_source(&name, &bytes)?;
// Bundles keep their document id; each open copy needs its own.
diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs
index 7330fc9e3..ac00eb347 100644
--- a/crates/engine/src/smart_cmds/tests.rs
+++ b/crates/engine/src/smart_cmds/tests.rs
@@ -320,6 +320,10 @@ fn edit_contents_updates_the_parent() {
s.undo();
assert_eq!(flat(&s), before, "the update is one undoable step in the parent");
s.redo();
+ // Editing the same smart object again switches to the open document, no second copy.
+ let open = s.documents().len();
+ let again = s.execute("layer.smartObjects.editContents", json!({})).unwrap();
+ assert_eq!((again["document"].as_u64(), s.active_index(), s.documents().len()), (Some(child as u64), Some(child), open));
// Closing an edited contents document also commits it.
s.set_active(child);
s.edit("paint2", |doc, _| {