From 6b2c68679c12a4614cc9a6b5b9a67418e8e85dc4 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Mon, 27 May 2024 20:33:16 -0300 Subject: [PATCH] feat(new reviewer): user action --- .../src/main/assets/scripts/ankidroid.js | 13 +++++ .../ichi2/anki/previewer/PreviewerHelpers.kt | 4 +- .../ui/windows/reviewer/ReviewerFragment.kt | 9 ++++ .../ui/windows/reviewer/ReviewerViewModel.kt | 7 +++ AnkiDroid/src/main/res/menu/reviewer2.xml | 54 +++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 AnkiDroid/src/main/assets/scripts/ankidroid.js diff --git a/AnkiDroid/src/main/assets/scripts/ankidroid.js b/AnkiDroid/src/main/assets/scripts/ankidroid.js new file mode 100644 index 000000000000..5cf49c6085ae --- /dev/null +++ b/AnkiDroid/src/main/assets/scripts/ankidroid.js @@ -0,0 +1,13 @@ +"use strict"; +globalThis.ankidroid = globalThis.ankidroid || {}; + +globalThis.ankidroid.userAction = function (number) { + try { + let userJs = globalThis[`userJs${number}`]; + if (userJs != null) { + userJs(); + } + } catch (e) { + alert(e); + } +}; diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt index 62e38ad0bc23..1275355d22f9 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/previewer/PreviewerHelpers.kt @@ -36,7 +36,7 @@ class NoteEditorDestination(val cardId: Long) { /** * Not exactly equal to anki's stdHtml. Some differences: - * * `ankidroid.css` is added + * * `ankidroid.css` and `ankidroid.js` are added * * `bridgeCommand()` is ignored * * Aimed to be used only for reviewing/previewing cards @@ -77,7 +77,6 @@ fun stdHtml( ":root[class*=night-mode] { --canvas: $canvasColor; --fg: $fgColor; }" } - @Suppress("UnnecessaryVariable") // necessary for the HTML notation @Language("HTML") val html = """ @@ -98,6 +97,7 @@ fun stdHtml(
+ diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt index 6757e7214074..d8a6b5bcc9da 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt @@ -130,6 +130,15 @@ class ReviewerFragment : R.id.action_flag_five -> viewModel.setFlag(Flag.PINK) R.id.action_flag_six -> viewModel.setFlag(Flag.TURQUOISE) R.id.action_flag_seven -> viewModel.setFlag(Flag.PURPLE) + R.id.user_action_1 -> viewModel.userAction(1) + R.id.user_action_2 -> viewModel.userAction(2) + R.id.user_action_3 -> viewModel.userAction(3) + R.id.user_action_4 -> viewModel.userAction(4) + R.id.user_action_5 -> viewModel.userAction(5) + R.id.user_action_6 -> viewModel.userAction(6) + R.id.user_action_7 -> viewModel.userAction(7) + R.id.user_action_8 -> viewModel.userAction(8) + R.id.user_action_9 -> viewModel.userAction(9) } return true } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt index 2dafb4cb8e78..d00110247a4e 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt @@ -25,6 +25,7 @@ import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.Ease import com.ichi2.anki.Flag import com.ichi2.anki.NoteEditor +import com.ichi2.anki.Reviewer import com.ichi2.anki.asyncIO import com.ichi2.anki.cardviewer.CardMediaPlayer import com.ichi2.anki.launchCatchingIO @@ -229,6 +230,12 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : } } + fun userAction(@Reviewer.UserAction number: Int) { + launchCatchingIO { + eval.emit("javascript: ankidroid.userAction($number);") + } + } + /* ********************************************************************************************* *************************************** Internal methods *************************************** ********************************************************************************************* */ diff --git a/AnkiDroid/src/main/res/menu/reviewer2.xml b/AnkiDroid/src/main/res/menu/reviewer2.xml index 6872767ab3dd..b4e74be38fb2 100644 --- a/AnkiDroid/src/main/res/menu/reviewer2.xml +++ b/AnkiDroid/src/main/res/menu/reviewer2.xml @@ -122,4 +122,58 @@ android:title="@string/menu_suspend_note" /> + + + + + + + + + \ No newline at end of file