mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-04 11:46:53 -06:00
* #7887: hovering a tracked change shows its kind, author and time Resting the pointer on a tracked insertion, deletion, move or formatting change shows a tip with what the change is (Inserted, Deleted, Moved to, Moved from, Formatted), its author and its time. Only changes the page marks count: none with markup hidden, insertions and deletions while Show Markup shows them, formatting while it shows formatting. A link's tip comes first. The time is shown as written in w:date, as Word does, since a revision has no UTC time beside it. * #7887: tips for paragraph formatting changes, and in notes and text boxes The hover tip resolves the story under the pointer as a click does, so a change in a footnote or text box shows its tip while the caret is in the body. A paragraph whose own formatting changed while tracking (alignment, indents) shows a Formatted tip on its text when the character under the pointer has no change of its own. * #7887: the link tip looks in the story under the pointer too A link in a footnote or endnote now shows its tip while the caret is in the body, so the link's tip still comes before the tracked change tip there. * #7887: a change made here shows its time in local time A change WordCraft makes keeps its UTC time beside the local w:date (#6876's Revision::date_utc). revision_time now passes it to stamp, as comment_time does, so the tip shows the user's local time for it. A change from a file with only a Z date still shows as written. changes_made_here_show_in_local_time builds the revision through made_revision, as new_revision does, and reads it at UTC+2: 14:05 for 12:05Z. Without the fix it shows the machine's own wall time instead. The canvas hover test sets a canned date, so it now clears date_utc too, as a file's change has none. * #7887: the tip's time is optional, and a w:date offset is converted (docs) --------- Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
This commit is contained in:
@@ -46,6 +46,7 @@ mod navigation;
|
||||
mod pages;
|
||||
mod reading;
|
||||
mod reading_metrics;
|
||||
mod revision_tip;
|
||||
pub(crate) mod rulers;
|
||||
mod source;
|
||||
mod story;
|
||||
|
||||
@@ -9,6 +9,7 @@ use craft::ui::canvas::{CanvasTool, ToolAction, ToolContext, ToolPath, ToolPoint
|
||||
use craft::ui::primitives::Draw;
|
||||
|
||||
use super::links::{field_showing_code, follow_link, is_generated_bookmark_link, link_under};
|
||||
use super::revision_tip::revision_under;
|
||||
use super::story::{enters_header_footer, in_text_box, part_kind, text_box_host};
|
||||
use super::{CanvasState, DocLayout, PartKind, Pos, Pos2, StoryRef, WordApp, json};
|
||||
use crate::objects::ObjectGesture;
|
||||
@@ -163,12 +164,23 @@ impl<'a> PageTextTool<'a> {
|
||||
let (page, x, y) = page_of(self.body.pages, pos)?;
|
||||
let layout = &*self.layout;
|
||||
// A linked picture or shape under the pointer comes first: it lies over the text (#5267).
|
||||
let story = app.session.sel.focus.story;
|
||||
// The text is the story under the pointer, as for the tracked change tip that follows.
|
||||
let story = pointer_story(app, layout, page, x, y, app.session.sel.focus.story);
|
||||
let link = crate::objects::link_at(app, layout, self.body, pos)
|
||||
.or_else(|| layout.hit(page, x, y, story).and_then(|at| link_under(app, layout, page, x, y, &at)))?;
|
||||
let target = if is_generated_bookmark_link(&link) { tl!("Current Document").to_string() } else { link };
|
||||
Some(format!("{target}\n{}", crate::i18n::fmt(tl!("{key}+Click to follow link"), &[("key", self.link_key)])))
|
||||
}
|
||||
|
||||
/// The tip for the tracked change under `pos`: its kind, author and time (#7887).
|
||||
fn revision_tip(&self, pos: Pos2) -> Option<String> {
|
||||
let (page, x, y) = page_of(self.body.pages, pos)?;
|
||||
let layout = &*self.layout;
|
||||
// The story under the pointer, as a click there takes it: a note or text box over the body.
|
||||
let story = pointer_story(&*self.app, layout, page, x, y, self.app.session.sel.focus.story);
|
||||
let at = layout.hit(page, x, y, story)?;
|
||||
revision_under(&*self.app, layout, page, x, y, &at)
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasTool for PageTextTool<'_> {
|
||||
@@ -249,7 +261,8 @@ impl CanvasTool for PageTextTool<'_> {
|
||||
|
||||
fn hover(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) {
|
||||
// PLAN: assumed `ToolContext::tip(&mut self, tip: impl Into<Tooltip>)` (canvas-w1, S-L238).
|
||||
if let Some(tip) = self.link_tip(pointer.pos) {
|
||||
// A link's tip comes first: following the link is what a click there can do.
|
||||
if let Some(tip) = self.link_tip(pointer.pos).or_else(|| self.revision_tip(pointer.pos)) {
|
||||
cx.tip(tip);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//! The hover tip on a tracked change (#7887): what kind of change it is, who made it and when.
|
||||
|
||||
use super::{DocLayout, Pos, WordApp};
|
||||
use wordcraft_doc::props::CharProps;
|
||||
use wordcraft_layout::display::MarkupOptions;
|
||||
|
||||
/// The tip for the tracked change under the pointer at (x, y) on `page`, where `pos` is the caret
|
||||
/// position the point hits: its kind, its author and its time, a line each. Only changes the page
|
||||
/// marks count: none with markup hidden, insertions, deletions and moves while Show Markup shows
|
||||
/// them, formatting changes while it shows formatting. As with a link, the point must be on a
|
||||
/// character, not beside, above or below it: on the changed character itself, or anywhere in the
|
||||
/// text of a paragraph whose own formatting changed (its alignment, indents…) when the character
|
||||
/// has no change of its own.
|
||||
pub(super) fn revision_under(app: &WordApp, layout: &DocLayout, page: usize, x: f32, y: f32, pos: &Pos) -> Option<String> {
|
||||
let session = &app.session;
|
||||
if !session.view.show_markup {
|
||||
return None;
|
||||
}
|
||||
let markup = &session.prefs.markup;
|
||||
let doc = &session.doc;
|
||||
let para = doc.para_at(pos)?;
|
||||
let para_change = para.props.fmt_change.as_ref().filter(|_| markup.formatting).map(|c| (c.rev, tl!("Formatted")));
|
||||
let at = wordcraft_geom::Point::new(x, y);
|
||||
// The caret falls between characters: the one under the point is on either side of it.
|
||||
[(pos.off, para.next_boundary(pos.off)), (para.prev_boundary(pos.off), pos.off)].into_iter().filter(|(a, b)| a < b).find_map(|(a, b)| {
|
||||
let (rev, kind) = change_of(para.props_of_char(a), markup, |i| doc.move_name(Some(i)).is_some()).or(para_change)?;
|
||||
let rev = doc.revisions.get(usize::try_from(rev).ok()?)?;
|
||||
let (from, to) = (Pos { off: a, ..pos.clone() }, Pos { off: b, ..pos.clone() });
|
||||
let hit = layout.selection_rects(doc, &from, &to, page).into_iter().any(|(pi, r)| pi == page && r.contains(at));
|
||||
hit.then(|| tip(kind, &rev.author, &crate::local_time::revision_time(rev)))
|
||||
})
|
||||
}
|
||||
|
||||
/// The revision a character's marks point to and what to call it, when the markup shown marks it.
|
||||
/// A deletion lies over an insertion it removes (inserted, then deleted by someone else): the
|
||||
/// deletion is the change the page shows.
|
||||
fn change_of(props: &CharProps, markup: &MarkupOptions, is_move: impl Fn(u32) -> bool) -> Option<(u32, &'static str)> {
|
||||
if markup.insertions_deletions {
|
||||
if let Some(i) = props.del {
|
||||
return Some((i, if is_move(i) { tl!("Moved from") } else { tl!("Deleted") }));
|
||||
}
|
||||
if let Some(i) = props.ins {
|
||||
return Some((i, if is_move(i) { tl!("Moved to") } else { tl!("Inserted") }));
|
||||
}
|
||||
}
|
||||
props.fmt_change.as_ref().filter(|_| markup.formatting).map(|c| (c.rev, tl!("Formatted")))
|
||||
}
|
||||
|
||||
/// The tip's lines: the change's kind, its author (or "Author" when the file names none, as the
|
||||
/// balloons do) and its time, left out when the file gives none.
|
||||
fn tip(kind: &str, author: &str, when: &str) -> String {
|
||||
let author = if author.trim().is_empty() { tl!("Author") } else { author };
|
||||
let mut lines = vec![kind, author];
|
||||
if !when.is_empty() {
|
||||
lines.push(when);
|
||||
}
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use wordcraft_doc::props::PropChange;
|
||||
|
||||
/// A deletion over an insertion is the change shown; formatting counts only while the markup
|
||||
/// shows formatting, and insertions and deletions only while it shows them.
|
||||
#[test]
|
||||
fn the_shown_markup_picks_the_change() {
|
||||
let all = MarkupOptions { insertions_deletions: true, formatting: true, ..Default::default() };
|
||||
let no_move = |_| false;
|
||||
let both = CharProps { ins: Some(0), del: Some(1), ..Default::default() };
|
||||
assert_eq!(change_of(&both, &all, no_move), Some((1, "Deleted")));
|
||||
assert_eq!(change_of(&both, &all, |i| i == 1), Some((1, "Moved from")));
|
||||
let inserted = CharProps { ins: Some(2), ..Default::default() };
|
||||
assert_eq!(change_of(&inserted, &all, no_move), Some((2, "Inserted")));
|
||||
assert_eq!(change_of(&inserted, &all, |_| true), Some((2, "Moved to")));
|
||||
let formatted = CharProps { fmt_change: PropChange::boxed(3, CharProps::default()), ..Default::default() };
|
||||
assert_eq!(change_of(&formatted, &all, no_move), Some((3, "Formatted")));
|
||||
assert_eq!(change_of(&formatted, &MarkupOptions { formatting: false, ..all.clone() }, no_move), None);
|
||||
assert_eq!(change_of(&inserted, &MarkupOptions { insertions_deletions: false, ..all.clone() }, no_move), None);
|
||||
assert_eq!(change_of(&CharProps::default(), &all, no_move), None);
|
||||
}
|
||||
|
||||
/// The tip's lines; an unnamed author reads "Author", and a missing time is left out.
|
||||
#[test]
|
||||
fn the_tip_names_kind_author_and_time() {
|
||||
assert_eq!(tip("Inserted", "Ada", "2026-11-20 14:05"), "Inserted\nAda\n2026-11-20 14:05");
|
||||
assert_eq!(tip("Deleted", " ", ""), "Deleted\nAuthor");
|
||||
}
|
||||
}
|
||||
@@ -889,3 +889,91 @@ fn a_right_click_keeps_selected_cells_only_on_them() {
|
||||
assert_menu(&h, &["Split Cells"], true);
|
||||
assert_menu(&h, &["Merge Cells"], false);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------
|
||||
// Tracked change tips
|
||||
// ---------------------------------------------------------------------------------------
|
||||
|
||||
/// #7887: resting the pointer on a tracked insertion shows a tip with the change's kind, its
|
||||
/// author and its time; beside the change, or with markup hidden, there is none.
|
||||
#[test]
|
||||
fn hovering_a_tracked_change_shows_its_author_and_time() {
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Plain words here")), Default::default());
|
||||
a.session.author = "Ada Reviewer".into();
|
||||
a.run("caret.docStart", json!({})).unwrap();
|
||||
a.run("review.trackChanges", json!({"value": true})).unwrap();
|
||||
a.run("text.insert", json!({"text": "Inserted "})).unwrap();
|
||||
assert_eq!(a.session.doc.revisions.len(), 1, "one tracked insertion");
|
||||
// A fixed time with no UTC time beside it, as a file's change has, so it shows as written
|
||||
// whatever the machine's zone (`local_time` covers the changes made here).
|
||||
a.session.doc.revisions[0].date = "2026-11-20T14:05:00Z".into();
|
||||
a.session.doc.revisions[0].date_utc = None;
|
||||
let mut h = window(a);
|
||||
let point_at = |h: &mut H, off: usize| {
|
||||
let c = h.state_mut().session.layout().caret(&Pos { story: StoryRef::Body, path: wordcraft_doc::Path::top(0), off }).unwrap();
|
||||
let at = screen(h, c.x, c.top + c.height / 2.0);
|
||||
h.event(egui::Event::PointerMoved(at));
|
||||
super::rest_for_tip(h);
|
||||
};
|
||||
let tips = |h: &H| h.query_all_by_label_contains("Inserted Ada Reviewer 2026-11-20 14:05").count();
|
||||
// Between "Inserted" and its "e", then on "words", which was there before tracking began.
|
||||
point_at(&mut h, 2);
|
||||
assert!(tips(&h) > 0, "the inserted text's tip says it was inserted, by whom and when");
|
||||
point_at(&mut h, "Inserted Plain w".len());
|
||||
assert_eq!(tips(&h), 0, "text that wasn't changed has no tip");
|
||||
h.state_mut().session.view.show_markup = false;
|
||||
point_at(&mut h, 2);
|
||||
assert_eq!(tips(&h), 0, "with markup hidden the change isn't marked, so it has no tip");
|
||||
}
|
||||
|
||||
/// #7887: a paragraph whose own formatting changed while tracking (here its alignment) shows a
|
||||
/// Formatted tip on its text, while the markup shows formatting.
|
||||
#[test]
|
||||
fn hovering_a_paragraph_with_tracked_formatting_shows_formatted() {
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("A centred line")), Default::default());
|
||||
a.session.author = "Ada Reviewer".into();
|
||||
a.run("review.trackChanges", json!({"value": true})).unwrap();
|
||||
a.run("para.alignCenter", json!({})).unwrap();
|
||||
assert!(a.session.doc.body[0].as_para().is_some_and(|p| p.props.fmt_change.is_some()), "the alignment is a tracked change");
|
||||
let mut h = window(a);
|
||||
let point_at = |h: &mut H| {
|
||||
let c = h.state_mut().session.layout().caret(&Pos { story: StoryRef::Body, path: wordcraft_doc::Path::top(0), off: 4 }).unwrap();
|
||||
let at = screen(h, c.x, c.top + c.height / 2.0);
|
||||
h.event(egui::Event::PointerMoved(at));
|
||||
super::rest_for_tip(h);
|
||||
};
|
||||
let tips = |h: &H| h.query_all_by_label_contains("Formatted Ada Reviewer").count();
|
||||
point_at(&mut h);
|
||||
assert!(tips(&h) > 0, "the paragraph's text shows its formatting change");
|
||||
h.state_mut().session.prefs.markup.formatting = false;
|
||||
// The canvas asks for a tip again only when the pointer moves: away to the toggle and back.
|
||||
h.event(egui::Event::PointerGone);
|
||||
point_at(&mut h);
|
||||
assert_eq!(tips(&h), 0, "with formatting markup off the change isn't marked, so it has no tip");
|
||||
}
|
||||
|
||||
/// #7887 review: a link in a footnote shows its tip while the caret is in the body. The hover
|
||||
/// looks in the story under the pointer, so the link's tip still comes before the tracked
|
||||
/// change tip there.
|
||||
#[test]
|
||||
fn a_footnote_link_shows_its_tip_with_the_caret_in_the_body() {
|
||||
let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::from_text("Body text")), Default::default());
|
||||
a.run("caret.docEnd", json!({})).unwrap();
|
||||
a.run("review.trackChanges", json!({"value": true})).unwrap();
|
||||
a.run("references.footnote", json!({"text": "Note link"})).unwrap();
|
||||
let note = a.session.sel.focus.story;
|
||||
let pos = |off: usize| json!({"story": note, "path": [0], "off": off});
|
||||
a.run("select.range", json!({"anchor": pos(5), "focus": pos(9)})).unwrap();
|
||||
a.run("insert.link", json!({"url": "https://example.com/note"})).unwrap();
|
||||
a.run("caret.set", json!({"pos": {"block": 0, "off": 2}})).unwrap();
|
||||
assert_eq!(a.session.sel.focus.story, StoryRef::Body);
|
||||
// The whole page in the window, footnote area and all.
|
||||
a.run("view.zoom", json!({"value": 40})).unwrap();
|
||||
let mut h = window(a);
|
||||
let c = h.state_mut().session.layout().caret(&Pos { story: note, path: wordcraft_doc::Path::top(0), off: 7 }).unwrap();
|
||||
let at = screen(&mut h, c.x, c.top + c.height / 2.0);
|
||||
h.event(egui::Event::PointerMoved(at));
|
||||
super::rest_for_tip(&mut h);
|
||||
assert!(h.query_all_by_label_contains("https://example.com/note").count() > 0, "the footnote's link shows its tip (pointer at {at:?})");
|
||||
assert_eq!(h.query_all_by_label_contains("Inserted ").count(), 0, "not the tracked insertion's tip");
|
||||
}
|
||||
|
||||
@@ -29,6 +29,19 @@ pub(crate) fn comment_time(c: &wordcraft_doc::Comment) -> String {
|
||||
stamp(&c.date, c.date_utc.as_deref(), offset_minutes)
|
||||
}
|
||||
|
||||
/// A tracked change's time for its hover tip, read as [`comment_time`] reads a comment: the UTC
|
||||
/// time (`date_utc`, Word's `w16du:dateUtc`, which WordCraft keeps for the changes it makes) wins
|
||||
/// and shows in local time. Without it (`w:date` only), Word writes local time there with a `Z`
|
||||
/// it doesn't mean and shows it as written, so it shows as written here too; a time with an
|
||||
/// offset (`±HH:MM`) is converted. Empty when it isn't a date.
|
||||
pub(crate) fn revision_time(r: &wordcraft_doc::Revision) -> String {
|
||||
revision_stamp(r, offset_minutes)
|
||||
}
|
||||
|
||||
fn revision_stamp(r: &wordcraft_doc::Revision, offset: impl Fn(i64) -> i32) -> String {
|
||||
stamp(&r.date, r.date_utc.as_deref(), offset)
|
||||
}
|
||||
|
||||
fn stamp(date: &str, date_utc: Option<&str>, offset: impl Fn(i64) -> i32) -> String {
|
||||
let utc = date_utc.and_then(parse).filter(|t| t.timed).map(|t| Time { zone: Some(t.zone.unwrap_or(0)), ..t });
|
||||
let local_date = || parse(date).map(|t| if date.trim_end().ends_with(['Z', 'z']) { Time { zone: None, ..t } } else { t });
|
||||
@@ -185,6 +198,28 @@ mod tests {
|
||||
assert_eq!(stamp("2026-10-12Z", Some("2026-10-12"), |_| -4 * 60), "2026-10-12");
|
||||
}
|
||||
|
||||
/// A change WordCraft makes keeps its UTC time, so its tip shows the user's local time, here
|
||||
/// two hours east of UTC; a change from a file with only a `Z` date still shows as written
|
||||
/// (#7887).
|
||||
#[test]
|
||||
fn changes_made_here_show_in_local_time() {
|
||||
let utc_plus_2 = |_: i64| 2 * 60;
|
||||
let made = wordcraft_engine::cmd::made_revision(wordcraft_doc::RevisionKind::Insert, "Ada".into(), "2026-11-20T12:05:00Z".into());
|
||||
assert_eq!(revision_stamp(&made, utc_plus_2), "2026-11-20 14:05");
|
||||
let imported = wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, "Ada", "2026-11-20T12:05:00Z");
|
||||
assert_eq!(revision_stamp(&imported, utc_plus_2), "2026-11-20 12:05");
|
||||
}
|
||||
|
||||
/// A tracked change from a file with only its `w:date` (#7887): shown as written, `Z` or not,
|
||||
/// as Word shows it.
|
||||
#[test]
|
||||
fn revision_times_show_as_written() {
|
||||
let at = |date: &str| revision_time(&wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, "Ada", date));
|
||||
assert_eq!(at("2026-11-20T14:05:00Z"), "2026-11-20 14:05");
|
||||
assert_eq!(at("2026-11-20"), "2026-11-20");
|
||||
assert_eq!(at("not a date"), "");
|
||||
}
|
||||
|
||||
/// Whatever a file holds, a stamp is a date or nothing, never a panic.
|
||||
#[test]
|
||||
fn hostile_times_show_nothing() {
|
||||
|
||||
+2
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user