Skip to content

Commit 5da7835

Browse files
authored
Merge pull request #739 from wordpress-mobile/feature/paste-as-plain-text
Feature / Android8 / Paste as plain text
2 parents a762602 + 4c0e205 commit 5da7835

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,8 +1310,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
13101310
}
13111311

13121312
when (id) {
1313-
android.R.id.paste,
1314-
android.R.id.pasteAsPlainText -> paste(text, min, max)
1313+
android.R.id.paste -> paste(text, min, max)
1314+
android.R.id.pasteAsPlainText -> paste(text, min, max, true)
13151315
android.R.id.copy -> {
13161316
copy(text, min, max)
13171317
setSelection(max) // dismiss the selection to make the action menu hide
@@ -1364,7 +1364,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
13641364
}
13651365

13661366
// copied from TextView with some changes
1367-
fun paste(editable: Editable, min: Int, max: Int) {
1367+
fun paste(editable: Editable, min: Int, max: Int, asPlainText: Boolean = false) {
13681368
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
13691369
val clip = clipboard.primaryClip
13701370

@@ -1394,7 +1394,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
13941394
enableTextChangedListener()
13951395

13961396
if (clip.itemCount > 0) {
1397-
val textToPaste = clip.getItemAt(0).coerceToHtmlText(AztecParser(plugins))
1397+
val textToPaste = if (asPlainText) clip.getItemAt(0).coerceToText(context).toString()
1398+
else clip.getItemAt(0).coerceToHtmlText(AztecParser(plugins))
13981399

13991400
val oldHtml = toPlainHtml().replace("<aztec_cursor>", "")
14001401
val newHtml = oldHtml.replace(Constants.REPLACEMENT_MARKER_STRING, textToPaste + "<" + AztecCursorSpan.AZTEC_CURSOR_TAG + ">")

aztec/src/test/kotlin/org/wordpress/aztec/ClipboardTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,32 @@ class ClipboardTest {
310310

311311
editText.setCalypsoMode(false)
312312
}
313+
314+
@Test
315+
@Throws(Exception::class)
316+
fun copyAndPasteAsPlainTextSameInlineStyle() {
317+
editText.fromHtml("<b>Bold</b>")
318+
319+
editText.setSelection(0, editText.length())
320+
TestUtils.copyToClipboard(editText)
321+
322+
editText.setSelection(editText.length())
323+
TestUtils.pasteFromClipboardAsPlainText(editText)
324+
325+
Assert.assertEquals("<b>Bold</b>Bold", editText.toHtml())
326+
}
327+
328+
@Test
329+
@Throws(Exception::class)
330+
fun copyAndReplaceAsPlainText() {
331+
editText.fromHtml(LONG_TEXT)
332+
333+
editText.setSelection(0, editText.length())
334+
TestUtils.copyToClipboard(editText)
335+
336+
editText.setSelection(0, editText.length())
337+
TestUtils.pasteFromClipboardAsPlainText(editText)
338+
339+
Assert.assertEquals(LONG_TEXT_EXPECTED, editText.toHtml())
340+
}
313341
}

aztec/src/test/kotlin/org/wordpress/aztec/TestUtils.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ object TestUtils {
6464
text.paste(text.text, text.selectionStart, text.selectionEnd)
6565
}
6666

67+
/**
68+
* Issue a copy as plain text to clipboard key event
69+
*
70+
* @param text The EditText to issue the key event to
71+
*/
72+
fun pasteFromClipboardAsPlainText(text: AztecText) {
73+
text.paste(text.text, text.selectionStart, text.selectionEnd, true)
74+
}
75+
6776
/**
6877
* Helper for calculating the EditText's length *without* counting the the end-of-text marker char if present
6978
*

0 commit comments

Comments
 (0)