mirror of
https://github.com/ankitects/anki.git
synced 2025-12-12 22:36:55 -05:00
expose step counter and undone op changes in hook
This commit is contained in:
parent
e9e1edc64d
commit
3736e63a57
5 changed files with 20 additions and 5 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from anki.collection import LegacyCheckpoint, LegacyReviewUndo
|
from anki.collection import LegacyCheckpoint, LegacyReviewUndo, OpChangesAfterUndo
|
||||||
from anki.errors import UndoEmpty
|
from anki.errors import UndoEmpty
|
||||||
from anki.types import assert_exhaustive
|
from anki.types import assert_exhaustive
|
||||||
from aqt import gui_hooks
|
from aqt import gui_hooks
|
||||||
|
|
@ -15,6 +15,10 @@ from aqt.utils import showInfo, showWarning, tooltip, tr
|
||||||
def undo(*, parent: QWidget) -> None:
|
def undo(*, parent: QWidget) -> None:
|
||||||
"Undo the last operation, and refresh the UI."
|
"Undo the last operation, and refresh the UI."
|
||||||
|
|
||||||
|
def on_success(out: OpChangesAfterUndo) -> None:
|
||||||
|
gui_hooks.state_did_undo(out)
|
||||||
|
tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
|
||||||
|
|
||||||
def on_failure(exc: Exception) -> None:
|
def on_failure(exc: Exception) -> None:
|
||||||
if isinstance(exc, UndoEmpty):
|
if isinstance(exc, UndoEmpty):
|
||||||
# backend has no undo, but there may be a checkpoint
|
# backend has no undo, but there may be a checkpoint
|
||||||
|
|
@ -23,9 +27,9 @@ def undo(*, parent: QWidget) -> None:
|
||||||
else:
|
else:
|
||||||
showWarning(str(exc), parent=parent)
|
showWarning(str(exc), parent=parent)
|
||||||
|
|
||||||
CollectionOp(parent, lambda col: col.undo()).success(
|
CollectionOp(parent, lambda col: col.undo()).success(on_success).failure(
|
||||||
lambda out: tooltip(tr.undo_action_undone(action=out.operation), parent=parent)
|
on_failure
|
||||||
).failure(on_failure).run_in_background()
|
).run_in_background()
|
||||||
|
|
||||||
|
|
||||||
def _legacy_undo(*, parent: QWidget) -> None:
|
def _legacy_undo(*, parent: QWidget) -> None:
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ from anki.cards import Card
|
||||||
from anki.decks import DeckDict, DeckConfigDict
|
from anki.decks import DeckDict, DeckConfigDict
|
||||||
from anki.hooks import runFilter, runHook
|
from anki.hooks import runFilter, runHook
|
||||||
from anki.models import NotetypeDict
|
from anki.models import NotetypeDict
|
||||||
|
from anki.collection import OpChangesAfterUndo
|
||||||
from aqt.qt import QDialog, QEvent, QMenu, QWidget
|
from aqt.qt import QDialog, QEvent, QMenu, QWidget
|
||||||
from aqt.tagedit import TagEdit
|
from aqt.tagedit import TagEdit
|
||||||
"""
|
"""
|
||||||
|
|
@ -475,7 +476,12 @@ hooks = [
|
||||||
name="state_did_revert",
|
name="state_did_revert",
|
||||||
args=["action: str"],
|
args=["action: str"],
|
||||||
legacy_hook="revertedState",
|
legacy_hook="revertedState",
|
||||||
doc="Called when user used the undo option to restore to an earlier database state.",
|
doc="Legacy hook, called after undoing.",
|
||||||
|
),
|
||||||
|
Hook(
|
||||||
|
name="state_did_undo",
|
||||||
|
args=["changes: OpChangesAfterUndo"],
|
||||||
|
doc="Called after backend undoes a change.",
|
||||||
),
|
),
|
||||||
Hook(
|
Hook(
|
||||||
name="state_did_reset",
|
name="state_did_reset",
|
||||||
|
|
|
||||||
|
|
@ -1562,6 +1562,7 @@ message OpChangesAfterUndo {
|
||||||
string operation = 2;
|
string operation = 2;
|
||||||
int64 reverted_to_timestamp = 3;
|
int64 reverted_to_timestamp = 3;
|
||||||
UndoStatus new_status = 4;
|
UndoStatus new_status = 4;
|
||||||
|
uint32 counter = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message DefaultsForAddingIn {
|
message DefaultsForAddingIn {
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,7 @@ impl OpOutput<UndoOutput> {
|
||||||
operation: self.output.undone_op.describe(tr),
|
operation: self.output.undone_op.describe(tr),
|
||||||
reverted_to_timestamp: self.output.reverted_to.0,
|
reverted_to_timestamp: self.output.reverted_to.0,
|
||||||
new_status: Some(self.output.new_undo_status.into_protobuf(tr)),
|
new_status: Some(self.output.new_undo_status.into_protobuf(tr)),
|
||||||
|
counter: self.output.counter as u32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ pub struct UndoOutput {
|
||||||
pub undone_op: Op,
|
pub undone_op: Op,
|
||||||
pub reverted_to: TimestampSecs,
|
pub reverted_to: TimestampSecs,
|
||||||
pub new_undo_status: UndoStatus,
|
pub new_undo_status: UndoStatus,
|
||||||
|
pub counter: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
|
|
@ -285,6 +286,7 @@ impl Collection {
|
||||||
let undone_op = step.kind;
|
let undone_op = step.kind;
|
||||||
let reverted_to = step.timestamp;
|
let reverted_to = step.timestamp;
|
||||||
let changes = step.changes;
|
let changes = step.changes;
|
||||||
|
let counter = step.counter;
|
||||||
self.state.undo.mode = mode;
|
self.state.undo.mode = mode;
|
||||||
let res = self.transact(undone_op.clone(), |col| {
|
let res = self.transact(undone_op.clone(), |col| {
|
||||||
for change in changes.into_iter().rev() {
|
for change in changes.into_iter().rev() {
|
||||||
|
|
@ -294,6 +296,7 @@ impl Collection {
|
||||||
undone_op,
|
undone_op,
|
||||||
reverted_to,
|
reverted_to,
|
||||||
new_undo_status: col.undo_status(),
|
new_undo_status: col.undo_status(),
|
||||||
|
counter,
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
self.state.undo.mode = UndoMode::NormalOp;
|
self.state.undo.mode = UndoMode::NormalOp;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue