mirror of
https://github.com/ankitects/anki.git
synced 2025-09-22 07:52:24 -04:00
Add overview_will_render_content hook
This commit is contained in:
parent
8ff1a2e770
commit
f7ae2fa1f7
3 changed files with 87 additions and 7 deletions
|
@ -781,6 +781,55 @@ class _OverviewDidRefreshHook:
|
||||||
overview_did_refresh = _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:
|
class _ProfileDidOpenHook:
|
||||||
_hooks: List[Callable[[], None]] = []
|
_hooks: List[Callable[[], None]] = []
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
from anki.lang import _
|
from anki.lang import _
|
||||||
from aqt import gui_hooks
|
from aqt import gui_hooks
|
||||||
|
@ -17,6 +19,14 @@ class OverviewBottomBar:
|
||||||
self.overview = overview
|
self.overview = overview
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class OverviewContent:
|
||||||
|
deck: str
|
||||||
|
shareLink: str
|
||||||
|
desc: str
|
||||||
|
table: str
|
||||||
|
|
||||||
|
|
||||||
class Overview:
|
class Overview:
|
||||||
"Deck overview."
|
"Deck overview."
|
||||||
|
|
||||||
|
@ -141,14 +151,15 @@ class Overview:
|
||||||
shareLink = '<a class=smallLink href="review">Reviews and Updates</a>'
|
shareLink = '<a class=smallLink href="review">Reviews and Updates</a>'
|
||||||
else:
|
else:
|
||||||
shareLink = ""
|
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.web.stdHtml(
|
||||||
self._body
|
self._body % content.__dict__,
|
||||||
% dict(
|
|
||||||
deck=deck["name"],
|
|
||||||
shareLink=shareLink,
|
|
||||||
desc=self._desc(deck),
|
|
||||||
table=self._table(),
|
|
||||||
),
|
|
||||||
css=["overview.css"],
|
css=["overview.css"],
|
||||||
js=["jquery.js", "overview.js"],
|
js=["jquery.js", "overview.js"],
|
||||||
context=self,
|
context=self,
|
||||||
|
|
|
@ -25,6 +25,26 @@ hooks = [
|
||||||
doc="""Allow to update the overview window. E.g. add the deck name in the
|
doc="""Allow to update the overview window. E.g. add the deck name in the
|
||||||
title.""",
|
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(
|
Hook(
|
||||||
name="deck_browser_did_render",
|
name="deck_browser_did_render",
|
||||||
args=["deck_browser: aqt.deckbrowser.DeckBrowser"],
|
args=["deck_browser: aqt.deckbrowser.DeckBrowser"],
|
||||||
|
|
Loading…
Reference in a new issue