From 108703df13d04f5cb60b0c5513e5904b35b09e03 Mon Sep 17 00:00:00 2001 From: ikkz Date: Fri, 6 Dec 2024 13:32:57 +0800 Subject: [PATCH] feat(mcq): single choice question auto flipping --- .changeset/khaki-beers-smoke.md | 5 +++++ src/entries/mcq.tsx | 2 ++ src/utils/bridge.ts | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 .changeset/khaki-beers-smoke.md create mode 100644 src/utils/bridge.ts diff --git a/.changeset/khaki-beers-smoke.md b/.changeset/khaki-beers-smoke.md new file mode 100644 index 0000000..71a256d --- /dev/null +++ b/.changeset/khaki-beers-smoke.md @@ -0,0 +1,5 @@ +--- +'anki-templates': minor +--- + +feat(mcq): single choice question auto flipping (单选题自动翻转) diff --git a/src/entries/mcq.tsx b/src/entries/mcq.tsx index ddf635d..7017268 100644 --- a/src/entries/mcq.tsx +++ b/src/entries/mcq.tsx @@ -12,6 +12,7 @@ import { useField } from '../hooks/use-field'; import { t } from '../utils/locale'; import { randomOptionsAtom } from '@/components/settings'; import '@/styles/mcq.css'; +import { flipToBack } from '@/utils/bridge'; import { FIELD_ID } from '@/utils/const'; import { getFieldText, isFieldEmpty } from '@/utils/field'; import { useAutoAnimate } from '@formkit/auto-animate/preact'; @@ -72,6 +73,7 @@ export default () => { toggle(name); } else { setSelected([name]); + setTimeout(flipToBack, 300); } }); diff --git a/src/utils/bridge.ts b/src/utils/bridge.ts new file mode 100644 index 0000000..c6b14ec --- /dev/null +++ b/src/utils/bridge.ts @@ -0,0 +1,25 @@ +declare const pycmd: (cmd: string) => void | undefined; +declare const study: { drawAnswer: () => void } | undefined; +declare const AnkiDroidJS: any; +declare const showAnswer: () => void | undefined; + +declare global { + interface Window { + sendMessage2?: (type: string, pos: string) => void; + anki: any; + } +} + +export function flipToBack() { + if (typeof pycmd !== 'undefined') { + pycmd('ans'); + } else if (typeof study !== 'undefined') { + study.drawAnswer(); + } else if (typeof AnkiDroidJS !== 'undefined') { + showAnswer(); + } else if (window.anki && window.sendMessage2) { + window.sendMessage2('ankitap', 'midCenter'); + } else if (process.env.NODE_ENV === 'development') { + window.setBack?.(true); + } +}