forked from software-mansion/react-native-enriched-html
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: drag and drop elements on android #122
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
Merged
IvanIhnatsiuk
merged 2 commits into
development
from
feat/drag-and-drop-elements-on-android
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
android/src/main/java/com/swmansion/enriched/EnrichedDragHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package com.swmansion.enriched | ||
|
|
||
| import android.graphics.Color | ||
| import android.graphics.drawable.GradientDrawable | ||
| import android.util.TypedValue | ||
| import android.view.DragEvent | ||
| import android.view.Gravity | ||
| import android.view.HapticFeedbackConstants | ||
| import android.view.MotionEvent | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import com.swmansion.enriched.utils.dp | ||
|
|
||
| class EnrichedDragHandler( | ||
| private val view: EnrichedTextInputView, | ||
| private val clipboardManager: EnrichedClipboardManager, | ||
| ) { | ||
| private data class EnrichedDragLocalState( | ||
| val sourceView: EnrichedTextInputView, | ||
| val start: Int, | ||
| val end: Int, | ||
| ) | ||
|
|
||
| private var lastTouchDownX = 0f | ||
| private var lastTouchDownY = 0f | ||
| private var blockNextActionUpAfterDrag = false | ||
|
|
||
| fun onDragEvent(event: DragEvent): Boolean? { | ||
| if (event.action != DragEvent.ACTION_DROP) { | ||
| return null | ||
| } | ||
|
|
||
| return handleDrop(event) | ||
| } | ||
|
|
||
| fun performLongClick(): Boolean { | ||
| if (!isLastTouchWithinSelection()) { | ||
| return false | ||
| } | ||
|
|
||
| if (!startEnrichedSelectionDrag()) { | ||
| return false | ||
| } | ||
|
|
||
| blockNextActionUpAfterDrag = true | ||
| view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) | ||
| return true | ||
| } | ||
|
|
||
| fun onTouchEvent(event: MotionEvent): Boolean { | ||
| if ( | ||
| blockNextActionUpAfterDrag && | ||
| ( | ||
| event.actionMasked == MotionEvent.ACTION_UP || | ||
| event.actionMasked == MotionEvent.ACTION_CANCEL | ||
| ) | ||
| ) { | ||
| blockNextActionUpAfterDrag = false | ||
| return true | ||
| } | ||
|
|
||
| if (event.actionMasked == MotionEvent.ACTION_DOWN) { | ||
| lastTouchDownX = event.x | ||
| lastTouchDownY = event.y | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| private fun handleDrop(event: DragEvent): Boolean? { | ||
| val localState = event.localState | ||
| val enrichedLocalState = localState as? EnrichedDragLocalState ?: return null | ||
|
|
||
| val clip = event.clipData ?: return null | ||
| val dropOffset = view.getOffsetForPosition(event.x, event.y) | ||
| val sameViewLocalState = enrichedLocalState.takeIf { it.sourceView == view } | ||
| if ( | ||
| sameViewLocalState != null && | ||
| dropOffset >= sameViewLocalState.start && | ||
| dropOffset < sameViewLocalState.end | ||
| ) { | ||
| return true | ||
| } | ||
|
|
||
| val lengthBeforeDrop = view.text?.length ?: 0 | ||
| val didInsert = clipboardManager.insertClipData(clip, dropOffset, dropOffset) | ||
| if (!didInsert) { | ||
| return null | ||
| } | ||
|
|
||
| if (sameViewLocalState != null) { | ||
| deleteSourceAfterLocalDrop(sameViewLocalState, dropOffset, lengthBeforeDrop) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| private fun isLastTouchWithinSelection(): Boolean { | ||
| val start = minOf(view.selectionStart, view.selectionEnd) | ||
| val end = maxOf(view.selectionStart, view.selectionEnd) | ||
| if (start >= end) return false | ||
|
|
||
| val touchOffset = view.getOffsetForPosition(lastTouchDownX, lastTouchDownY) | ||
| return touchOffset >= start && touchOffset < end | ||
| } | ||
|
|
||
| private fun startEnrichedSelectionDrag(): Boolean { | ||
| val start = minOf(view.selectionStart, view.selectionEnd) | ||
| val end = maxOf(view.selectionStart, view.selectionEnd) | ||
| if (start >= end) return false | ||
|
|
||
| val clip = clipboardManager.createSelectedTextClipData(start, end) ?: return false | ||
| val started = | ||
| view.startDragAndDrop( | ||
| clip, | ||
| createTextDragShadow(start, end), | ||
| EnrichedDragLocalState(view, start, end), | ||
| View.DRAG_FLAG_GLOBAL, | ||
| ) | ||
|
|
||
| if (started) { | ||
| view.setSelection(end) | ||
| } | ||
|
|
||
| return started | ||
| } | ||
|
|
||
| private fun createTextDragBackgroundDrawable(): GradientDrawable = | ||
| GradientDrawable().apply { | ||
| shape = GradientDrawable.RECTANGLE | ||
| cornerRadius = 10.dp.toFloat() | ||
| setColor(Color.LTGRAY) | ||
| } | ||
|
|
||
| private fun createTextDragShadow( | ||
| start: Int, | ||
| end: Int, | ||
| ): View.DragShadowBuilder { | ||
| val shadowView = TextView(view.context) | ||
|
|
||
| val padding = 10.dp | ||
|
|
||
| shadowView.apply { | ||
| text = view.editableText?.subSequence(start, end) ?: "" | ||
| setTextColor(view.textColors) | ||
| setTextSize(TypedValue.COMPLEX_UNIT_PX, view.textSize) | ||
| gravity = Gravity.CENTER | ||
| layoutParams = | ||
| ViewGroup.LayoutParams( | ||
| ViewGroup.LayoutParams.WRAP_CONTENT, | ||
| ViewGroup.LayoutParams.WRAP_CONTENT, | ||
| ) | ||
| maxWidth = view.width | ||
| background = createTextDragBackgroundDrawable() | ||
| setPadding(padding, padding / 2, padding, padding / 2) | ||
| } | ||
|
|
||
| val size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) | ||
| shadowView.measure(size, size) | ||
| shadowView.layout(0, 0, shadowView.measuredWidth, shadowView.measuredHeight) | ||
|
|
||
| return View.DragShadowBuilder(shadowView) | ||
| } | ||
|
|
||
| private fun deleteSourceAfterLocalDrop( | ||
| localState: EnrichedDragLocalState, | ||
| dropOffset: Int, | ||
| lengthBeforeDrop: Int, | ||
| ) { | ||
| val editable = view.editableText | ||
| var dragSourceStart = localState.start | ||
| var dragSourceEnd = localState.end | ||
|
|
||
| if (dropOffset <= dragSourceStart) { | ||
| val shift = editable.length - lengthBeforeDrop | ||
| dragSourceStart += shift | ||
| dragSourceEnd += shift | ||
| } | ||
|
|
||
| val safeStart = dragSourceStart.coerceIn(0, editable.length) | ||
| val safeEnd = dragSourceEnd.coerceIn(safeStart, editable.length) | ||
| if (safeStart >= safeEnd) return | ||
|
|
||
| view.transactionManager.runSilently { | ||
| editable.replace(safeStart, safeEnd, "") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.