mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 08:46:37 -04:00
move note undo into separate file
This commit is contained in:
parent
d2ebfc5482
commit
ad74e26c84
5 changed files with 117 additions and 106 deletions
|
@ -183,7 +183,7 @@ impl Collection {
|
|||
}
|
||||
for nid in nids {
|
||||
if self.storage.note_is_orphaned(nid)? {
|
||||
self.remove_note_only(nid, usn)?;
|
||||
self.remove_note_only_undoable(nid, usn)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use crate::prelude::*;
|
|||
pub(crate) struct CardAdded(Card);
|
||||
|
||||
impl Undo for CardAdded {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.remove_card_only(self.0)
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ impl Undo for CardAdded {
|
|||
pub(crate) struct CardRemoved(Card);
|
||||
|
||||
impl Undo for CardRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.readd_deleted_card(self.0)
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ impl Undo for CardRemoved {
|
|||
pub(crate) struct CardGraveAdded(CardID, Usn);
|
||||
|
||||
impl Undo for CardGraveAdded {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.remove_card_grave(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ impl Undo for CardGraveAdded {
|
|||
pub(crate) struct CardGraveRemoved(CardID, Usn);
|
||||
|
||||
impl Undo for CardGraveRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.add_card_grave_undoable(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ impl Undo for CardGraveRemoved {
|
|||
pub(crate) struct CardUpdated(Card);
|
||||
|
||||
impl Undo for CardUpdated {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
let current = col
|
||||
.storage
|
||||
.get_card(self.0.id)?
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
mod undo;
|
||||
|
||||
use crate::backend_proto::note_is_duplicate_or_empty_out::State as DuplicateState;
|
||||
use crate::{
|
||||
backend_proto as pb,
|
||||
decks::DeckID,
|
||||
|
@ -13,7 +16,6 @@ use crate::{
|
|||
timestamp::TimestampSecs,
|
||||
types::Usn,
|
||||
};
|
||||
use crate::{backend_proto::note_is_duplicate_or_empty_out::State as DuplicateState, undo::Undo};
|
||||
use itertools::Itertools;
|
||||
use num_integer::Integer;
|
||||
use regex::{Regex, Replacer};
|
||||
|
@ -318,17 +320,10 @@ impl Collection {
|
|||
self.canonify_note_tags(note, ctx.usn)?;
|
||||
note.prepare_for_update(&ctx.notetype, normalize_text)?;
|
||||
note.set_modified(ctx.usn);
|
||||
self.storage.add_note(note)?;
|
||||
self.save_undo(Box::new(NoteAdded(note.clone())));
|
||||
self.add_note_only_undoable(note)?;
|
||||
self.generate_cards_for_new_note(ctx, note, did)
|
||||
}
|
||||
|
||||
fn add_note_for_undo(&mut self, note: Note) -> Result<()> {
|
||||
self.storage.add_or_update_note(¬e)?;
|
||||
self.save_undo(Box::new(NoteAdded(note)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_note(&mut self, note: &mut Note) -> Result<()> {
|
||||
self.update_note_with_op(note, Some(UndoableOp::UpdateNote))
|
||||
}
|
||||
|
@ -387,46 +382,7 @@ impl Collection {
|
|||
if mark_note_modified {
|
||||
note.set_modified(usn);
|
||||
}
|
||||
self.update_note_inner_undo_only(note, original)
|
||||
}
|
||||
|
||||
/// Saves in the undo queue, and commits to DB.
|
||||
/// No validation, card generation or normalization is done.
|
||||
pub(crate) fn update_note_inner_undo_only(
|
||||
&mut self,
|
||||
note: &mut Note,
|
||||
original: &Note,
|
||||
) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteUpdated(original.clone())));
|
||||
self.storage.update_note(note)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a note. Cards must already have been deleted.
|
||||
pub(crate) fn remove_note_only(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
if let Some(note) = self.storage.get_note(nid)? {
|
||||
self.save_undo(Box::new(NoteRemoved(note)));
|
||||
self.storage.remove_note(nid)?;
|
||||
self.add_note_grave(nid, usn)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_note_grave(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteGraveAdded(nid, usn)));
|
||||
self.storage.add_note_grave(nid, usn)
|
||||
}
|
||||
|
||||
fn remove_note_grave_for_undo(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteGraveRemoved(nid, usn)));
|
||||
self.storage.remove_note_grave(nid)
|
||||
}
|
||||
|
||||
fn remove_note_for_undo(&mut self, note: Note) -> Result<()> {
|
||||
self.storage.remove_note(note.id)?;
|
||||
self.save_undo(Box::new(NoteRemoved(note)));
|
||||
Ok(())
|
||||
self.update_note_undoable(note, original)
|
||||
}
|
||||
|
||||
/// Remove provided notes, and any cards that use them.
|
||||
|
@ -439,7 +395,7 @@ impl Collection {
|
|||
for card in col.storage.all_cards_of_note(nid)? {
|
||||
col.remove_card_and_add_grave_undoable(card, usn)?;
|
||||
}
|
||||
col.remove_note_only(nid, usn)?;
|
||||
col.remove_note_only_undoable(nid, usn)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -590,55 +546,6 @@ fn note_modified(existing_note: &mut Note, note: &Note) -> bool {
|
|||
notes_differ
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteAdded(Note);
|
||||
|
||||
impl Undo for NoteAdded {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
col.remove_note_for_undo(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteRemoved(Note);
|
||||
|
||||
impl Undo for NoteRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
col.add_note_for_undo(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteGraveAdded(NoteID, Usn);
|
||||
|
||||
impl Undo for NoteGraveAdded {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
col.remove_note_grave_for_undo(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteGraveRemoved(NoteID, Usn);
|
||||
|
||||
impl Undo for NoteGraveRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
col.add_note_grave(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteUpdated(Note);
|
||||
|
||||
impl Undo for NoteUpdated {
|
||||
fn undo(self: Box<Self>, col: &mut crate::collection::Collection) -> Result<()> {
|
||||
let current = col
|
||||
.storage
|
||||
.get_note(self.0.id)?
|
||||
.ok_or_else(|| AnkiError::invalid_input("note disappeared"))?;
|
||||
col.update_note_inner_undo_only(&mut self.0.clone(), ¤t)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{anki_base91, field_checksum};
|
104
rslib/src/notes/undo.rs
Normal file
104
rslib/src/notes/undo.rs
Normal file
|
@ -0,0 +1,104 @@
|
|||
// Copyright: Ankitects Pty Ltd and contributors
|
||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::undo::Undo;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteAdded(Note);
|
||||
|
||||
impl Undo for NoteAdded {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.remove_note_for_undo(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteRemoved(Note);
|
||||
|
||||
impl Undo for NoteRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.add_note_for_undo(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteGraveAdded(NoteID, Usn);
|
||||
|
||||
impl Undo for NoteGraveAdded {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.remove_note_grave_for_undo(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteGraveRemoved(NoteID, Usn);
|
||||
|
||||
impl Undo for NoteGraveRemoved {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
col.add_note_grave(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct NoteUpdated(Note);
|
||||
|
||||
impl Undo for NoteUpdated {
|
||||
fn undo(self: Box<Self>, col: &mut Collection) -> Result<()> {
|
||||
let current = col
|
||||
.storage
|
||||
.get_note(self.0.id)?
|
||||
.ok_or_else(|| AnkiError::invalid_input("note disappeared"))?;
|
||||
col.update_note_undoable(&mut self.0.clone(), ¤t)
|
||||
}
|
||||
}
|
||||
|
||||
impl Collection {
|
||||
/// Saves in the undo queue, and commits to DB.
|
||||
/// No validation, card generation or normalization is done.
|
||||
pub(super) fn update_note_undoable(&mut self, note: &mut Note, original: &Note) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteUpdated(original.clone())));
|
||||
self.storage.update_note(note)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a note. Cards must already have been deleted.
|
||||
pub(crate) fn remove_note_only_undoable(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
if let Some(note) = self.storage.get_note(nid)? {
|
||||
self.save_undo(Box::new(NoteRemoved(note)));
|
||||
self.storage.remove_note(nid)?;
|
||||
self.add_note_grave(nid, usn)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a note, not adding any cards.
|
||||
pub(super) fn add_note_only_undoable(&mut self, note: &mut Note) -> Result<(), AnkiError> {
|
||||
self.storage.add_note(note)?;
|
||||
self.save_undo(Box::new(NoteAdded(note.clone())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_note_grave(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteGraveAdded(nid, usn)));
|
||||
self.storage.add_note_grave(nid, usn)
|
||||
}
|
||||
|
||||
fn remove_note_grave_for_undo(&mut self, nid: NoteID, usn: Usn) -> Result<()> {
|
||||
self.save_undo(Box::new(NoteGraveRemoved(nid, usn)));
|
||||
self.storage.remove_note_grave(nid)
|
||||
}
|
||||
|
||||
fn remove_note_for_undo(&mut self, note: Note) -> Result<()> {
|
||||
self.storage.remove_note(note.id)?;
|
||||
self.save_undo(Box::new(NoteRemoved(note)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_note_for_undo(&mut self, note: Note) -> Result<()> {
|
||||
self.storage.add_or_update_note(¬e)?;
|
||||
self.save_undo(Box::new(NoteAdded(note)));
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1531,7 +1531,7 @@ mod test {
|
|||
// fixme: inconsistent usn arg
|
||||
col1.remove_cards_and_orphaned_notes(&[cardid])?;
|
||||
let usn = col1.usn()?;
|
||||
col1.remove_note_only(noteid, usn)?;
|
||||
col1.remove_note_only_undoable(noteid, usn)?;
|
||||
col1.remove_deck_and_child_decks(deckid)?;
|
||||
|
||||
let out = ctx.normal_sync(&mut col1).await;
|
||||
|
|
Loading…
Reference in a new issue