Skip to content

Commit

Permalink
Disable toggleFormat for alignment when AlignmentApproach is VIEW_LEVEL
Browse files Browse the repository at this point in the history
We have not implemented proper handling for using toggleFormat with
VIEW_LEVEL alignment, so instead of having it half-work (updating the
visual appearance of the text but not updating the html), just
disabling it entirely.
  • Loading branch information
mchowning committed Apr 23, 2020
1 parent cc8c9e8 commit 0b93da0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.text.Editable
import android.text.Layout
import android.text.Spanned
import android.text.TextUtils
import org.wordpress.android.util.AppLog
import org.wordpress.aztec.AlignmentApproach
import org.wordpress.aztec.AztecAttributes
import org.wordpress.aztec.AztecText
Expand Down Expand Up @@ -132,15 +133,24 @@ class BlockFormatter(editor: AztecText,
}

fun toggleTextAlignment(textFormat: ITextFormat) {
when (textFormat) {
AztecTextFormat.FORMAT_ALIGN_LEFT,
AztecTextFormat.FORMAT_ALIGN_CENTER,
AztecTextFormat.FORMAT_ALIGN_RIGHT ->
if (containsAlignment(textFormat)) {
removeTextAlignment(textFormat)
} else {
applyTextAlignment(textFormat)
when (alignmentApproach) {
AlignmentApproach.VIEW_LEVEL -> {
val message = "cannot toggle text alignment when ${AlignmentApproach.VIEW_LEVEL} is being used"
AppLog.d(AppLog.T.EDITOR, message)
}

AlignmentApproach.SPAN_LEVEL -> {
when (textFormat) {
AztecTextFormat.FORMAT_ALIGN_LEFT,
AztecTextFormat.FORMAT_ALIGN_CENTER,
AztecTextFormat.FORMAT_ALIGN_RIGHT ->
if (containsAlignment(textFormat)) {
removeTextAlignment(textFormat)
} else {
applyTextAlignment(textFormat)
}
}
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions aztec/src/test/kotlin/org/wordpress/aztec/BlockElementsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ class BlockElementsTest(val alignmentApproach: AlignmentApproach) {
Assert.assertEquals(expectedHtml, editText.toHtml())
}

@Test
fun testTogglingFormattingAlignment() {
val html = "<p>hi</p>"
editText.fromHtml(html)
editText.toggleFormatting(AztecTextFormat.FORMAT_ALIGN_CENTER)

val expected = when (editText.alignmentApproach) {
AlignmentApproach.SPAN_LEVEL -> "<p style=\"text-align:center;\">hi</p>"

// changing alignment with togglingFormatting is a no-op with VIEW_LEVEL AlignmentApproach
AlignmentApproach.VIEW_LEVEL -> html
}
Assert.assertEquals(expected, editText.toHtml())
}

@Test
fun alignmentApproachEffectOnLeftAlignment() {
assertNoChangeWithFromHtmlToHtmlRoundTrip("<p style=\"text-align:left;\">left</p>")
Expand Down

0 comments on commit 0b93da0

Please sign in to comment.