Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handleBackspaceAndEnter to prevent removing the current styles using Gutenberg #1077

Draft
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 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 @@ -839,6 +844,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

var wasStyleRemoved = false
if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) {
if (isInGutenbergMode && (selectionStart == 0 && isTextSelected())) {
return false
}

if (!consumeHistoryEvent) {
history.beforeTextChanged(this@AztecText)
}
Expand Down Expand Up @@ -887,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 @@ -31,9 +31,24 @@ 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 var watcherRef: EndOfBufferMarkerAdder? = null

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

fun uninstall(editText: AztecText) {
if (watcherRef != null) {
editText.removeTextChangedListener(watcherRef)
}
}

fun ensureEndOfTextMarker(text: Editable, deletedText: Boolean = false): Editable {
Expand Down
Loading