Skip to content

Commit b1fad43

Browse files
committed
Allow paragraph spans that do not extend AlignmentSpan
Needed to allow paragraph spans that do not override the view's gravity due to their implementation of AlignmentSpan.
1 parent 167ffe6 commit b1fad43

File tree

17 files changed

+107
-74
lines changed

17 files changed

+107
-74
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import org.wordpress.aztec.spans.AztecMediaSpan
4444
import org.wordpress.aztec.spans.AztecURLSpan
4545
import org.wordpress.aztec.spans.AztecVisualLinebreak
4646
import org.wordpress.aztec.spans.CommentSpan
47+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
4748
import org.wordpress.aztec.spans.IAztecBlockSpan
4849
import org.wordpress.aztec.spans.IAztecFullWidthImageSpan
4950
import org.wordpress.aztec.spans.IAztecInlineSpan
@@ -415,7 +416,7 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
415416
private fun withinNestable(out: StringBuilder, text: Spanned, start: Int, end: Int,
416417
nestable: IAztecParagraphStyle, parents: ArrayList<IAztecNestable>?, nestingLevel: Int) {
417418

418-
if (nestable.shouldParseAlignmentToHtml()) {
419+
if (nestable is IAztecAlignmentSpan && nestable.shouldParseAlignmentToHtml()) {
419420
CssStyleFormatter.removeStyleAttribute(nestable.attributes, CssStyleFormatter.CSS_TEXT_ALIGN_ATTRIBUTE)
420421

421422
nestable.align?.let {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import org.wordpress.aztec.spans.HiddenHtmlBlock
4545
import org.wordpress.aztec.spans.HiddenHtmlSpan
4646
import org.wordpress.aztec.spans.IAztecAttributedSpan
4747
import org.wordpress.aztec.spans.IAztecNestable
48-
import org.wordpress.aztec.spans.ParagraphSpan
48+
import org.wordpress.aztec.spans.createParagraphSpan
4949
import org.wordpress.aztec.util.getLast
5050
import org.xml.sax.Attributes
5151
import java.util.ArrayList
@@ -118,7 +118,7 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
118118
return true
119119
}
120120
PARAGRAPH -> {
121-
handleElement(output, opening, ParagraphSpan(nestingLevel, AztecAttributes(attributes)))
121+
handleElement(output, opening, createParagraphSpan(nestingLevel, AztecAttributes(attributes)))
122122
return true
123123
}
124124
LINE -> {

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ import org.wordpress.aztec.spans.AztecOrderedListSpan
2020
import org.wordpress.aztec.spans.AztecPreformatSpan
2121
import org.wordpress.aztec.spans.AztecQuoteSpan
2222
import org.wordpress.aztec.spans.AztecUnorderedListSpan
23+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
2324
import org.wordpress.aztec.spans.IAztecBlockSpan
2425
import org.wordpress.aztec.spans.IAztecCompositeBlockSpan
2526
import org.wordpress.aztec.spans.IAztecLineBlockSpan
2627
import org.wordpress.aztec.spans.IAztecNestable
27-
import org.wordpress.aztec.spans.IAztecParagraphStyle
2828
import org.wordpress.aztec.spans.ParagraphSpan
29+
import org.wordpress.aztec.spans.createParagraphSpan
2930
import org.wordpress.aztec.util.SpanWrapper
3031
import java.util.Arrays
3132

@@ -279,21 +280,6 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
279280
}
280281
}
281282

282-
fun getOuterBlockSpanType(textFormat: ITextFormat): Class<out IAztecBlockSpan> {
283-
when (textFormat) {
284-
AztecTextFormat.FORMAT_ORDERED_LIST -> return AztecOrderedListSpan::class.java
285-
AztecTextFormat.FORMAT_UNORDERED_LIST -> return AztecUnorderedListSpan::class.java
286-
AztecTextFormat.FORMAT_QUOTE -> return AztecQuoteSpan::class.java
287-
AztecTextFormat.FORMAT_HEADING_1,
288-
AztecTextFormat.FORMAT_HEADING_2,
289-
AztecTextFormat.FORMAT_HEADING_3,
290-
AztecTextFormat.FORMAT_HEADING_4,
291-
AztecTextFormat.FORMAT_HEADING_5,
292-
AztecTextFormat.FORMAT_HEADING_6 -> return AztecHeadingSpan::class.java
293-
else -> return ParagraphSpan::class.java
294-
}
295-
}
296-
297283
// TODO: Come up with a better way to init spans and get their classes (all the "make" methods)
298284
fun makeBlock(textFormat: ITextFormat, nestingLevel: Int, attrs: AztecAttributes = AztecAttributes()): List<IAztecBlockSpan> {
299285
when (textFormat) {
@@ -307,7 +293,7 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
307293
AztecTextFormat.FORMAT_HEADING_5,
308294
AztecTextFormat.FORMAT_HEADING_6 -> return Arrays.asList(AztecHeadingSpan(nestingLevel, textFormat, attrs, headerStyle))
309295
AztecTextFormat.FORMAT_PREFORMAT -> return Arrays.asList(AztecPreformatSpan(nestingLevel, attrs, preformatStyle))
310-
else -> return Arrays.asList(ParagraphSpan(nestingLevel, attrs))
296+
else -> return Arrays.asList(createParagraphSpan(nestingLevel, attrs))
311297
}
312298
}
313299

@@ -335,7 +321,7 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
335321
AztecTextFormat.FORMAT_HEADING_5,
336322
AztecTextFormat.FORMAT_HEADING_6 -> makeBlockSpan(AztecHeadingSpan::class.java, textFormat, nestingLevel, attrs)
337323
AztecTextFormat.FORMAT_PREFORMAT -> makeBlockSpan(AztecPreformatSpan::class.java, textFormat, nestingLevel, attrs)
338-
else -> ParagraphSpan(nestingLevel, attrs)
324+
else -> createParagraphSpan(nestingLevel, attrs)
339325
}
340326
}
341327

