Skip to content

Commit cde2466

Browse files
authored
Merge pull request #227 from wordpress-mobile/issue/225-allow-images-in-block-elements
Issue/225 allow images in block elements
2 parents 9401b6e + a2c2435 commit cde2466

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,31 @@ class LineBlockFormatter(editor: AztecText, val headerStyle: LineBlockFormatter.
270270
}
271271

272272
fun insertMedia(drawable: Drawable, source: String) {
273-
val mediaStartIndex = selectionStart
274-
val mediaEndIndex = selectionStart + source.length
275273

276-
editor.disableTextChangedListener()
274+
val isAddingMediaToEndOfBlockElement = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java)
275+
.any {
276+
selectionStart == editableText.getSpanEnd(it)
277+
}
278+
279+
val spanAtStartOfWhichMediaIsAdded = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java)
280+
.firstOrNull {
281+
selectionStart == editableText.getSpanStart(it)
282+
}
283+
284+
val modifier = if (isAddingMediaToEndOfBlockElement) 1 else 0
285+
286+
val mediaStartIndex = selectionStart - modifier
287+
val mediaEndIndex = selectionStart + source.length - modifier
288+
289+
if (!isAddingMediaToEndOfBlockElement)
290+
editor.disableTextChangedListener()
291+
277292
editableText.replace(selectionStart, selectionEnd, source)
278293

279-
editor.removeBlockStylesFromRange(mediaStartIndex, mediaEndIndex, true)
280-
editor.removeHeadingStylesFromRange(mediaStartIndex, mediaEndIndex)
294+
if (spanAtStartOfWhichMediaIsAdded != null) {
295+
editableText.setSpan(spanAtStartOfWhichMediaIsAdded, mediaStartIndex, editableText.getSpanEnd(spanAtStartOfWhichMediaIsAdded), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
296+
}
297+
281298
editor.removeInlineStylesFromRange(mediaStartIndex, mediaEndIndex)
282299

283300
val span = AztecMediaSpan(editor.context, drawable, source)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract class AztecListSpan(val verticalPadding: Int) : LeadingMarginSpan.Stand
5151
val currentLineNumber = getIndexOfProcessedLine(text, end)
5252

5353
if (totalNumberOfLinesInList > 1 && currentLineNumber == 1) {
54-
adjustment = if (this is AztecOrderedListSpan) 0 else verticalPadding
54+
adjustment = 0
5555
} else if ((totalNumberOfLinesInList > 1 && totalNumberOfLinesInList == currentLineNumber) || totalNumberOfLinesInList == 1) {
5656
adjustment = -verticalPadding
5757
}

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package org.wordpress.aztec.spans
1919

2020
import android.graphics.Canvas
2121
import android.graphics.Paint
22-
import android.graphics.Path
2322
import android.text.Layout
2423
import android.text.Spanned
2524
import android.text.TextUtils
@@ -64,7 +63,6 @@ class AztecUnorderedListSpan : AztecListSpan {
6463
}
6564

6665

67-
6866
override fun getLeadingMargin(first: Boolean): Int {
6967
return bulletMargin + 2 * bulletWidth + bulletPadding
7068
}
@@ -86,24 +84,14 @@ class AztecUnorderedListSpan : AztecListSpan {
8684
p.color = bulletColor
8785
p.style = Paint.Style.FILL
8886

89-
if (c.isHardwareAccelerated) {
90-
bulletPath = Path()
91-
bulletPath!!.addCircle(0.0f, 0.0f + getIndicatorAdjustment(text, end) / 2, bulletWidth.toFloat(), Path.Direction.CW)
87+
val textToDraw = "\u2022"
9288

93-
c.save()
94-
c.translate((x + bulletMargin + dir * bulletWidth).toFloat(), ((top + bottom) / 2.0f))
95-
c.drawPath(bulletPath!!, p)
96-
c.restore()
97-
} else {
98-
c.drawCircle((x + bulletMargin + dir * bulletWidth).toFloat(), ((top + bottom) / 2.0f) + getIndicatorAdjustment(text, end) / 2, bulletWidth.toFloat(), p)
99-
}
89+
val width = p.measureText(textToDraw)
90+
c.drawText(textToDraw, (bulletMargin + x + dir - width) * dir, (bottom - p.descent()) - width / 2 + getIndicatorAdjustment(text, end), p)
10091

10192
p.color = oldColor
10293
p.style = style
10394

10495
}
10596

106-
companion object {
107-
private var bulletPath: Path? = null
108-
}
10997
}

0 commit comments

Comments
 (0)