-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnkiConnectorJs.kt
61 lines (48 loc) · 2 KB
/
AnkiConnectorJs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@file:OptIn(ExperimentalJsExport::class)
import deckmarkdown.AnkiConnector
import deckmarkdown.AnkiConnectorResult
import deckmarkdown.FileResult
import deckmarkdown.api.AnkiApi
import deckmarkdown.note.DefaultParser
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.promise
import kotlin.js.Promise
@JsExport
@JsName("AnkiConnector")
class AnkiConnectorJs {
private val connector = AnkiConnector(AnkiApi(), DefaultParser)
private val scope = CoroutineScope(SupervisorJob())
fun checkConnection(): Promise<Boolean> = scope.promise {
connector.checkConnection()
}
fun getDeckNames(): Promise<Array<String>> = scope.promise {
connector.getDeckNames().toTypedArray()
}
fun generateArticle(fileContent: String): FileResult? =
connector.generateArticle(fileContent)
fun exportAnkiPackage(fileContent: String): Promise<Boolean> = scope.promise {
connector.exportAnkiPackage(fileContent)
}
fun pushDeck(deckName: String, markdown: String): Promise<AnkiConnectorResult> = scope.promise {
connector.pushDeck(deckName, markdown)
}
fun pushFile(fileContent: String): Promise<AnkiConnectorResult> = scope.promise {
connector.pushFile(fileContent)
}
fun createFile(deckName: String): Promise<AnkiConnectorResult> = scope.promise {
connector.createFile(deckName)
}
fun pullFile(fileContent: String): Promise<AnkiConnectorResult> = scope.promise {
connector.pullFile(fileContent)
}
fun pullDeck(deckName: String, currentMarkdown: String? = null): Promise<AnkiConnectorResult> = scope.promise {
connector.pullDeck(deckName, currentMarkdown)
}
fun storeMediaFile(fileName: String, fileContentBase64: String): Promise<Boolean> = scope.promise {
connector.storeMediaFile(fileName, fileContentBase64)
}
fun retrieveMediaFile(fileName: String): Promise<String> = scope.promise {
connector.retrieveMediaFile(fileName)
}
}