From 136ee26fe84e36d51040ad89b6fc2d8e536f780b 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 7b28ec356975..c5a882d679f8 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 @@ -120,6 +120,15 @@ class ReviewerFragment : R.id.action_open_deck_options -> launchDeckOptions() R.id.action_suspend_card -> viewModel.suspendCard() R.id.action_suspend_note -> viewModel.suspendNote() + 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 61b75bf30a8a..582df45eea72 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 @@ -24,6 +24,7 @@ import com.ichi2.anki.CollectionManager import com.ichi2.anki.CollectionManager.withCol import com.ichi2.anki.Ease 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 @@ -212,6 +213,12 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : CardViewerViewModel( } } + 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 7b64ccc43606..5164cf079711 100644 --- a/AnkiDroid/src/main/res/menu/reviewer2.xml +++ b/AnkiDroid/src/main/res/menu/reviewer2.xml @@ -82,4 +82,58 @@ android:title="@string/menu_suspend_note" /> + + + + + + + + + \ No newline at end of file