Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M7: keep a paragraph open with its typing when its edit fails

komo1iddin08513ad906eb
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index eb9244a28..98faf48d9 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -499,6 +499,9 @@ pub struct DocView {
/// line being edited.
pub(crate) edit_lines: HashMap<usize, (u64, Vec<pdfcraft_engine::TextBlock>)>,
pub line_editor: Option<crate::edit_text_ui::LineEditor>,
+ /// The paragraph editor whose edit was just queued. It opens again with its typing if the
+ /// edit fails, as a refused form field does.
+ pub(crate) applied_editor: Option<crate::edit_text_ui::LineEditor>,
/// The box a click switched to while another paragraph was being edited: it opens once the
/// queued edit has applied, so its index and geometry come from the document it now is (#2022).
pub(crate) pending_text_open: Option<(usize, usize)>,
@@ -769,6 +772,7 @@ impl DocView {
pending_edit: None,
edit_lines: HashMap::new(),
line_editor: None,
+ applied_editor: None,
pending_text_open: None,
text_background: Default::default(),
move_background: Default::default(),
diff --git a/crates/ui-egui/src/edit_text_ui.rs b/crates/ui-egui/src/edit_text_ui.rs
index 66ad98ffe..cb9dd3628 100644
--- a/crates/ui-egui/src/edit_text_ui.rs
+++ b/crates/ui-egui/src/edit_text_ui.rs
@@ -158,7 +158,8 @@ pub struct LineEditor {
max_width: f32,
/// Current screen-space size. Recomputed from the current page transform before painting.
size: f32,
- focus: bool,
+ /// Take the keyboard focus on the next frame.
+ pub(crate) focus: bool,
/// The Format text panel's values, and what the paragraph had (to send only changes).
pub look: pdfcraft_engine::AddedText,
look0: pdfcraft_engine::AddedText,
@@ -1101,6 +1102,7 @@ pub(crate) fn page_input(
if let Some(old) = view.line_editor.take() {
let style = old.style();
if old.text != old.original || style != pdfcraft_engine::BlockStyle::default() {
+ view.applied_editor = Some(old.clone());
view.pending_edit = Some(Edit::EditTextBlock { page: old.page, block: old.block, text: old.text, style });
}
}
@@ -1422,7 +1424,11 @@ pub(crate) fn overlay(ctx: &egui::Context, view: &mut DocView, info: &DocInfo) -
match done {
Some(apply) => {
let ed = view.line_editor.take()?;
- if apply { finished_edit(ed) } else { None }
+ if !apply {
+ return None;
+ }
+ view.applied_editor = Some(ed.clone());
+ finished_edit(ed)
}
None => None,
}
diff --git a/crates/ui-egui/src/editing.rs b/crates/ui-egui/src/editing.rs
index 91480ac7b..4d390fd99 100644
--- a/crates/ui-egui/src/editing.rs
+++ b/crates/ui-egui/src/editing.rs
@@ -53,6 +53,7 @@ impl PdfCraftApp {
let Some(doc) = self.session.get(id) else { return true };
let info = &doc.info;
let view = &mut self.views[i];
+ view.applied_editor = None;
// A picture picked with the Select tool may be the one this edit removed or moved:
// drop its highlight now, not on the next frame (which only comes with the next input).
view.picked_image = None;
@@ -181,6 +182,15 @@ impl PdfCraftApp {
if let Some(view) = self.views.get_mut(i) {
// A drawing whose annotation wasn't added must not stay painted.
view.comments.drawings_refused();
+ // A paragraph that couldn't be applied opens again with its typing, instead of
+ // the box the click moved to.
+ if let Edit::EditTextBlock { page, block, .. } = &edit
+ && let Some(mut ed) = view.applied_editor.take().filter(|ed| (ed.page, ed.block) == (*page, *block))
+ {
+ ed.focus = true;
+ view.line_editor = Some(ed);
+ view.pending_text_open = None;
+ }
}
let message = if matches!(edit, Edit::MoveObjects { .. }) { crate::object_ui::move_error(&e) } else { e.to_string() };
self.notify_fmt("{label} failed: {e}", &[("label", &crate::i18n::edit_label(&label)), ("e", &message)]);
diff --git a/crates/ui-egui/tests/editing.rs b/crates/ui-egui/tests/editing.rs
index d41041e52..839e39832 100644
--- a/crates/ui-egui/tests/editing.rs
+++ b/crates/ui-egui/tests/editing.rs
@@ -2735,6 +2735,46 @@ fn switching_straight_to_another_box_applies_the_open_paragraph() {
assert_eq!(ed.text, "Bottom");
}
+/// A paragraph whose edit fails (here a letter no font can draw) stays open with what was typed,
+/// as a form field does; the editor used to close with the error and the typing was lost.
+#[test]
+fn a_paragraph_whose_edit_fails_stays_open_with_its_typing() {
+ let bytes = source_font_fixture_with_text("BT /F1 24 Tf 20 220 Td (Top) Tj ET BT /F2 24 Tf 20 120 Td (Bottom) Tj ET");
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("boxes.pdf", None, bytes).expect("opens");
+ app
+ });
+ h.run_steps(4);
+ assert!(h.state_mut().execute("edit.edit_text"));
+ h.run_steps(2);
+ let r = h.state().views[0].page_screen_rect(0).expect("on screen");
+ let k = r.width() / 300.0;
+ let top = egui::pos2(r.left() + 30.0 * k, r.top() + (300.0 - 228.0) * k);
+ click_at(&mut h, top);
+ assert_eq!(h.state().views[0].line_editor.as_ref().map(|e| e.text.as_str()), Some("Top"));
+ // U+049B (Kazakh and Uzbek Cyrillic): neither the page's font nor a fallback face draws it.
+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = "Top қ".into();
+ h.run_steps(1);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
+ h.run_steps(3);
+ let s = h.state();
+ assert!(s.toast.as_ref().is_some_and(|(m, _)| m.starts_with("Edit text failed")), "the failure is said: {:?}", s.toast);
+ assert_eq!(s.session.get(s.views[0].id).unwrap().can_undo(), None, "nothing was applied");
+ let ed = s.views[0].line_editor.as_ref().expect("the paragraph is still open");
+ assert_eq!((ed.block, ed.text.as_str()), (0, "Top қ"), "with what was typed");
+ // Taking the letter out applies it.
+ h.state_mut().views[0].line_editor.as_mut().unwrap().text = "Top edited".into();
+ h.run_steps(1);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::Enter);
+ h.run_steps(3);
+ let s = h.state();
+ assert!(s.views[0].line_editor.is_none());
+ let doc = s.session.get(s.views[0].id).unwrap();
+ assert_eq!(doc.text_blocks(0).iter().map(|b| b.text.clone()).collect::<Vec<_>>(), ["Top edited", "Bottom"]);
+}
+
#[test]
fn editing_existing_text_in_place() {
let mut h = harness(1, |_| {});