mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Keep recorded layer targets portable across documents
This commit is contained in:
1 parent
21c0444559
commit
03799b62bc
11 files changed
+70
-41
No files matched your search
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use photocraft_doc::{Document, LayerContent, LayerId};
|
||||
use photocraft_doc::{DocId, Document, LayerContent, LayerId};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::Session;
|
||||
@@ -63,10 +63,11 @@ pub(crate) fn portable(id: &str, mut params: Value, before: &Before) -> Value {
|
||||
/// The text layer a journaled `type.create` made or `type.edit` changed, so the keystrokes that
|
||||
/// follow fold into that entry only when they type into the same layer (the Type tool selects
|
||||
/// a clicked layer without a command, so the journal alone cannot tell).
|
||||
pub(crate) fn typed_layer(s: &Session, id: &str, params: &Value) -> Option<LayerId> {
|
||||
pub(crate) fn typed_layer(s: &Session, id: &str, params: &Value) -> Option<(DocId, LayerId)> {
|
||||
let doc_id = s.active()?.doc.id;
|
||||
match id {
|
||||
"type.create" => s.active()?.active_layer,
|
||||
"type.edit" => crate::commands::layer_param(s, params).ok(),
|
||||
"type.create" => s.active()?.active_layer.map(|layer| (doc_id, layer)),
|
||||
"type.edit" => crate::commands::layer_param(s, params).ok().map(|layer| (doc_id, layer)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -83,7 +84,7 @@ pub(crate) enum Keystroke {
|
||||
|
||||
/// Folds a Type-tool keystroke into the journal entry before it (see the module doc), or turns
|
||||
/// it into a whole-text step. `previous` is the layer the last entry typed into.
|
||||
pub(crate) fn fold_keystroke(s: &mut Session, id: &str, params: &Value, previous: Option<LayerId>) -> Keystroke {
|
||||
pub(crate) fn fold_keystroke(s: &mut Session, id: &str, params: &Value, previous: Option<(DocId, LayerId)>) -> Keystroke {
|
||||
if id != "type.edit" {
|
||||
return Keystroke::None;
|
||||
}
|
||||
@@ -107,7 +108,7 @@ pub(crate) fn fold_keystroke(s: &mut Session, id: &str, params: &Value, previous
|
||||
step.insert("text".into(), Value::String(text.clone()));
|
||||
Keystroke::Step(Value::Object(step))
|
||||
};
|
||||
if previous != Some(target) {
|
||||
if previous != s.active().map(|st| (st.doc.id, target)) {
|
||||
return whole();
|
||||
}
|
||||
// A new recording starts at a journal boundary. Its first edit cannot change an entry
|
||||
|
||||
@@ -158,6 +158,25 @@ fn keystrokes_into_another_layer_do_not_fold() {
|
||||
assert!(last(&s).1["layer"].is_string() && last(&s).1.get("replace").is_some(), "{}", last(&s).1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keystrokes_in_another_document_do_not_fold_into_the_previous_document() {
|
||||
let mut s = session();
|
||||
let first = s.execute("type.create", json!({"x": 2, "y": 20, "text": "A", "size": 12})).unwrap()["layer"].as_u64().unwrap();
|
||||
let mut copy = s.active().unwrap().doc.as_ref().clone();
|
||||
if let Some(layer) = copy.layer_mut(LayerId(first))
|
||||
&& let photocraft_doc::LayerContent::Text(text) = &mut layer.content
|
||||
{
|
||||
text.text = "B".into();
|
||||
}
|
||||
s.add_document(copy, None);
|
||||
let second = first;
|
||||
let before = s.journal.len();
|
||||
s.execute("type.edit", json!({"layer": second, "replace": {"start": 1, "end": 1, "text": "!"}, "coalesce": "typing"})).unwrap();
|
||||
assert_eq!(s.journal.len(), before + 1, "typing in a different document needs a new journal step");
|
||||
assert_eq!(s.journal[before - 1].1["text"], "A");
|
||||
assert_eq!(last(&s).1["text"], "B!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_recording_replays_on_a_document_with_other_layer_ids() {
|
||||
let mut s = session();
|
||||
@@ -193,3 +212,27 @@ fn a_recording_replays_on_a_document_with_other_layer_ids() {
|
||||
assert_eq!(text.as_deref(), Some("Hi"), "the typed text replayed as one step");
|
||||
assert!(s.documents()[0].doc.layer(LayerId(bg)).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recorded_edit_targets_a_named_nonactive_type_layer_on_another_document() {
|
||||
let mut s = session();
|
||||
let first = s.execute("type.create", json!({"x": 2, "y": 20, "text": "Old", "size": 12})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("layer.setProps", json!({"layer": first, "name": "Caption"})).unwrap();
|
||||
new_layer(&mut s, "Top");
|
||||
s.execute("actions.record", json!({"name": "Edit caption"})).unwrap();
|
||||
s.execute("type.edit", json!({"layer": first, "text": "New"})).unwrap();
|
||||
s.execute("actions.stop", json!({})).unwrap();
|
||||
let steps = s.execute("actions.get", json!({"action": "Edit caption"})).unwrap()["steps"].clone();
|
||||
assert_eq!(steps, json!([["type.edit", {"layer": "Caption", "text": "New"}]]));
|
||||
|
||||
s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
let second = s.execute("type.create", json!({"x": 2, "y": 20, "text": "Other", "size": 12})).unwrap()["layer"].as_u64().unwrap();
|
||||
s.execute("layer.setProps", json!({"layer": second, "name": "Caption"})).unwrap();
|
||||
new_layer(&mut s, "Top");
|
||||
assert_ne!(first, second);
|
||||
let played = s.execute("actions.play", json!({"action": "Edit caption"})).unwrap();
|
||||
assert!(played.get("failed").is_none(), "{played}");
|
||||
let layer = s.active().unwrap().doc.layer(LayerId(second)).unwrap();
|
||||
let photocraft_doc::LayerContent::Text(text) = &layer.content else { panic!("expected type layer") };
|
||||
assert_eq!(text.text, "New");
|
||||
}
|
||||
@@ -303,7 +303,7 @@ fn new_artboard(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
fn artboard_from_group(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id = p.get("layer").and_then(Value::as_u64).map(LayerId).or(d.active_layer).ok_or(EngineError::Other("no active layer".into()))?;
|
||||
let id = crate::commands::layer_param(s, p)?;
|
||||
let canvas = d.doc.bounds();
|
||||
let background = background_param(p, "layer.new.artboardFromGroup")?.unwrap_or_default();
|
||||
let rect = s.edit("Artboard from Group", |doc, active| {
|
||||
@@ -377,10 +377,7 @@ fn artboard_from_layers(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "layer.artboard.set";
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id = match p.get("layer").and_then(Value::as_u64) {
|
||||
Some(v) => LayerId(v),
|
||||
None => active_artboard(s).ok_or_else(|| bad(cmd, "no artboard selected"))?,
|
||||
};
|
||||
let id = crate::commands::explicit_layer(s, p)?.or_else(|| active_artboard(s)).ok_or_else(|| bad(cmd, "no artboard selected"))?;
|
||||
let old = d.doc.layer(id).and_then(Layer::artboard).cloned().ok_or_else(|| bad(cmd, "the layer is not an artboard"))?;
|
||||
let preset = p.get("preset").and_then(Value::as_str).map(str::to_string);
|
||||
let mut base = xywh(old.rect);
|
||||
@@ -441,10 +438,7 @@ fn set_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
fn add_beside(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "layer.artboard.addBeside";
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id = match p.get("layer").and_then(Value::as_u64) {
|
||||
Some(v) => LayerId(v),
|
||||
None => active_artboard(s).ok_or_else(|| bad(cmd, "no artboard selected"))?,
|
||||
};
|
||||
let id = crate::commands::explicit_layer(s, p)?.or_else(|| active_artboard(s)).ok_or_else(|| bad(cmd, "no artboard selected"))?;
|
||||
let src = d.doc.layer(id).and_then(Layer::artboard).cloned().ok_or_else(|| bad(cmd, "the layer is not an artboard"))?;
|
||||
let side = p.get("side").and_then(Value::as_str).unwrap_or("right");
|
||||
let duplicate = p.get("duplicate").and_then(Value::as_bool).unwrap_or(false);
|
||||
|
||||
@@ -124,10 +124,7 @@ fn new_frame(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(Some("ellipse")) => true,
|
||||
Some(_) => return Err(bad("`shape` must be \"rectangle\" or \"ellipse\"".into())),
|
||||
};
|
||||
let content = match p.get("layer") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(LayerId(v.as_u64().ok_or_else(|| bad(format!("`layer` = {v} is not a layer id")))?)),
|
||||
};
|
||||
let content = crate::commands::explicit_layer(s, p)?;
|
||||
let name = p.get("name").and_then(Value::as_str).map(str::to_string);
|
||||
let gid = s.edit("New Frame", |doc, active| {
|
||||
let mask =
|
||||
|
||||
@@ -24,10 +24,7 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {
|
||||
}
|
||||
|
||||
fn layer_id(s: &Session, p: &Value) -> Result<LayerId> {
|
||||
match p.get("layer").and_then(Value::as_u64) {
|
||||
Some(id) => Ok(LayerId(id)),
|
||||
None => s.active().and_then(|d| d.active_layer).ok_or(EngineError::Other("no active layer".into())),
|
||||
}
|
||||
crate::commands::layer_param(s, p)
|
||||
}
|
||||
|
||||
fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {
|
||||
|
||||
@@ -42,21 +42,21 @@ fn text_of(doc: &Document, id: LayerId) -> Option<&TextLayer> {
|
||||
}
|
||||
|
||||
/// Type layers a style command acts on: `layer`, `layers`, else the selected type layers.
|
||||
fn targets(s: &Session, p: &Value) -> Vec<LayerId> {
|
||||
let Some(st) = s.active() else { return Vec::new() };
|
||||
let ids: Vec<LayerId> = if let Some(id) = p.get("layer").and_then(Value::as_u64) {
|
||||
vec![LayerId(id)]
|
||||
fn targets(s: &Session, p: &Value) -> Result<Vec<LayerId>> {
|
||||
let Some(st) = s.active() else { return Ok(Vec::new()) };
|
||||
let ids: Vec<LayerId> = if let Some(id) = crate::commands::explicit_layer(s, p)? {
|
||||
vec![id]
|
||||
} else if let Some(a) = p.get("layers").and_then(Value::as_array) {
|
||||
a.iter().filter_map(Value::as_u64).map(LayerId).collect()
|
||||
} else {
|
||||
st.selected_layers()
|
||||
};
|
||||
ids.into_iter().filter(|id| text_of(&st.doc, *id).is_some()).collect()
|
||||
Ok(ids.into_iter().filter(|id| text_of(&st.doc, *id).is_some()).collect())
|
||||
}
|
||||
|
||||
fn has_target(s: &Session) -> std::result::Result<(), String> {
|
||||
has_doc(s)?;
|
||||
if targets(s, &Value::Null).is_empty() { Err("select a type layer".into()) } else { Ok(()) }
|
||||
if targets(s, &Value::Null).is_ok_and(|targets| !targets.is_empty()) { Ok(()) } else { Err("select a type layer".into()) }
|
||||
}
|
||||
|
||||
fn byte_at(text: &str, ci: usize) -> usize {
|
||||
@@ -317,7 +317,7 @@ type Target = (LayerId, Option<(usize, usize)>);
|
||||
/// Target list with byte ranges (`range` applies only to a single layer).
|
||||
fn target_ranges(s: &Session, p: &Value) -> Result<Vec<Target>> {
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let ids = targets(s, p);
|
||||
let ids = targets(s, p)?;
|
||||
if ids.is_empty() {
|
||||
return Err(EngineError::Other("no type layer to apply the style to".into()));
|
||||
}
|
||||
@@ -334,7 +334,7 @@ fn target_ranges(s: &Session, p: &Value) -> Result<Vec<Target>> {
|
||||
/// run's byte range.
|
||||
fn sample(s: &Session, p: &Value) -> Option<(CharStyle, ParagraphStyle, LayerId, (usize, usize))> {
|
||||
let st = s.active()?;
|
||||
let id = *targets(s, p).first()?;
|
||||
let id = *targets(s, p).ok()?.first()?;
|
||||
let t = text_of(&st.doc, id)?;
|
||||
let (a, _) = range_of(&t.text, p);
|
||||
let mut at = 0;
|
||||
|
||||
@@ -362,10 +362,7 @@ const MAX_WARP_SIDE: u32 = 100_000;
|
||||
|
||||
fn target(s: &Session, cmd: &str, p: &Value) -> Result<(LayerId, Rect)> {
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let id = match p.get("layer").and_then(Value::as_u64) {
|
||||
Some(v) => LayerId(v),
|
||||
None => st.active_layer.ok_or(EngineError::Other("no active layer".into()))?,
|
||||
};
|
||||
let id = crate::commands::layer_param(s, p)?;
|
||||
let layer = st.doc.layer(id).ok_or(EngineError::NoLayer(id))?;
|
||||
let rect = match p.get("rect").and_then(Value::as_array) {
|
||||
Some(r) if r.len() == 4 => {
|
||||
|
||||
@@ -200,7 +200,7 @@ pub struct Session {
|
||||
/// Log of executed commands (for action recording and debugging).
|
||||
pub journal: Vec<(String, Value)>,
|
||||
/// Text layer changed by the last journal entry, for folding Type tool keystrokes.
|
||||
typed_layer: Option<LayerId>,
|
||||
typed_layer: Option<(photocraft_doc::DocId, LayerId)>,
|
||||
/// Coalescing key of the command being executed.
|
||||
coalesce_request: Option<String>,
|
||||
/// Pixels copied with Edit › Copy / Cut (shared by all documents, like Photoshop).
|
||||
|
||||
@@ -152,7 +152,7 @@ store expose and persist the same setting; scripts keep using canonical command
|
||||
| `actions.list` | `{}` → `{actions:[{name, steps}], recording}` (the name being recorded, or null) |
|
||||
| `actions.get` | `{"action": name or index}` → `{name, steps:[[id, params], …], disabled:[step, …]}`. `steps` is the shape `file.automate.batch` and droplets take; `disabled` lists the 0-based steps playback skips |
|
||||
| `actions.record` | `{"name":"Red"}` starts a new action (default name `Action N`). `{"action": name or index}` appends to one that exists |
|
||||
| `actions.stop` | `{}` → `{action, steps}`. Copies replayable journal entries since `actions.record` (queries and action-editing commands omitted). For portable playback, an active `layer` id is dropped, other layers are named when unique (`layer.select` included), and Type tool keystrokes fold into a whole-text `type.create`/`type.edit` step. Ambiguous layer names keep the id and require a unique name for portable playback |
|
||||
| `actions.stop` | `{}` → `{action, steps}`. Copies replayable journal entries since `actions.record` (queries and action-editing commands omitted). For portable playback, an active `layer` id is dropped; other layers are recorded by their unique names, while `layer.select` records `{"name":"Layer","occurrence":0}`. Type tool keystrokes fold into a whole-text `type.create`/`type.edit` step within the same document and layer. Ambiguous layer names keep the id and require a unique name for portable playback |
|
||||
| `actions.play` | `{"action": name or index, "from": step?}`. `from` and `failed.step` are 0-based. Returns `{action, ran, failed?:{step, id, error}}` and still returns ok when a step fails, so a partial run is reported. Leaves one history step per step that ran. Supports nested action calls up to 16 levels, rejecting cycles. While recording, playing another action appends one named `actions.play` step instead of its expanded commands. On an untrusted session each step is authorized the same way as a top-level command |
|
||||
| `actions.move` | `{"action": name or index, "step": index?, "to": index}` moves a step within its action, or moves the whole action when `step` is omitted. `to` is the final zero-based index. Pending steps can be reordered while recording; invalid moves leave the list unchanged. |
|
||||
| `actions.delete` | `{"action": name or index, "step": index?}`. With a zero-based `step`: `{action, deletedStep, steps}`, also allowed while recording. Without `step`: `{deleted}`, refused while recording |
|
||||
|
||||
+2
-2
@@ -11,7 +11,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
|
||||
| [Tools](#tools) | 36 | 9 | 0 | |
|
||||
| [Workspace and UI](#workspace-and-ui) | 15 | 6 | 2 | settings that do nothing: 11 of 183 (target 0) |
|
||||
| [Type](#type) | 5 | 5 | 0 | |
|
||||
| [Automation](#automation) | 6 | 2 | 3 | 631 of 631 menu items live (100.0%) |
|
||||
| [Automation](#automation) | 5 | 3 | 3 | 631 of 631 menu items live (100.0%) |
|
||||
| [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 30 of 30 crates; panic_hunt present |
|
||||
| [Distribution](#distribution) | 5 | 5 | 1 | |
|
||||
| [Open bugs](#open-bugs) | 31 | 1 | 0 | |
|
||||
@@ -255,7 +255,7 @@ Menu wiring (`docs/parity-checklist.md`): 631 of 631 menu items live (100.0%). T
|
||||
| Id | Target | Status | Issue | Note |
|
||||
|---|---|---|---|---|
|
||||
| AUTO-1 | Every menu item dispatches a command id (UI, CLI, control channel and MCP share them) | done | | docs/parity-checklist.md: all menu items live; menus.rs is_live |
|
||||
| AUTO-219-1 | Record actions in a document-independent form (implicit layer, merged type keystrokes) | done | [#219](https://github.com/storytold/photocraft/issues/219) | actions_record.rs: the active layer's id is dropped, other layers are journaled by name (layer.select always), Type tool keystrokes fold into one type.create/type.edit with the whole text; replay test on a document with other layer ids |
|
||||
| AUTO-219-1 | Record actions in a document-independent form (implicit layer, merged type keystrokes) | partial | [#219](https://github.com/storytold/photocraft/issues/219) | actions_record.rs: active layer ids are dropped, uniquely named layers replay by name, and Type tool keystrokes fold per document and layer; full coverage of all numeric-only command receivers and ambiguous layer names is still open |
|
||||
| AUTO-219-2 | Actions panel: step and dialog toggles, reorder, delete a step, Stop, sets, .atn import | done | [#219](https://github.com/storytold/photocraft/issues/219) | 2026-10-12: set folders (collapsible, drag between sets, actions.assignSet), dialog toggle column (playback opens the step's dialog seeded with its values and resumes on OK; actions.dialog), Insert Stop… (actions.insert), step toggles, reorder, delete a step; Load/Save Actions (.atn) in the panel menu, CLI batch and actions.load/save (atn_cmds.rs): 90 Photoshop events map to commands both ways, the rest are kept verbatim and fail at playback by name; step toggles, reorder and delete a step done; Stop steps play (actions.stopMessage) but there is no Insert Stop menu item; sets and dialog toggles are stored and round-trip but the panel neither groups by set nor edits the dialog toggle |
|
||||
| AUTO-219-3 | actions.record/stop/play/list commands for agents and the CLI | done | [#219](https://github.com/storytold/photocraft/issues/219) | actions.list/get/record/stop/play/delete in actions_cmds.rs; the panel, CLI and MCP share them. Nested steps pass the session authorize hook. |
|
||||
| AUTO-219-4 | Replay tests for crop, filters, selections and styled text on a second document | done | [#219](https://github.com/storytold/photocraft/issues/219) | actions_cmds.rs: a_recorded_edit_run_replays_on_a_second_document records a crop, a Gaussian Blur, a selection fill and faux-bold type on one document and replays all of them onto a second (plus the original layer/select/fill replay) |
|
||||
|
||||
@@ -14,9 +14,9 @@ note = "docs/parity-checklist.md: all menu items live; menus.rs is_live"
|
||||
[[item]]
|
||||
id = "AUTO-219-1"
|
||||
target = "Record actions in a document-independent form (implicit layer, merged type keystrokes)"
|
||||
status = "done"
|
||||
status = "partial"
|
||||
issue = 219
|
||||
note = "actions_record.rs: the active layer's id is dropped, other layers are journaled by name (layer.select always), Type tool keystrokes fold into one type.create/type.edit with the whole text; replay test on a document with other layer ids"
|
||||
note = "actions_record.rs: active layer ids are dropped, uniquely named layers replay by name, and Type tool keystrokes fold per document and layer; full coverage of all numeric-only command receivers and ambiguous layer names is still open"
|
||||
|
||||
[[item]]
|
||||
id = "AUTO-219-2"
|
||||
|
||||
Reference in new issue
Block a user