From d845fbf4989acc4fbfddc877fd67c7671412fbe0 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Wed, 13 Mar 2024 15:01:50 +0100 Subject: [PATCH 1/5] Update handleBackspaceAndEnter to prevent removing the current styles when backspacing if Gutenberg mode is enabled and it's at the start of the EditText --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index a5d97bfec..2f2b3d671 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -839,6 +839,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown var wasStyleRemoved = false if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) { + if (isInGutenbergMode && (selectionStart == 0 && isTextSelected())) { + return false + } + if (!consumeHistoryEvent) { history.beforeTextChanged(this@AztecText) } From bf7d6e2bedc25538f5c80a8efad0c28cb85e0a1c Mon Sep 17 00:00:00 2001 From: Gerardo Date: Thu, 23 May 2024 17:51:44 +0200 Subject: [PATCH 2/5] Fix issue when auto-corrected words would be deleted --- .../wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt index f6d12e1a5..85f7158ef 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt @@ -18,7 +18,8 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - deletedText = before > 0 + // count < before to take into account auto-corrected words + deletedText = before > 0 && count < before } override fun afterTextChanged(text: Editable) { @@ -43,8 +44,10 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { if (text.isEmpty()) { if (text.getSpans(0, 0, IAztecBlockSpan::class.java).isNotEmpty()) { - // need to add a end-of-text marker so a block element can render in the empty text. - text.append(Constants.END_OF_BUFFER_MARKER) + // need to add a end-of-text marker so a block element can render in the empty text. + if (!deletedText) { + text.append(Constants.END_OF_BUFFER_MARKER) + } } return text } else if (text.length == 1 && text[0] == Constants.END_OF_BUFFER_MARKER && deletedText) { From 8bd5798dd29f59aad779e719ea4fcec622b219ae Mon Sep 17 00:00:00 2001 From: Gerardo Date: Fri, 24 May 2024 12:13:24 +0200 Subject: [PATCH 3/5] Fix lint issues --- .../wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt index 85f7158ef..8e95eeb84 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt @@ -44,10 +44,10 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { if (text.isEmpty()) { if (text.getSpans(0, 0, IAztecBlockSpan::class.java).isNotEmpty()) { - // need to add a end-of-text marker so a block element can render in the empty text. - if (!deletedText) { - text.append(Constants.END_OF_BUFFER_MARKER) - } + // need to add a end-of-text marker so a block element can render in the empty text. + if (!deletedText) { + text.append(Constants.END_OF_BUFFER_MARKER) + } } return text } else if (text.length == 1 && text[0] == Constants.END_OF_BUFFER_MARKER && deletedText) { From eb9f683762c9c7f545667df9b5369a28f235005c Mon Sep 17 00:00:00 2001 From: Gerardo Date: Mon, 27 May 2024 16:24:31 +0200 Subject: [PATCH 4/5] Add uninstall method to EndOfBufferMarkerAdder --- .../kotlin/org/wordpress/aztec/AztecText.kt | 7 ++++- .../aztec/watchers/EndOfBufferMarkerAdder.kt | 28 ++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index e23e2c3f7..f6b7bde98 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -310,6 +310,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown var observationQueue: ObservationQueue = ObservationQueue(this) var textWatcherEventBuilder: TextWatcherEvent.Builder = TextWatcherEvent.Builder() + var endOfBufferMarkerAdderWatcher: EndOfBufferMarkerAdder? = null private var accessibilityDelegate = AztecTextAccessibilityDelegate(this) @@ -640,6 +641,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown } } + fun removeEndOfBufferMarkerAdder() { + endOfBufferMarkerAdderWatcher?.uninstallEndOfBuffer(this) + } + private fun selectionHasExactlyOneMarker(start: Int, end: Int, type: Class): Boolean { val spanFound: Array = editableText.getSpans( start, @@ -891,7 +896,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown FullWidthImageElementWatcher.install(this) - EndOfBufferMarkerAdder.install(this) + endOfBufferMarkerAdderWatcher = EndOfBufferMarkerAdder.install(this) ZeroIndexContentWatcher.install(this) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt index 8e95eeb84..76e95da62 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt @@ -18,8 +18,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - // count < before to take into account auto-corrected words - deletedText = before > 0 && count < before + deletedText = before > 0 } override fun afterTextChanged(text: Editable) { @@ -32,9 +31,26 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { // by the way, the cursor will be adjusted "automatically" by RichTextEditText's onSelectionChanged to before the marker } + fun uninstallEndOfBuffer(aztecText: AztecText) { + uninstall(aztecText) + } + companion object { - fun install(editText: AztecText) { - editText.addTextChangedListener(EndOfBufferMarkerAdder(editText.text)) + private val watchers = mutableMapOf() + + fun install(editText: AztecText): EndOfBufferMarkerAdder { + var watcher = EndOfBufferMarkerAdder(editText.text) + editText.addTextChangedListener(watcher) + watchers[editText] = watcher + return watcher + } + + fun uninstall(editText: AztecText) { + val watcher = watchers[editText] + if (watcher != null) { + editText.removeTextChangedListener(watcher) + watchers.remove(editText) + } } fun ensureEndOfTextMarker(text: Editable, deletedText: Boolean = false): Editable { @@ -45,9 +61,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { if (text.isEmpty()) { if (text.getSpans(0, 0, IAztecBlockSpan::class.java).isNotEmpty()) { // need to add a end-of-text marker so a block element can render in the empty text. - if (!deletedText) { - text.append(Constants.END_OF_BUFFER_MARKER) - } + text.append(Constants.END_OF_BUFFER_MARKER) } return text } else if (text.length == 1 && text[0] == Constants.END_OF_BUFFER_MARKER && deletedText) { From fac06be6604f8cb24d34878c9961fa8503fd0e33 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Mon, 27 May 2024 16:53:02 +0200 Subject: [PATCH 5/5] Keep current watcher ref --- .../wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt index 76e95da62..858d14561 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfBufferMarkerAdder.kt @@ -36,20 +36,18 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher { } companion object { - private val watchers = mutableMapOf() + private var watcherRef: EndOfBufferMarkerAdder? = null fun install(editText: AztecText): EndOfBufferMarkerAdder { var watcher = EndOfBufferMarkerAdder(editText.text) editText.addTextChangedListener(watcher) - watchers[editText] = watcher + watcherRef = watcher return watcher } fun uninstall(editText: AztecText) { - val watcher = watchers[editText] - if (watcher != null) { - editText.removeTextChangedListener(watcher) - watchers.remove(editText) + if (watcherRef != null) { + editText.removeTextChangedListener(watcherRef) } }