Skip to content

Commit

Permalink
Merge pull request #227 from wordpress-mobile/issue/225-allow-images-…
Browse files Browse the repository at this point in the history
…in-block-elements

Issue/225 allow images in block elements
  • Loading branch information
0nko authored Feb 7, 2017
2 parents 9401b6e + a2c2435 commit cde2466
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,31 @@ class LineBlockFormatter(editor: AztecText, val headerStyle: LineBlockFormatter.
}

fun insertMedia(drawable: Drawable, source: String) {
val mediaStartIndex = selectionStart
val mediaEndIndex = selectionStart + source.length

editor.disableTextChangedListener()
val isAddingMediaToEndOfBlockElement = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java)
.any {
selectionStart == editableText.getSpanEnd(it)
}

val spanAtStartOfWhichMediaIsAdded = editableText.getSpans(selectionStart, selectionEnd, AztecBlockSpan::class.java)
.firstOrNull {
selectionStart == editableText.getSpanStart(it)
}

val modifier = if (isAddingMediaToEndOfBlockElement) 1 else 0

val mediaStartIndex = selectionStart - modifier
val mediaEndIndex = selectionStart + source.length - modifier

if (!isAddingMediaToEndOfBlockElement)
editor.disableTextChangedListener()

editableText.replace(selectionStart, selectionEnd, source)

editor.removeBlockStylesFromRange(mediaStartIndex, mediaEndIndex, true)
editor.removeHeadingStylesFromRange(mediaStartIndex, mediaEndIndex)
if (spanAtStartOfWhichMediaIsAdded != null) {
editableText.setSpan(spanAtStartOfWhichMediaIsAdded, mediaStartIndex, editableText.getSpanEnd(spanAtStartOfWhichMediaIsAdded), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}

editor.removeInlineStylesFromRange(mediaStartIndex, mediaEndIndex)

val span = AztecMediaSpan(editor.context, drawable, source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AztecListSpan(val verticalPadding: Int) : LeadingMarginSpan.Stand
val currentLineNumber = getIndexOfProcessedLine(text, end)

if (totalNumberOfLinesInList > 1 && currentLineNumber == 1) {
adjustment = if (this is AztecOrderedListSpan) 0 else verticalPadding
adjustment = 0
} else if ((totalNumberOfLinesInList > 1 && totalNumberOfLinesInList == currentLineNumber) || totalNumberOfLinesInList == 1) {
adjustment = -verticalPadding
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package org.wordpress.aztec.spans

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.text.Layout
import android.text.Spanned
import android.text.TextUtils
Expand Down Expand Up @@ -64,7 +63,6 @@ class AztecUnorderedListSpan : AztecListSpan {
}



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

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

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

p.color = oldColor
p.style = style

}

companion object {
private var bulletPath: Path? = null
}
}

0 comments on commit cde2466

Please sign in to comment.