teleposter/app/src/main/java/telegra/ph/Editor.kt

45 lines
1.3 KiB
Kotlin
Raw Normal View History

2016-12-21 14:52:07 +00:00
package telegra.ph
import android.annotation.SuppressLint
2016-12-21 14:52:07 +00:00
import android.content.Context
import android.util.AttributeSet
import android.webkit.JavascriptInterface
2018-02-12 16:59:30 +00:00
import im.delight.android.webview.AdvancedWebView
2016-12-21 14:52:07 +00:00
2019-02-05 21:18:51 +00:00
class Editor @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AdvancedWebView(context, attrs, defStyleAttr) {
2016-12-21 19:57:36 +00:00
private var getCallback: (json: String?) -> Unit? = {}
2016-12-21 14:52:07 +00:00
2019-02-05 21:18:51 +00:00
init {
prepare()
2016-12-21 14:52:07 +00:00
}
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
2019-02-05 21:18:51 +00:00
fun prepare() {
2016-12-21 14:52:07 +00:00
this.settings.javaScriptEnabled = true
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
this.settings.loadWithOverviewMode = true
this.settings.useWideViewPort = true
2019-01-23 16:36:46 +00:00
setMixedContentAllowed(true)
this.loadDataWithBaseURL("https://telegra.ph", context.assets.open("editor.html").bufferedReader().readText(), "text/html", "utf-8", null)
2016-12-21 14:52:07 +00:00
}
private inner class MyJavaScriptInterface {
@JavascriptInterface
2019-02-05 21:18:51 +00:00
@SuppressWarnings("unused")
2016-12-21 19:57:36 +00:00
fun getText(json: String) {
getCallback(json)
2016-12-21 14:52:07 +00:00
}
}
2018-02-12 18:36:19 +00:00
fun setContent(content: String?) {
this.loadUrl("javascript:setContent('${content?.replace("'", "\\'")}');")
}
2016-12-21 19:57:36 +00:00
fun getText(callback: (json: String?) -> Unit) {
getCallback = callback
this.loadUrl("javascript:getNodeJson();")
2016-12-21 14:52:07 +00:00
}
}