@@ -74,8 +74,9 @@ 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
78+ import org.wordpress.aztec.plugins.IClipboardPastePlugin.*
7779import org.wordpress.aztec.plugins.IOnDrawPlugin
78- import org.wordpress.aztec.plugins.ITextPastePlugin
7980import org.wordpress.aztec.plugins.IToolbarButton
8081import org.wordpress.aztec.source.Format
8182import org.wordpress.aztec.source.SourceViewEditText
@@ -259,6 +260,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
259260 var isInCalypsoMode = true
260261 var isInGutenbergMode: Boolean = false
261262 val alignmentRendering: AlignmentRendering
263+
262264 // If this field is true, the media and horizontal line are added inline. If it's false, they are added after the
263265 // current block.
264266 var shouldAddMediaInline: Boolean = true
@@ -353,8 +355,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
353355 }
354356
355357 interface OnAztecKeyListener {
356- fun onEnterKey (text : Spannable , firedAfterTextChanged : Boolean , selStart : Int , selEnd : Int ) : Boolean
357- fun onBackspaceKey () : Boolean
358+ fun onEnterKey (text : Spannable , firedAfterTextChanged : Boolean , selStart : Int , selEnd : Int ): Boolean
359+ fun onBackspaceKey (): Boolean
358360 }
359361
360362 interface OnLinkTappedListener {
@@ -435,11 +437,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
435437 commentsVisible = styles.getBoolean(R .styleable.AztecText_commentsVisible , commentsVisible)
436438
437439 verticalParagraphPadding = styles.getDimensionPixelSize(R .styleable.AztecText_blockVerticalPadding ,
438- resources.getDimensionPixelSize(R .dimen.block_vertical_padding))
440+ resources.getDimensionPixelSize(R .dimen.block_vertical_padding))
439441 verticalParagraphMargin = styles.getDimensionPixelSize(R .styleable.AztecText_paragraphVerticalMargin ,
440- resources.getDimensionPixelSize(R .dimen.block_vertical_margin))
442+ resources.getDimensionPixelSize(R .dimen.block_vertical_margin))
441443 verticalHeadingMargin = styles.getDimensionPixelSize(R .styleable.AztecText_headingVerticalPadding ,
442- resources.getDimensionPixelSize(R .dimen.heading_vertical_padding))
444+ resources.getDimensionPixelSize(R .dimen.heading_vertical_padding))
443445
444446 inlineFormatter = InlineFormatter (this ,
445447 InlineFormatter .CodeStyle (
@@ -597,7 +599,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
597599 isViewInitialized = true
598600 }
599601
600- private fun <T >selectionHasExactlyOneMarker (start : Int , end : Int , type : Class <T >): Boolean {
602+ private fun <T > selectionHasExactlyOneMarker (start : Int , end : Int , type : Class <T >): Boolean {
601603 val spanFound: Array <T > = editableText.getSpans(
602604 start,
603605 end,
@@ -670,11 +672,11 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
670672 // problem is fixed at the Android OS level as described in the following url
671673 // https://android-review.googlesource.com/c/platform/frameworks/base/+/634929
672674 val dynamicLayoutCrashPreventer = InputFilter { source, start, end, dest, dstart, dend ->
673- var temp : CharSequence? = null
675+ var temp: CharSequence? = null
674676 if (! bypassCrashPreventerInputFilter && dend < dest.length && source != Constants .NEWLINE_STRING ) {
675677
676678 // if there are any images right after the destination position, hack the text
677- val spans = dest.getSpans(dend, dend+ 1 , AztecImageSpan ::class .java)
679+ val spans = dest.getSpans(dend, dend + 1 , AztecImageSpan ::class .java)
678680 if (spans.isNotEmpty()) {
679681
680682 // prevent this filter from running recursively
@@ -730,7 +732,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
730732 }
731733
732734 private fun isCleanStringEmpty (text : CharSequence ): Boolean {
733- if ( isInGutenbergMode ) {
735+ if (isInGutenbergMode) {
734736 return (text.count() == 1 && text[0 ] == Constants .END_OF_BUFFER_MARKER )
735737 } else {
736738 return text.count() == 0
@@ -1045,7 +1047,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
10451047 this .onSelectionChangedListener = onSelectionChangedListener
10461048 }
10471049
1048- fun getAztecKeyListener () : OnAztecKeyListener ? {
1050+ fun getAztecKeyListener (): OnAztecKeyListener ? {
10491051 return this .onAztecKeyListener
10501052 }
10511053
@@ -1823,7 +1825,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18231825 } else {
18241826 return super .onTextContextMenuItem(id)
18251827 }
1826- } else -> return super .onTextContextMenuItem(id)
1828+ }
1829+ else -> return super .onTextContextMenuItem(id)
18271830 }
18281831
18291832 return true
@@ -1900,21 +1903,42 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19001903 enableTextChangedListener()
19011904
19021905 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)
1906+ val firstItem = clip.getItemAt(0 )
1907+ val itemToPaste = when {
1908+ firstItem.text.isNotBlank() -> {
1909+ val textToPaste = if (asPlainText) clip.getItemAt(0 ).coerceToText(context).toString()
1910+ else clip.getItemAt(0 ).coerceToHtmlText(AztecParser (alignmentRendering, plugins))
1911+ PastedItem .HtmlText (textToPaste)
1912+ }
1913+ firstItem.uri != null -> {
1914+ PastedItem .Url (firstItem.uri)
1915+ }
1916+ firstItem.intent != null -> {
1917+ PastedItem .PastedIntent (firstItem.intent)
1918+ }
1919+ else -> {
1920+ null
19121921 }
19131922 }
1914- val newHtml = oldHtml.replace(Constants .REPLACEMENT_MARKER_STRING , pastedHtmlText + " <" + AztecCursorSpan .AZTEC_CURSOR_TAG + " >" )
1923+ if (itemToPaste != null ) {
1924+ val oldHtml = toPlainHtml().replace(" <aztec_cursor>" , " " )
1925+ val pastedHtmlText: String = plugins.filterIsInstance<IClipboardPastePlugin <* >>()
1926+ .fold(null as ? String? ) { acc, plugin ->
1927+ plugin.itemToHtml(itemToPaste, acc ? : selectedText?.takeIf { it.isNotBlank() }) ? : acc
1928+ } ? : when (itemToPaste) {
1929+ is PastedItem .HtmlText -> itemToPaste.text
1930+ is PastedItem .Url -> itemToPaste.uri.path
1931+ is PastedItem .PastedIntent -> itemToPaste.intent.toString()
1932+ }
19151933
1916- fromHtml(newHtml, false )
1917- inlineFormatter.joinStyleSpans(0 , length())
1934+ val newHtml = oldHtml.replace(
1935+ Constants .REPLACEMENT_MARKER_STRING ,
1936+ pastedHtmlText + " <" + AztecCursorSpan .AZTEC_CURSOR_TAG + " >"
1937+ )
1938+
1939+ fromHtml(newHtml, false )
1940+ inlineFormatter.joinStyleSpans(0 , length())
1941+ }
19181942 }
19191943 contentChangeWatcher.notifyContentChanged()
19201944 }
@@ -1964,7 +1988,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19641988 }
19651989
19661990 @SuppressLint(" InflateParams" )
1967- fun showLinkDialog (presetUrl : String = "", presetAnchor : String = "", presetOpenInNewWindow : String = "" ) {
1991+ fun showLinkDialog (presetUrl : String = "", presetAnchor : String = "", presetOpenInNewWindow : String = "") {
19681992 val urlAndAnchor = linkFormatter.getSelectedUrlWithAnchor()
19691993
19701994 val url = if (TextUtils .isEmpty(presetUrl)) urlAndAnchor.first else presetUrl
0 commit comments