Skip to content

Commit c97e5d8

Browse files
committed
Add UrlPastePlugin.kt to handle pasted links
1 parent 87fd9d6 commit c97e5d8

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import org.wordpress.aztec.glideloader.GlideImageLoader
4646
import org.wordpress.aztec.glideloader.GlideVideoThumbnailLoader
4747
import org.wordpress.aztec.plugins.CssUnderlinePlugin
4848
import org.wordpress.aztec.plugins.IMediaToolbarButton
49+
import org.wordpress.aztec.plugins.UrlPastePlugin
4950
import org.wordpress.aztec.plugins.shortcodes.AudioShortcodePlugin
5051
import org.wordpress.aztec.plugins.shortcodes.CaptionShortcodePlugin
5152
import org.wordpress.aztec.plugins.shortcodes.VideoShortcodePlugin
@@ -449,6 +450,7 @@ open class MainActivity : AppCompatActivity(),
449450
.addPlugin(HiddenGutenbergPlugin(visualEditor))
450451
.addPlugin(galleryButton)
451452
.addPlugin(cameraButton)
453+
.addPlugin(UrlPastePlugin())
452454

453455
// initialize the plugins, text & HTML
454456
if (!isRunningTest) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import org.wordpress.aztec.handlers.ListItemHandler
7474
import org.wordpress.aztec.handlers.PreformatHandler
7575
import org.wordpress.aztec.handlers.QuoteHandler
7676
import org.wordpress.aztec.plugins.IAztecPlugin
77+
import org.wordpress.aztec.plugins.ITextPastePlugin
7778
import org.wordpress.aztec.plugins.IToolbarButton
7879
import org.wordpress.aztec.source.Format
7980
import org.wordpress.aztec.source.SourceViewEditText
@@ -1845,11 +1846,13 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18451846
disableTextChangedListener()
18461847

18471848
val length = text.length
1849+
var selectedText: String? = null
18481850
if (min == 0 && max == 0 && length == 1 && text.toString() == Constants.END_OF_BUFFER_MARKER_STRING) {
18491851
editable.insert(min, Constants.REPLACEMENT_MARKER_STRING)
18501852
} else if (min == 0 && max == length) {
18511853
setText(Constants.REPLACEMENT_MARKER_STRING)
18521854
} else {
1855+
selectedText = editable.substring(min, max)
18531856
// prevent changes here from triggering the crash preventer
18541857
disableCrashPreventerInputFilter()
18551858
editable.delete(min, max)
@@ -1875,7 +1878,14 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18751878
else clip.getItemAt(0).coerceToHtmlText(AztecParser(alignmentRendering, plugins))
18761879

18771880
val oldHtml = toPlainHtml().replace("<aztec_cursor>", "")
1878-
val newHtml = oldHtml.replace(Constants.REPLACEMENT_MARKER_STRING, textToPaste + "<" + AztecCursorSpan.AZTEC_CURSOR_TAG + ">")
1881+
val pastedHtmlText = plugins.filterIsInstance<ITextPastePlugin>().fold(textToPaste) { acc, plugin ->
1882+
if (selectedText.isNullOrEmpty()) {
1883+
plugin.toHtml(acc)
1884+
} else {
1885+
plugin.toHtml(selectedText, acc)
1886+
}
1887+
}
1888+
val newHtml = oldHtml.replace(Constants.REPLACEMENT_MARKER_STRING, pastedHtmlText + "<" + AztecCursorSpan.AZTEC_CURSOR_TAG + ">")
18791889

18801890
fromHtml(newHtml, false)
18811891
inlineFormatter.joinStyleSpans(0, length())
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wordpress.aztec.plugins
2+
3+
/**
4+
* Use this plugin in order to override the default text paste behaviour. An example is overriding the paste so that
5+
* you can handle pasted URLs as links over the selected text.
6+
*/
7+
interface ITextPastePlugin: IAztecPlugin {
8+
/**
9+
* This method is called when the cursor is placed into the editor, no text is selected and the user pastes text
10+
* into the editor. This method should return HTML (plain text is OK if you don't apply any changes to the pasted
11+
* text).
12+
* @param pastedText pasted text
13+
* @return html of the result
14+
*/
15+
fun toHtml(pastedText: String): String {
16+
return pastedText
17+
}
18+
19+
/**
20+
* This method is called when some text is selected in the editor and the user pastes text. The default behaviour
21+
* is to replace the selected text with the pasted text but it can be changed in this method. This method should
22+
* return HTML (plain text is OK if you don't apply any changes to the pasted text).
23+
* @param selectedText currently selected text
24+
* @param pastedText text pasted over selected text
25+
* @return html of the result
26+
*/
27+
fun toHtml(selectedText: String, pastedText: String): String {
28+
return pastedText
29+
}
30+
}
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wordpress.aztec.plugins
2+
3+
import android.util.Patterns
4+
5+
/**
6+
* This plugin overrides the paste logic of URLs in the AztecText. The purpose is to make sure inserted links are
7+
* treated as HTML links.
8+
*/
9+
class UrlPastePlugin : ITextPastePlugin {
10+
/**
11+
* If the pasted text is a link, make sure it's wrapped with the `a` tag so that it's rendered as a link.
12+
*/
13+
override fun toHtml(pastedText: String): String {
14+
return if (Patterns.WEB_URL.matcher(pastedText).matches()) {
15+
"<a href=\"$pastedText\">$pastedText</a>"
16+
} else {
17+
pastedText
18+
}
19+
}
20+
/**
21+
* If the pasted text is a link, make sure the selected text is wrapped with `a` tag and not removed.
22+
*/
23+
override fun toHtml(selectedText: String, pastedText: String): String {
24+
return if (Patterns.WEB_URL.matcher(pastedText).matches()) {
25+
"<a href=\"$pastedText\">$selectedText</a>"
26+
} else {
27+
pastedText
28+
}
29+
}
30+
}
31+

0 commit comments

Comments
 (0)