mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
Add deck_browser_will_render_section hook
Allows add-on authors to specifcally target and modify individual sections of the deck browser HTML body at string composition time.
This commit is contained in:
parent
7894ab3312
commit
d02de28f21
3 changed files with 116 additions and 3 deletions
|
@ -5,6 +5,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from enum import Enum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
|
@ -23,6 +24,12 @@ class DeckBrowserBottomBar:
|
||||||
self.deck_browser = deck_browser
|
self.deck_browser = deck_browser
|
||||||
|
|
||||||
|
|
||||||
|
class DeckBrowserSection(Enum):
|
||||||
|
TREE = 0
|
||||||
|
STATS = 1
|
||||||
|
WARN = 2
|
||||||
|
|
||||||
|
|
||||||
class DeckBrowser:
|
class DeckBrowser:
|
||||||
_dueTree: Any
|
_dueTree: Any
|
||||||
|
|
||||||
|
@ -103,10 +110,17 @@ class DeckBrowser:
|
||||||
gui_hooks.deck_browser_did_render(self)
|
gui_hooks.deck_browser_did_render(self)
|
||||||
|
|
||||||
def __renderPage(self, offset):
|
def __renderPage(self, offset):
|
||||||
tree = self._renderDeckTree(self._dueTree)
|
tree = gui_hooks.deck_browser_will_render_section(
|
||||||
stats = self._renderStats()
|
self._renderDeckTree(self._dueTree), DeckBrowserSection.TREE, self
|
||||||
|
)
|
||||||
|
stats = gui_hooks.deck_browser_will_render_section(
|
||||||
|
self._renderStats(), DeckBrowserSection.STATS, self
|
||||||
|
)
|
||||||
|
warn = gui_hooks.deck_browser_will_render_section(
|
||||||
|
self._countWarn(), DeckBrowserSection.WARN, self
|
||||||
|
)
|
||||||
self.web.stdHtml(
|
self.web.stdHtml(
|
||||||
self._body % dict(tree=tree, stats=stats, countwarn=self._countWarn()),
|
self._body % dict(tree=tree, stats=stats, countwarn=warn),
|
||||||
css=["deckbrowser.css"],
|
css=["deckbrowser.css"],
|
||||||
js=["jquery.js", "jquery-ui.js", "deckbrowser.js"],
|
js=["jquery.js", "jquery-ui.js", "deckbrowser.js"],
|
||||||
context=self,
|
context=self,
|
||||||
|
|
|
@ -435,6 +435,75 @@ class _DeckBrowserDidRenderHook:
|
||||||
deck_browser_did_render = _DeckBrowserDidRenderHook()
|
deck_browser_did_render = _DeckBrowserDidRenderHook()
|
||||||
|
|
||||||
|
|
||||||
|
class _DeckBrowserWillRenderSectionFilter:
|
||||||
|
"""Used to modify HTML content sections in the deck browser body
|
||||||
|
|
||||||
|
'html' is the content a particular section will be populated with
|
||||||
|
|
||||||
|
'section' is an enum describing the current section. For an overview
|
||||||
|
of all the possible values please see aqt.deckbrowser.DeckBrowserSection.
|
||||||
|
|
||||||
|
If you do not want to modify the content of a particular section,
|
||||||
|
return 'html' unmodified, e.g.:
|
||||||
|
|
||||||
|
def on_deck_browser_will_render_section(html, section, deck_browser):
|
||||||
|
|
||||||
|
if section != DeckBrowserSection.TREE:
|
||||||
|
# not the tree section we want to modify, return unchanged
|
||||||
|
return html
|
||||||
|
|
||||||
|
# tree section, perform changes to html
|
||||||
|
html += "<div>my code</div>"
|
||||||
|
|
||||||
|
return html
|
||||||
|
"""
|
||||||
|
|
||||||
|
_hooks: List[
|
||||||
|
Callable[
|
||||||
|
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||||
|
str,
|
||||||
|
]
|
||||||
|
] = []
|
||||||
|
|
||||||
|
def append(
|
||||||
|
self,
|
||||||
|
cb: Callable[
|
||||||
|
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||||
|
str,
|
||||||
|
],
|
||||||
|
) -> None:
|
||||||
|
"""(html: str, section: aqt.deckbrowser.DeckBrowserSection, deck_browser: aqt.deckbrowser.DeckBrowser)"""
|
||||||
|
self._hooks.append(cb)
|
||||||
|
|
||||||
|
def remove(
|
||||||
|
self,
|
||||||
|
cb: Callable[
|
||||||
|
[str, "aqt.deckbrowser.DeckBrowserSection", "aqt.deckbrowser.DeckBrowser"],
|
||||||
|
str,
|
||||||
|
],
|
||||||
|
) -> None:
|
||||||
|
if cb in self._hooks:
|
||||||
|
self._hooks.remove(cb)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
html: str,
|
||||||
|
section: aqt.deckbrowser.DeckBrowserSection,
|
||||||
|
deck_browser: aqt.deckbrowser.DeckBrowser,
|
||||||
|
) -> str:
|
||||||
|
for filter in self._hooks:
|
||||||
|
try:
|
||||||
|
html = filter(html, section, deck_browser)
|
||||||
|
except:
|
||||||
|
# if the hook fails, remove it
|
||||||
|
self._hooks.remove(filter)
|
||||||
|
raise
|
||||||
|
return html
|
||||||
|
|
||||||
|
|
||||||
|
deck_browser_will_render_section = _DeckBrowserWillRenderSectionFilter()
|
||||||
|
|
||||||
|
|
||||||
class _DeckBrowserWillShowOptionsMenuHook:
|
class _DeckBrowserWillShowOptionsMenuHook:
|
||||||
_hooks: List[Callable[[QMenu, int], None]] = []
|
_hooks: List[Callable[[QMenu, int], None]] = []
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,36 @@ hooks = [
|
||||||
args=["deck_browser: aqt.deckbrowser.DeckBrowser"],
|
args=["deck_browser: aqt.deckbrowser.DeckBrowser"],
|
||||||
doc="""Allow to update the deck browser window. E.g. change its title.""",
|
doc="""Allow to update the deck browser window. E.g. change its title.""",
|
||||||
),
|
),
|
||||||
|
Hook(
|
||||||
|
name="deck_browser_will_render_section",
|
||||||
|
args=[
|
||||||
|
"html: str",
|
||||||
|
"section: aqt.deckbrowser.DeckBrowserSection",
|
||||||
|
"deck_browser: aqt.deckbrowser.DeckBrowser",
|
||||||
|
],
|
||||||
|
return_type="str",
|
||||||
|
doc="""Used to modify HTML content sections in the deck browser body
|
||||||
|
|
||||||
|
'html' is the content a particular section will be populated with
|
||||||
|
|
||||||
|
'section' is an enum describing the current section. For an overview
|
||||||
|
of all the possible values please see aqt.deckbrowser.DeckBrowserSection.
|
||||||
|
|
||||||
|
If you do not want to modify the content of a particular section,
|
||||||
|
return 'html' unmodified, e.g.:
|
||||||
|
|
||||||
|
def on_deck_browser_will_render_section(html, section, deck_browser):
|
||||||
|
|
||||||
|
if section != DeckBrowserSection.TREE:
|
||||||
|
# not the tree section we want to modify, return unchanged
|
||||||
|
return html
|
||||||
|
|
||||||
|
# tree section, perform changes to html
|
||||||
|
html += "<div>my code</div>"
|
||||||
|
|
||||||
|
return html
|
||||||
|
""",
|
||||||
|
),
|
||||||
Hook(
|
Hook(
|
||||||
name="reviewer_did_show_question",
|
name="reviewer_did_show_question",
|
||||||
args=["card: Card"],
|
args=["card: Card"],
|
||||||
|
|
Loading…
Reference in a new issue