Skip to content

Commit

Permalink
Intercept touch event to handle it before the cursor is moved
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo04 committed Jan 23, 2025
1 parent 806c3a4 commit ed211fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 30 additions & 3 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,34 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
return containingBoxBounds
}

private fun getTaskListHandler(): TaskListClickHandler? {
return EnhancedMovementMethod.taskListClickHandler
}

override fun onTouchEvent(event: MotionEvent): Boolean {
var x = event.x.toInt()
var y = event.y.toInt()

x -= totalPaddingLeft
y -= totalPaddingTop

x += scrollX
y += scrollY

// Check if we're in the task list area
if (x + totalPaddingStart <= blockFormatter.listStyleLeadingMargin()) {
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())
if (getTaskListHandler()?.handleTaskListClick(
text,
off,
x,
totalPaddingStart
) == true) {
return false
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
&& event.action == MotionEvent.ACTION_DOWN) {
// we'll use these values in OnLongClickListener
Expand Down Expand Up @@ -1788,8 +1815,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

private fun refreshTaskListSpan(taskList: AztecTaskListSpan) {
val selStart = selectionStart
val selEnd = selectionEnd
// val selStart = selectionStart
// val selEnd = selectionEnd
val spanStart = this.editableText.getSpanStart(taskList)
val spanEnd = this.editableText.getSpanEnd(taskList)
val flags = this.editableText.getSpanFlags(taskList)
Expand All @@ -1804,7 +1831,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
refreshTaskListSpan(it)
}
this.editableText.setSpan(newSpan, spanStart, spanEnd, flags)
setSelection(selStart, selEnd)
//setSelection(selStart, selEnd)
}

private fun clearTaskListRefreshListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class BlockFormatter(editor: AztecText,
indentFormatter.outdent()
}

fun listStyleLeadingMargin(): Int {
return listStyle.leadingMargin()
}

fun isIndentAvailable(): Boolean {
if (listFormatter.isIndentAvailable()) return true
return indentFormatter.isIndentAvailable()
Expand Down

0 comments on commit ed211fb

Please sign in to comment.