Skip to content
Merged
34 changes: 34 additions & 0 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,41 @@ 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

// We check whether the user tap on a checkbox of a task list. The aztec text field should be enabled and we
// check whether the tap event was on the leading margin which is where the checkbox is located plus a padding
// space used to increase the target size for the tap event.
if (isEnabled &&
x + totalPaddingStart <= (blockFormatter.listStyleLeadingMargin() + AztecTaskListSpan.PADDING_SPACE)) {
val line = layout.getLineForVertical(y)
val off = layout.getOffsetForHorizontal(line, x.toFloat())
// If the tap event was on the leading margin, we double check whether we are tapping on a task list item.
// If that is true, then we return false because we don't want to propagate the tap event to stop moving
// the cursor to the item tapped.
if (getTaskListHandler()?.handleTaskListClick(
text,
off,
x,
totalPaddingStart
) == true) {
refreshText(stealEditorFocus = false)
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.wordpress.aztec.spans.AztecTaskListSpan
class TaskListClickHandler(val listStyle: BlockFormatter.ListStyle) {
fun handleTaskListClick(text: Spannable, off: Int, x: Int, startMargin: Int): Boolean {
// We want to make sure that text click will not trigger the checked change
if (x + startMargin > listStyle.leadingMargin()) return false
if (x + startMargin > (listStyle.leadingMargin() + AztecTaskListSpan.PADDING_SPACE)) return false
val clickedList = text.getSpans(off, off, AztecTaskListSpan::class.java).firstOrNull()
val clickedLines = text.getSpans(off, off, AztecListItemSpan::class.java)
val clickedLine = clickedLines.find {
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,9 @@ open class AztecTaskListSpan(
}

override val textFormat: ITextFormat = AztecTextFormat.FORMAT_TASK_LIST

companion object {
// Extra padding added to the target tap area for checkboxes.
const val PADDING_SPACE = 15
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ interface ComposePlaceholderAdapter : PlaceholderManager.PlaceholderAdapter {
* @param attrs aztec attributes of the view
*/
@Composable
fun Placeholder(
placeholderUuid: String,
attrs: AztecAttributes,
)
fun Placeholder(placeholderUuid: String, attrs: AztecAttributes)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("ktlint")

package org.wordpress.aztec.placeholders

import android.graphics.Rect
Expand Down Expand Up @@ -101,7 +103,7 @@ class ComposePlaceholderManager(
.zIndex(9f)
.padding(
top = with(density) { composeView.topMargin.toDp() },
start = with(density) { composeView.leftMargin.toDp() },
start = with(density) { composeView.leftMargin.toDp() }
)
.width(
with(density) { composeView.width.toDp() }
Expand All @@ -113,7 +115,7 @@ class ComposePlaceholderManager(
key(composeView.uuid, composeView.width, composeView.height) {
adapters[composeView.adapterKey]?.Placeholder(
composeView.uuid,
composeView.attrs,
composeView.attrs
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ViewPlaceholderManager(
* @param type placeholder type
* @param attributes other attributes passed to the view. For example a `src` for an image.
*/
override suspend fun insertItem(type: String, vararg attributes: Pair<String, String>) {
suspend override fun insertItem(type: String, vararg attributes: Pair<String, String>) {
val adapter = adapters[type]
?: throw IllegalArgumentException("Adapter for inserted type not found. Register it with `registerAdapter` method")
val attrs = getAttributesForMedia(type, attributes)
Expand All @@ -113,7 +113,7 @@ class ViewPlaceholderManager(
* @param shouldMergeItem this method should return true when the previous type is compatible and should be updated
* @param updateItem function to update current parameters with new params
*/
override suspend fun insertOrUpdateItem(
suspend override fun insertOrUpdateItem(
type: String,
shouldMergeItem: (currentItemType: String) -> Boolean,
updateItem: (
Expand Down Expand Up @@ -186,7 +186,7 @@ class ViewPlaceholderManager(
* @param shouldUpdateItem This function should return true if the span can be updated, false if it should be removed
* @param updateItem Function that updates the selected item
*/
override suspend fun removeOrUpdate(uuid: String, shouldUpdateItem: (Attributes) -> Boolean, updateItem: (currentAttributes: Map<String, String>) -> Map<String, String>): Boolean {
suspend override fun removeOrUpdate(uuid: String, shouldUpdateItem: (Attributes) -> Boolean, updateItem: (currentAttributes: Map<String, String>) -> Map<String, String>): Boolean {
val currentItem = aztecText.editableText.getSpans(0, aztecText.length(), AztecPlaceholderSpan::class.java).find {
it.attributes.getValue(UUID_ATTRIBUTE) == uuid
} ?: return false
Expand Down
Loading