Skip to content

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

Type: Paste Lorem Ipsum inserts into the type layer named by "layer" (#5659) (#5660)

David Mann5a9fb94b9b88
diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rs
index efa2d791c..91d75f033 100644
--- a/crates/engine/src/type_extra_cmds.rs
+++ b/crates/engine/src/type_extra_cmds.rs
@@ -13,7 +13,7 @@ use photocraft_geom::Point;
use photocraft_text::render::PathEl;
use serde_json::{Value, json};
-use crate::commands::{CommandSpec, layer_param};
+use crate::commands::{CommandSpec, explicit_layer, layer_param};
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,
};
@@ -477,7 +477,10 @@ fn toggle_opentype(s: &mut Session, p: &Value, tag: &'static str) -> Result<Valu
// ---------- lorem ipsum / update / fonts / defaults ----------
fn paste_lorem(s: &mut Session, p: &Value) -> Result<Value> {
- if has_text(s).is_ok() && p.get("new").and_then(Value::as_bool) != Some(true) {
+ // Into the layer `with_text` edits: an explicit "layer" (an error unless it is a type layer),
+ // else the active layer when it is a type layer.
+ let new = p.get("new").and_then(Value::as_bool) == Some(true);
+ if !new && (explicit_layer(s, p)?.is_some() || has_text(s).is_ok()) {
let caret = with_text(s, p, "Paste Lorem Ipsum", |t, _| {
let at = p.get("at").and_then(Value::as_u64).map_or(t.text.len(), |c| byte_index(&t.text, c as usize));
replace_text(t, at, at, LOREM_IPSUM);
diff --git a/crates/engine/src/type_extra_cmds/tests.rs b/crates/engine/src/type_extra_cmds/tests.rs
index 7386927bb..d87d4370a 100644
--- a/crates/engine/src/type_extra_cmds/tests.rs
+++ b/crates/engine/src/type_extra_cmds/tests.rs
@@ -329,6 +329,27 @@ fn all_locked_layer_rejects_paste_lorem_ipsum_without_history() {
}
}
+#[test]
+fn paste_lorem_ipsum_inserts_into_the_layer_param_not_the_active_layer() {
+ // #5659: with a pixel layer active, an explicit type "layer" got a new text box instead.
+ let mut s = session(8);
+ let id = text_layer(&mut s, "Hi");
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let pixel = s.active().unwrap().active_layer.unwrap();
+ assert_ne!(pixel, id);
+ let count = s.active().unwrap().doc.walk().len();
+ let r = s.execute("type.pasteLoremIpsum", json!({"layer": id.0, "at": 0})).unwrap();
+ assert!(text(&s, id).text.starts_with(LOREM_IPSUM), "{}", text(&s, id).text);
+ assert!(text(&s, id).text.ends_with("Hi"));
+ assert_eq!(r["caret"], LOREM_IPSUM.chars().count());
+ assert_eq!(s.active().unwrap().doc.walk().len(), count);
+ // An explicit layer that is not a type layer is an error, not a new layer.
+ let history = s.active().unwrap().history.past_len();
+ assert!(s.execute("type.pasteLoremIpsum", json!({"layer": pixel.0})).is_err());
+ assert_eq!(s.active().unwrap().doc.walk().len(), count);
+ assert_eq!(s.active().unwrap().history.past_len(), history);
+}
+
/// A type layer stored with a family label that is not installed, as a PSD keeps the family it
/// guessed from a PostScript name whose face was absent when the file was opened.
fn guessed_family_layer(s: &mut Session, text: &str, ps: Option<&str>) -> (LayerId, Value) {