@@ -86,13 +86,15 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
8686 private var addLinkDialog: AlertDialog ? = null
8787 private var blockEditorDialog: AlertDialog ? = null
8888 private var consumeEditEvent: Boolean = false
89+ private var consumeSelectionChangedEvent: Boolean = false
8990
9091 private var onSelectionChangedListener: OnSelectionChangedListener ? = null
9192 private var onImeBackListener: OnImeBackListener ? = null
9293 private var onImageTappedListener: OnImageTappedListener ? = null
9394 private var onVideoTappedListener: OnVideoTappedListener ? = null
9495
9596 private var isViewInitialized = false
97+ private var isLeadingStyleRemoved = false
9698 private var previousCursorPosition = 0
9799
98100 var isInCalypsoMode = true
@@ -235,9 +237,13 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
235237 // detect the press of backspace from hardware keyboard when no characters are deleted (eg. at 0 index of EditText)
236238 setOnKeyListener { v, keyCode, event ->
237239 var consumeKeyEvent = false
238- history.beforeTextChanged(toFormattedHtml())
239240 if (keyCode == KeyEvent .KEYCODE_DEL && event.action == KeyEvent .ACTION_DOWN ) {
240- inlineFormatter.tryRemoveLeadingInlineStyle()
241+ history.beforeTextChanged(toFormattedHtml())
242+ if (selectionStart == 0 || selectionEnd == 0 ) {
243+ inlineFormatter.tryRemoveLeadingInlineStyle()
244+ isLeadingStyleRemoved = true
245+ onSelectionChanged(0 , 0 )
246+ }
241247 consumeKeyEvent = blockFormatter.tryRemoveBlockStyleFromFirstLine()
242248 }
243249
@@ -282,6 +288,7 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
282288 FullWidthImageElementWatcher .install(this )
283289
284290 EndOfBufferMarkerAdder .install(this )
291+ ZeroIndexContentWatcher .install(this )
285292 }
286293
287294 override fun onWindowFocusChanged (hasWindowFocus : Boolean ) {
@@ -477,6 +484,11 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
477484 super .onSelectionChanged(selStart, selEnd)
478485 if (! isViewInitialized) return
479486
487+ if (isOnSelectionListenerDisabled()) {
488+ enableOnSelectionListener()
489+ return
490+ }
491+
480492 if (length() != 0 ) {
481493 // if the text end has the marker, let's make sure the cursor never includes it or surpasses it
482494 if ((selStart == length() || selEnd == length()) && text[length() - 1 ] == Constants .END_OF_BUFFER_MARKER ) {
@@ -498,16 +510,23 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
498510
499511 previousCursorPosition = selEnd
500512
501- onSelectionChangedListener?.onSelectionChanged(selStart, selEnd)
502513
514+ // do not update toolbar or selected styles when we removed the last character in editor
515+ if (! isLeadingStyleRemoved && length() == 1 && text[0 ] == Constants .END_OF_BUFFER_MARKER ) {
516+ return
517+ }
518+
519+ onSelectionChangedListener?.onSelectionChanged(selStart, selEnd)
503520 setSelectedStyles(getAppliedStyles(selStart, selEnd))
521+
522+ isLeadingStyleRemoved = false
504523 }
505524
525+
506526 override fun getSelectionStart (): Int {
507527 return Math .min(super .getSelectionStart(), super .getSelectionEnd())
508528 }
509529
510-
511530 override fun getSelectionEnd (): Int {
512531 return Math .max(super .getSelectionStart(), super .getSelectionEnd())
513532 }
@@ -843,6 +862,19 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
843862 return consumeEditEvent
844863 }
845864
865+ fun disableOnSelectionListener () {
866+ consumeSelectionChangedEvent = true
867+ }
868+
869+ fun enableOnSelectionListener () {
870+ consumeSelectionChangedEvent = false
871+ }
872+
873+ fun isOnSelectionListenerDisabled (): Boolean {
874+ return consumeSelectionChangedEvent
875+ }
876+
877+
846878 fun refreshText () {
847879 disableTextChangedListener()
848880 val selStart = selectionStart
@@ -893,6 +925,13 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
893925 android.R .id.cut -> {
894926 copy(text, min, max)
895927 text.delete(min, max) // this will hide text action menu
928+
929+ // if we are cutting text from the beginning of editor, remove leading inline style
930+ if (min == 0 ) {
931+ inlineFormatter.tryRemoveLeadingInlineStyle()
932+ isLeadingStyleRemoved = true
933+ onSelectionChanged(0 , 0 )
934+ }
896935 }
897936 else -> return super .onTextContextMenuItem(id)
898937 }
@@ -1067,10 +1106,16 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
10671106
10681107 override fun sendKeyEvent (event : KeyEvent ): Boolean {
10691108 if (event.action == KeyEvent .ACTION_DOWN && event.keyCode == KeyEvent .KEYCODE_DEL ) {
1109+ val isStyleRemoved = blockFormatter.tryRemoveBlockStyleFromFirstLine()
1110+
10701111 history.beforeTextChanged(toFormattedHtml())
1112+ if (selectionStart == 0 || selectionEnd == 0 ) {
1113+ inlineFormatter.tryRemoveLeadingInlineStyle()
1114+ isLeadingStyleRemoved = true
1115+ onSelectionChanged(0 , 0 )
1116+ return false
1117+ }
10711118
1072- inlineFormatter.tryRemoveLeadingInlineStyle()
1073- val isStyleRemoved = blockFormatter.tryRemoveBlockStyleFromFirstLine()
10741119 if (isStyleRemoved) {
10751120 history.handleHistory(this @AztecText)
10761121 return false
@@ -1080,6 +1125,10 @@ class AztecText : AppCompatAutoCompleteTextView, TextWatcher, UnknownHtmlSpan.On
10801125 }
10811126
10821127 override fun deleteSurroundingText (beforeLength : Int , afterLength : Int ): Boolean {
1128+ // detect pressing of backspace with soft keyboard on 0 index, when no text is deleted
1129+ if (beforeLength == 1 && afterLength == 0 && selectionStart == 0 && selectionEnd == 0 ) {
1130+ sendKeyEvent(KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_DEL ))
1131+ }
10831132 return super .deleteSurroundingText(beforeLength, afterLength)
10841133 }
10851134 }
0 commit comments