mirror of
https://github.com/ankitects/anki.git
synced 2025-12-12 22:36:55 -05:00
hook note_is_being_flushed
I created multiple add-ons which want to transform a note before it is being saved. For example, one add-on trim it, and remove useless line break which arrived by accident. Another add-on want to compile LaTeX as soon as the note is done, and warn the user if LaTeX can't be compiled. Having a hook in pre-flush would be useful here
This commit is contained in:
parent
f7ebb8c28f
commit
b2ef003207
3 changed files with 34 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ import decorator
|
||||||
|
|
||||||
import anki
|
import anki
|
||||||
from anki.cards import Card
|
from anki.cards import Card
|
||||||
|
from anki.notes import Note
|
||||||
|
|
||||||
# New hook/filter handling
|
# New hook/filter handling
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
@ -277,6 +278,32 @@ class _NoteTypeAddedHook:
|
||||||
note_type_added = _NoteTypeAddedHook()
|
note_type_added = _NoteTypeAddedHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _NoteWillFlushHook:
|
||||||
|
"""Allow to change a note before it is added/updated in the database."""
|
||||||
|
|
||||||
|
_hooks: List[Callable[[Note], None]] = []
|
||||||
|
|
||||||
|
def append(self, cb: Callable[[Note], None]) -> None:
|
||||||
|
"""(note: Note)"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(self, cb: Callable[[Note], None]) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def __call__(self, note: Note) -> None:
|
||||||
|
for hook in self._hooks:
|
||||||
|
try:
|
||||||
|
hook(note)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(hook)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
note_will_flush = _NoteWillFlushHook()
|
||||||
|
|
||||||
|
|
||||||
class _NotesWillBeDeletedHook:
|
class _NotesWillBeDeletedHook:
|
||||||
_hooks: List[Callable[["anki.storage._Collection", List[int]], None]] = []
|
_hooks: List[Callable[["anki.storage._Collection", List[int]], None]] = []
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from __future__ import annotations
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
import anki # pylint: disable=unused-import
|
import anki # pylint: disable=unused-import
|
||||||
|
from anki import hooks
|
||||||
from anki.models import Field, NoteType
|
from anki.models import Field, NoteType
|
||||||
from anki.utils import (
|
from anki.utils import (
|
||||||
fieldChecksum,
|
fieldChecksum,
|
||||||
|
|
@ -202,6 +203,7 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)""",
|
||||||
##################################################
|
##################################################
|
||||||
|
|
||||||
def _preFlush(self) -> None:
|
def _preFlush(self) -> None:
|
||||||
|
hooks.note_will_flush(self)
|
||||||
# have we been added yet?
|
# have we been added yet?
|
||||||
self.newlyAdded = not self.col.db.scalar(
|
self.newlyAdded = not self.col.db.scalar(
|
||||||
"select 1 from cards where nid = ?", self.id
|
"select 1 from cards where nid = ?", self.id
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,11 @@ hooks = [
|
||||||
Your add-on can check filter_name to decide whether it should modify
|
Your add-on can check filter_name to decide whether it should modify
|
||||||
field_text or not before returning it.""",
|
field_text or not before returning it.""",
|
||||||
),
|
),
|
||||||
|
Hook(
|
||||||
|
name="note_will_flush",
|
||||||
|
args=["note: Note"],
|
||||||
|
doc="Allow to change a note before it is added/updated in the database.",
|
||||||
|
),
|
||||||
Hook(
|
Hook(
|
||||||
name="card_did_render",
|
name="card_did_render",
|
||||||
args=[
|
args=[
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue