Skip to content

Commit

Permalink
JS function to setTags and getTags
Browse files Browse the repository at this point in the history
  • Loading branch information
Haz3-jolt committed Jan 14, 2025
1 parent 6495275 commit bc86eea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions AnkiDroid/src/main/assets/scripts/js-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const jsApiList = {
ankiSttStart: "sttStart",
ankiSttStop: "sttStop",
ankiAddTagToNote: "addTagToNote",
ankiSetNoteTags: "setNoteTags",
ankiGetNoteTags: "getNoteTags",
};

class AnkiDroidJS {
Expand Down Expand Up @@ -119,12 +121,29 @@ class AnkiDroidJS {
Object.keys(jsApiList).forEach(method => {
if (method === "ankiAddTagToNote") {
AnkiDroidJS.prototype[method] = async function (noteId, tag) {
console.warn("ankiAddTagToNote is deprecated. Use ankiSetNoteTags instead.");
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId, tag });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiSetNoteTags") {
AnkiDroidJS.prototype[method] = async function (noteId, tags) {
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId, tags });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiGetNoteTags") {
AnkiDroidJS.prototype[method] = async function (noteId) {
const endpoint = jsApiList[method];
const data = JSON.stringify({ noteId });
return await this.handleRequest(endpoint, data);
};
return;
}
if (method === "ankiTtsSpeak") {
AnkiDroidJS.prototype[method] = async function (text, queueMode = 0) {
const endpoint = jsApiList[method];
Expand Down
27 changes: 27 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidJsAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ import com.ichi2.libanki.Collection
import com.ichi2.libanki.Decks
import com.ichi2.libanki.SortOrder
import com.ichi2.utils.NetworkUtils
import com.ichi2.utils.stringIterable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber
Expand Down Expand Up @@ -373,6 +375,31 @@ open class AnkiDroidJsAPI(
getColUnsafe.updateNote(note)
convertToByteArray(apiContract, true)
}

"setNoteTags" -> {
val jsonObject = JSONObject(apiParams)
val noteId = jsonObject.getLong("noteId")
val tags = jsonObject.getJSONArray("tags")
withCol {
val note =
getNote(noteId).apply {
setTagsFromStr(this@withCol, tags.stringIterable().joinToString(","))
}
updateNote(note)
}
convertToByteArray(apiContract, true)
}

"getNoteTags" -> {
val jsonObject = JSONObject(apiParams)
val noteId = jsonObject.getLong("noteId")
val noteTags =
withCol {
getNote(noteId).tags
}
convertToByteArray(apiContract, JSONArray(noteTags).toString())
}

"sttSetLanguage" -> convertToByteArray(apiContract, speechRecognizer.setLanguage(apiParams))
"sttStart" -> {
val callback =
Expand Down

0 comments on commit bc86eea

Please sign in to comment.