Skip to content

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

Address CodeRabbit review: doc.replaceSmartObject restores the active document (#8150)

David Mann4604cfc4bb9a
diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs
index 738b99843..7348a8cbe 100644
--- a/crates/automation/src/headless.rs
+++ b/crates/automation/src/headless.rs
@@ -156,24 +156,22 @@ impl Headless {
/// `layer.smartObjects.replaceContents`, which takes an ambient `path` and is refused in
/// automation (#8061). `params` is the command's own (`layer`).
pub fn replace_smart_object(&mut self, index: Option<usize>, path: &str, params: &Value) -> Result<Value, AutomationError> {
- let i = self.doc_index(index)?;
- if self.session.active_index() != Some(i) && !self.session.set_active(i) {
- return Err(AutomationError::BadRequest(format!("no document at index {i}")));
- }
- let mut opts = params.as_object().cloned().unwrap_or_default();
- opts.insert("path".into(), json!(path));
- if matches!(&self.filesystem, Filesystem::TrustedLocal) {
- return self.command_run("layer.smartObjects.replaceContents", Value::Object(opts));
- }
- let bytes = match &self.filesystem {
- Filesystem::Workspace(workspace) => workspace.read(path)?,
- _ => {
- return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
+ self.on_doc(index, |this| {
+ let mut opts = params.as_object().cloned().unwrap_or_default();
+ opts.insert("path".into(), json!(path));
+ if matches!(&this.filesystem, Filesystem::TrustedLocal) {
+ return this.command_run("layer.smartObjects.replaceContents", Value::Object(opts));
}
- };
- let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
- self.sync_jobs();
- Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut self.session, &Value::Object(opts), name, bytes)?)
+ let bytes = match &this.filesystem {
+ Filesystem::Workspace(workspace) => workspace.read(path)?,
+ _ => {
+ return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into()));
+ }
+ };
+ let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path);
+ this.sync_jobs();
+ Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut this.session, &Value::Object(opts), name, bytes)?)
+ })
}
/// Export each exported layer of a document as its own file beneath the write root: the
diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs
index 19de89e7c..0b388ff76 100644
--- a/crates/automation/src/rpc.rs
+++ b/crates/automation/src/rpc.rs
@@ -880,7 +880,7 @@ mod tests {
}
#[test]
- fn indexed_place_and_data_set_methods_keep_the_active_document() {
+ fn indexed_place_replace_and_data_set_methods_keep_the_active_document() {
let base = std::env::temp_dir().join(format!("pc-doc-index-active-{}", std::process::id()));
let root = base.join("workspace");
std::fs::create_dir_all(&root).unwrap();
@@ -897,6 +897,9 @@ mod tests {
let steps = [
("doc.place", json!({"path": "logo.png", "index": 1}), true),
+ // Document 1's active layer is now the placed Smart Object.
+ ("doc.replaceSmartObject", json!({"path": "logo.png", "index": 1}), true),
+ ("doc.replaceSmartObject", json!({"path": "missing.png", "index": 1}), false),
("doc.importDataSets", json!({"path": "sets.csv", "index": 1}), true),
("doc.exportLayers", json!({"dir": "layers", "index": 1}), true),
("doc.exportDataSets", json!({"dir": "sets", "index": 1}), true),