Progress with Editor

This commit is contained in:
Jan-Lk Else 2016-12-21 15:52:07 +01:00
parent 90d134dc70
commit c88dd9c944
8 changed files with 153 additions and 13 deletions

View File

@ -1,2 +1,6 @@
-keep class * extends android.webkit.WebChromeClient { *; } -keep class * extends android.webkit.WebChromeClient { *; }
-dontwarn im.delight.android.webview.** -dontwarn im.delight.android.webview.**
-keepclassmembers class telegra.ph.Editor$MyJavaScriptInterface {
public *;
}
-keepattributes JavascriptInterface

View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel="stylesheet">
<link href="https://rawgit.com/summernote/summernote/master/dist/summernote.css"
rel="stylesheet">
</head>
<body>
<div id="summernote" style="width:100%;height:100%"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://rawgit.com/summernote/summernote/master/dist/summernote.min.js"></script>
<script>
$(document).ready(function() {
$('#summernote').summernote({
height: 1200,
focus: true,
placeholder: '',
toolbar: [
// [groupName, [list of button]]
['style', ['bold', 'italic']],
['para', ['ul', 'ol']],
['insert', ['link']],
['history', ['undo', 'redo']],
['other', ['codeview']]
],
callbacks: {
onInit: function(e) {
$("#summernote").summernote("fullscreen.toggle");
}
}
});
});
</script>
</body>
</html>

View File

@ -0,0 +1,71 @@
package telegra.ph
import android.content.Context
import android.support.annotation.Keep
import android.util.AttributeSet
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
class Editor : WebView {
private var text = ""
internal var context: Context
constructor(context: Context) : super(context) {
this.context = context
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.context = context
init()
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
this.context = context
init()
}
private fun init() {
this.settings.javaScriptEnabled = true
this.settings.cacheMode = WebSettings.LOAD_NO_CACHE
this.addJavascriptInterface(MyJavaScriptInterface(), "android")
this.settings.loadWithOverviewMode = true
this.settings.useWideViewPort = true
this.loadUrl("file:///android_asset/editor.html")
}
private inner class MyJavaScriptInterface {
@JavascriptInterface
fun getText(html: String) {
text = html
}
}
fun setText(html: String) {
setWebViewClient(object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
setText(html)
}
})
this.loadUrl("javascript:$('#summernote').summernote('reset');")
this.loadUrl("javascript:$('#summernote').summernote('code', '" + html.replace("'", "\\'") + "');")
}
fun getText(): String {
text = "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8"
this.loadUrl("javascript:window.android.getText" + "(document.getElementsByClassName('note-editable')[0].innerHTML);")
var i = 0
try {
while (text == "P/%TE5XpkAijBc%LjA;_-pZcbiU25E6feX5y/n6qxCTmhprLrqC3H%^hU!%q2,k'm`SHheoW^'mQ~zW93,C?~GtYk!wi/&'3KxW8" && i < 100) {
Thread.sleep(50)
i++
}
} catch (e: Exception) {
text = ""
}
return text
}
}

View File

@ -16,14 +16,30 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
private val htmlHead = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"><style> * { max-width: 100%; height: auto; word-break: break-all; word-break: break-word; }</style></head><body>" private val htmlHead = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"><style> * { max-width: 100%; height: auto; word-break: break-all; word-break: break-word; }</style></head><body>"
private val htmlEnd = "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>" private val htmlEnd = "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></body></html>"
private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView } private val webView: AdvancedWebView? by lazy { findViewById(R.id.webView) as AdvancedWebView? }
private val editor: Editor? by lazy { findViewById(R.id.editor) as Editor? }
private var url = "" private var url = ""
private var editorMode = true
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
else loadEditor()
}
private fun loadEditor() {
editorMode = true
invalidateOptionsMenu()
// Init
setContentView(R.layout.main_write)
}
private fun loadPage(path: String) {
editorMode = false
invalidateOptionsMenu()
// Init
setContentView(R.layout.main_read)
webView?.apply { webView?.apply {
setListener(this@MainActivity, this@MainActivity) setListener(this@MainActivity, this@MainActivity)
setMixedContentAllowed(true) setMixedContentAllowed(true)
@ -34,17 +50,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
isVerticalScrollBarEnabled = false isVerticalScrollBarEnabled = false
overScrollMode = View.OVER_SCROLL_NEVER overScrollMode = View.OVER_SCROLL_NEVER
} }
// Load
if (intent.action == Intent.ACTION_VIEW && !intent.dataString.isNullOrBlank() && intent.dataString.contains("telegra.ph"))
loadPage(intent.dataString.split("/").last())
else loadEditor()
}
private fun loadEditor() {
webView?.loadUrl(TELEGRAPH)
}
private fun loadPage(path: String) {
Api().getPage(path) { page -> Api().getPage(path) { page ->
page?.let { page?.let {
var html = htmlHead var html = htmlHead
@ -107,6 +113,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
return true return true
} }
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
super.onPrepareOptionsMenu(menu)
menu?.findItem(R.id.create)?.isVisible = !editorMode
menu?.findItem(R.id.publish)?.isVisible = editorMode
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.create -> { R.id.create -> {

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<telegra.ph.Editor
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editor"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

View File

@ -5,6 +5,10 @@
android:id="@+id/create" android:id="@+id/create"
android:title="@string/create" android:title="@string/create"
app:showAsAction="ifRoom"/> app:showAsAction="ifRoom"/>
<item
android:id="@+id/publish"
android:title="@string/publish"
app:showAsAction="ifRoom"/>
<item <item
android:id="@+id/bookmarks" android:id="@+id/bookmarks"
android:title="@string/bookmarks" android:title="@string/bookmarks"

View File

@ -12,4 +12,5 @@
<string name="title_hint">Awesome Post #1</string> <string name="title_hint">Awesome Post #1</string>
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="delete_question">Do you really want to delete this?</string> <string name="delete_question">Do you really want to delete this?</string>
<string name="publish">Publish</string>
</resources> </resources>