Skip to content

Commit bff8cac

Browse files
committed
Disable toggleFormat for alignment when AlignmentApproach is VIEW_LEVEL
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.
1 parent 3875c09 commit bff8cac

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.text.Editable
55
import android.text.Layout
66
import android.text.Spanned
77
import android.text.TextUtils
8+
import org.wordpress.android.util.AppLog
89
import org.wordpress.aztec.AlignmentApproach
910
import org.wordpress.aztec.AztecAttributes
1011
import org.wordpress.aztec.AztecText
@@ -132,15 +133,24 @@ class BlockFormatter(editor: AztecText,
132133
}
133134

134135
fun toggleTextAlignment(textFormat: ITextFormat) {
135-
when (textFormat) {
136-
AztecTextFormat.FORMAT_ALIGN_LEFT,
137-
AztecTextFormat.FORMAT_ALIGN_CENTER,
138-
AztecTextFormat.FORMAT_ALIGN_RIGHT ->
139-
if (containsAlignment(textFormat)) {
140-
removeTextAlignment(textFormat)
141-
} else {
142-
applyTextAlignment(textFormat)
136+
when (alignmentApproach) {
137+
AlignmentApproach.VIEW_LEVEL -> {
138+
val message = "cannot toggle text alignment when ${AlignmentApproach.VIEW_LEVEL} is being used"
139+
AppLog.d(AppLog.T.EDITOR, message)
140+
}
141+
142+
AlignmentApproach.SPAN_LEVEL -> {
143+
when (textFormat) {
144+
AztecTextFormat.FORMAT_ALIGN_LEFT,
145+
AztecTextFormat.FORMAT_ALIGN_CENTER,
146+
AztecTextFormat.FORMAT_ALIGN_RIGHT ->
147+
if (containsAlignment(textFormat)) {
148+
removeTextAlignment(textFormat)
149+
} else {
150+
applyTextAlignment(textFormat)
151+
}
143152
}
153+
}
144154
}
145155
}
146156

aztec/src/test/kotlin/org/wordpress/aztec/BlockElementsTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ class BlockElementsTest(val alignmentApproach: AlignmentApproach) {
344344
Assert.assertEquals(expectedHtml, editText.toHtml())
345345
}
346346

347+
@Test
348+
fun testTogglingFormattingAlignment() {
349+
val html = "<p>hi</p>"
350+
editText.fromHtml(html)
351+
editText.toggleFormatting(AztecTextFormat.FORMAT_ALIGN_CENTER)
352+
353+
val expected = when (editText.alignmentApproach) {
354+
AlignmentApproach.SPAN_LEVEL -> "<p style=\"text-align:center;\">hi</p>"
355+
356+
// changing alignment with togglingFormatting is a no-op with VIEW_LEVEL AlignmentApproach
357+
AlignmentApproach.VIEW_LEVEL -> html
358+
}
359+
Assert.assertEquals(expected, editText.toHtml())
360+
}
361+
347362
@Test
348363
fun alignmentApproachEffectOnLeftAlignment() {
349364
assertNoChangeWithFromHtmlToHtmlRoundTrip("<p style=\"text-align:left;\">left</p>")

0 commit comments

Comments
 (0)