Skip to content

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

Autosave: snapshot a new unsaved document before its first edit (#815) (#850)

MarekSpodBiedryf3f4019fad4f
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 335528745..7b4efab47 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -2890,6 +2890,8 @@ impl Session {
let id = self.open(name, None, bytes, None)?;
if let Some(d) = self.docs.iter_mut().find(|d| d.id == id) {
d.dirty = true;
+ // Unsaved work with no edit yet still needs its first recovery snapshot.
+ d.generation += 1;
}
Ok(id)
}
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index e1c57481b..55af82606 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -577,6 +577,19 @@ fn autosave_snapshots_only_changed_documents() {
assert!(s.autosave_snapshots().is_empty(), "saved documents need no recovery");
}
+#[test]
+fn autosave_snapshots_a_new_unsaved_document_once() {
+ let (mut s, clean) = session_with(1);
+ let id = s.open_new("Untitled.pdf", Arc::new(fixture(1))).unwrap();
+ let snaps = s.autosave_snapshots();
+ assert_eq!(snaps.len(), 1, "the new document is unsaved work");
+ assert_eq!(snaps[0].doc, id);
+ assert!(snaps.iter().all(|x| x.doc != clean), "the clean document stays out");
+ assert!(s.autosave_snapshots().is_empty(), "nothing new since the last snapshot");
+ s.apply(id, Edit::RotatePages { pages: vec![0], degrees: 90 }).unwrap();
+ assert_eq!(s.autosave_snapshots().len(), 1, "a later edit is snapshotted again");
+}
+
#[test]
fn recovered_documents_reopen_unsaved_at_their_original_path() {
let (mut s, id) = session_with(2);