@@ -74,8 +74,8 @@ import org.wordpress.aztec.handlers.ListItemHandler
7474import org.wordpress.aztec.handlers.PreformatHandler
7575import org.wordpress.aztec.handlers.QuoteHandler
7676import org.wordpress.aztec.plugins.IAztecPlugin
77+ import org.wordpress.aztec.plugins.IClipboardPastePlugin
7778import org.wordpress.aztec.plugins.IOnDrawPlugin
78- import org.wordpress.aztec.plugins.ITextPastePlugin
7979import org.wordpress.aztec.plugins.IToolbarButton
8080import org.wordpress.aztec.source.Format
8181import org.wordpress.aztec.source.SourceViewEditText
@@ -259,6 +259,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
259259 var isInCalypsoMode = true
260260 var isInGutenbergMode: Boolean = false
261261 val alignmentRendering: AlignmentRendering
262+
262263 // If this field is true, the media and horizontal line are added inline. If it's false, they are added after the
263264 // current block.
264265 var shouldAddMediaInline: Boolean = true
@@ -353,8 +354,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
353354 }
354355
355356 interface OnAztecKeyListener {
356- fun onEnterKey (text : Spannable , firedAfterTextChanged : Boolean , selStart : Int , selEnd : Int ) : Boolean
357- fun onBackspaceKey () : Boolean
357+ fun onEnterKey (text : Spannable , firedAfterTextChanged : Boolean , selStart : Int , selEnd : Int ): Boolean
358+ fun onBackspaceKey (): Boolean
358359 }
359360
360361 interface OnLinkTappedListener {
@@ -435,11 +436,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
435436 commentsVisible = styles.getBoolean(R .styleable.AztecText_commentsVisible , commentsVisible)
436437
437438 verticalParagraphPadding = styles.getDimensionPixelSize(R .styleable.AztecText_blockVerticalPadding ,
438- resources.getDimensionPixelSize(R .dimen.block_vertical_padding))
439+ resources.getDimensionPixelSize(R .dimen.block_vertical_padding))
439440 verticalParagraphMargin = styles.getDimensionPixelSize(R .styleable.AztecText_paragraphVerticalMargin ,
440- resources.getDimensionPixelSize(R .dimen.block_vertical_margin))
441+ resources.getDimensionPixelSize(R .dimen.block_vertical_margin))
441442 verticalHeadingMargin = styles.getDimensionPixelSize(R .styleable.AztecText_headingVerticalPadding ,
442- resources.getDimensionPixelSize(R .dimen.heading_vertical_padding))
443+ resources.getDimensionPixelSize(R .dimen.heading_vertical_padding))
443444
444445 inlineFormatter = InlineFormatter (this ,
445446 InlineFormatter .CodeStyle (
@@ -597,7 +598,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
597598 isViewInitialized = true
598599 }
599600
600- private fun <T >selectionHasExactlyOneMarker (start : Int , end : Int , type : Class <T >): Boolean {
601+ private fun <T > selectionHasExactlyOneMarker (start : Int , end : Int , type : Class <T >): Boolean {
601602 val spanFound: Array <T > = editableText.getSpans(
602603 start,
603604 end,
@@ -670,11 +671,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
670671 // problem is fixed at the Android OS level as described in the following url
671672 // https://android-review.googlesource.com/c/platform/frameworks/base/+/634929
672673 val dynamicLayoutCrashPreventer = InputFilter { source, start, end, dest, dstart, dend ->
673- var temp : CharSequence? = null
674+ var temp: CharSequence? = null
674675 if (! bypassCrashPreventerInputFilter && dend < dest.length && source != Constants .NEWLINE_STRING ) {
675676
676677 // if there are any images right after the destination position, hack the text
677- val spans = dest.getSpans(dend, dend+ 1 , AztecImageSpan ::class .java)
678+ val spans = dest.getSpans(dend, dend + 1 , AztecImageSpan ::class .java)
678679 if (spans.isNotEmpty()) {
679680
680681 // prevent this filter from running recursively
@@ -730,7 +731,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
730731 }
731732
732733 private fun isCleanStringEmpty (text : CharSequence ): Boolean {
733- if ( isInGutenbergMode ) {
734+ if (isInGutenbergMode) {
734735 return (text.count() == 1 && text[0 ] == Constants .END_OF_BUFFER_MARKER )
735736 } else {
736737 return text.count() == 0
@@ -1045,7 +1046,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
10451046 this .onSelectionChangedListener = onSelectionChangedListener
10461047 }
10471048
1048- fun getAztecKeyListener () : OnAztecKeyListener ? {
1049+ fun getAztecKeyListener (): OnAztecKeyListener ? {
10491050 return this .onAztecKeyListener
10501051 }
10511052
@@ -1823,7 +1824,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18231824 } else {
18241825 return super .onTextContextMenuItem(id)
18251826 }
1826- } else -> return super .onTextContextMenuItem(id)
1827+ }
1828+ else -> return super .onTextContextMenuItem(id)
18271829 }
18281830
18291831 return true
@@ -1900,21 +1902,42 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19001902 enableTextChangedListener()
19011903
19021904 if (clip.itemCount > 0 ) {
1903- val textToPaste = if (asPlainText) clip.getItemAt(0 ).coerceToText(context).toString()
1904- else clip.getItemAt(0 ).coerceToHtmlText(AztecParser (alignmentRendering, plugins))
1905-
1906- val oldHtml = toPlainHtml().replace(" <aztec_cursor>" , " " )
1907- val pastedHtmlText = plugins.filterIsInstance<ITextPastePlugin >().fold(textToPaste) { acc, plugin ->
1908- if (selectedText.isNullOrEmpty()) {
1909- plugin.toHtml(acc)
1910- } else {
1911- plugin.toHtml(selectedText, acc)
1905+ val firstItem = clip.getItemAt(0 )
1906+ val itemToPaste = when {
1907+ ! firstItem.text.isNullOrEmpty() -> {
1908+ val textToPaste = if (asPlainText) clip.getItemAt(0 ).coerceToText(context).toString()
1909+ else clip.getItemAt(0 ).coerceToHtmlText(AztecParser (alignmentRendering, plugins))
1910+ IClipboardPastePlugin .PastedItem .HtmlText (textToPaste)
1911+ }
1912+ firstItem.uri != null -> {
1913+ IClipboardPastePlugin .PastedItem .Url (firstItem.uri)
1914+ }
1915+ firstItem.intent != null -> {
1916+ IClipboardPastePlugin .PastedItem .PastedIntent (firstItem.intent)
1917+ }
1918+ else -> {
1919+ null
19121920 }
19131921 }
1914- val newHtml = oldHtml.replace(Constants .REPLACEMENT_MARKER_STRING , pastedHtmlText + " <" + AztecCursorSpan .AZTEC_CURSOR_TAG + " >" )
1922+ if (itemToPaste != null ) {
1923+ val oldHtml = toPlainHtml().replace(" <aztec_cursor>" , " " )
1924+ val pastedHtmlText: String = plugins.filterIsInstance<IClipboardPastePlugin <* >>()
1925+ .fold(null as ? String? ) { acc, plugin ->
1926+ plugin.itemToHtml(itemToPaste, acc ? : selectedText?.takeIf { it.isNotBlank() }) ? : acc
1927+ } ? : when (itemToPaste) {
1928+ is IClipboardPastePlugin .PastedItem .HtmlText -> itemToPaste.text
1929+ is IClipboardPastePlugin .PastedItem .Url -> itemToPaste.uri.path
1930+ is IClipboardPastePlugin .PastedItem .PastedIntent -> itemToPaste.intent.toString()
1931+ }
19151932
1916- fromHtml(newHtml, false )
1917- inlineFormatter.joinStyleSpans(0 , length())
1933+ val newHtml = oldHtml.replace(
1934+ Constants .REPLACEMENT_MARKER_STRING ,
1935+ pastedHtmlText + " <" + AztecCursorSpan .AZTEC_CURSOR_TAG + " >"
1936+ )
1937+
1938+ fromHtml(newHtml, false )
1939+ inlineFormatter.joinStyleSpans(0 , length())
1940+ }
19181941 }
19191942 contentChangeWatcher.notifyContentChanged()
19201943 }
@@ -1964,7 +1987,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19641987 }
19651988
19661989 @SuppressLint(" InflateParams" )
1967- fun showLinkDialog (presetUrl : String = "", presetAnchor : String = "", presetOpenInNewWindow : String = "" ) {
1990+ fun showLinkDialog (presetUrl : String = "", presetAnchor : String = "", presetOpenInNewWindow : String = "") {
19681991 val urlAndAnchor = linkFormatter.getSelectedUrlWithAnchor()
19691992
19701993 val url = if (TextUtils .isEmpty(presetUrl)) urlAndAnchor.first else presetUrl
0 commit comments