Skip to content

Commit 83e4b9f

Browse files
authored
Remove formatting when pasting into Input Field (#6434)
Task/Issue URL: https://app.asana.com/1/137249556945/project/1200204095367872/task/1210847833508795?focus=true ### Description - Removes formatting when pasting text into the Input Field on the Input Screen. ### Steps to test this PR - [ ] Paste some formatted text into the Input Field (amazon.com listings work) - [ ] Verify that the text is not formatted - [ ] Verify no trailing newlines
1 parent a5e092c commit 83e4b9f

File tree

1 file changed

+30
-0
lines changed
  • duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/inputscreen/ui/view

1 file changed

+30
-0
lines changed

duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/inputscreen/ui/view/InputModeWidget.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ package com.duckduckgo.duckchat.impl.inputscreen.ui.view
1818

1919
import android.content.Context
2020
import android.os.Build
21+
import android.text.Editable
2122
import android.text.InputType
23+
import android.text.Spanned
24+
import android.text.style.CharacterStyle
25+
import android.text.style.ImageSpan
26+
import android.text.style.ParagraphStyle
27+
import android.text.style.URLSpan
2228
import android.transition.ChangeBounds
2329
import android.transition.Fade
2430
import android.transition.TransitionManager
@@ -32,6 +38,7 @@ import android.widget.EditText
3238
import androidx.annotation.IdRes
3339
import androidx.constraintlayout.widget.ConstraintLayout
3440
import androidx.core.view.isVisible
41+
import androidx.core.widget.doAfterTextChanged
3542
import androidx.core.widget.doOnTextChanged
3643
import com.duckduckgo.anvil.annotations.InjectWith
3744
import com.duckduckgo.common.ui.view.addBottomShadow
@@ -149,6 +156,29 @@ class InputModeWidget @JvmOverloads constructor(
149156
}
150157
}
151158
}
159+
160+
doAfterTextChanged { text ->
161+
text?.let {
162+
removeFormatting(text)
163+
}
164+
}
165+
}
166+
167+
private fun removeFormatting(text: Editable) {
168+
val spans = buildList<Any> {
169+
addAll(text.getSpans(0, text.length, CharacterStyle::class.java))
170+
addAll(text.getSpans(0, text.length, ParagraphStyle::class.java))
171+
addAll(text.getSpans(0, text.length, URLSpan::class.java))
172+
addAll(text.getSpans(0, text.length, ImageSpan::class.java))
173+
}.filter { span ->
174+
(text.getSpanFlags(span) and Spanned.SPAN_COMPOSING) == 0
175+
}
176+
177+
if (spans.isNotEmpty()) {
178+
spans.forEach(text::removeSpan)
179+
// Remove trailing newlines
180+
text.delete(text.indexOfLast { it != '\n' } + 1, text.length)
181+
}
152182
}
153183

154184
private fun configureTabBehavior() {

0 commit comments

Comments
 (0)