@@ -268,24 +268,15 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
268268 }
269269
270270 fun insertMedia (drawable : Drawable , source : String ) {
271- // check if we add media into a block element, at the end of the line, but not at the end of last line
272- var applyingOnTheEndOfBlockLine = false
273- editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan ::class .java).forEach {
274- if (editableText.getSpanEnd(it) > selectionEnd && editableText[selectionEnd] == ' \n ' ) {
275- applyingOnTheEndOfBlockLine = true
276- return @forEach
277- }
278- }
279-
280- val mediaStartIndex = selectionStart + 1
281- val mediaEndIndex = selectionStart + source.length + 1
271+ val mediaStartIndex = selectionStart
272+ val mediaEndIndex = selectionStart + source.length
282273
283274 editor.disableTextChangedListener()
284- editableText.replace(selectionStart, selectionEnd, " \n " + source + if (applyingOnTheEndOfBlockLine) " " else " \n " )
275+ editableText.replace(selectionStart, selectionEnd, source)
285276
286- editor.removeBlockStylesFromRange(mediaStartIndex, mediaEndIndex + 1 , true )
287- editor.removeHeadingStylesFromRange(mediaStartIndex, mediaEndIndex + 1 )
288- editor.removeInlineStylesFromRange(mediaStartIndex, mediaEndIndex + 1 )
277+ editor.removeBlockStylesFromRange(mediaStartIndex, mediaEndIndex, true )
278+ editor.removeHeadingStylesFromRange(mediaStartIndex, mediaEndIndex)
279+ editor.removeInlineStylesFromRange(mediaStartIndex, mediaEndIndex)
289280
290281 val span = AztecMediaSpan (editor.context, drawable, source)
291282
@@ -303,7 +294,7 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
303294 Spanned .SPAN_EXCLUSIVE_EXCLUSIVE
304295 )
305296
306- editor.setSelection(mediaEndIndex + 1 )
297+ editor.setSelection(mediaEndIndex)
307298 editor.isMediaAdded = true
308299 }
309300}
0 commit comments