From 44cc5f82e9896cb954f171674562034a08dbec44 Mon Sep 17 00:00:00 2001 From: Luc Mcgrady Date: Sat, 4 Oct 2025 18:02:21 +0100 Subject: [PATCH] Added: Untested easeButtonPressed --- proto/anki/scheduler.proto | 7 +++--- rslib/src/scheduler/service/mod.rs | 1 + ts/routes/reviewer/reviewer.ts | 34 ++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/proto/anki/scheduler.proto b/proto/anki/scheduler.proto index d027278c8..316c7ac7c 100644 --- a/proto/anki/scheduler.proto +++ b/proto/anki/scheduler.proto @@ -292,10 +292,11 @@ message NextCardDataRequest { message NextCardDataResponse { message NextCardData { - string front = 1; - string back = 2; + int64 card_id = 1; + string front = 2; + string back = 3; - SchedulingStates states = 3; + SchedulingStates states = 4; } optional NextCardData next_card = 1; diff --git a/rslib/src/scheduler/service/mod.rs b/rslib/src/scheduler/service/mod.rs index e363c66ea..6f4dd219c 100644 --- a/rslib/src/scheduler/service/mod.rs +++ b/rslib/src/scheduler/service/mod.rs @@ -400,6 +400,7 @@ impl crate::services::SchedulerService for Collection { Ok(NextCardDataResponse { next_card: Some(NextCardData { + card_id: cid.0, front: [style.clone(), render.question().to_string()].concat(), back: [style, render.answer().to_string()].concat(), diff --git a/ts/routes/reviewer/reviewer.ts b/ts/routes/reviewer/reviewer.ts index c5d1f3b4d..c74539783 100644 --- a/ts/routes/reviewer/reviewer.ts +++ b/ts/routes/reviewer/reviewer.ts @@ -1,6 +1,9 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import type { CardAnswer, SchedulingStates } from "@generated/anki/scheduler_pb"; +import { + CardAnswer, + type NextCardDataResponse_NextCardData, +} from "@generated/anki/scheduler_pb"; import { nextCardData } from "@generated/backend"; import { bridgeCommand } from "@tslib/bridgecommand"; import { writable } from "svelte/store"; @@ -8,7 +11,8 @@ import { writable } from "svelte/store"; export function setupReviewer(iframe: HTMLIFrameElement) { const cardClass = writable(""); let answer_html = ""; - let _states: SchedulingStates | undefined = undefined; + let cardData: NextCardDataResponse_NextCardData | undefined = undefined; + let startAnswering = Date.now(); function updateHtml(htmlString) { iframe.contentWindow?.postMessage({ type: "html", value: htmlString }, "*"); @@ -21,7 +25,7 @@ export function setupReviewer(iframe: HTMLIFrameElement) { // TODO: "Congratulation screen" logic const question = resp.nextCard?.front || ""; answer_html = resp.nextCard?.back || ""; - _states = resp.nextCard?.states; + cardData = resp.nextCard; console.log({ resp }); updateHtml(question); } @@ -35,6 +39,28 @@ export function setupReviewer(iframe: HTMLIFrameElement) { showQuestion(null); } + function easeButtonPressed(rating: number) { + const states = cardData!.states!; + + let newState = ({ + [1]: states.again!, + [2]: states.hard!, + [3]: states.good!, + [4]: states.easy!, + })[rating]!; + + showQuestion( + new CardAnswer({ + rating: rating, + currentState: states!.current!, + newState, + cardId: cardData!.cardId, + answeredAtMillis: BigInt(Date.now()), + millisecondsTaken: Date.now() - startAnswering, + }), + ); + } + iframe?.addEventListener("load", onReady); addEventListener("message", (e) => { @@ -58,5 +84,5 @@ export function setupReviewer(iframe: HTMLIFrameElement) { globalThis._showQuestion = showQuestion; globalThis._showAnswer = showAnswer; - return { cardClass }; + return { cardClass, easeButtonPressed }; }