mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-06 23:09:47 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dbfa312f4 |
@@ -128,7 +128,7 @@ fn turn(s: &mut Session) -> Option<TextDirection> {
|
||||
}
|
||||
|
||||
/// Vertical text: to the next (`next`) or previous line, at the same distance along it. With no
|
||||
/// such line in the caret's flow (a table cell's only line, the last column), Left/Right step along
|
||||
/// such line in the caret's flow, Left/Right stop in a table cell or text box and otherwise step along
|
||||
/// the text as they did before (`is_left`: backwards).
|
||||
fn line_step(s: &mut Session, v: &Value, next: bool, is_left: bool) -> CmdResult {
|
||||
let ext = extend(v);
|
||||
@@ -145,6 +145,8 @@ fn line_step(s: &mut Session, v: &Value, next: bool, is_left: bool) -> CmdResult
|
||||
s.goal_x = Some(g);
|
||||
p
|
||||
}
|
||||
// Inside a table cell or text box the caret stops at the edge rather than leaving it.
|
||||
None if from.path.0.len() > 1 => return sel_result(s),
|
||||
None => return lr_along(s, v, is_left),
|
||||
};
|
||||
let gx = s.goal_x;
|
||||
|
||||
@@ -4812,3 +4812,19 @@ fn enter_in_a_drop_cap_paragraph_makes_a_normal_paragraph() {
|
||||
assert_eq!(second.drop_cap_font, None, "{position}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #8986: in top-to-bottom text Left/Right change line (lines stack leftwards), keeping the
|
||||
/// distance along the line, and stop at the first/last line of a table cell.
|
||||
#[test]
|
||||
fn arrows_in_vertical_text_change_line_and_stop_in_a_cell() {
|
||||
let mut d = wordcraft_doc::Document::from_text("abc\ndef");
|
||||
d.last_section.text_direction = wordcraft_doc::props::TextDirection::Down;
|
||||
let mut s = Session::new(d);
|
||||
s.sel = crate::Selection::caret(Pos::body(0, 1));
|
||||
run(&mut s, "caret.left", json!({}));
|
||||
assert_eq!(s.sel.focus, Pos::body(1, 1), "Left goes to the next, leftward line");
|
||||
run(&mut s, "caret.right", json!({}));
|
||||
assert_eq!(s.sel.focus, Pos::body(0, 1));
|
||||
run(&mut s, "caret.left", json!({"extend": true}));
|
||||
assert!(!s.sel.is_collapsed(), "Shift+Left extends over the line change");
|
||||
}
|
||||
|
||||
+28
-15
@@ -801,25 +801,38 @@ impl DocLayout {
|
||||
/// caret's flow on the page.
|
||||
pub fn turned_line(&self, pos: &Pos, goal: Option<f32>, next: bool, page_hint: usize) -> Option<(Pos, f32)> {
|
||||
let c = self.caret_on(pos, page_hint)?;
|
||||
let lines: Vec<(&Path, &std::sync::Arc<ParaLayout>, usize)> = items_of(self.pages.get(c.page)?, pos.story)
|
||||
.filter_map(|it| match it {
|
||||
Placed::Lines { story, path, para, l0, l1, turn, .. }
|
||||
if *story == pos.story && turn.is_turned() && path.parent() == pos.path.parent() =>
|
||||
{
|
||||
Some((path, para, *l0, *l1))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.flat_map(|(path, para, l0, l1)| (l0..l1).map(move |li| (path, para, li)))
|
||||
.collect();
|
||||
let flow = |pi: usize| -> Option<Vec<(&Path, &std::sync::Arc<ParaLayout>, usize)>> {
|
||||
Some(
|
||||
items_of(self.pages.get(pi)?, pos.story)
|
||||
.filter_map(|it| match it {
|
||||
Placed::Lines { story, path, para, l0, l1, turn, .. }
|
||||
if *story == pos.story && turn.is_turned() && path.parent() == pos.path.parent() =>
|
||||
{
|
||||
Some((path, para, *l0, *l1))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.flat_map(|(path, para, l0, l1)| (l0..l1).map(move |li| (path, para, li)))
|
||||
.collect(),
|
||||
)
|
||||
};
|
||||
let lines = flow(c.page)?;
|
||||
let here = lines.iter().position(|(path, para, li)| *path == &pos.path && *li == para.line_of(pos.off))?;
|
||||
let (path, para, li) = lines.get(if next { here + 1 } else { here.checked_sub(1)? })?;
|
||||
// Past the page's first/last line, the flow goes on at the adjacent page's last/first.
|
||||
let target = if next { lines.get(here + 1).copied() } else { here.checked_sub(1).and_then(|i| lines.get(i).copied()) }.or_else(|| {
|
||||
let other = flow(if next { c.page + 1 } else { c.page.checked_sub(1)? })?;
|
||||
if next { other.first().copied() } else { other.last().copied() }
|
||||
});
|
||||
let (path, para, li) = target?;
|
||||
let gx = match goal {
|
||||
Some(g) => g,
|
||||
None => lines[here].1.x_of(lines[here].2, pos.off)?,
|
||||
None => {
|
||||
let x = lines[here].1.x_of(lines[here].2, pos.off)?;
|
||||
lines[here].1.caret_limit(lines[here].2).map_or(x, |m| x.min(m))
|
||||
}
|
||||
};
|
||||
let off = para.off_at_x(*li, gx);
|
||||
Some((Pos { story: pos.story, path: (*path).clone(), off }, gx))
|
||||
let off = para.off_at_x(li, gx);
|
||||
Some((Pos { story: pos.story, path: path.clone(), off }, gx))
|
||||
}
|
||||
|
||||
/// The position one line up (`dir < 0`) or down from `pos`, keeping `goal_x` (page x).
|
||||
|
||||
Reference in New Issue
Block a user