11package org.wordpress.aztec.plugins.shortcodes.extensions
22
3+ import android.text.Layout
34import android.text.Spanned
45import org.wordpress.aztec.AztecAttributes
56import org.wordpress.aztec.AztecText
@@ -44,17 +45,20 @@ fun AztecText.removeImageCaption(attributePredicate: AztecText.AttributePredicat
4445
4546fun AztecText.hasImageCaption (attributePredicate : AztecText .AttributePredicate ): Boolean {
4647 this .text.getSpans(0 , this .text.length, AztecImageSpan ::class .java)
47- .firstOrNull {
48- val wrapper = SpanWrapper <AztecImageSpan >(text, it)
49- return text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java).isNotEmpty()
48+ .firstOrNull {
49+ attributePredicate.matches(it.attributes)
50+ }
51+ ?.let {
52+ val wrapper = SpanWrapper (text, it)
53+ return text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java).isNotEmpty()
5054 }
5155
5256 return false
5357}
5458
5559fun AztecImageSpan.getCaption (): String {
5660 textView?.text?.let {
57- val wrapper = SpanWrapper < AztecImageSpan > (textView!! .text, this )
61+ val wrapper = SpanWrapper (textView!! .text, this )
5862 textView!! .text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java).firstOrNull()?.let {
5963 return it.caption
6064 }
@@ -64,7 +68,7 @@ fun AztecImageSpan.getCaption(): String {
6468
6569fun AztecImageSpan.getCaptionAttributes (): AztecAttributes {
6670 textView?.text?.let {
67- val wrapper = SpanWrapper < AztecImageSpan > (textView!! .text, this )
71+ val wrapper = SpanWrapper (textView!! .text, this )
6872 textView!! .text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java).firstOrNull()?.let {
6973 return it.attributes
7074 }
@@ -75,7 +79,7 @@ fun AztecImageSpan.getCaptionAttributes(): AztecAttributes {
7579@JvmOverloads
7680fun AztecImageSpan.setCaption (value : String , attrs : AztecAttributes ? = null) {
7781 textView?.text?.let {
78- val wrapper = SpanWrapper < AztecImageSpan > (textView!! .text, this )
82+ val wrapper = SpanWrapper (textView!! .text, this )
7983
8084 var captionSpan = textView?.text?.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java)?.firstOrNull()
8185 if (captionSpan == null ) {
@@ -86,14 +90,24 @@ fun AztecImageSpan.setCaption(value: String, attrs: AztecAttributes? = null) {
8690 captionSpan.caption = value
8791
8892 attrs?.let {
89- captionSpan!! .attributes = attrs
93+ captionSpan.attributes = attrs
94+
95+ if (captionSpan.attributes.hasAttribute(CaptionShortcodePlugin .ALIGN_ATTRIBUTE )) {
96+ when (captionSpan.attributes.getValue(CaptionShortcodePlugin .ALIGN_ATTRIBUTE )) {
97+ CaptionShortcodePlugin .ALIGN_RIGHT_ATTRIBUTE_VALUE -> captionSpan.align = Layout .Alignment .ALIGN_OPPOSITE
98+ CaptionShortcodePlugin .ALIGN_CENTER_ATTRIBUTE_VALUE -> captionSpan.align = Layout .Alignment .ALIGN_CENTER
99+ CaptionShortcodePlugin .ALIGN_LEFT_ATTRIBUTE_VALUE -> captionSpan.align = Layout .Alignment .ALIGN_NORMAL
100+ }
101+ } else {
102+ captionSpan.align = null
103+ }
90104 }
91105 }
92106}
93107
94108fun AztecImageSpan.removeCaption () {
95109 textView?.text?.let {
96- val wrapper = SpanWrapper < AztecImageSpan > (textView!! .text, this )
110+ val wrapper = SpanWrapper (textView!! .text, this )
97111 textView!! .text.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan ::class .java).firstOrNull()?.remove()
98112 }
99113}
0 commit comments