From 8f08763fa1223d6f39f857780c34321ec4845cf3 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 4 Jun 2019 14:16:21 +0200 Subject: [PATCH 1/3] Make sure to call the BackSpace listener when the backspace key is pressed, regarless of the position of the caret in the field. Previously it was only called when the caret was at beginning of the field. --- .../kotlin/org/wordpress/aztec/AztecText.kt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 3edbf11f8..26da33cf5 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -497,17 +497,29 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown temp } - val emptyEditTextBackspaceDetector = InputFilter { source, start, end, dest, dstart, dend -> - if (selectionStart == 0 && selectionEnd == 0 - && end == 0 && start == 0 - && dstart == 0 && dend == 0 - && !isHandlingBackspaceEvent) { + val backspaceDetector = InputFilter { source, start, end, dest, dstart, dend -> + if (isHandlingBackspaceEvent) { + // It's already handling a backspace event. Do nothing. + } else if (end != 0 || start != 0) { + // Not a backspace event + } else if (selectionStart != 0 || selectionEnd != 0) { + // there is something selected on the editor, let's make Aztec do the work + } else if(onAztecKeyListener != null) { isHandlingBackspaceEvent = true // Prevent the forced backspace from being added to the history stack consumeHistoryEvent = true - handleBackspaceAndEnter(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) + handleBackspaceAndEnter(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL), start, end, dstart, dend) + isHandlingBackspaceEvent = false + } else if (dstart == 0 && dend == 0) { + // Make sure we're at the beginning of the editor field + isHandlingBackspaceEvent = true + + // Prevent the forced backspace from being added to the history stack + consumeHistoryEvent = true + + handleBackspaceAndEnter(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL), start, end, dstart, dend) isHandlingBackspaceEvent = false } source @@ -515,13 +527,13 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O || Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) { // dynamicLayoutCrashPreventer needs to be first in array as these are going to be chained when processed - filters = arrayOf(dynamicLayoutCrashPreventer, emptyEditTextBackspaceDetector) + filters = arrayOf(dynamicLayoutCrashPreventer, backspaceDetector) } else { - filters = arrayOf(emptyEditTextBackspaceDetector) + filters = arrayOf(backspaceDetector) } } - private fun handleBackspaceAndEnter(event: KeyEvent): Boolean { + private fun handleBackspaceAndEnter(event: KeyEvent, start: Int = -1, end: Int = -1, dstart: Int = -1, dend: Int = -1): Boolean { if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) { // Check if the external listener has consumed the enter pressed event // In that case stop the execution From fbf98c1af64552a01f1ae203d295b79b8362c629 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 4 Jun 2019 15:45:38 +0200 Subject: [PATCH 2/3] Make sure to properly catch the selection status and do not fire the backspace in case selection is active in the editor --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 26da33cf5..fb8d9bac1 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -499,12 +499,12 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown val backspaceDetector = InputFilter { source, start, end, dest, dstart, dend -> if (isHandlingBackspaceEvent) { - // It's already handling a backspace event. Do nothing. + // It's already handling a backspace event. Do nothing. } else if (end != 0 || start != 0) { // Not a backspace event - } else if (selectionStart != 0 || selectionEnd != 0) { + } else if (selectionStart != selectionEnd) { // there is something selected on the editor, let's make Aztec do the work - } else if(onAztecKeyListener != null) { + } else if (onAztecKeyListener != null) { isHandlingBackspaceEvent = true // Prevent the forced backspace from being added to the history stack From 6bcf5bcfdf93d9c39d529fba3d6b098809438229 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 4 Jun 2019 18:21:56 +0200 Subject: [PATCH 3/3] Add better comments to the code --- aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index fb8d9bac1..a00fe84f3 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -501,10 +501,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown if (isHandlingBackspaceEvent) { // It's already handling a backspace event. Do nothing. } else if (end != 0 || start != 0) { - // Not a backspace event + // Not a text erase event } else if (selectionStart != selectionEnd) { // there is something selected on the editor, let's make Aztec do the work } else if (onAztecKeyListener != null) { + // There is a listener set, let's use it isHandlingBackspaceEvent = true // Prevent the forced backspace from being added to the history stack @@ -513,6 +514,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown handleBackspaceAndEnter(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL), start, end, dstart, dend) isHandlingBackspaceEvent = false } else if (dstart == 0 && dend == 0) { + // Fallback to the old Aztec implementation that does check only the beginnning of the field // Make sure we're at the beginning of the editor field isHandlingBackspaceEvent = true