Skip to content

Commit

Permalink
Add uninstall method to EndOfBufferMarkerAdder
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo committed May 27, 2024
1 parent 8bd5798 commit eb9f683
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 6 additions & 1 deletion aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

var observationQueue: ObservationQueue = ObservationQueue(this)
var textWatcherEventBuilder: TextWatcherEvent.Builder = TextWatcherEvent.Builder()
var endOfBufferMarkerAdderWatcher: EndOfBufferMarkerAdder? = null

private var accessibilityDelegate = AztecTextAccessibilityDelegate(this)

Expand Down Expand Up @@ -640,6 +641,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}
}

fun removeEndOfBufferMarkerAdder() {
endOfBufferMarkerAdderWatcher?.uninstallEndOfBuffer(this)
}

private fun <T> selectionHasExactlyOneMarker(start: Int, end: Int, type: Class<T>): Boolean {
val spanFound: Array<T> = editableText.getSpans(
start,
Expand Down Expand Up @@ -891,7 +896,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

FullWidthImageElementWatcher.install(this)

EndOfBufferMarkerAdder.install(this)
endOfBufferMarkerAdderWatcher = EndOfBufferMarkerAdder.install(this)
ZeroIndexContentWatcher.install(this)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
// count < before to take into account auto-corrected words
deletedText = before > 0 && count < before
deletedText = before > 0
}

override fun afterTextChanged(text: Editable) {
Expand All @@ -32,9 +31,26 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher {
// by the way, the cursor will be adjusted "automatically" by RichTextEditText's onSelectionChanged to before the marker
}

fun uninstallEndOfBuffer(aztecText: AztecText) {
uninstall(aztecText)
}

companion object {
fun install(editText: AztecText) {
editText.addTextChangedListener(EndOfBufferMarkerAdder(editText.text))
private val watchers = mutableMapOf<AztecText, EndOfBufferMarkerAdder>()

fun install(editText: AztecText): EndOfBufferMarkerAdder {
var watcher = EndOfBufferMarkerAdder(editText.text)
editText.addTextChangedListener(watcher)
watchers[editText] = watcher
return watcher
}

fun uninstall(editText: AztecText) {
val watcher = watchers[editText]
if (watcher != null) {
editText.removeTextChangedListener(watcher)
watchers.remove(editText)
}
}

fun ensureEndOfTextMarker(text: Editable, deletedText: Boolean = false): Editable {
Expand All @@ -45,9 +61,7 @@ class EndOfBufferMarkerAdder(text: Editable) : TextWatcher {
if (text.isEmpty()) {
if (text.getSpans(0, 0, IAztecBlockSpan::class.java).isNotEmpty()) {
// need to add a end-of-text marker so a block element can render in the empty text.
if (!deletedText) {
text.append(Constants.END_OF_BUFFER_MARKER)
}
text.append(Constants.END_OF_BUFFER_MARKER)
}
return text
} else if (text.length == 1 && text[0] == Constants.END_OF_BUFFER_MARKER && deletedText) {
Expand Down

0 comments on commit eb9f683

Please sign in to comment.