Skip to content

Commit

Permalink
feat(mcq): single choice question auto flipping
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkz committed Dec 6, 2024
1 parent 9fba0fc commit 108703d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-beers-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'anki-templates': minor
---

feat(mcq): single choice question auto flipping (单选题自动翻转)
2 changes: 2 additions & 0 deletions src/entries/mcq.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -72,6 +73,7 @@ export default () => {
toggle(name);
} else {
setSelected([name]);
setTimeout(flipToBack, 300);
}
});

Expand Down
25 changes: 25 additions & 0 deletions src/utils/bridge.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 108703d

Please sign in to comment.