Skip to content

Commit 5d00583

Browse files
authored
Merge pull request #624 from wordpress-mobile/issue/465-text-alignment
Text alignment support
2 parents e8d359f + 3ee1b53 commit 5d00583

File tree

49 files changed

+1003
-131
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1003
-131
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## Unreleased
3+
### Added
4+
- Text alignment support
5+
26
## [1.1](https://github.com/wordpress-mobile/AztecEditor-Android/releases/tag/v1.1) - 2018-01-29
37
### Added
48
- Plugin that detects and hides Gutenberg editor blocks in the visual editor

app/src/androidTest/kotlin/org/wordpress/aztec/demo/tests/CssUnderlineFormattingTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CssUnderlineFormattingTests : BaseTest() {
2727
fun testSimpleCssUnderlineFormatting() {
2828
val text1 = "some"
2929
val text2 = "text"
30-
val html = "$text1<span style=\"text-decoration: underline\">$text2</span>"
30+
val html = "$text1<span style=\"text-decoration: underline;\">$text2</span>"
3131

3232
EditorPage()
3333
.insertText(text1)
@@ -56,7 +56,7 @@ class CssUnderlineFormattingTests : BaseTest() {
5656
val text1 = "some"
5757
val text2 = "text"
5858
val html = "$text1<u>$text2</u>"
59-
val expectedHtml = "$text1<span style=\"text-decoration: underline\">$text2</span>"
59+
val expectedHtml = "$text1<span style=\"text-decoration: underline;\">$text2</span>"
6060

6161
EditorPage()
6262
.toggleHtml()
@@ -78,8 +78,8 @@ class CssUnderlineFormattingTests : BaseTest() {
7878
val text1 = "some"
7979
val text2 = "text"
8080
val html = "$text1<u>$text2</u>"
81-
val expectedHtml = "$text1<span style=\"text-decoration: underline\">te</span>\n\n" +
82-
"<span style=\"text-decoration: underline\">xt</span>"
81+
val expectedHtml = "$text1<span style=\"text-decoration: underline;\">te</span>\n\n" +
82+
"<span style=\"text-decoration: underline;\">xt</span>"
8383

8484
EditorPage()
8585
.toggleHtml()

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.wordpress.aztec.spans.FontSpan;
5555
import org.wordpress.aztec.spans.IAztecBlockSpan;
5656
import org.wordpress.aztec.spans.IAztecInlineSpan;
57+
import org.wordpress.aztec.spans.IAztecParagraphStyle;
5758
import org.wordpress.aztec.spans.UnknownClickableSpan;
5859
import org.wordpress.aztec.spans.UnknownHtmlSpan;
5960
import org.xml.sax.Attributes;
@@ -254,7 +255,7 @@ public Spanned convert() {
254255
// Fix flags and range for paragraph-type markup.
255256
Object[] paragraphs = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), ParagraphStyle.class);
256257
for (Object paragraph : paragraphs) {
257-
if (paragraph instanceof UnknownHtmlSpan || paragraph instanceof IAztecBlockSpan || paragraph instanceof AztecMediaSpan) {
258+
if (paragraph instanceof UnknownHtmlSpan || paragraph instanceof IAztecParagraphStyle || paragraph instanceof AztecMediaSpan) {
258259
continue;
259260
}
260261
int start = spannableStringBuilder.getSpanStart(paragraph);

aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
package org.wordpress.aztec
2020

2121
import android.content.Context
22+
import android.support.v4.text.TextDirectionHeuristicsCompat
2223
import android.text.Editable
2324
import android.text.Spannable
2425
import android.text.SpannableStringBuilder
2526
import android.text.Spanned
2627
import android.text.TextUtils
2728
import android.text.style.CharacterStyle
2829
import android.text.style.ForegroundColorSpan
30+
import org.wordpress.aztec.extensions.toCssString
2931
import org.wordpress.aztec.plugins.IAztecPlugin
3032
import org.wordpress.aztec.plugins.html2visual.ISpanPostprocessor
3133
import org.wordpress.aztec.plugins.visual2html.IHtmlPostprocessor
3234
import org.wordpress.aztec.plugins.visual2html.IInlineSpanHandler
3335
import org.wordpress.aztec.plugins.visual2html.ISpanPreprocessor
36+
import org.wordpress.aztec.source.CssStyleFormatter
3437
import org.wordpress.aztec.spans.AztecCursorSpan
3538
import org.wordpress.aztec.spans.AztecHorizontalRuleSpan
3639
import org.wordpress.aztec.spans.AztecListItemSpan
@@ -39,12 +42,11 @@ import org.wordpress.aztec.spans.AztecMediaSpan
3942
import org.wordpress.aztec.spans.AztecURLSpan
4043
import org.wordpress.aztec.spans.AztecVisualLinebreak
4144
import org.wordpress.aztec.spans.CommentSpan
42-
import org.wordpress.aztec.spans.HiddenHtmlSpan
4345
import org.wordpress.aztec.spans.IAztecBlockSpan
4446
import org.wordpress.aztec.spans.IAztecFullWidthImageSpan
4547
import org.wordpress.aztec.spans.IAztecInlineSpan
4648
import org.wordpress.aztec.spans.IAztecNestable
47-
import org.wordpress.aztec.spans.IAztecSpan
49+
import org.wordpress.aztec.spans.IAztecParagraphStyle
4850
import org.wordpress.aztec.spans.IAztecSurroundedWithNewlines
4951
import org.wordpress.aztec.spans.UnknownHtmlSpan
5052
import org.wordpress.aztec.util.SpanWrapper
@@ -182,7 +184,7 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
182184
if (spanned[spanEnd] == '\n'
183185
&& spanned.getSpans(spanEnd, spanEnd, AztecVisualLinebreak::class.java).isNotEmpty()) {
184186
// but still, expand the span to include the newline for block spans, because they are paragraphs
185-
if (it is IAztecBlockSpan) {
187+
if (it is IAztecParagraphStyle) {
186188
spanned.setSpan(it, spanned.getSpanStart(it), spanEnd + 1, spanned.getSpanFlags(it))
187189
}
188190

@@ -193,7 +195,7 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
193195
spanned.insert(spanEnd, "\n")
194196

195197
// expand the span to include the new newline for block spans, because they are paragraphs
196-
if (it is IAztecBlockSpan) {
198+
if (it is IAztecParagraphStyle) {
197199
spanned.setSpan(it, spanned.getSpanStart(it), spanEnd + 1, spanned.getSpanFlags(it))
198200
}
199201

@@ -349,8 +351,7 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
349351
}
350352

351353
when (nestable) {
352-
is HiddenHtmlSpan -> withinNestable(out, text, i, next, nestable, parents, nestable.nestingLevel)
353-
is IAztecBlockSpan -> withinNestable(out, text, i, next, nestable, parents, nestable.nestingLevel)
354+
is IAztecParagraphStyle -> withinNestable(out, text, i, next, nestable, parents, nestable.nestingLevel)
354355
is UnknownHtmlSpan -> withinUnknown(out, text, i, next, nestable)
355356
else -> withinContent(out, text, i, next, parents)
356357
}
@@ -368,7 +369,20 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
368369
}
369370

370371
private fun withinNestable(out: StringBuilder, text: Spanned, start: Int, end: Int,
371-
nestable: IAztecSpan, parents: ArrayList<IAztecNestable>?, nestingLevel: Int) {
372+
nestable: IAztecParagraphStyle, parents: ArrayList<IAztecNestable>?, nestingLevel: Int) {
373+
374+
if (nestable.shouldParseAlignmentToHtml()) {
375+
CssStyleFormatter.removeStyleAttribute(nestable.attributes, CssStyleFormatter.CSS_TEXT_ALIGN_ATTRIBUTE)
376+
377+
nestable.align?.let {
378+
val direction = TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR
379+
val isRtl = direction.isRtl(text, start, end - start)
380+
381+
CssStyleFormatter.addStyleAttribute(nestable.attributes,
382+
CssStyleFormatter.CSS_TEXT_ALIGN_ATTRIBUTE, nestable.align!!.toCssString(isRtl))
383+
}
384+
}
385+
372386
out.append("<${nestable.startTag}>")
373387
withinHtml(out, text, start, end, parents, nestingLevel)
374388
out.append("</${nestable.endTag}>")

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,9 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
807807
AztecTextFormat.FORMAT_CODE -> inlineFormatter.toggle(textFormat)
808808
AztecTextFormat.FORMAT_UNORDERED_LIST -> blockFormatter.toggleUnorderedList()
809809
AztecTextFormat.FORMAT_ORDERED_LIST -> blockFormatter.toggleOrderedList()
810+
AztecTextFormat.FORMAT_ALIGN_LEFT,
811+
AztecTextFormat.FORMAT_ALIGN_CENTER,
812+
AztecTextFormat.FORMAT_ALIGN_RIGHT -> return blockFormatter.toggleTextAlignment(textFormat)
810813
AztecTextFormat.FORMAT_QUOTE -> blockFormatter.toggleQuote()
811814
AztecTextFormat.FORMAT_HORIZONTAL_RULE -> lineBlockFormatter.applyHorizontalRule()
812815
else -> {
@@ -832,6 +835,9 @@ class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlT
832835
AztecTextFormat.FORMAT_CODE -> return inlineFormatter.containsInlineStyle(format, selStart, selEnd)
833836
AztecTextFormat.FORMAT_UNORDERED_LIST,
834837
AztecTextFormat.FORMAT_ORDERED_LIST -> return blockFormatter.containsList(format, selStart, selEnd)
838+
AztecTextFormat.FORMAT_ALIGN_LEFT,
839+
AztecTextFormat.FORMAT_ALIGN_CENTER,
840+
AztecTextFormat.FORMAT_ALIGN_RIGHT -> return blockFormatter.containsAlignment(format, selStart, selEnd)
835841
AztecTextFormat.FORMAT_QUOTE -> return blockFormatter.containsQuote(selectionStart, selectionEnd)
836842
AztecTextFormat.FORMAT_PREFORMAT -> return blockFormatter.containsPreformat(selectionStart, selectionEnd)
837843
AztecTextFormat.FORMAT_LINK -> return linkFormatter.containLink(selStart, selEnd)

aztec/src/main/kotlin/org/wordpress/aztec/AztecTextFormat.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ enum class AztecTextFormat : ITextFormat {
1717
FORMAT_ITALIC,
1818
FORMAT_UNDERLINE,
1919
FORMAT_STRIKETHROUGH,
20+
FORMAT_ALIGN_LEFT,
21+
FORMAT_ALIGN_CENTER,
22+
FORMAT_ALIGN_RIGHT,
2023
FORMAT_QUOTE,
2124
FORMAT_LINK,
2225
FORMAT_HORIZONTAL_RULE,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.wordpress.aztec.extensions
2+
3+
import android.text.Layout
4+
5+
fun Layout.Alignment.toCssString(isRtl: Boolean): String {
6+
return when (this) {
7+
Layout.Alignment.ALIGN_NORMAL -> if (!isRtl) "left" else "right"
8+
Layout.Alignment.ALIGN_CENTER -> "center"
9+
else -> if (isRtl) "left" else "right"
10+
}
11+
}

0 commit comments

Comments
 (0)