mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 22:33:08 +00:00
Type: an insert that flows on into the next frame takes the caret with it
When type.insert at the Type tool's caret pushes the caret's place into the next frame of threaded text, the caret now goes to that frame, so typing continues after the insertion instead of before the text that moved on.
This commit is contained in:
@@ -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::<usize>()
|
||||
};
|
||||
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::<usize>();
|
||||
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)) };
|
||||
|
||||
@@ -1640,9 +1640,13 @@ fn insert_at_type_caret(s: &mut Session, text: &str) -> Result<Option<Value>> {
|
||||
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])))
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user