@@ -347,7 +333,7 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
347333
AztecQuoteSpan::class.java -> AztecQuoteSpan(nestingLevel, attrs, quoteStyle)
348334
AztecHeadingSpan::class.java -> AztecHeadingSpan(nestingLevel, textFormat, attrs, headerStyle)
349335
AztecPreformatSpan::class.java -> AztecPreformatSpan(nestingLevel, attrs, preformatStyle)
350-
else -> ParagraphSpan(nestingLevel, attrs)
336+
else -> createParagraphSpan(nestingLevel, attrs)
351337
}
352338
}
353339

@@ -509,13 +495,13 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
509495

510496
val alignment = getAlignment(textFormat,
511497
editableText.subSequence(boundsOfSelectedText.start until boundsOfSelectedText.endInclusive))
512-
editableText.setSpan(ParagraphSpan(nestingLevel, AztecAttributes(), alignment),
498+
editableText.setSpan(createParagraphSpan(nestingLevel, AztecAttributes(), alignment),
513499
boundsOfSelectedText.start, boundsOfSelectedText.endInclusive, Spanned.SPAN_PARAGRAPH)
514500
}
515501
}
516502

517-
private fun changeAlignment(it: IAztecParagraphStyle, blockElementType: ITextFormat?) {
518-
val wrapper = SpanWrapper<IAztecParagraphStyle>(editableText, it)
503+
private fun changeAlignment(it: IAztecAlignmentSpan, blockElementType: ITextFormat?) {
504+
val wrapper = SpanWrapper(editableText, it)
519505
it.align = getAlignment(blockElementType, editableText.substring(wrapper.start until wrapper.end))
520506

521507
editableText.setSpan(it, wrapper.start, wrapper.end, wrapper.flags)
@@ -905,10 +891,10 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
905891
return getAlignedSpans(textFormat, selStart, selEnd).isNotEmpty()
906892
}
907893

908-
private fun getAlignedSpans(textFormat: ITextFormat?, selStart: Int = selectionStart, selEnd: Int = selectionEnd): List<IAztecParagraphStyle> {
894+
private fun getAlignedSpans(textFormat: ITextFormat?, selStart: Int = selectionStart, selEnd: Int = selectionEnd): List<IAztecAlignmentSpan> {
909895
if (selStart < 0 || selEnd < 0) return emptyList()
910896

911-
return editableText.getSpans(selStart, selEnd, IAztecParagraphStyle::class.java)
897+
return editableText.getSpans(selStart, selEnd, IAztecAlignmentSpan::class.java)
912898
.filter {
913899
textFormat == null || it.align == getAlignment(textFormat,
914900
editableText.substring(editableText.getSpanStart(it) until editableText.getSpanEnd(it)))

aztec/src/main/kotlin/org/wordpress/aztec/source/CssStyleFormatter.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.text.Layout
66
import android.text.Spannable
77
import android.text.style.ForegroundColorSpan
88
import org.wordpress.aztec.AztecAttributes
9+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
910
import org.wordpress.aztec.spans.IAztecAttributedSpan
1011
import org.wordpress.aztec.spans.IAztecParagraphStyle
1112
import org.wordpress.aztec.util.ColorConverter
@@ -47,18 +48,20 @@ class CssStyleFormatter {
4748
}
4849

4950
private fun processAlignment(blockSpan: IAztecParagraphStyle, text: Editable, start: Int, end: Int) {
50-
val alignment = getStyleAttribute(blockSpan.attributes, CSS_TEXT_ALIGN_ATTRIBUTE)
51-
if (!alignment.isBlank()) {
52-
val direction = TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR
53-
val isRtl = direction.isRtl(text, start, end - start)
54-
55-
val align = when (alignment) {
56-
"right" -> if (isRtl) Layout.Alignment.ALIGN_NORMAL else Layout.Alignment.ALIGN_OPPOSITE
57-
"center" -> Layout.Alignment.ALIGN_CENTER
58-
else -> if (!isRtl) Layout.Alignment.ALIGN_NORMAL else Layout.Alignment.ALIGN_OPPOSITE
51+
if (blockSpan is IAztecAlignmentSpan) {
52+
val alignment = getStyleAttribute(blockSpan.attributes, CSS_TEXT_ALIGN_ATTRIBUTE)
53+
if (!alignment.isBlank()) {
54+
val direction = TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR
55+
val isRtl = direction.isRtl(text, start, end - start)
56+
57+
val align = when (alignment) {
58+
"right" -> if (isRtl) Layout.Alignment.ALIGN_NORMAL else Layout.Alignment.ALIGN_OPPOSITE
59+
"center" -> Layout.Alignment.ALIGN_CENTER
60+
else -> if (!isRtl) Layout.Alignment.ALIGN_NORMAL else Layout.Alignment.ALIGN_OPPOSITE
61+
}
62+
63+
blockSpan.align = align
5964
}
60-
61-
blockSpan.align = align
6265
}
6366
}
6467

aztec/src/main/kotlin/org/wordpress/aztec/source/Format.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.jsoup.nodes.Document
88
import org.wordpress.aztec.spans.AztecQuoteSpan
99
import org.wordpress.aztec.spans.AztecVisualLinebreak
1010
import org.wordpress.aztec.spans.EndOfParagraphMarker
11+
import org.wordpress.aztec.spans.IAztecAlignmentSpan
1112
import org.wordpress.aztec.spans.IAztecParagraphStyle
1213
import org.wordpress.aztec.spans.ParagraphSpan
1314
import org.wordpress.aztec.util.CleaningUtils
@@ -356,8 +357,12 @@ object Format {
356357

357358
// we don't care about actual ParagraphSpan in calypso that don't have attributes or are empty (paragraphs are made from double newline)
358359
text.getSpans(0, text.length, ParagraphSpan::class.java)
359-
.filter { it.attributes.isEmpty() && it.align == null || text.getSpanStart(it) == text.getSpanEnd(it) - 1 }
360-
.forEach {
360+
.filter {
361+
val hasNoAttributes = it.attributes.isEmpty()
362+
val isAligned = it is IAztecAlignmentSpan && it.align != null
363+
val isEmpty = text.getSpanStart(it) == text.getSpanEnd(it) - 1
364+
(hasNoAttributes && !isAligned) || isEmpty
365+
}.forEach {
361366
text.removeSpan(it)
362367
}
363368
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AztecHeadingSpan @JvmOverloads constructor(
1818
override var attributes: AztecAttributes,
1919
var headerStyle: BlockFormatter.HeaderStyle = BlockFormatter.HeaderStyle(0),
2020
override var align: Layout.Alignment? = null
21-
) : MetricAffectingSpan(), IAztecLineBlockSpan, LineHeightSpan, UpdateLayout {
21+
) : MetricAffectingSpan(), IAztecAlignmentSpan, IAztecLineBlockSpan, LineHeightSpan, UpdateLayout {
2222
override val TAG: String
2323
get() = heading.tag
2424

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecListItemSpan.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import org.wordpress.aztec.AztecAttributes
55

66
class AztecListItemSpan(override var nestingLevel: Int,
77
override var attributes: AztecAttributes = AztecAttributes(),
8-
override var align: Layout.Alignment? = null) : IAztecCompositeBlockSpan {
8+
override var align: Layout.Alignment? = null
9+
) : IAztecAlignmentSpan, IAztecCompositeBlockSpan {
910
override val TAG = "li"
1011

1112
override var endBeforeBleed: Int = -1

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecListSpan.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import org.wordpress.aztec.Constants
1010

1111
abstract class AztecListSpan(override var nestingLevel: Int,
1212
var verticalPadding: Int = 0,
13-
override var align: Layout.Alignment? = null) : LeadingMarginSpan.Standard(0),
14-
LineHeightSpan, UpdateLayout, IAztecBlockSpan {
13+
override var align: Layout.Alignment? = null
14+
) : LeadingMarginSpan.Standard(0),
15+
LineHeightSpan,
16+
UpdateLayout,
17+
IAztecAlignmentSpan,
18+
IAztecBlockSpan {
1519
override var endBeforeBleed: Int = -1
1620
override var startBeforeCollapse: Int = -1
1721

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecPreformatSpan.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ class AztecPreformatSpan(
1818
override var attributes: AztecAttributes = AztecAttributes(),
1919
var preformatStyle: BlockFormatter.PreformatStyle = BlockFormatter.PreformatStyle(0, 0f, 0, 0),
2020
override var align: Layout.Alignment? = null
21-
) : IAztecBlockSpan, LeadingMarginSpan, LineBackgroundSpan, LineHeightSpan, TypefaceSpan("monospace") {
21+
) : IAztecAlignmentSpan,
22+
IAztecBlockSpan,
23+
LeadingMarginSpan,
24+
LineBackgroundSpan,
25+
LineHeightSpan,
26+
TypefaceSpan("monospace")
27+
{
2228
override val TAG: String = "pre"
2329

2430
override var endBeforeBleed: Int = -1

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecQuoteSpan.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ class AztecQuoteSpan(
4141
override var nestingLevel: Int,
4242
override var attributes: AztecAttributes = AztecAttributes(),
4343
var quoteStyle: BlockFormatter.QuoteStyle = BlockFormatter.QuoteStyle(0, 0, 0f, 0, 0, 0, 0),
44-
override var align: Layout.Alignment? = null)
45-
: QuoteSpan(), LineBackgroundSpan, IAztecBlockSpan, LineHeightSpan, UpdateLayout {
44+
override var align: Layout.Alignment? = null
45+
) : QuoteSpan(),
46+
LineBackgroundSpan,
47+
IAztecAlignmentSpan,
48+
IAztecBlockSpan,
49+
LineHeightSpan,
50+
UpdateLayout
51+
{
4652

4753
override var endBeforeBleed: Int = -1
4854
override var startBeforeCollapse: Int = -1

0 commit comments

Comments
 (0)