mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-03 22:40:27 +00:00
Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
753c3977a9 | ||
|
|
956a45baf7 | ||
|
|
f124aef0d3 | ||
|
|
59eaad690f | ||
|
|
1575ef8825 | ||
|
|
7659c56182 | ||
|
|
d94773663c | ||
|
|
e077bfd06e | ||
|
|
f43e8b1cd9 | ||
|
|
667a5cb0ab | ||
|
|
5edee413ce | ||
|
|
c210879b85 | ||
|
|
8cb6a7a48b | ||
|
|
4109b592d4 | ||
|
|
62f71654e8 | ||
|
|
3bb2bd93ad | ||
|
|
a57fae4fdb | ||
|
|
e451caec50 | ||
|
|
4ac343519a | ||
|
|
00ca7859ef | ||
|
|
da6816a565 | ||
|
|
66eae86296 | ||
|
|
aefa4298da |
@@ -1127,6 +1127,78 @@ impl Document {
|
||||
Fragment { blocks, parts, media }
|
||||
}
|
||||
|
||||
/// The text Copy puts on the clipboard for `a..b`, whose copy is `f`: [`Fragment::clipboard_text`]
|
||||
/// with the list label and what follows it (`1.` and a tab, `•` and a tab) before each list
|
||||
/// paragraph, as Word's plain-text copy has them (#2015). A paragraph gets its label when its
|
||||
/// start is copied and some of it with it: not one copied from its middle, nor one the
|
||||
/// selection only reaches the start of; a selection inside one paragraph only when it is all
|
||||
/// of it. Lists are counted from the top of the body, as the page numbers them. Paragraphs in
|
||||
/// tables, headers and notes keep their text without labels.
|
||||
pub fn copy_text(&self, f: &Fragment, a: &Pos, b: &Pos) -> String {
|
||||
let (a, b) = order(a, b);
|
||||
if a.story != crate::StoryRef::Body {
|
||||
return f.clipboard_text();
|
||||
}
|
||||
// The path each copied block comes from, as `copy_blocks` copies them: the blocks between
|
||||
// two places in one container, or every paragraph between them when the selection
|
||||
// crosses a table (the ones in its cells included).
|
||||
let sources: Vec<Path> = if a.path.parent() == b.path.parent() {
|
||||
(a.path.last()..=b.path.last()).map(|i| a.path.with_last(i)).collect()
|
||||
} else {
|
||||
self.paths_between(&a, &b).into_iter().filter(|p| self.para(a.story, p).is_some()).collect()
|
||||
};
|
||||
let whole_one = a.path != b.path || (a.off == 0 && self.para(a.story, &a.path).is_some_and(|p| b.off >= p.len()));
|
||||
// Part of one paragraph: no label to give.
|
||||
if !whole_one {
|
||||
return f.clipboard_text();
|
||||
}
|
||||
let labels = self.body_list_labels();
|
||||
let mut labelled = f.clone();
|
||||
let mut any = false;
|
||||
for (blk, path) in labelled.blocks.iter_mut().zip(&sources) {
|
||||
// Only top-level paragraphs get labels; the ones in tables keep their text.
|
||||
if path.0.len() != 1 {
|
||||
continue;
|
||||
}
|
||||
let (is_first, is_last) = (*path == a.path, *path == b.path);
|
||||
let start_copied = !is_first || a.off == 0;
|
||||
let reached = !(is_last && b.off == 0 && !is_first);
|
||||
let (Block::Para(p), Some(Some((label, level)))) = (blk, labels.get(path.last())) else { continue };
|
||||
if !(start_copied && reached) {
|
||||
continue;
|
||||
}
|
||||
let after = match level.suffix {
|
||||
crate::numbering::LevelSuffix::Tab => "\t",
|
||||
crate::numbering::LevelSuffix::Space => " ",
|
||||
crate::numbering::LevelSuffix::Nothing => "",
|
||||
};
|
||||
// In the level's own font, so a Symbol-font bullet reads as its Unicode character.
|
||||
any |= p.insert_text(0, &format!("{label}{after}"), &level.chr).is_ok();
|
||||
}
|
||||
if any { labelled.clipboard_text() } else { f.clipboard_text() }
|
||||
}
|
||||
|
||||
/// The list label (and its level) each top-level body block shows, `None` for a table or a
|
||||
/// paragraph without one, counted in reading order with the paragraphs in tables, as layout
|
||||
/// counts them.
|
||||
fn body_list_labels(&self) -> Vec<Option<(String, crate::numbering::Level)>> {
|
||||
let mut counters = crate::numbering::Counters::default();
|
||||
let mut out = Vec::with_capacity(self.body.len());
|
||||
for b in &self.body {
|
||||
let top = matches!(&**b, Block::Para(_));
|
||||
let mut label = None;
|
||||
each_para(b, 0, &mut |p| {
|
||||
let n = p.props.numbering.or_else(|| self.styles.resolve_para(&p.props).numbering).filter(|n| n.num != 0);
|
||||
let l = n.and_then(|n| counters.next_label(&self.numbering, n.num, n.level));
|
||||
if top {
|
||||
label = l;
|
||||
}
|
||||
});
|
||||
out.push(label);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A copy of the block at `path` (a whole table, say) as a fragment, with its text boxes.
|
||||
pub fn copy_block(&self, story: crate::StoryRef, path: &Path) -> Option<Fragment> {
|
||||
Some(self.fragment_of(vec![self.block(story, path)?.clone()]))
|
||||
|
||||
@@ -203,12 +203,13 @@ fn copy_with(s: &mut Session, whole: Option<&Path>) -> CmdResult {
|
||||
(None, Some(r)) => s.doc.copy_table_cells(r.story, &r.table, r.rows, r.cols),
|
||||
(None, None) => None,
|
||||
};
|
||||
let f = match table {
|
||||
Some(f) => f,
|
||||
let (f, range) = match table {
|
||||
Some(f) => (f, false),
|
||||
None if a == b => return Ok(json!({"text": ""})),
|
||||
None => s.doc.copy_range(&a, &b),
|
||||
None => (s.doc.copy_range(&a, &b), true),
|
||||
};
|
||||
let text = f.clipboard_text();
|
||||
// A range copies with its list labels, as Word's plain-text copy has them (#2015).
|
||||
let text = if range { s.doc.copy_text(&f, &a, &b) } else { f.clipboard_text() };
|
||||
let r = copy_result(s, &f, &text)?;
|
||||
s.clip_history.push(&f, &text);
|
||||
Ok(r)
|
||||
@@ -630,6 +631,8 @@ fn search_matches(s: &Session, story: StoryRef, with_rep: bool, chain: bool) ->
|
||||
Some(Pos { story, path: paths.get(sg.para)?.clone(), off: sg.raw + (x - sg.hay) })
|
||||
};
|
||||
cuts.push(hay.len());
|
||||
// `^c`: the copied text without the list labels the plain-text clipboard has (#2015 review).
|
||||
let clip = s.clipboard.as_ref().map_or_else(|| s.clipboard_text.clone(), |f| f.clipboard_text());
|
||||
let mut from = 0;
|
||||
// Where the last match ends and the character that stands there once it is replaced.
|
||||
let mut last: Option<(usize, Option<char>)> = None;
|
||||
@@ -668,7 +671,7 @@ fn search_matches(s: &Session, story: StoryRef, with_rep: bool, chain: bool) ->
|
||||
let quotes = if smart { find_pattern::Quotes::Smart { before } } else { find_pattern::Quotes::Straight };
|
||||
let rep = pieces.as_ref().map(|pc| {
|
||||
let group = |n: usize| caps.get_group(n).and_then(|g| hay.get(g.range())).map(str::to_string);
|
||||
find_pattern::expand(pc, found, &group, &s.clipboard_text, quotes)
|
||||
find_pattern::expand(pc, found, &group, &clip, quotes)
|
||||
});
|
||||
let keep = smart && rep.as_deref().is_some_and(|r| find_pattern::already_curled(found, r));
|
||||
let stays = if keep { found } else { rep.as_deref().unwrap_or(found) };
|
||||
|
||||
@@ -53,7 +53,10 @@ fn in_use(s: &Session, rev: u32) -> bool {
|
||||
/// done) when it isn't such a cut.
|
||||
pub fn cut_tracked(s: &mut Session) -> Result<bool, CmdError> {
|
||||
let (a, b) = s.sel.ordered();
|
||||
if !s.doc.settings.track_changes || !s.doc.settings.track_moves || a == b || a.story != b.story || !move_sized(&s.clipboard_text) {
|
||||
// The cut's own text, as the paste compares it (`take_paste`): the plain-text clipboard also
|
||||
// has the list labels of the paragraphs cut (#2015 review).
|
||||
let text = s.clipboard.as_ref().map_or_else(|| s.clipboard_text.clone(), Fragment::plain_text);
|
||||
if !s.doc.settings.track_changes || !s.doc.settings.track_moves || a == b || a.story != b.story || !move_sized(&text) {
|
||||
return Ok(false);
|
||||
}
|
||||
s.doc.revisions.push(crate::cmd::made_revision(RevisionKind::Delete, s.author.clone(), now_iso()));
|
||||
@@ -62,7 +65,7 @@ pub fn cut_tracked(s: &mut Session) -> Result<bool, CmdError> {
|
||||
s.sel = Selection::caret(at);
|
||||
// Only the author's own insertions were cut: they are gone, not moved.
|
||||
if in_use(s, rid) {
|
||||
s.pending_move = Some(PendingMove { rev: rid, text: s.clipboard_text.clone(), author: s.author.clone() });
|
||||
s.pending_move = Some(PendingMove { rev: rid, text, author: s.author.clone() });
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
@@ -41,11 +41,12 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
]
|
||||
}
|
||||
|
||||
/// Clipboard text from the params (system clipboard), else WordCraft's own copy.
|
||||
/// Clipboard text from the params (system clipboard), else WordCraft's own copy: the text Copy
|
||||
/// put on the clipboard, list labels and all (#2015).
|
||||
fn clip_text(s: &Session, v: &Value) -> Option<String> {
|
||||
match p::str(v, "text") {
|
||||
Some(t) => Some(t.replace("\r\n", "\n")),
|
||||
None => s.clipboard.as_ref().map(Fragment::plain_text),
|
||||
None => s.clipboard.as_ref().map(|f| if s.clipboard_text.is_empty() { f.plain_text() } else { s.clipboard_text.clone() }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,12 +102,11 @@ pub(crate) fn matches_session_clipboard(s: &Session, v: &Value) -> bool {
|
||||
let rtf_matches = rtf.is_none_or(|r| r == s.clipboard_rtf);
|
||||
|
||||
owns_clipboard(v)
|
||||
&& s.clipboard.as_ref().is_some_and(|f| {
|
||||
text.as_ref().is_none_or(|t| f.clipboard_text() == *t || (t.is_empty() && has_rich))
|
||||
&& html_matches
|
||||
&& rtf_matches
|
||||
&& (text.is_some() || has_rich || (html.is_none() && rtf.is_none()))
|
||||
})
|
||||
&& s.clipboard.is_some()
|
||||
&& text.as_ref().is_none_or(|t| s.clipboard_text == *t || (t.is_empty() && has_rich))
|
||||
&& html_matches
|
||||
&& rtf_matches
|
||||
&& (text.is_some() || has_rich || (html.is_none() && rtf.is_none()))
|
||||
}
|
||||
|
||||
/// The source for a format, if the clipboard has it.
|
||||
|
||||
@@ -2044,6 +2044,141 @@ fn copying_symbol_font_text_puts_the_unicode_symbols_on_the_clipboard() {
|
||||
assert_eq!(text(&s), "ok J\u{F0FC}ok J\u{F0FC}");
|
||||
}
|
||||
|
||||
/// A session holding "Intro", a numbered list (Item 1, Item 2) and a bulleted one (Point A,
|
||||
/// Point B).
|
||||
fn session_with_lists() -> Session {
|
||||
let mut s = s();
|
||||
run(&mut s, "document.setText", json!({"text": "Intro\nItem 1\nItem 2\nPoint A\nPoint B"}));
|
||||
run(&mut s, "select.range", json!({"anchor": Pos::body(1, 0), "focus": Pos::body(2, 6)}));
|
||||
run(&mut s, "para.numbering", json!({}));
|
||||
run(&mut s, "select.range", json!({"anchor": Pos::body(3, 0), "focus": Pos::body(4, 7)}));
|
||||
run(&mut s, "para.bullets", json!({}));
|
||||
s
|
||||
}
|
||||
|
||||
/// #2015 review: with Track Changes (and Track Moves) on, numbered items cut and pasted elsewhere
|
||||
/// are a tracked move, though the clipboard's text has their labels; and `^c` in Replace puts in
|
||||
/// the copied text without them.
|
||||
#[test]
|
||||
fn a_cut_list_item_pasted_elsewhere_is_a_tracked_move() {
|
||||
let mut s = session_with_lists();
|
||||
run(&mut s, "review.trackChanges", json!({"value": true}));
|
||||
run(&mut s, "select.range", json!({"anchor": Pos::body(1, 0), "focus": Pos::body(3, 0)}));
|
||||
run(&mut s, "edit.cut", json!({}));
|
||||
assert!(s.clipboard_text.starts_with("1.\tItem 1"), "{:?}", s.clipboard_text);
|
||||
run(&mut s, "caret.docEnd", json!({}));
|
||||
let clip = s.clipboard_text.clone();
|
||||
run(&mut s, "edit.paste", json!({"text": clip}));
|
||||
let moves: Vec<wordcraft_doc::RevisionKind> = s.doc.revisions.iter().filter(|r| r.move_name.is_some()).map(|r| r.kind).collect();
|
||||
assert_eq!(moves, [wordcraft_doc::RevisionKind::Delete, wordcraft_doc::RevisionKind::Insert], "a move, its source and its destination");
|
||||
|
||||
let mut s = session_with_lists();
|
||||
run(&mut s, "select.range", json!({"anchor": Pos::body(1, 0), "focus": Pos::body(1, 6)}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
assert_eq!(s.clipboard_text, "1.\tItem 1");
|
||||
run(&mut s, "edit.replaceAll", json!({"text": "Intro", "with": "^c"}));
|
||||
assert!(text(&s).starts_with("Item 1\n"), "{:?}", text(&s));
|
||||
}
|
||||
|
||||
/// #2015: Copy puts each list paragraph's number or bullet, and the tab after it, in the
|
||||
/// clipboard's text, as Word's plain-text copy has them. Pasting that copy back here still pastes
|
||||
/// our own copy: the lists, not their labels typed out as text.
|
||||
#[test]
|
||||
fn copying_a_list_puts_its_numbers_and_bullets_on_the_clipboard() {
|
||||
let mut s = session_with_lists();
|
||||
run(&mut s, "select.all", json!({}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
assert_eq!(s.clipboard_text, "Intro\n1.\tItem 1\n2.\tItem 2\n•\tPoint A\n•\tPoint B");
|
||||
|
||||
run(&mut s, "caret.docEnd", json!({}));
|
||||
run(&mut s, "text.newParagraph", json!({}));
|
||||
let clip = s.clipboard_text.clone();
|
||||
run(&mut s, "edit.paste", json!({"text": clip}));
|
||||
assert!(!text(&s).contains('\t') && !text(&s).contains('•'), "{}", text(&s));
|
||||
let numbered = |s: &Session, i: usize| s.doc.para(StoryRef::Body, &wordcraft_doc::Path::top(i)).and_then(|p| p.props.numbering).is_some();
|
||||
assert!(numbered(&s, 6) && numbered(&s, 7), "the pasted items are list items");
|
||||
|
||||
// A bullet stored as a Symbol-font character (as Word documents have them) reads as •.
|
||||
let mut s = session_with_lists();
|
||||
let num = s.doc.para(StoryRef::Body, &wordcraft_doc::Path::top(3)).and_then(|p| p.props.numbering).map(|n| n.num).unwrap();
|
||||
let abs = s.doc.numbering.num(num).map(|n| n.abstract_id).unwrap();
|
||||
let level = std::sync::Arc::make_mut(&mut s.doc.numbering).abstracts.iter_mut().find(|a| a.id == abs).and_then(|a| a.levels.first_mut()).unwrap();
|
||||
level.text = "\u{F0B7}".into();
|
||||
level.chr.font = Some("Symbol".into());
|
||||
run(&mut s, "select.range", json!({"anchor": Pos::body(3, 0), "focus": Pos::body(4, 7)}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
assert_eq!(s.clipboard_text, "•\tPoint A\n•\tPoint B");
|
||||
|
||||
// Paste Special › Unformatted Text with no system clipboard text (a command, not the app)
|
||||
// pastes the copy's text as Copy put it: the labels typed out.
|
||||
run(&mut s, "caret.docEnd", json!({}));
|
||||
run(&mut s, "text.newParagraph", json!({}));
|
||||
run(&mut s, "edit.pasteSpecial", json!({"as": "text"}));
|
||||
assert!(text(&s).ends_with("•\tPoint A\n•\tPoint B"), "{:?}", text(&s));
|
||||
}
|
||||
|
||||
/// #2015: a list paragraph gets its label only when its start is copied with some of it: not one
|
||||
/// copied from its middle, nor one the selection only reaches; inside one paragraph only when it
|
||||
/// is all of it.
|
||||
#[test]
|
||||
fn copied_list_labels_follow_what_the_selection_covers() {
|
||||
let mut s = session_with_lists();
|
||||
let copy = |s: &mut Session, a: Pos, b: Pos| {
|
||||
run(s, "select.range", json!({"anchor": a, "focus": b}));
|
||||
run(s, "edit.copy", json!({}));
|
||||
s.clipboard_text.clone()
|
||||
};
|
||||
assert_eq!(copy(&mut s, Pos::body(1, 2), Pos::body(2, 6)), "em 1\n2.\tItem 2", "from the middle of an item");
|
||||
assert_eq!(copy(&mut s, Pos::body(1, 1), Pos::body(1, 4)), "tem", "inside one item");
|
||||
assert_eq!(copy(&mut s, Pos::body(1, 0), Pos::body(1, 6)), "1.\tItem 1", "all of one item");
|
||||
let up_to_next = copy(&mut s, Pos::body(1, 0), Pos::body(3, 0));
|
||||
assert!(up_to_next.starts_with("1.\tItem 1\n2.\tItem 2") && !up_to_next.contains('•'), "{up_to_next:?}");
|
||||
assert_eq!(copy(&mut s, Pos::body(0, 0), Pos::body(0, 5)), "Intro", "no list, no label");
|
||||
}
|
||||
|
||||
/// #2015: a nested list item's copy has its own level's label, and the item after it on the
|
||||
/// first level goes on counting from the one before the nested item.
|
||||
#[test]
|
||||
fn a_copied_nested_list_item_has_its_level_s_label() {
|
||||
let mut s = session_with_lists();
|
||||
run(&mut s, "document.setText", json!({"text": "Item 1\nSub item\nItem 2"}));
|
||||
run(&mut s, "select.all", json!({}));
|
||||
run(&mut s, "para.numbering", json!({}));
|
||||
let path = wordcraft_doc::Path::top(1);
|
||||
let sub = s.doc.para_mut(StoryRef::Body, &path).unwrap();
|
||||
sub.props.numbering = sub.props.numbering.map(|n| wordcraft_doc::props::NumRef { level: 1, ..n });
|
||||
run(&mut s, "select.all", json!({}));
|
||||
run(&mut s, "edit.copy", json!({}));
|
||||
assert_eq!(s.clipboard_text, "1.\tItem 1\na.\tSub item\n2.\tItem 2");
|
||||
}
|
||||
|
||||
/// #2015: a selection that crosses a table (Select All in a document that starts with one, or a
|
||||
/// list down into a table) keeps the labels of the list paragraphs outside the table; the
|
||||
/// paragraphs in its cells keep their text without labels.
|
||||
#[test]
|
||||
fn copied_list_labels_survive_a_selection_across_a_table() {
|
||||
use wordcraft_doc::{Block, Path, Table};
|
||||
let cell_table = |s: &mut Session, at: usize| {
|
||||
s.doc.insert_block(StoryRef::Body, &Path::top(at), Block::Table(Table::new(1, 1, 300.0))).unwrap();
|
||||
let cell = Path(vec![at as u32, 0, 0, 0]);
|
||||
s.doc.para_mut(StoryRef::Body, &cell).unwrap().insert_text(0, "Cell", &Default::default()).unwrap();
|
||||
Pos { story: StoryRef::Body, path: cell, off: 0 }
|
||||
};
|
||||
let copy = |s: &mut Session, a: Pos, b: Pos| {
|
||||
run(s, "select.range", json!({"anchor": a, "focus": b}));
|
||||
run(s, "edit.copy", json!({}));
|
||||
s.clipboard_text.clone()
|
||||
};
|
||||
// A table first: from its cell to the end of the last bullet.
|
||||
let mut s = session_with_lists();
|
||||
let cell = cell_table(&mut s, 0);
|
||||
assert_eq!(copy(&mut s, cell, Pos::body(5, 7)), "Cell\nIntro\n1.\tItem 1\n2.\tItem 2\n•\tPoint A\n•\tPoint B");
|
||||
// A table last: from the first item down into its cell.
|
||||
let mut s = session_with_lists();
|
||||
let cell = cell_table(&mut s, 5);
|
||||
assert_eq!(copy(&mut s, Pos::body(1, 0), Pos { off: 4, ..cell }), "1.\tItem 1\n2.\tItem 2\n•\tPoint A\n•\tPoint B\nCell");
|
||||
}
|
||||
|
||||
/// The text drawn on the first page.
|
||||
fn drawn_text(s: &mut Session) -> String {
|
||||
let layout = s.layout();
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user