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

54 lines
1.4 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
2018-02-12 16:59:30 +00:00
class Editor : AdvancedWebView {
2016-12-21 19:57:36 +00:00
private var getCallback: (json: String?) -> Unit? = {}
2016-12-21 14:52:07 +00:00
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
init()
}
@SuppressLint("SetJavaScriptEnabled", "AddJavascriptInterface")
2016-12-21 14:52:07 +00:00
private fun init() {
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
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 17:18:16 +00:00
fun reset() {
2018-02-12 18:36:19 +00:00
this.loadUrl("javascript:reset();")
2018-02-12 17:18:16 +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
}
}