Skip to content

Commit d570aca

Browse files
authored
Merge pull request #775 from wordpress-mobile/issue/774-caption-alignment
Issue/774 caption alignment
2 parents 6526c45 + 496872d commit d570aca

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## [v1.3.15](https://github.com/wordpress-mobile/AztecEditor-Android/releases/tag/v1.3.15)
3+
### Changed
4+
- Synced the caption span alignment attribute with its the align property
5+
26
## [1.3.14](https://github.com/wordpress-mobile/AztecEditor-Android/releases/tag/v1.3.14) - 2019-01-11
37
### Fixed
48
- Fixed issue where pasting over the whole text emitted a delete char, causing Gutenberg to remove whole block

wordpress-shortcodes/src/main/java/org/wordpress/aztec/plugins/shortcodes/extensions/CaptionExtensions.kt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.wordpress.aztec.plugins.shortcodes.extensions
22

3+
import android.text.Layout
34
import android.text.Spanned
45
import org.wordpress.aztec.AztecAttributes
56
import org.wordpress.aztec.AztecText
@@ -44,17 +45,20 @@ fun AztecText.removeImageCaption(attributePredicate: AztecText.AttributePredicat
4445

4546
fun 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

5559
fun 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

6569
fun 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
7680
fun 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

94108
fun 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

Comments
 (0)