Skip to content
Merged
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
45 changes: 45 additions & 0 deletions .maestro/flows/extending_paragraph_style_on_paste_after_copy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
appId: swmansion.enriched.example
tags:
- android-only
---
- launchApp
- tapOn:
id: "toggle-screen-button"

- tapOn:
id: "focus-button"

- inputText: "EADING"
- doubleTapOn:
id: "editor-input"
point: "10%, 50%"

- tapOn:
id: "android:id/floating_toolbar_menu_item_text"
text: "Copy"

# Dismiss toolbar with Copy/Cut etc. options
- tapOn:
id: "editor-input"
- tapOn:
id: "clear-button"
- tapOn:
id: "focus-button"

- tapOn:
id: "toolbar-heading-1"

- inputText: "H"

- longPressOn:
id: "editor-input"
point: "50%, 50%"

- tapOn:
id: "android:id/floating_toolbar_menu_item_text"
text: "Paste"

- runFlow:
file: "../subflows/capture_or_assert_screenshot.yaml"
env:
SCREENSHOT_NAME: "extending_paragraph_style_on_paste_after_copy"
37 changes: 37 additions & 0 deletions .maestro/flows/extending_paragraph_style_on_paste_after_cut.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
appId: swmansion.enriched.example
tags:
- android-only
---
- launchApp
- tapOn:
id: "toggle-screen-button"

- tapOn:
id: "focus-button"

- inputText: "EADING"
- doubleTapOn:
id: "editor-input"
point: "10%, 50%"

- tapOn:
id: "android:id/floating_toolbar_menu_item_text"
text: "Cut"

- tapOn:
id: "toolbar-heading-1"

- inputText: "H"

- longPressOn:
id: "editor-input"
point: "50%, 50%"

- tapOn:
id: "android:id/floating_toolbar_menu_item_text"
text: "Paste"

- runFlow:
file: "../subflows/capture_or_assert_screenshot.yaml"
env:
SCREENSHOT_NAME: "extending_paragraph_style_on_paste_after_cut"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Build
import android.text.Editable
import android.text.InputType
import android.text.Spannable
import android.text.SpannableString
import android.util.AttributeSet
import android.util.Log
import android.util.Patterns
Expand Down Expand Up @@ -338,28 +339,37 @@ class EnrichedTextInputView :
}

fun handleTextPaste(item: ClipData.Item) {
val htmlText = item.htmlText
val currentText = text as Spannable
val start = selectionStart.coerceAtLeast(0)
val end = selectionEnd.coerceAtLeast(0)
val lengthBefore = currentText.length

val pastedSpannable: Spannable =
when {
item.htmlText != null -> {
val parsed = parseText(item.htmlText)
(parsed as? Spannable) ?: return
}

item.text != null -> {
SpannableString(item.text.toString())
}

if (htmlText != null) {
val parsedText = parseText(htmlText)
if (parsedText is Spannable) {
val finalText = currentText.mergeSpannables(start, end, parsedText)
setValue(finalText, false)
return
else -> {
return
}
}
}

if (item.text == null) return
val lengthBefore = currentText.length
val finalText = currentText.mergeSpannables(start, end, item.text.toString())
setValue(finalText)
val finalText = currentText.mergeSpannables(start, end, pastedSpannable)
setValue(finalText, false)

// replacement-safe: oldLength - removed + inserted
val insertedLength = finalText.length - (lengthBefore - (end - start))
val pasteEnd = (start + insertedLength).coerceIn(0, finalText.length)
setSelection(pasteEnd)

// Detect links in the newly pasted range
val finalEndIndex = start + finalText.length - lengthBefore
parametrizedStyles?.detectLinksInRange(finalText, start, finalEndIndex)
parametrizedStyles?.detectLinksInRange(finalText, start.coerceAtMost(pasteEnd), pasteEnd)
}

fun requestFocusProgrammatically() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ class ParametrizedStyles(
end: Int,
) {
val regex = view.linkRegex ?: return
val contextText = spannable.subSequence(start, end).toString()
val textLength = spannable.length
val safeStart = minOf(start, end).coerceIn(0, textLength)
val safeEnd = maxOf(start, end).coerceIn(0, textLength)
if (safeStart >= safeEnd) return

val spans = spannable.getSpans(start, end, EnrichedInputLinkSpan::class.java)
val contextText = spannable.subSequence(safeStart, safeEnd).toString()

val spans = spannable.getSpans(safeStart, safeEnd, EnrichedInputLinkSpan::class.java)
for (span in spans) {
spannable.removeSpan(span)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fun Spannable.mergeSpannables(
val isNewLineStart = startBlockSpans.isNotEmpty() || startParagraphSpans.isNotEmpty()
val isNewLineEnd = endBlockSpans.isNotEmpty() || endParagraphSpans.isNotEmpty()

val pastedHasOwnStyles =
spannable.getSpans(0, spannable.length, EnrichedBlockSpan::class.java).isNotEmpty() ||
spannable.getSpans(0, spannable.length, EnrichedParagraphSpan::class.java).isNotEmpty()

if (isNewLineStart && start != paragraphStart) {
builder.insert(start, "\n")
finalStart = start + 1
Expand All @@ -78,5 +82,25 @@ fun Spannable.mergeSpannables(

builder.replace(finalStart, finalEnd, spannable)

// Manually extend existing paragraph/block spans to cover the pasted text.
if (!pastedHasOwnStyles) {
val pasteEnd = finalStart + spannable.length

val affectedParagraphSpans = builder.getSpans(finalStart, finalStart, EnrichedParagraphSpan::class.java)
val affectedBlockSpans = builder.getSpans(finalStart, finalStart, EnrichedBlockSpan::class.java)
val affectedSpans = affectedBlockSpans.toList() + affectedParagraphSpans.toList()

for (span in affectedSpans) {
val spanStart = builder.getSpanStart(span)
val spanEnd = builder.getSpanEnd(span)
if (spanStart == -1 || spanEnd >= pasteEnd) continue

val (_, newParagraphEnd) = builder.getParagraphBounds(spanStart, pasteEnd)
val flags = builder.getSpanFlags(span)
builder.removeSpan(span)
builder.setSpan(span, spanStart, newParagraphEnd, flags)
}
}

return builder
}
Loading