From a9769813ba45fffc86b4f729d7e74759537104f3 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 15 Apr 2022 14:36:50 +1000 Subject: [PATCH] Add back support for custom mountpoint in card stats The move to separate .html files broke our legacy card stats routine. Related: d1d71ffdbbe639c309e1fe06d0c2db0bfcdbea09 --- pylib/anki/collection.py | 6 ++++++ pylib/anki/stats.py | 2 +- qt/aqt/browser/card_info.py | 1 + ts/card-info/index.ts | 10 +++++----- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index 20ef2a8af..3d695b9a3 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -853,6 +853,12 @@ class Collection(DeprecatedNamesMixin): return CollectionStats(self) def card_stats_data(self, card_id: CardId) -> stats_pb2.CardStatsResponse: + """Returns the data required to show card stats. + + If you wish to display the stats in a HTML table like Anki does, + you can use the .js file directly - see this add-on for an example: + https://ankiweb.net/shared/info/2179254157 + """ return self._backend.card_stats(card_id) def studied_today(self) -> str: diff --git a/pylib/anki/stats.py b/pylib/anki/stats.py index 70702737d..a9b1c7a56 100644 --- a/pylib/anki/stats.py +++ b/pylib/anki/stats.py @@ -39,7 +39,7 @@ def _legacy_card_stats( if ({1 if _legacy_nightmode else 0}) {{ document.documentElement.className = "night-mode"; }} - const {varName} = anki.cardInfo(document.getElementById('{random_id}')); + const {varName} = anki.setupCardInfo(document.getElementById('{random_id}')); {varName}.then((c) => c.$set({{ cardId: {card_id}, includeRevlog: {str(include_revlog).lower()} }})); """ diff --git a/qt/aqt/browser/card_info.py b/qt/aqt/browser/card_info.py index 54f44c0bd..05a9312ba 100644 --- a/qt/aqt/browser/card_info.py +++ b/qt/aqt/browser/card_info.py @@ -63,6 +63,7 @@ class CardInfoDialog(QDialog): layout.addWidget(buttons) qconnect(buttons.rejected, self.reject) self.setLayout(layout) + self.web.eval("anki.cardInfoPromise = anki.setupCardInfo(document.body);") self.update_card(card_id) def update_card(self, card_id: CardId | None) -> None: diff --git a/ts/card-info/index.ts b/ts/card-info/index.ts index a1bb07621..e2d9b40c5 100644 --- a/ts/card-info/index.ts +++ b/ts/card-info/index.ts @@ -11,17 +11,17 @@ const i18n = setupI18n({ modules: [ModuleName.CARD_STATS, ModuleName.SCHEDULING, ModuleName.STATISTICS], }); -export async function setupCardInfo(): Promise { +export async function setupCardInfo(target: HTMLElement): Promise { checkNightMode(); await i18n; - return new CardInfo({ target: document.body, props: { includeRevlog: true } }); + return new CardInfo({ target, props: {} }); } -export const cardInfoPromise = setupCardInfo(); - if (window.location.hash.startsWith("#test")) { // use #testXXXX where XXXX is card ID to test const cardId = parseInt(window.location.hash.substr("#test".length), 10); - cardInfoPromise.then((cardInfo: CardInfo): void => cardInfo.$set({ cardId })); + setupCardInfo(document.body).then((cardInfo: CardInfo): void => + cardInfo.$set({ cardId }), + ); }