Skip to content

Commit 22ea632

Browse files
authored
Merge pull request #242 from wordpress-mobile/issue/fix-visual-glicthes-with-block-elements
Issue/fix visual glicthes with block elements
2 parents 7a3ac1c + 7a9aaec commit 22ea632

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,22 @@ class BlockFormatter(editor: AztecText, listStyle: ListStyle, quoteStyle: QuoteS
123123
spanEnd,
124124
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
125125
} else if (textChangedEvent.isAddingCharacters) {
126+
127+
val spanEndsWithNewline = editableText.indexOf('\n', textChangedEvent.inputStart, true) == spanEnd - textChangedEvent.numberOfAddedCharacters
128+
129+
//if span we are inserting text into is ending with newline we don't need to extend it
130+
if (spanEndsWithNewline) {
131+
return@forEach
132+
}
133+
126134
val indexOfLineEnd = text.indexOf('\n', spanEnd - 1, true)
127135

128136
if (indexOfLineEnd == spanEnd) {
129137
spanEnd += textChangedEvent.count
130138
} else if (indexOfLineEnd == -1) {
131139
spanEnd = text.length
132140
} else {
133-
spanEnd = indexOfLineEnd
141+
spanEnd = indexOfLineEnd + if (textChangedEvent.isAfterZeroWidthJoiner()) 1 else 0
134142
}
135143

136144
if (spanEnd <= textLength) {
@@ -224,6 +232,9 @@ class BlockFormatter(editor: AztecText, listStyle: ListStyle, quoteStyle: QuoteS
224232
}
225233
}
226234
} else if (!textChangedEvent.isAddingCharacters && !textChangedEvent.isNewLineButNotAtTheBeginning()) {
235+
val deletedCharacterIsNewline = textChangedEvent.textBefore[textChangedEvent.inputEnd] == '\n'
236+
if (!deletedCharacterIsNewline) return
237+
227238
// backspace on a line right after a list attaches the line to the last item
228239
val blockSpan = editableText.getSpans(inputEnd, inputEnd, AztecBlockSpan::class.java).firstOrNull()
229240
val before = Math.min(inputStart, inputEnd)

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,4 @@ abstract class AztecListSpan(val verticalPadding: Int) : LeadingMarginSpan.Stand
3737
return lineIndex + 1
3838
}
3939

40-
fun getTotalNumberOfLines(text: CharSequence): Int {
41-
val spanStart = (text as Spanned).getSpanStart(this)
42-
val spanEnd = text.getSpanEnd(this)
43-
44-
return text.substring(spanStart..spanEnd - 2).split("\n").count()
45-
}
46-
47-
fun getIndicatorAdjustment(text: CharSequence, end: Int): Int {
48-
var adjustment = 0
49-
50-
val totalNumberOfLinesInList = getTotalNumberOfLines(text)
51-
val currentLineNumber = getIndexOfProcessedLine(text, end)
52-
53-
if (totalNumberOfLinesInList > 1 && currentLineNumber == 1) {
54-
adjustment = 0
55-
} else if ((totalNumberOfLinesInList > 1 && totalNumberOfLinesInList == currentLineNumber) || totalNumberOfLinesInList == 1) {
56-
adjustment = -verticalPadding
57-
}
58-
59-
return adjustment
60-
}
61-
6240
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class AztecOrderedListSpan : AztecListSpan {
8686
val textToDraw = getIndexOfProcessedLine(text, end).toString() + "."
8787

8888
val width = p.measureText(textToDraw)
89-
c.drawText(textToDraw, (textMargin + x + dir - width) * dir, (bottom - p.descent()) + getIndicatorAdjustment(text, end), p)
89+
c.drawText(textToDraw, (textMargin + x + dir - width) * dir, baseline.toFloat(), p)
9090

9191
p.color = oldColor
9292
p.style = style

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AztecUnorderedListSpan : AztecListSpan {
8787
val textToDraw = "\u2022"
8888

8989
val width = p.measureText(textToDraw)
90-
c.drawText(textToDraw, (bulletMargin + x + dir - width) * dir, (bottom - p.descent()) - width / 2 + getIndicatorAdjustment(text, end), p)
90+
c.drawText(textToDraw, (bulletMargin + x + dir - width) * dir, (baseline + (width - p.descent())), p)
9191

9292
p.color = oldColor
9393
p.style = style

0 commit comments

Comments
 (0)