Added: ShowQusestion TypedAnswer replacement

This commit is contained in:
Luc Mcgrady 2025-10-31 12:38:11 +00:00
parent bbf575e491
commit b66a10ea26
No known key found for this signature in database
GPG key ID: 4F3D7A0B17CC3D9C
3 changed files with 32 additions and 3 deletions

View file

@ -763,6 +763,7 @@ exposed_backend_list = [
"get_retention_workload", "get_retention_workload",
# CardsService # CardsService
"set_flag", "set_flag",
"compare_answer",
] ]

View file

@ -65,3 +65,14 @@ function pycmd(cmd: string) {
} }
} }
globalThis.pycmd = pycmd; globalThis.pycmd = pycmd;
function _typeAnsPress() {
const elem = document.getElementById("typeans")! as HTMLInputElement;
let key = (window.event as KeyboardEvent).key;
key = key.length == 1 ? key : "";
window.parent.postMessage(
{ type: "typed", value: elem.value + key } satisfies ReviewerRequest,
"*",
);
}
globalThis._typeAnsPress = _typeAnsPress;

View file

@ -1,7 +1,7 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { CardAnswer, type NextCardDataResponse_NextCardData } from "@generated/anki/scheduler_pb"; import { CardAnswer, type NextCardDataResponse_NextCardData } from "@generated/anki/scheduler_pb";
import { nextCardData, playAvtags } from "@generated/backend"; import { compareAnswer, nextCardData, playAvtags } from "@generated/backend";
import { derived, get, writable } from "svelte/store"; import { derived, get, writable } from "svelte/store";
import type { InnerReviewerRequest } from "../reviewer-inner/innerReviewerRequest"; import type { InnerReviewerRequest } from "../reviewer-inner/innerReviewerRequest";
import type { ReviewerRequest } from "./reviewerRequest"; import type { ReviewerRequest } from "./reviewerRequest";
@ -25,6 +25,8 @@ export function updateNightMode() {
} }
} }
const typedAnswerRegex = /\[\[type:(.+?:)?(.+?)\]\]/m;
export class ReviewerState { export class ReviewerState {
answerHtml = ""; answerHtml = "";
currentTypedAnswer = ""; currentTypedAnswer = "";
@ -125,12 +127,27 @@ export class ReviewerState {
return this._cardData?.queue?.cards[0]; return this._cardData?.queue?.cards[0];
} }
public showAnswer() { async showTypedAnswer(html: string) {
if (!this._cardData?.typedAnswer) {
return html;
}
const compareAnswerResp = await compareAnswer({
expected: this._cardData?.typedAnswer,
provided: this.currentTypedAnswer,
combining: false,
});
const display = compareAnswerResp.val;
console.log({ typedAnswerRegex, html, display });
return html.replace(typedAnswerRegex, display);
}
public async showAnswer() {
this.answerShown.set(true); this.answerShown.set(true);
if (this._cardData?.autoplay) { if (this._cardData?.autoplay) {
playAvtags({ tags: this._cardData!.answerAvTags }); playAvtags({ tags: this._cardData!.answerAvTags });
} }
this.updateHtml(this._cardData?.back || ""); this.updateHtml(await this.showTypedAnswer(this._cardData?.back || ""));
} }
public easeButtonPressed(rating: number) { public easeButtonPressed(rating: number) {