@@ -4,19 +4,48 @@ import android.graphics.Typeface
44import android.text.Layout
55import android.text.Spanned
66import android.text.style.StyleSpan
7+ import org.wordpress.aztec.AlignmentApproach
78import org.wordpress.aztec.AztecAttributes
89import org.wordpress.aztec.AztecText
910import org.wordpress.aztec.Constants
1011import org.wordpress.aztec.spans.IAztecAlignmentSpan
1112import org.wordpress.aztec.spans.IAztecBlockSpan
1213import 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