diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt index 5e7e6e314..305e3dd9f 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt @@ -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 diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt b/aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt index 2767867a2..d0545bbff 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/TaskListClickHandler.kt @@ -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 { diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt index a6e83c467..3e9f3802c 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt @@ -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() diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt index 38baa6298..b359fba5c 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecTaskListSpan.kt @@ -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 + } } diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt index b4150e7eb..bd5465b6c 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderAdapter.kt @@ -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) } diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt index 0ce253677..600b89182 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ComposePlaceholderManager.kt @@ -1,3 +1,5 @@ +@file:Suppress("ktlint") + package org.wordpress.aztec.placeholders import android.graphics.Rect @@ -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() } @@ -113,7 +115,7 @@ class ComposePlaceholderManager( key(composeView.uuid, composeView.width, composeView.height) { adapters[composeView.adapterKey]?.Placeholder( composeView.uuid, - composeView.attrs, + composeView.attrs ) } } diff --git a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt index 77ae017b8..75edb375f 100644 --- a/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt +++ b/media-placeholders/src/main/java/org/wordpress/aztec/placeholders/ViewPlaceholderManager.kt @@ -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) { + suspend override fun insertItem(type: String, vararg attributes: Pair) { val adapter = adapters[type] ?: throw IllegalArgumentException("Adapter for inserted type not found. Register it with `registerAdapter` method") val attrs = getAttributesForMedia(type, attributes) @@ -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: ( @@ -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) -> Map): Boolean { + suspend override fun removeOrUpdate(uuid: String, shouldUpdateItem: (Attributes) -> Boolean, updateItem: (currentAttributes: Map) -> Map): Boolean { val currentItem = aztecText.editableText.getSpans(0, aztecText.length(), AztecPlaceholderSpan::class.java).find { it.attributes.getValue(UUID_ATTRIBUTE) == uuid } ?: return false