Export getTypedAnswer

This commit is contained in:
Henrik Giesel 2021-07-12 15:00:36 +02:00
parent af12756980
commit afa8f8a6f0
2 changed files with 16 additions and 8 deletions

View file

@ -694,7 +694,7 @@ class Reviewer:
return s return s
def _getTypedAnswer(self) -> None: def _getTypedAnswer(self) -> None:
self.web.evalWithCallback("typeans ? typeans.value : null", self._onTypedAnswer) self.web.evalWithCallback("getTypedAnswer();", self._onTypedAnswer)
def _onTypedAnswer(self, val: None) -> None: def _onTypedAnswer(self, val: None) -> None:
self.typedAnswer = val or "" self.typedAnswer = val or ""

View file

@ -8,13 +8,16 @@ declare const MathJax: any;
type Callback = () => void | Promise<void>; type Callback = () => void | Promise<void>;
export const ankiPlatform = "desktop";
export let typeans: HTMLElement | undefined;
let _updatingQueue: Promise<void> = Promise.resolve();
export const onUpdateHook: Array<Callback> = []; export const onUpdateHook: Array<Callback> = [];
export const onShownHook: Array<Callback> = []; export const onShownHook: Array<Callback> = [];
export const ankiPlatform = "desktop";
let typeans: HTMLInputElement | undefined;
export function getTypedAnswer(): string | null {
return typeans?.value ?? null;
}
function _runHook(arr: Array<Callback>): Promise<void[]> { function _runHook(arr: Array<Callback>): Promise<void[]> {
const promises: (Promise<void> | void)[] = []; const promises: (Promise<void> | void)[] = [];
@ -25,6 +28,8 @@ function _runHook(arr: Array<Callback>): Promise<void[]> {
return Promise.all(promises); return Promise.all(promises);
} }
let _updatingQueue: Promise<void> = Promise.resolve();
function _queueAction(action: Callback): void { function _queueAction(action: Callback): void {
_updatingQueue = _updatingQueue.then(action); _updatingQueue = _updatingQueue.then(action);
} }
@ -60,8 +65,11 @@ async function _updateQA(
onupdate: Callback, onupdate: Callback,
onshown: Callback onshown: Callback
): Promise<void> { ): Promise<void> {
onUpdateHook.splice(0, Infinity, onupdate); onUpdateHook.length = 0;
onShownHook.splice(0, Infinity, onshown); onUpdateHook.push(onupdate);
onShownHook.length = 0;
onShownHook.push(onshown);
const qa = document.getElementById("qa")!; const qa = document.getElementById("qa")!;
const renderError = const renderError =
@ -116,7 +124,7 @@ export function _showQuestion(q: string, a: string, bodyclass: string): void {
}, },
function () { function () {
// focus typing area if visible // focus typing area if visible
typeans = document.getElementById("typeans") as HTMLElement; typeans = document.getElementById("typeans") as HTMLInputElement;
if (typeans) { if (typeans) {
typeans.focus(); typeans.focus();
} }