diff --git a/crates/engine/src/cmd/captions.rs b/crates/engine/src/cmd/captions.rs index c2fbbc6ee..b00dac1be 100644 --- a/crates/engine/src/cmd/captions.rs +++ b/crates/engine/src/cmd/captions.rs @@ -112,6 +112,19 @@ fn caption(s: &mut Session, p: &Value) -> Result { }) } +/// Whether any item on the document spreads, inside groups included, is a live caption: the +/// items [`refresh_live`] visits. +pub(crate) fn has_live(d: &Document) -> bool { + let mut found = false; + for it in d.spreads.iter().flat_map(|sp| &sp.items) { + it.walk(&mut |i| found |= i.live_caption.is_some()); + if found { + return true; + } + } + false +} + /// Bring live captions up to date with their source objects (the text frame's story is /// replaced when its text changed; a caption whose source is gone keeps its last text). pub(crate) fn refresh_live(d: &mut Document) -> bool { @@ -136,6 +149,17 @@ pub(crate) fn refresh_live(d: &mut Document) -> bool { changed } +/// Keep a text selection inside its story after `refresh_live` replaced the story's text. +pub(crate) fn clamp_text_selection(d: &Document, sel: &mut Selection) { + if let Some(ts) = sel.text.as_mut() + && ts.cell.is_none() + && let Some(st) = d.story(ts.story) + { + ts.anchor = designcraft_doc::floor_char_boundary(&st.text, ts.anchor); + ts.focus = designcraft_doc::floor_char_boundary(&st.text, ts.focus); + } +} + #[cfg(test)] mod tests { use serde_json::json; @@ -178,4 +202,107 @@ mod tests { s.execute("edit.undo", &json!({})).unwrap(); assert_eq!(text(&s), "Photo: First", "one undo step for both"); } + + #[test] + fn live_caption_follows_a_committed_gesture() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + let text = |s: &Session| { + let d = &s.doc().unwrap().doc; + d.stories[&d.item(cid).unwrap().text_frame().unwrap().story].text.clone() + }; + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": "Second", "ids": [a]})).unwrap(); + let revision = s.doc().unwrap().revision; + s.commit_interaction().unwrap(); + assert_eq!(text(&s), "Photo: Second", "updated when the gesture commits"); + assert_ne!(s.doc().unwrap().revision, revision, "caches keyed by the revision draw the new caption"); + s.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(text(&s), "Photo: First", "one undo step for both"); + } + + #[test] + fn auto_sized_live_caption_fits_a_committed_gesture() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = r["ids"][0].as_u64().unwrap(); + s.execute("object.textFrameOptions", &json!({"ids": [cid], "autoSize": "heightOnly"})).unwrap(); + let height = |s: &Session| s.doc().unwrap().doc.item(designcraft_doc::ItemId(cid)).unwrap().bounds().height(); + let short = height(&s); + let long = "A harbour at dawn, with the fishing boats coming in one after another past the lighthouse and the long stone pier"; + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": long, "ids": [a]})).unwrap(); + s.commit_interaction().unwrap(); + assert!(height(&s) > short + 5.0, "the caption frame grows with its text: {short} -> {}", height(&s)); + } + + #[test] + fn grouped_live_caption_follows_its_source() { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + let b = s.execute("frame.create", &json!({"rect": [300, 72, 400, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + // The only live caption sits inside a group, not at the top of the spread. + s.execute("object.group", &json!({"ids": [cid.0, b]})).unwrap(); + let text = |s: &Session| { + let d = &s.doc().unwrap().doc; + d.stories[&d.item(cid).unwrap().text_frame().unwrap().story].text.clone() + }; + s.execute("object.altText", &json!({"text": "Second", "ids": [a]})).unwrap(); + assert_eq!(text(&s), "Photo: Second", "updated by a command"); + s.begin_interaction("Alt Text").unwrap(); + s.preview("object.altText", json!({"text": "Third", "ids": [a]})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(text(&s), "Photo: Third", "updated when the gesture commits"); + } + + /// A session with a live caption reading "Photo: First" and the caret at its end. + fn caret_at_end_of_live_caption() -> (Session, designcraft_doc::StoryId) { + let mut s = Session::new(); + s.execute("file.new", &json!({})).unwrap(); + let a = s.execute("frame.create", &json!({"rect": [72, 72, 272, 172]})).unwrap()["id"].as_u64().unwrap(); + s.execute("object.altText", &json!({"text": "First", "ids": [a]})).unwrap(); + let r = s.execute("object.caption", &json!({"ids": [a], "text": "Photo: {altText}", "live": true})).unwrap(); + let cid = designcraft_doc::ItemId(r["ids"][0].as_u64().unwrap()); + let sid = s.doc().unwrap().doc.item(cid).unwrap().text_frame().unwrap().story; + let len = "Photo: First".len(); + s.execute("text.select", &json!({"story": sid.0, "anchor": len, "focus": len})).unwrap(); + (s, sid) + } + + fn assert_caret_inside(s: &Session, sid: designcraft_doc::StoryId) { + let st = s.doc().unwrap(); + let len = st.doc.stories[&sid].text.len(); + let ts = st.selection.text.unwrap(); + assert!(ts.anchor <= len && ts.focus <= len, "caret {}..{} past the end of {len} bytes", ts.anchor, ts.focus); + assert!(s.ime_text_context().is_some(), "the caret still describes its text"); + } + + #[test] + fn caret_stays_inside_a_live_caption_that_discards_typing() { + let (mut s, sid) = caret_at_end_of_live_caption(); + s.execute("text.insert", &json!({"text": " and more"})).unwrap(); + assert_eq!(s.doc().unwrap().doc.stories[&sid].text, "Photo: First"); + assert_caret_inside(&s, sid); + } + + #[test] + fn caret_stays_inside_a_live_caption_after_a_committed_gesture() { + let (mut s, sid) = caret_at_end_of_live_caption(); + s.begin_interaction("Typing").unwrap(); + s.preview("text.insert", json!({"text": " and more"})).unwrap(); + s.commit_interaction().unwrap(); + assert_eq!(s.doc().unwrap().doc.stories[&sid].text, "Photo: First"); + assert_caret_inside(&s, sid); + } } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 6a50bc016..1c59a6054 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -684,10 +684,11 @@ impl Session { if spec.undoable && let Some(st) = self.active_mut() && st.interaction.is_none() - && st.doc.spreads.iter().any(|sp| sp.items.iter().any(|i| i.live_caption.is_some())) + && cmd::captions::has_live(&st.doc) { let mut d = (*st.doc).clone(); if cmd::captions::refresh_live(&mut d) { + cmd::captions::clamp_text_selection(&d, &mut st.selection); st.doc = Arc::new(d); } } diff --git a/crates/engine/src/tooling.rs b/crates/engine/src/tooling.rs index 9bde0acfd..2b1ab7dc2 100644 --- a/crates/engine/src/tooling.rs +++ b/crates/engine/src/tooling.rs @@ -321,6 +321,18 @@ impl Session { let st = self.doc_mut()?; let Some(it) = st.interaction.take() else { return Ok(()) }; if !Arc::ptr_eq(&it.doc, &st.doc) { + // Previews skip the live caption refresh, so the committed change brings captions up + // to date here, in the same undo step, and auto-sizes the caption frames it changed. + if crate::cmd::captions::has_live(&st.doc) { + let mut d = (*st.doc).clone(); + if crate::cmd::captions::refresh_live(&mut d) { + let todo = crate::autosize::candidates(&d, Some(&st.doc)); + crate::autosize::fit(&mut d, todo); + crate::cmd::captions::clamp_text_selection(&d, &mut st.selection); + st.doc = Arc::new(d); + st.bump_revision(); + } + } record_undo(st, HistoryEntry { label: it.label, doc: it.doc, selection: it.selection }); } if let Some((c, p)) = it.preview {