@@ -7,8 +7,11 @@ import org.wordpress.aztec.AztecText
77import org.wordpress.aztec.Constants
88import org.wordpress.aztec.spans.AztecFullWidthImageSpan
99import org.wordpress.aztec.spans.FullWidthImageProcessingMarker
10+ import java.lang.ref.WeakReference
1011
11- class FullWidthImageElementWatcher (val aztecText : AztecText ) : TextWatcher {
12+ class FullWidthImageElementWatcher (aztecText : AztecText ) : TextWatcher {
13+
14+ private val aztecTextRef: WeakReference <AztecText ?> = WeakReference (aztecText)
1215
1316 private var deletedNewline: Boolean = false
1417 private var changeCount: Int = 0
@@ -28,11 +31,14 @@ class FullWidthImageElementWatcher(val aztecText: AztecText) : TextWatcher {
2831 }
2932
3033 private fun insertVisualNewline (position : Int ) {
31- aztecText. text.insert(position, Constants .NEWLINE_STRING )
34+ aztecTextRef.get()?. text? .insert(position, Constants .NEWLINE_STRING )
3235 }
3336
3437 private fun normalizeEditingAroundImageSpans (count : Int , start : Int ) {
35- if (aztecText.text.getSpans(0 , 0 , FullWidthImageProcessingMarker ::class .java).isEmpty()) {
38+ val aztecText = aztecTextRef.get()
39+ if (aztecText != null && ! aztecText.isTextChangedListenerDisabled() &&
40+ aztecText.text.getSpans(0 , 0 , FullWidthImageProcessingMarker ::class .java).isEmpty()) {
41+
3642 val end = start + count
3743 var lines = aztecText.text.getSpans(start, start, AztecFullWidthImageSpan ::class .java)
3844 lines + = aztecText.text.getSpans(end, end, AztecFullWidthImageSpan ::class .java)
0 commit comments