Add overview_will_render_content hook

This commit is contained in:
Glutanimate 2020-02-17 16:49:21 +01:00
parent 8ff1a2e770
commit f7ae2fa1f7
3 changed files with 87 additions and 7 deletions

View file

@ -781,6 +781,55 @@ class _OverviewDidRefreshHook:
overview_did_refresh = _OverviewDidRefreshHook()
class _OverviewWillRenderContentHook:
"""Used to modify HTML content sections in the overview body
'content' contains the sections of HTML content the overview body
will be updated with.
When modifying the content of a particular section, please make sure your
changes only perform the minimum required edits to make your add-on work.
You should avoid overwriting or interfering with existing data as much
as possible, instead opting to append your own changes, e.g.:
def on_overview_will_render_content(overview, content):
content.table += "
<div>my html</div>"
"""
_hooks: List[
Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None]
] = []
def append(
self,
cb: Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None],
) -> None:
"""(overview: aqt.overview.Overview, content: aqt.overview.OverviewContent)"""
self._hooks.append(cb)
def remove(
self,
cb: Callable[["aqt.overview.Overview", "aqt.overview.OverviewContent"], None],
) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(
self, overview: aqt.overview.Overview, content: aqt.overview.OverviewContent
) -> None:
for hook in self._hooks:
try:
hook(overview, content)
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
overview_will_render_content = _OverviewWillRenderContentHook()
class _ProfileDidOpenHook:
_hooks: List[Callable[[], None]] = []

View file

@ -4,6 +4,8 @@
from __future__ import annotations
from dataclasses import dataclass
import aqt
from anki.lang import _
from aqt import gui_hooks
@ -17,6 +19,14 @@ class OverviewBottomBar:
self.overview = overview
@dataclass
class OverviewContent:
deck: str
shareLink: str
desc: str
table: str
class Overview:
"Deck overview."
@ -141,14 +151,15 @@ class Overview:
shareLink = '<a class=smallLink href="review">Reviews and Updates</a>'
else:
shareLink = ""
content = OverviewContent(
deck=deck["name"],
shareLink=shareLink,
desc=self._desc(deck),
table=self._table(),
)
gui_hooks.overview_will_render_content(self, content)
self.web.stdHtml(
self._body
% dict(
deck=deck["name"],
shareLink=shareLink,
desc=self._desc(deck),
table=self._table(),
),
self._body % content.__dict__,
css=["overview.css"],
js=["jquery.js", "overview.js"],
context=self,

View file

@ -25,6 +25,26 @@ hooks = [
doc="""Allow to update the overview window. E.g. add the deck name in the
title.""",
),
Hook(
name="overview_will_render_content",
args=[
"overview: aqt.overview.Overview",
"content: aqt.overview.OverviewContent",
],
doc="""Used to modify HTML content sections in the overview body
'content' contains the sections of HTML content the overview body
will be updated with.
When modifying the content of a particular section, please make sure your
changes only perform the minimum required edits to make your add-on work.
You should avoid overwriting or interfering with existing data as much
as possible, instead opting to append your own changes, e.g.:
def on_overview_will_render_content(overview, content):
content.table += "\n<div>my html</div>"
""",
),
Hook(
name="deck_browser_did_render",
args=["deck_browser: aqt.deckbrowser.DeckBrowser"],