mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-05 12:16:55 -06:00
Merge pull request #9126
This commit is contained in:
@@ -44,8 +44,7 @@ fn card(app: &mut WordApp, ui: &mut Ui, ctx: &egui::Context, document: u64, id:
|
||||
Text::new(tl!("Resolved").to_owned()).show(ui);
|
||||
}
|
||||
});
|
||||
// Editable comment text (one undo step per edit, like the balloons).
|
||||
comment_field(app, ui, document, id);
|
||||
comment_field(app, ui, document, id, resolved);
|
||||
let open_key = egui::Id::unique(("comment_reply_open", document, id));
|
||||
let mut replying = ui.data(|d| d.get_temp::<bool>(open_key)).unwrap_or(false);
|
||||
Row::new().wrap(true).show(ui, |ui, _| {
|
||||
@@ -134,15 +133,16 @@ pub(crate) const COMMENT_ROWS: usize = 10;
|
||||
/// at its own, narrower length would come out lines short of its text.
|
||||
const FIELD_LINES: usize = 2;
|
||||
|
||||
/// A comment's text, editable in place. Typing stays in a draft until the field's value lands (focus leaves it);
|
||||
/// An open comment's text is editable in place. Typing stays in a draft until the field's value lands (focus leaves it);
|
||||
/// then [`commit_comment`] writes it back with one `review.editComment`, so each editing session is one undo step.
|
||||
/// Escape gives back the text from before, so the draft goes.
|
||||
fn comment_field(app: &mut WordApp, ui: &mut Ui, document: u64, id: u32) {
|
||||
fn comment_field(app: &mut WordApp, ui: &mut Ui, document: u64, id: u32, resolved: bool) {
|
||||
let Some(part) = app.session.doc.comments.get(&id).map(|c| c.part) else { return };
|
||||
let (thread, index) = (thread_of(app, id), message_index(app, id));
|
||||
let shown = draft_of(app, id).unwrap_or_else(|| app.session.doc.plain_text(StoryRef::Part(part)));
|
||||
// Read-only where comments can't change (a protected document), so nothing is typed in vain.
|
||||
let editable = crate::app::command_board::command_enabled(app, "review.editComment");
|
||||
let draft = if resolved { None } else { draft_of(app, id) };
|
||||
let shown = draft.unwrap_or_else(|| app.session.doc.plain_text(StoryRef::Part(part)));
|
||||
// Resolved text stays visible, but the card cannot start or commit an edit until reopened.
|
||||
let editable = !resolved && crate::app::command_board::command_enabled(app, "review.editComment");
|
||||
let field = ui.make_persistent_id(("comment_edit", document, id));
|
||||
let out = TextField::new(tl!("Comment").to_owned(), Bind::given(shown))
|
||||
.id(field)
|
||||
@@ -152,6 +152,9 @@ fn comment_field(app: &mut WordApp, ui: &mut Ui, document: u64, id: u32) {
|
||||
.placeholder(tl!("Add a comment").to_owned())
|
||||
.enabled(editable)
|
||||
.show(ui);
|
||||
if resolved {
|
||||
return;
|
||||
}
|
||||
for event in out.events.iter() {
|
||||
match event {
|
||||
EditEvent::Update { value, .. } => drafts(app).editing = Some((thread, index, value.clone())),
|
||||
|
||||
@@ -408,6 +408,64 @@ mod comment_wrapping {
|
||||
assert_eq!(text, "Parent comment. Edited.\nSecond line.", "leaving the field wrote both lines");
|
||||
assert_eq!(h.state().session.undo_depth(), depth + 1, "as one undo step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resolved_comment_card_stays_read_only_until_reopened() {
|
||||
use serde_json::json;
|
||||
use wordcraft_doc::StoryRef;
|
||||
|
||||
const FIRST: &str = "First comment.";
|
||||
const SECOND: &str = "Second comment.";
|
||||
let mut h = pane(&[FIRST.to_owned(), SECOND.to_owned()]);
|
||||
let ids: Vec<u32> = h.state().session.doc.comments.keys().copied().collect();
|
||||
let read = |h: &H, id| {
|
||||
let doc = &h.state().session.doc;
|
||||
doc.plain_text(StoryRef::Part(doc.comments[&id].part))
|
||||
};
|
||||
let attempt_edit = |h: &mut H, text, editable| {
|
||||
let field = field_lines(h, "Comment", text).0.center();
|
||||
click_at(h, field);
|
||||
let focused = [Role::TextInput, Role::MultilineTextInput]
|
||||
.into_iter()
|
||||
.any(|role| h.query_all_by_role_and_label(role, "Comment").any(|node| node.value().as_deref() == Some(text) && node.is_focused()));
|
||||
assert_eq!(focused, editable, "focus for {text:?}");
|
||||
press_key(h, egui::Key::End);
|
||||
type_text(h, " Edited.");
|
||||
let page = h.state().canvas.page_rects().next().map(|(_, r)| r.center()).expect("a page is shown");
|
||||
click_at(h, page);
|
||||
};
|
||||
let resolve =
|
||||
h.query_all_by_role_and_label(Role::Button, "Resolve").map(|node| node.rect().center()).min_by(|a, b| a.y.total_cmp(&b.y)).unwrap();
|
||||
click_at(&mut h, resolve);
|
||||
assert!(h.state().session.doc.comments[&ids[0]].resolved);
|
||||
|
||||
attempt_edit(&mut h, FIRST, false);
|
||||
assert_eq!(read(&h, ids[0]), FIRST);
|
||||
assert!(!crate::panes::has_comment_drafts(h.state()));
|
||||
attempt_edit(&mut h, SECOND, true);
|
||||
assert_eq!(read(&h, ids[1]), "Second comment. Edited.");
|
||||
|
||||
let own_author = h.state().session.doc.comments[&ids[0]].author.clone();
|
||||
h.state_mut().run("file.setAuthor", json!({"name": "Different Author"})).unwrap();
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
assert_ne!(h.state().session.author, own_author);
|
||||
attempt_edit(&mut h, FIRST, false);
|
||||
assert_eq!(read(&h, ids[0]), FIRST);
|
||||
h.state_mut().run("file.setAuthor", json!({"name": own_author})).unwrap();
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
|
||||
let reopen = h.get_by_role_and_label(Role::Button, "Reopen").rect().center();
|
||||
click_at(&mut h, reopen);
|
||||
assert!(!h.state().session.doc.comments[&ids[0]].resolved);
|
||||
attempt_edit(&mut h, FIRST, true);
|
||||
assert_eq!(read(&h, ids[0]), "First comment. Edited.");
|
||||
|
||||
h.state_mut().run("review.restrict", json!({"mode": "readOnly"})).unwrap();
|
||||
crate::test_harness::frames(&mut h, 3);
|
||||
attempt_edit(&mut h, "First comment. Edited.", false);
|
||||
assert_eq!(read(&h, ids[0]), "First comment. Edited.");
|
||||
assert!(!crate::panes::has_comment_drafts(h.state()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Numbered headings are listed with the number they show, and numbered body paragraphs in
|
||||
|
||||
Reference in New Issue
Block a user