diff --git a/crates/engine/src/cmd/panelcmds.rs b/crates/engine/src/cmd/panelcmds.rs index a155245fbc..aa83466693 100644 --- a/crates/engine/src/cmd/panelcmds.rs +++ b/crates/engine/src/cmd/panelcmds.rs @@ -1276,7 +1276,7 @@ fn aligned_between(tab: &vectorcraft_doc::TabStop, from: f64, x: f64) -> bool { /// The frames of the story that type object `id` belongs to (its thread in thread order, else `id` /// alone), each with the length of its text in bytes. -fn story_frames(s: &Session, id: NodeId) -> Vec<(NodeId, usize)> { +pub(crate) fn story_frames(s: &Session, id: NodeId) -> Vec<(NodeId, usize)> { let Some(st) = s.active() else { return vec![] }; let d = &st.doc; let frames = d.text_threads.iter().find(|thread| thread.contains(&id)).cloned().unwrap_or_else(|| vec![id]); @@ -1314,6 +1314,16 @@ fn move_editing_selection( at + inserted.iter().filter(|(b, _)| *b <= at).map(|(_, n)| n).sum::() }; let (anchor, caret) = (story(anchor), story(caret)); + select_story_range(s, id, anchor, caret) +} + +/// The Type tool's anchor and caret become story positions `anchor` and `caret` of the story that +/// frame `id` belongs to, as its frames are now. The caret stays in `id` while its place is there, +/// and else goes to the frame that holds it, with the anchor in that frame; that frame becomes the +/// selection when it was not selected. +pub(crate) fn select_story_range(s: &mut Session, id: NodeId, anchor: usize, caret: usize) -> Result<()> { + let start_of = |frames: &[(NodeId, usize)], f: NodeId| frames.iter().take_while(|(g, _)| *g != f).map(|(_, n)| n).sum::(); + let len_of = |frames: &[(NodeId, usize)], f: NodeId| frames.iter().find(|(g, _)| *g == f).map_or(0, |(_, n)| *n); let after = story_frames(s, id); let fits = |f: NodeId| (start_of(&after, f)..=start_of(&after, f) + len_of(&after, f)).contains(&caret); let frame = if fits(id) { Some(id) } else { after.iter().map(|(f, _)| *f).find(|f| fits(*f)) }; diff --git a/crates/engine/src/cmd/typemenu.rs b/crates/engine/src/cmd/typemenu.rs index 01ea5e9379..737e5e9fcd 100644 --- a/crates/engine/src/cmd/typemenu.rs +++ b/crates/engine/src/cmd/typemenu.rs @@ -1640,9 +1640,13 @@ fn insert_at_type_caret(s: &mut Session, text: &str) -> Result> { s.set_tool_option("commitTyping", &Value::Bool(true)); s.commit_interaction()?; } + // The frame's place in the story once the edit has cleared the frames in `clear`. + let start: usize = + super::panelcmds::story_frames(s, id).iter().take_while(|(f, _)| *f != id).filter(|(f, _)| !clear.contains(&f.0)).map(|(_, n)| n).sum(); let r = super::textedit::edit_range(s, &json!({"id": id.0, "start": a, "end": b, "insert": text, "clear": clear}))?; - let caret = r.get("caret").cloned().unwrap_or(Value::Null); - s.set_tool_option("select", &json!({"start": caret, "end": caret})); + let caret = r.get("caret").and_then(Value::as_u64).map_or(a + text.len(), |c| c as usize); + // Threaded text re-flows in the edit: the caret goes to the frame its place moved to. + super::panelcmds::select_story_range(s, id, start + caret, start + caret)?; Ok(Some(ids_json(&[id]))) } diff --git a/crates/engine/src/tests/textedit.rs b/crates/engine/src/tests/textedit.rs index f05ad681a4..4f1edcc62a 100644 --- a/crates/engine/src/tests/textedit.rs +++ b/crates/engine/src/tests/textedit.rs @@ -2303,6 +2303,27 @@ fn insert_character_replaces_the_whole_story_that_select_all_selected() { assert_eq!(format!("{}{}", obj(&s, a).plain_text(), obj(&s, b).plain_text()), story); } +/// When `type.insert` at the caret in the first frame of threaded text pushes the caret's place on +/// into the next frame, the Type tool's caret goes there with it, and typing goes after the +/// insertion. +#[test] +fn insert_character_moves_the_caret_with_the_text_it_flows_on() { + let (mut s, a, b, story) = threaded_story_edited_in_its_second_frame(); + let first = obj(&s, a).plain_text(); + // Before the last word of the first frame: two line breaks there push the line after the + // first one on into the second frame. + let at = first.trim_end().rfind(' ').map_or(0, |k| k + 1); + s.execute("select.set", &json!({"ids": [a.0]})).unwrap(); + s.set_tool_option("textSelection", &json!({"editing": a.0, "anchor": at, "caret": at})); + for _ in 0..2 { + s.execute("type.insert", &json!({"char": "forcedLineBreak"})).unwrap(); + } + assert!(obj(&s, a).plain_text().len() < at + 6, "the second break flows on into the second frame"); + s.tool_text("X", ViewInfo::default()).unwrap(); + let want = format!("{}\u{2028}\u{2028}X{}", &story[..at], &story[at..]); + assert_eq!(format!("{}{}", obj(&s, a).plain_text(), obj(&s, b).plain_text()), want); +} + #[test] fn remove_breaks_takes_the_whole_story_that_select_all_selected() { let (mut s, a, b, story) = threaded_list_edited_in_its_second_frame();