Skip to content

Commit

Permalink
feat(new reviewer): user action
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed May 27, 2024
1 parent 0e702a7 commit 6b2c686
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
13 changes: 13 additions & 0 deletions AnkiDroid/src/main/assets/scripts/ankidroid.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = """
<!DOCTYPE html>
Expand All @@ -98,6 +97,7 @@ fun stdHtml(
<div id="qa"></div>
<script src="file:///android_asset/jquery.min.js"></script>
<script src="file:///android_asset/mathjax/tex-chtml.js"></script>
<script src="file:///android_asset/scripts/ankidroid.js"></script>
<script src="file:///android_asset/backend/web/reviewer.js"></script>
<script>bridgeCommand = function(){};</script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -229,6 +230,12 @@ class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) :
}
}

fun userAction(@Reviewer.UserAction number: Int) {
launchCatchingIO {
eval.emit("javascript: ankidroid.userAction($number);")
}
}

/* *********************************************************************************************
*************************************** Internal methods ***************************************
********************************************************************************************* */
Expand Down
54 changes: 54 additions & 0 deletions AnkiDroid/src/main/res/menu/reviewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,58 @@
android:title="@string/menu_suspend_note" />
</menu>
</item>
<item
android:id="@+id/user_action_1"
android:icon="@drawable/user_action_1"
android:title="@string/user_action_1"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_2"
android:icon="@drawable/user_action_2"
android:title="@string/user_action_2"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_3"
android:icon="@drawable/user_action_3"
android:title="@string/user_action_3"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_4"
android:icon="@drawable/user_action_4"
android:title="@string/user_action_4"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_5"
android:icon="@drawable/user_action_5"
android:title="@string/user_action_5"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_6"
android:icon="@drawable/user_action_6"
android:title="@string/user_action_6"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_7"
android:icon="@drawable/user_action_7"
android:title="@string/user_action_7"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_8"
android:icon="@drawable/user_action_8"
android:title="@string/user_action_8"
app:showAsAction="never"
/>
<item
android:id="@+id/user_action_9"
android:icon="@drawable/user_action_9"
android:title="@string/user_action_9"
app:showAsAction="never"
/>
</menu>

0 comments on commit 6b2c686

Please sign in to comment.