Skip to content

Commit b26d3da

Browse files
committed
Only use weak reference to AztecText and check for disabled text listener
1 parent 9dd5c35 commit b26d3da

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/watchers/FullWidthImageElementWatcher.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import org.wordpress.aztec.AztecText
77
import org.wordpress.aztec.Constants
88
import org.wordpress.aztec.spans.AztecFullWidthImageSpan
99
import 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

Comments
 (0)