Skip to content

Commit

Permalink
feat: Editor history navigation (#55)
Browse files Browse the repository at this point in the history
* feat: Synchronize iOS host undo/redo controls

Communicate undo/redo availability state to iOS host app.

* feat: Expose undo/redo actions to host app

Enable navigating editor history from the host app.

* task: Capture latest build output

* feat: Synchronize Android host undo/redo controls

Communicate undo/redo availability state to Android host app.

* feat: Expose undo/redo actions to the Android host app

Enable navigating editor history from the host app.

* refactor: Avoid deprecated WebView function

Address build warning.
  • Loading branch information
dcalhoun authored Jan 9, 2025
1 parent 44f0705 commit 5a43a1b
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ class GutenbergView : WebView {

private var onFileChooserRequested: ((Intent, Int) -> Unit)? = null
private var contentChangeListener: ContentChangeListener? = null
private var historyChangeListener: HistoryChangeListener? = null
private var openMediaLibraryListener: OpenMediaLibraryListener? = null
private var editorDidBecomeAvailableListener: EditorAvailableListener? = null

fun setContentChangeListener(listener: ContentChangeListener) {
contentChangeListener = listener
}

fun setHistoryChangeListener(listener: HistoryChangeListener) {
historyChangeListener = listener
}

fun setOpenMediaLibraryListener(listener: OpenMediaLibraryListener) {
openMediaLibraryListener = listener
}
Expand Down Expand Up @@ -113,8 +118,9 @@ class GutenbergView : WebView {
return super.shouldInterceptRequest(view, request)
}

override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
url?.let {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
val url = request.url.toString()
url.let {
// Allow the WebView to handle `blob:` requests, which are utilized in the block
// inserter's Patterns tab
if (it.startsWith("blob:")) {
Expand Down Expand Up @@ -263,6 +269,10 @@ class GutenbergView : WebView {
fun onContentChanged(title: String, content: String)
}

interface HistoryChangeListener {
fun onHistoryChanged(hasUndo: Boolean, hasRedo: Boolean)
}

sealed class Value {
data class Single(val value: Int): Value()
data class Multiple(val values: IntArray): Value() {
Expand Down Expand Up @@ -301,6 +311,18 @@ class GutenbergView : WebView {
}
}

fun undo() {
handler.post {
this.evaluateJavascript("editor.undo();", null)
}
}

fun redo() {
handler.post {
this.evaluateJavascript("editor.redo();", null)
}
}

@JavascriptInterface
fun onEditorLoaded() {
Log.i("GutenbergView", "EditorLoaded received in native code")
Expand Down Expand Up @@ -329,6 +351,11 @@ class GutenbergView : WebView {
}, false)
}

@JavascriptInterface
fun onEditorHistoryChanged(hasUndo: Boolean, hasRedo: Boolean) {
historyChangeListener?.onHistoryChanged(hasUndo, hasRedo)
}

@JavascriptInterface
fun onBlocksChanged(isEmpty: Boolean) {
if(isEmpty) {
Expand Down Expand Up @@ -400,6 +427,7 @@ class GutenbergView : WebView {
clearConfig()
this.stopLoading()
contentChangeListener = null
historyChangeListener = null
editorDidBecomeAvailable = null
filePathCallback = null
onFileChooserRequested = null
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5a43a1b

Please sign in to comment.