Skip to content

Commit d2d8a5d

Browse files
committed
Use AlignmentApproach with CaptionShortcodeSpan
1 parent d697b0b commit d2d8a5d

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import org.wordpress.aztec.plugins.html2visual.IHtmlPreprocessor
1111
import org.wordpress.aztec.plugins.html2visual.IHtmlTagHandler
1212
import org.wordpress.aztec.plugins.shortcodes.handlers.CaptionHandler
1313
import org.wordpress.aztec.plugins.shortcodes.spans.CaptionShortcodeSpan
14+
import org.wordpress.aztec.plugins.shortcodes.spans.createCaptionShortcodeSpan
1415
import org.wordpress.aztec.plugins.shortcodes.watchers.CaptionWatcher
1516
import org.wordpress.aztec.plugins.visual2html.IHtmlPostprocessor
1617
import org.wordpress.aztec.plugins.visual2html.ISpanPreprocessor
1718
import org.wordpress.aztec.source.CssStyleFormatter
19+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
1820
import org.wordpress.aztec.util.SpanWrapper
1921
import org.wordpress.aztec.util.getLast
2022
import org.xml.sax.Attributes
@@ -45,7 +47,12 @@ class CaptionShortcodePlugin @JvmOverloads constructor(private val aztecText: Az
4547

4648
override fun handleTag(opening: Boolean, tag: String, output: Editable, attributes: Attributes, nestingLevel: Int): Boolean {
4749
if (opening) {
48-
output.setSpan(CaptionShortcodeSpan(AztecAttributes(attributes), HTML_TAG, nestingLevel, aztecText), output.length, output.length, Spannable.SPAN_MARK_MARK)
50+
val captionShortcodeSpan = createCaptionShortcodeSpan(
51+
AztecAttributes(attributes),
52+
HTML_TAG,
53+
nestingLevel,
54+
aztecText)
55+
output.setSpan(captionShortcodeSpan, output.length, output.length, Spannable.SPAN_MARK_MARK)
4956
} else {
5057
val span = output.getLast<CaptionShortcodeSpan>()
5158
span?.let {
@@ -68,7 +75,7 @@ class CaptionShortcodePlugin @JvmOverloads constructor(private val aztecText: Az
6875
}
6976
output.setSpan(span, wrapper.start, output.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
7077

71-
if (span.attributes.hasAttribute(ALIGN_ATTRIBUTE)) {
78+
if (span is IAztecAlignmentSpan && span.attributes.hasAttribute(ALIGN_ATTRIBUTE)) {
7279
when (span.attributes.getValue(ALIGN_ATTRIBUTE)) {
7380
ALIGN_RIGHT_ATTRIBUTE_VALUE -> span.align = Layout.Alignment.ALIGN_OPPOSITE
7481
ALIGN_CENTER_ATTRIBUTE_VALUE -> span.align = Layout.Alignment.ALIGN_CENTER
@@ -89,7 +96,7 @@ class CaptionShortcodePlugin @JvmOverloads constructor(private val aztecText: Az
8996
override fun beforeSpansProcessed(spannable: SpannableStringBuilder) {
9097
spannable.getSpans(0, spannable.length, CaptionShortcodeSpan::class.java).forEach {
9198
it.attributes.removeAttribute(CaptionShortcodePlugin.ALIGN_ATTRIBUTE)
92-
if (it.align != null) {
99+
if (it is IAztecAlignmentSpan && it.align != null) {
93100
it.attributes.setValue(CaptionShortcodePlugin.ALIGN_ATTRIBUTE,
94101
when (it.align) {
95102
Layout.Alignment.ALIGN_NORMAL -> CaptionShortcodePlugin.ALIGN_LEFT_ATTRIBUTE_VALUE

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import org.wordpress.aztec.AztecAttributes
66
import org.wordpress.aztec.AztecText
77
import org.wordpress.aztec.plugins.shortcodes.CaptionShortcodePlugin
88
import org.wordpress.aztec.plugins.shortcodes.spans.CaptionShortcodeSpan
9+
import org.wordpress.aztec.plugins.shortcodes.spans.createCaptionShortcodeSpan
910
import org.wordpress.aztec.spans.AztecImageSpan
11+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
1012
import org.wordpress.aztec.spans.IAztecNestable
1113
import org.wordpress.aztec.util.SpanWrapper
1214

@@ -83,23 +85,29 @@ fun AztecImageSpan.setCaption(value: String, attrs: AztecAttributes? = null) {
8385

8486
var captionSpan = textView?.text?.getSpans(wrapper.start, wrapper.end, CaptionShortcodeSpan::class.java)?.firstOrNull()
8587
if (captionSpan == null) {
86-
captionSpan = CaptionShortcodeSpan(AztecAttributes(), CaptionShortcodePlugin.HTML_TAG, IAztecNestable.getNestingLevelAt(textView!!.text, wrapper.start), textView!!)
88+
captionSpan = createCaptionShortcodeSpan(
89+
AztecAttributes(),
90+
CaptionShortcodePlugin.HTML_TAG,
91+
IAztecNestable.getNestingLevelAt(textView!!.text, wrapper.start),
92+
textView!!)
8793
textView!!.text.setSpan(captionSpan, wrapper.start, wrapper.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
8894
}
8995

9096
captionSpan.caption = value
9197

92-
attrs?.let {
93-
captionSpan.attributes = attrs
98+
if (captionSpan is IAztecAlignmentSpan) {
99+
attrs?.let {
100+
captionSpan.attributes = attrs
94101

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
102+
if (captionSpan.attributes.hasAttribute(CaptionShortcodePlugin.ALIGN_ATTRIBUTE)) {
103+
when (captionSpan.attributes.getValue(CaptionShortcodePlugin.ALIGN_ATTRIBUTE)) {
104+
CaptionShortcodePlugin.ALIGN_RIGHT_ATTRIBUTE_VALUE -> captionSpan.align = Layout.Alignment.ALIGN_OPPOSITE
105+
CaptionShortcodePlugin.ALIGN_CENTER_ATTRIBUTE_VALUE -> captionSpan.align = Layout.Alignment.ALIGN_CENTER
106+
CaptionShortcodePlugin.ALIGN_LEFT_ATTRIBUTE_VALUE -> captionSpan.align = Layout.Alignment.ALIGN_NORMAL
107+
}
108+
} else {
109+
captionSpan.align = null
100110
}
101-
} else {
102-
captionSpan.align = null
103111
}
104112
}
105113
}

wordpress-shortcodes/src/main/java/org/wordpress/aztec/plugins/shortcodes/spans/CaptionShortcodeSpan.kt

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,48 @@ import android.graphics.Typeface
44
import android.text.Layout
55
import android.text.Spanned
66
import android.text.style.StyleSpan
7+
import org.wordpress.aztec.AlignmentApproach
78
import org.wordpress.aztec.AztecAttributes
89
import org.wordpress.aztec.AztecText
910
import org.wordpress.aztec.Constants
1011
import org.wordpress.aztec.spans.IAztecAlignmentSpan
1112
import org.wordpress.aztec.spans.IAztecBlockSpan
1213
import org.wordpress.aztec.util.SpanWrapper
1314

14-
class CaptionShortcodeSpan @JvmOverloads constructor(override var attributes: AztecAttributes,
15-
override val TAG: String,
16-
override var nestingLevel: Int,
17-
private val aztecText: AztecText? = null,
18-
override var align: Layout.Alignment? = null)
19-
: StyleSpan(Typeface.ITALIC), IAztecAlignmentSpan, IAztecBlockSpan {
15+
fun createCaptionShortcodeSpan(
16+
attributes: AztecAttributes,
17+
TAG: String,
18+
nestingLevel: Int,
19+
aztecText: AztecText? = null
20+
) = when (aztecText?.alignmentApproach ?: AztecText.DEFAULT_ALIGNMENT_APPROACH) {
21+
AlignmentApproach.SPAN_LEVEL -> CaptionShortcodeSpanAligned(attributes, TAG, nestingLevel, aztecText, null)
22+
AlignmentApproach.VIEW_LEVEL -> CaptionShortcodeSpan(attributes, TAG, nestingLevel, aztecText)
23+
}
24+
25+
/**
26+
* We need to have two classes for handling alignment at either the Span-level (CaptionShortcodeSpanAligned)
27+
* or the View-level (CaptionShortcodeSpan). IAztecAlignment implements AlignmentSpan, which has a
28+
* getAlignment method that returns a non-null Layout.Alignment. The Android system checks for
29+
* AlignmentSpans and, if present, overrides the view's gravity with their value. Having a class
30+
* that does not implement AlignmentSpan allows the view's gravity to control. These classes should
31+
* be created using the createCaptionShortcodeSpan(...) methods.
32+
*/
33+
class CaptionShortcodeSpanAligned(
34+
attributes: AztecAttributes,
35+
TAG: String,
36+
nestingLevel: Int,
37+
aztecText: AztecText? = null,
38+
override var align: Layout.Alignment? = null
39+
) : CaptionShortcodeSpan(attributes, TAG, nestingLevel, aztecText), IAztecAlignmentSpan {
40+
override fun shouldParseAlignmentToHtml() = false
41+
}
42+
43+
open class CaptionShortcodeSpan(
44+
override var attributes: AztecAttributes,
45+
override val TAG: String,
46+
override var nestingLevel: Int,
47+
private val aztecText: AztecText?
48+
) : StyleSpan(Typeface.ITALIC), IAztecBlockSpan {
2049

2150
override var endBeforeBleed: Int = -1
2251
override var startBeforeCollapse: Int = -1
@@ -86,8 +115,4 @@ class CaptionShortcodeSpan @JvmOverloads constructor(override var attributes: Az
86115
}
87116
return end
88117
}
89-
90-
override fun shouldParseAlignmentToHtml(): Boolean {
91-
return false
92-
}
93118
}

0 commit comments

Comments
 (0)