Add back support for custom mountpoint in card stats

The move to separate .html files broke our legacy card stats routine.

Related: d1d71ffdbb
This commit is contained in:
Damien Elmes 2022-04-15 14:36:50 +10:00
parent 24ab8ac423
commit a9769813ba
4 changed files with 13 additions and 6 deletions

View File

@ -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:

View File

@ -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()} }}));
</script>
"""

View File

@ -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:

View File

@ -11,17 +11,17 @@ const i18n = setupI18n({
modules: [ModuleName.CARD_STATS, ModuleName.SCHEDULING, ModuleName.STATISTICS],
});
export async function setupCardInfo(): Promise<CardInfo> {
export async function setupCardInfo(target: HTMLElement): Promise<CardInfo> {
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 }),
);
}