Skip to content

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

Type: every text edit respects Lock All (#4188) (#4189)

David Mann60c825513c7b
diff --git a/crates/engine/src/edit_menu_cmds.rs b/crates/engine/src/edit_menu_cmds.rs
index 4933ed93f..734f7a1b5 100644
--- a/crates/engine/src/edit_menu_cmds.rs
+++ b/crates/engine/src/edit_menu_cmds.rs
@@ -894,6 +894,7 @@ fn find_replace(s: &mut Session, p: &Value) -> Result<Value> {
&& let Some((id, a, b)) = cursor
&& text_of(&doc, id).is_some_and(|t| find_matches(&t, &find, case, whole).contains(&(a, b)))
{
+ crate::type_cmds::check_text_unlocked(&doc, id)?;
s.edit("Replace Text", |doc, _| {
let snapshot = doc.clone();
if let Some(LayerContent::Text(t)) = doc.layer_mut(id).map(|l| &mut l.content) {
diff --git a/crates/engine/src/edit_menu_cmds/tests.rs b/crates/engine/src/edit_menu_cmds/tests.rs
index 0a1bab463..1bede8725 100644
--- a/crates/engine/src/edit_menu_cmds/tests.rs
+++ b/crates/engine/src/edit_menu_cmds/tests.rs
@@ -446,6 +446,27 @@ fn find_and_replace_across_type_layers() {
assert!(s.execute("edit.findAndReplaceText", json!({"find": ""})).is_err());
}
+#[test]
+fn find_and_replace_change_rejects_a_layer_locked_after_the_find() {
+ // #4188: the found match's layer gets Lock All before Change; the text must stay.
+ let mut s = session(8);
+ let id = s.execute("type.create", json!({"text": "abc", "x": 2, "y": 12})).unwrap()["layer"].as_u64().unwrap();
+ // A second, unlocked type layer keeps the command enabled.
+ s.execute("type.create", json!({"text": "zzz", "x": 2, "y": 40})).unwrap();
+ let found = s.execute("edit.findAndReplaceText", json!({"find": "abc", "action": "find"})).unwrap();
+ assert_eq!(found["found"]["layer"].as_u64(), Some(id));
+ s.execute("layer.setProps", json!({"layer": id, "locked": true})).unwrap();
+ let history = s.active().unwrap().history.past_len();
+ let result = s.execute("edit.findAndReplaceText", json!({"find": "abc", "replace": "x", "action": "change"}));
+ assert!(matches!(&result, Err(EngineError::Other(message)) if message.contains("locked")), "{result:?}");
+ let text = match &s.active().unwrap().doc.layer(LayerId(id)).unwrap().content {
+ LayerContent::Text(t) => t.text.clone(),
+ _ => String::new(),
+ };
+ assert_eq!(text, "abc");
+ assert_eq!(s.active().unwrap().history.past_len(), history);
+}
+
#[test]
fn find_in_the_active_layer_finds_nothing_when_it_is_not_type() {
// #703: `allLayers: false` with a raster layer active leaves nothing to search.
diff --git a/crates/engine/src/type_cmds.rs b/crates/engine/src/type_cmds.rs
index 4a4cb347b..a53b6baba 100644
--- a/crates/engine/src/type_cmds.rs
+++ b/crates/engine/src/type_cmds.rs
@@ -466,17 +466,21 @@ pub fn refresh(doc: &Document, t: &mut TextLayer) {
t.psd_raw = Some(Arc::new(photocraft_text::psd::build_tysh(t, dpi, layout.bounds())));
}
+/// Refuses to change a type layer under Lock All (its own or an enclosing group's), with
+/// Photoshop's message. Every command that edits a type layer's text calls it first (#3132, #4188).
+pub(crate) fn check_text_unlocked(doc: &Document, id: LayerId) -> Result<()> {
+ let layer = doc.layer(id).ok_or(EngineError::NoLayer(id))?;
+ if doc.effective_locks(id).all {
+ return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", layer.name)));
+ }
+ Ok(())
+}
+
/// Runs `f` on a type layer as one undo step. `f` may set an explicit layer name (`type.edit`'s
/// `name`), which is applied in the same step; otherwise an auto-named layer follows its text.
fn with_text_layer<R>(s: &mut Session, p: &Value, label: &str, f: impl FnOnce(&mut TextLayer, &Document, &mut Option<String>) -> Result<R>) -> Result<R> {
let id = layer_id(s, p)?;
- {
- let doc = &s.active().ok_or(EngineError::NoDocument)?.doc;
- let layer = doc.layer(id).ok_or(EngineError::NoLayer(id))?;
- if doc.effective_locks(id).all {
- return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", layer.name)));
- }
- }
+ check_text_unlocked(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?;
let (r, damage) = s.edit(label, |doc, _| {
let snapshot = doc.clone();
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rs
index e460a975b..a11db9976 100644
--- a/crates/engine/src/type_extra_cmds.rs
+++ b/crates/engine/src/type_extra_cmds.rs
@@ -14,7 +14,9 @@ use photocraft_text::render::PathEl;
use serde_json::{Value, json};
use crate::commands::{CommandSpec, layer_param};
-use crate::type_cmds::{format_targets, has_format_target, is_auto_named, layer_name, refresh, replace_text, style_paragraphs, style_range, with_text_targets};
+use crate::type_cmds::{
+ check_text_unlocked, format_targets, has_format_target, is_auto_named, layer_name, refresh, replace_text, style_paragraphs, style_range, with_text_targets,
+};
use crate::{EngineError, Result, Session};
/// Photoshop's Paste Lorem Ipsum filler.
@@ -67,6 +69,7 @@ fn has_defaults(s: &Session) -> std::result::Result<(), String> {
/// Edits a type layer as one history step and re-renders it.
fn with_text<R>(s: &mut Session, p: &Value, label: &str, f: impl FnOnce(&mut TextLayer, &Document) -> Result<R>) -> Result<R> {
let id = layer_param(s, p)?;
+ check_text_unlocked(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?;
s.edit(label, |doc, _| {
let snapshot = doc.clone();
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
diff --git a/crates/engine/src/type_extra_cmds/tests.rs b/crates/engine/src/type_extra_cmds/tests.rs
index 993ae41e8..395128a44 100644
--- a/crates/engine/src/type_extra_cmds/tests.rs
+++ b/crates/engine/src/type_extra_cmds/tests.rs
@@ -282,3 +282,18 @@ fn rasterize_type_layer() {
assert!(matches!(s.active().unwrap().doc.layer(id).unwrap().content, LayerContent::Raster(_)));
assert!(!s.is_enabled("type.rasterizeTypeLayer"));
}
+
+#[test]
+fn all_locked_layer_rejects_paste_lorem_ipsum_without_history() {
+ // #4188: Lock All stopped type.edit (#3132) but not Paste Lorem Ipsum into the layer.
+ let mut s = session(8);
+ let id = text_layer(&mut s, "Hi");
+ s.execute("layer.setProps", json!({"layer": id.0, "locked": true})).unwrap();
+ let history = s.active().unwrap().history.past_len();
+ for params in [json!({}), json!({"layer": id.0})] {
+ let result = s.execute("type.pasteLoremIpsum", params);
+ assert!(matches!(&result, Err(EngineError::Other(message)) if message.contains("locked")), "{result:?}");
+ assert_eq!(text(&s, id).text, "Hi");
+ assert_eq!(s.active().unwrap().history.past_len(), history);
+ }
+}
diff --git a/crates/engine/src/type_spell_cmds.rs b/crates/engine/src/type_spell_cmds.rs
index cbebb0664..12a63a364 100644
--- a/crates/engine/src/type_spell_cmds.rs
+++ b/crates/engine/src/type_spell_cmds.rs
@@ -16,7 +16,7 @@ use photocraft_text::spell::Dictionary;
use serde_json::{Value, json};
use crate::commands::{CommandSpec, layer_param};
-use crate::type_cmds::{norm_text, refresh, replace_text};
+use crate::type_cmds::{check_text_unlocked, norm_text, refresh, replace_text};
use crate::{EngineError, Result, Session};
fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
@@ -94,6 +94,7 @@ fn change(s: &mut Session, p: &Value) -> Result<Value> {
let to = p.get("replace").and_then(Value::as_str).ok_or_else(|| bad(cmd, "`change` needs `replace`"))?.to_string();
let doc = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let t = text(&doc, id).ok_or_else(|| bad(cmd, format!("layer {} is not a type layer", id.0)))?;
+ check_text_unlocked(&doc, id)?;
let (a, b) = (byte_at(t, start), byte_at(t, end.max(start)));
if let Some(w) = p.get("word").and_then(Value::as_str)
&& &t[a..b] != w
@@ -199,6 +200,7 @@ fn insert_text(s: &mut Session, p: &Value) -> Result<Value> {
let new = norm_text(&new);
let doc = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
let t = text(&doc, id).ok_or_else(|| bad(cmd, format!("layer {} is not a type layer", id.0)))?;
+ check_text_unlocked(&doc, id)?;
let n = t.chars().count();
let (a, b) = match (p.get("range").and_then(Value::as_array), p.get("at").and_then(Value::as_u64)) {
(Some(r), _) if r.len() == 2 => {
@@ -272,6 +274,28 @@ mod tests {
text(&s.active().unwrap().doc, LayerId(id)).unwrap().to_string()
}
+ #[test]
+ fn all_locked_layer_rejects_insert_text_and_spelling_change_without_history() {
+ // #4188: Lock All stopped type.edit (#3132) but not these two text edits.
+ let (mut s, id) = session("Teh");
+ // A second, unlocked type layer keeps Check Spelling enabled.
+ s.execute("type.create", json!({"x": 10, "y": 90, "text": "zzz", "size": 12})).unwrap();
+ s.execute("layer.setProps", json!({"layer": id, "locked": true})).unwrap();
+ let history = s.active().unwrap().history.past_len();
+ for (cmd, params) in [
+ ("type.insertText", json!({"layer": id, "text": "b"})),
+ ("edit.checkSpelling", json!({"action": "change", "layer": id, "start": 0, "end": 3, "word": "Teh", "replace": "The"})),
+ ] {
+ let result = s.execute(cmd, params);
+ assert!(matches!(&result, Err(EngineError::Other(message)) if message.contains("locked")), "{cmd}: {result:?}");
+ assert_eq!(text_of(&s, id), "Teh", "{cmd}");
+ assert_eq!(s.active().unwrap().history.past_len(), history, "{cmd}");
+ }
+ s.execute("layer.setProps", json!({"layer": id, "locked": false})).unwrap();
+ s.execute("type.insertText", json!({"layer": id, "text": "b"})).unwrap();
+ assert_eq!(text_of(&s, id), "Tehb");
+ }
+
#[test]
fn lists_changes_and_learns_words() {
let (mut s, id) = session("Teh quick brwon fox, teh end. Photocraft");