@@ -245,8 +245,19 @@ class BlockFormatter(editor: AztecText,
245245 }
246246 val selectionStart = editor.selectionStart
247247
248+ var newSelStart = if (selectionStart > selectionEnd) selectionEnd else selectionStart
249+ var newSelEnd = selectionEnd
250+
251+ if (newSelStart == 0 && newSelEnd == 0 ) {
252+ newSelEnd++
253+ } else if (newSelStart == newSelEnd && editableText.length > selectionStart && editableText[selectionStart - 1 ] == Constants .NEWLINE ) {
254+ newSelEnd++
255+ } else if (newSelStart > 0 && ! editor.isTextSelected()) {
256+ newSelStart--
257+ }
258+
248259 // try to remove block styling when pressing backspace at the beginning of the text
249- editableText.getSpans(selectionStart, selectionEnd , IAztecBlockSpan ::class .java).forEach {
260+ editableText.getSpans(newSelStart, newSelEnd , IAztecBlockSpan ::class .java).forEach {
250261 // We want to remove any list item span that's being converted to another block
251262 if (it is AztecListItemSpan ) {
252263 editableText.removeSpan(it)
@@ -267,15 +278,15 @@ class BlockFormatter(editor: AztecText,
267278 val spanFlags = editableText.getSpanFlags(it)
268279 val nextLineLength = " \n " .length
269280 // Defines end of a line in a block
270- val previousLineBreak = editableText.indexOf(" \n " , selectionEnd )
281+ val previousLineBreak = editableText.indexOf(" \n " , newSelEnd )
271282 val lineEnd = if (previousLineBreak > - 1 ) {
272283 previousLineBreak + nextLineLength
273284 } else spanEnd
274285 // Defines start of a line in a block
275- val nextLineBreak = if (lineEnd == selectionStart + nextLineLength) {
276- editableText.lastIndexOf(" \n " , selectionStart - 1 )
286+ val nextLineBreak = if (lineEnd == newSelStart + nextLineLength) {
287+ editableText.lastIndexOf(" \n " , newSelStart - 1 )
277288 } else {
278- editableText.lastIndexOf(" \n " , selectionStart )
289+ editableText.lastIndexOf(" \n " , newSelStart )
279290 }
280291 val lineStart = if (nextLineBreak > - 1 ) {
281292 nextLineBreak + nextLineLength
0 commit comments