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

61 lines
1.7 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
import android.webkit.WebView
import android.webkit.WebViewClient
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
this.loadDataWithBaseURL("http://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
}
}
fun setText(html: String) {
webViewClient = object : WebViewClient() {
2016-12-21 14:52:07 +00:00
override fun onPageFinished(view: WebView, url: String) {
setText(html)
}
}
2016-12-21 14:52:07 +00:00
this.loadUrl("javascript:$('#summernote').summernote('reset');")
this.loadUrl("javascript:$('#summernote').summernote('code', '" + html.replace("'", "\\'") + "');")
}
fun addImage(url: String) {
this.loadUrl("javascript:$('#summernote').summernote('insertImage', '$url');")
}
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
}
}