Add a separate route for card info in the sidebar

The move to Sveltekit broke the 'card info during review' add-on and
its descendants. This didn't get noticed in 24.11 due to the old
card-info.js file still being shipped.

I considered adding back the card-info.js generation, but it ended up
being simpler to move parts of the add-on into a separate page instead.
This is a stop-gap solution - in the future I'd like to get us to a
point where such component compositions can be done by add-ons,
and don't need to be done as part of Anki's build process.

Related: #3187
This commit is contained in:
Damien Elmes 2025-01-24 18:08:11 +11:00
parent 774d57cdc8
commit 2d1c6c64e8
4 changed files with 62 additions and 32 deletions

View file

@ -0,0 +1,40 @@
<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import { page } from "$app/stores";
import CardInfo from "../../CardInfo.svelte";
import type { PageData } from "./$types";
import { goto } from "$app/navigation";
export let data: PageData;
const showRevlog = $page.url.searchParams.get("revlog") !== "0";
globalThis.anki ||= {};
globalThis.anki.updateCardInfos = async (card_id: string): Promise<void> => {
const path = `/card-info/${card_id}`;
return goto(path).catch(() => {
window.location.href = path;
});
};
</script>
<center>
{#if data.currentInfo}
<h3>Current</h3>
<CardInfo stats={data.currentInfo} {showRevlog} />
{/if}
{#if data.previousInfo}
<h3>Previous</h3>
<CardInfo stats={data.previousInfo} {showRevlog} />
{/if}
</center>
<style>
:global(body) {
font-size: 80%;
}
</style>

View file

@ -0,0 +1,22 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { cardStats } from "@generated/backend";
import type { PageLoad } from "./$types";
function optionalBigInt(x: any): bigint | null {
try {
return BigInt(x);
} catch (e) {
return null;
}
}
export const load = (async ({ params }) => {
const currentId = optionalBigInt(params.cardId);
const currentInfo = currentId !== null ? await cardStats({ cid: currentId }) : null;
const previousId = optionalBigInt(params.previousId);
const previousInfo = previousId !== null ? await cardStats({ cid: previousId }) : null;
return { currentInfo, previousInfo };
}) satisfies PageLoad;

View file

@ -1 +0,0 @@
@import "../lib/sass/base";

View file

@ -1,31 +0,0 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import "./card-info-base.scss";
import { ModuleName, setupI18n } from "@tslib/i18n";
import { checkNightMode } from "@tslib/nightmode";
import CardInfo from "./CardInfo.svelte";
const i18n = setupI18n({
modules: [ModuleName.CARD_STATS, ModuleName.SCHEDULING, ModuleName.STATISTICS],
});
export async function setupCardInfo(
target: HTMLElement,
props = {},
): Promise<CardInfo> {
checkNightMode();
await i18n;
return new CardInfo({ target, props });
}
if (window.location.hash.startsWith("#test")) {
// use #testXXXX where XXXX is card ID to test
const cardId = parseInt(window.location.hash.substring(0, "#test".length), 10);
setupCardInfo(document.body).then(
(cardInfo: CardInfo): Promise<void> => cardInfo.updateStats(BigInt(cardId)),
);
}