mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
update note ops
remove_note() now returns the count of removed cards, allowing us to unify the tooltip between browser and review screen I've left the old translation in - we'll need to write a script at one point that gathers all references to translations in the code, and shows ones that are unused.
This commit is contained in:
parent
bc78b6ef17
commit
2de8cc1a94
9 changed files with 30 additions and 38 deletions
|
@ -385,7 +385,7 @@ class Collection:
|
|||
note.id = NoteId(out.note_id)
|
||||
return out.changes
|
||||
|
||||
def remove_notes(self, note_ids: Sequence[NoteId]) -> OpChanges:
|
||||
def remove_notes(self, note_ids: Sequence[NoteId]) -> OpChangesWithCount:
|
||||
hooks.notes_will_be_deleted(self, note_ids)
|
||||
return self._backend.remove_notes(note_ids=note_ids, card_ids=[])
|
||||
|
||||
|
|
|
@ -208,9 +208,9 @@ class AddCards(QDialog):
|
|||
self._load_new_note(sticky_fields_from=note)
|
||||
gui_hooks.add_cards_did_add_note(note)
|
||||
|
||||
add_note(
|
||||
mw=self.mw, note=note, target_deck_id=target_deck_id, success=on_success
|
||||
)
|
||||
add_note(parent=self, note=note, target_deck_id=target_deck_id).success(
|
||||
on_success
|
||||
).run_in_background()
|
||||
|
||||
def _note_can_be_added(self, note: Note) -> bool:
|
||||
result = note.duplicate_or_empty()
|
||||
|
|
|
@ -642,11 +642,7 @@ where id in %s"""
|
|||
self.focusTo = self.editor.currentField
|
||||
self.table.to_next_row()
|
||||
|
||||
remove_notes(
|
||||
mw=self.mw,
|
||||
note_ids=nids,
|
||||
success=lambda _: tooltip(tr.browsing_note_deleted(count=len(nids))),
|
||||
)
|
||||
remove_notes(parent=self, note_ids=nids).run_in_background()
|
||||
|
||||
# legacy
|
||||
|
||||
|
|
|
@ -558,7 +558,9 @@ class Editor:
|
|||
|
||||
def _save_current_note(self) -> None:
|
||||
"Call after note is updated with data from webview."
|
||||
update_note(mw=self.mw, note=self.note, handler=self)
|
||||
update_note(parent=self.widget, note=self.note).run_in_background(
|
||||
initiator=self
|
||||
)
|
||||
|
||||
def fonts(self) -> List[Tuple[str, int, bool]]:
|
||||
return [
|
||||
|
|
|
@ -3,35 +3,34 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, Sequence
|
||||
from typing import Sequence
|
||||
|
||||
from anki.collection import OpChanges, OpChangesWithCount
|
||||
from anki.decks import DeckId
|
||||
from anki.notes import Note, NoteId
|
||||
from aqt import AnkiQt
|
||||
from aqt.main import PerformOpOptionalSuccessCallback
|
||||
from aqt.operations import CollectionOp
|
||||
from aqt.qt import QWidget
|
||||
from aqt.utils import tooltip, tr
|
||||
|
||||
|
||||
def add_note(
|
||||
*,
|
||||
mw: AnkiQt,
|
||||
parent: QWidget,
|
||||
note: Note,
|
||||
target_deck_id: DeckId,
|
||||
success: PerformOpOptionalSuccessCallback = None,
|
||||
) -> None:
|
||||
mw.perform_op(lambda: mw.col.add_note(note, target_deck_id), success=success)
|
||||
) -> CollectionOp[OpChanges]:
|
||||
return CollectionOp(parent, lambda col: col.add_note(note, target_deck_id))
|
||||
|
||||
|
||||
def update_note(*, mw: AnkiQt, note: Note, handler: Optional[object]) -> None:
|
||||
mw.perform_op(
|
||||
lambda: mw.col.update_note(note),
|
||||
handler=handler,
|
||||
)
|
||||
def update_note(*, parent: QWidget, note: Note) -> CollectionOp[OpChanges]:
|
||||
return CollectionOp(parent, lambda col: col.update_note(note))
|
||||
|
||||
|
||||
def remove_notes(
|
||||
*,
|
||||
mw: AnkiQt,
|
||||
parent: QWidget,
|
||||
note_ids: Sequence[NoteId],
|
||||
success: PerformOpOptionalSuccessCallback = None,
|
||||
) -> None:
|
||||
mw.perform_op(lambda: mw.col.remove_notes(note_ids), success=success)
|
||||
) -> CollectionOp[OpChangesWithCount]:
|
||||
return CollectionOp(parent, lambda col: col.remove_notes(note_ids)).success(
|
||||
lambda out: tooltip(tr.browsing_cards_deleted(count=out.count)),
|
||||
)
|
||||
|
|
|
@ -901,14 +901,7 @@ time = %(time)d;
|
|||
if self.mw.state != "review" or not self.card:
|
||||
return
|
||||
|
||||
# fixme: pass this back from the backend method instead
|
||||
cnt = len(self.card.note().cards())
|
||||
|
||||
remove_notes(
|
||||
mw=self.mw,
|
||||
note_ids=[self.card.nid],
|
||||
success=lambda _: tooltip(tr.studying_note_and_its_card_deleted(count=cnt)),
|
||||
)
|
||||
remove_notes(parent=self.mw, note_ids=[self.card.nid]).run_in_background()
|
||||
|
||||
def onRecordVoice(self) -> None:
|
||||
def after_record(path: str) -> None:
|
||||
|
|
|
@ -163,7 +163,7 @@ service NotesService {
|
|||
rpc DefaultDeckForNotetype(NotetypeId) returns (DeckId);
|
||||
rpc UpdateNote(UpdateNoteIn) returns (OpChanges);
|
||||
rpc GetNote(NoteId) returns (Note);
|
||||
rpc RemoveNotes(RemoveNotesIn) returns (OpChanges);
|
||||
rpc RemoveNotes(RemoveNotesIn) returns (OpChangesWithCount);
|
||||
rpc ClozeNumbersInNote(Note) returns (ClozeNumbersInNoteOut);
|
||||
rpc AfterNoteUpdates(AfterNoteUpdatesIn) returns (OpChanges);
|
||||
rpc FieldNamesForNotes(FieldNamesForNotesIn) returns (FieldNamesForNotesOut);
|
||||
|
|
|
@ -63,7 +63,7 @@ impl NotesService for Backend {
|
|||
})
|
||||
}
|
||||
|
||||
fn remove_notes(&self, input: pb::RemoveNotesIn) -> Result<pb::OpChanges> {
|
||||
fn remove_notes(&self, input: pb::RemoveNotesIn) -> Result<pb::OpChangesWithCount> {
|
||||
self.with_col(|col| {
|
||||
if !input.note_ids.is_empty() {
|
||||
col.remove_notes(
|
||||
|
|
|
@ -402,19 +402,21 @@ impl Collection {
|
|||
}
|
||||
|
||||
/// Remove provided notes, and any cards that use them.
|
||||
pub(crate) fn remove_notes(&mut self, nids: &[NoteId]) -> Result<OpOutput<()>> {
|
||||
pub(crate) fn remove_notes(&mut self, nids: &[NoteId]) -> Result<OpOutput<usize>> {
|
||||
let usn = self.usn()?;
|
||||
self.transact(Op::RemoveNote, |col| {
|
||||
let mut card_count = 0;
|
||||
for nid in nids {
|
||||
let nid = *nid;
|
||||
if let Some(_existing_note) = col.storage.get_note(nid)? {
|
||||
for card in col.storage.all_cards_of_note(nid)? {
|
||||
card_count += 1;
|
||||
col.remove_card_and_add_grave_undoable(card, usn)?;
|
||||
}
|
||||
col.remove_note_only_undoable(nid, usn)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(card_count)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue