Skip to content

Commit

Permalink
Update AlignmentApproach to AlignmentRendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mchowning committed Apr 24, 2020
1 parent bff8cac commit ea627b6
Show file tree
Hide file tree
Showing 26 changed files with 155 additions and 152 deletions.

This file was deleted.

12 changes: 12 additions & 0 deletions aztec/src/main/kotlin/org/wordpress/aztec/AlignmentRendering.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.wordpress.aztec

/**
* With [SPAN_LEVEL] any alignment must be specified at the span level. Importantly, this
* means that the View's gravity will always be ignored in determining the rendering of
* the text's alignment.
*
* With [VIEW_LEVEL] alignment, the rendering of alignment is determined by the View's gravity.
* Note that it is not possible to update the underlying alignment using [AztecText.toggleFormatting]
* when you are using [VIEW_LEVEL] alignment rendering.
*/
enum class AlignmentRendering { SPAN_LEVEL, VIEW_LEVEL }
6 changes: 3 additions & 3 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import java.util.ArrayList
import java.util.Collections
import java.util.Comparator

class AztecParser @JvmOverloads constructor(private val alignmentApproach: AlignmentApproach,
class AztecParser @JvmOverloads constructor(private val alignmentRendering: AlignmentRendering,
val plugins: List<IAztecPlugin> = listOf(),
private val ignoredTags: List<String> = listOf("body", "html")) {
/**
Expand All @@ -69,7 +69,7 @@ class AztecParser @JvmOverloads constructor(private val alignmentApproach: Align
val tidySource = tidy(source)

val spanned = SpannableString(Html.fromHtml(tidySource,
AztecTagHandler(context, plugins, alignmentApproach), context, plugins, ignoredTags, true))
AztecTagHandler(context, plugins, alignmentRendering), context, plugins, ignoredTags, true))

postprocessSpans(spanned)

Expand All @@ -83,7 +83,7 @@ class AztecParser @JvmOverloads constructor(private val alignmentApproach: Align
val tidySource = if (shouldSkipTidying) source else tidy(source)

val spanned = SpannableStringBuilder(Html.fromHtml(tidySource,
AztecTagHandler(context, plugins, alignmentApproach), context, plugins, ignoredTags, shouldIgnoreWhitespace))
AztecTagHandler(context, plugins, alignmentRendering), context, plugins, ignoredTags, shouldIgnoreWhitespace))

addVisualNewlinesToBlockElements(spanned)
markBlockElementsAsParagraphs(spanned)
Expand Down
20 changes: 10 additions & 10 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import org.wordpress.aztec.util.getLast
import org.xml.sax.Attributes
import java.util.ArrayList

class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = ArrayList(), private val alignmentApproach: AlignmentApproach
class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = ArrayList(), private val alignmentRendering: AlignmentRendering
) : Html.TagHandler {
private val loadingDrawable: Drawable

Expand All @@ -74,7 +74,7 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar

when (tag.toLowerCase()) {
LIST_LI -> {
val span = createListItemSpan(nestingLevel, alignmentApproach, AztecAttributes(attributes))
val span = createListItemSpan(nestingLevel, alignmentRendering, AztecAttributes(attributes))
handleElement(output, opening, span)
return true
}
Expand All @@ -83,25 +83,25 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
return true
}
SPAN -> {
val span = createHiddenHtmlSpan(tag, AztecAttributes(attributes), nestingLevel, alignmentApproach)
val span = createHiddenHtmlSpan(tag, AztecAttributes(attributes), nestingLevel, alignmentRendering)
handleElement(output, opening, span)
return true
}
DIV, FIGURE, FIGCAPTION, SECTION -> {
val hiddenHtmlBlockSpan = createHiddenHtmlBlockSpan(tag, alignmentApproach, nestingLevel, AztecAttributes(attributes))
val hiddenHtmlBlockSpan = createHiddenHtmlBlockSpan(tag, alignmentRendering, nestingLevel, AztecAttributes(attributes))
handleElement(output, opening, hiddenHtmlBlockSpan)
return true
}
LIST_UL -> {
handleElement(output, opening, createUnorderedListSpan(nestingLevel, alignmentApproach, AztecAttributes(attributes)))
handleElement(output, opening, createUnorderedListSpan(nestingLevel, alignmentRendering, AztecAttributes(attributes)))
return true
}
LIST_OL -> {
handleElement(output, opening, createOrderedListSpan(nestingLevel, alignmentApproach, AztecAttributes(attributes)))
handleElement(output, opening, createOrderedListSpan(nestingLevel, alignmentRendering, AztecAttributes(attributes)))
return true
}
BLOCKQUOTE -> {
val span = createAztecQuoteSpan(nestingLevel, AztecAttributes(attributes), alignmentApproach)
val span = createAztecQuoteSpan(nestingLevel, AztecAttributes(attributes), alignmentRendering)
handleElement(output, opening, span)
return true
}
Expand All @@ -124,7 +124,7 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
return true
}
PARAGRAPH -> {
val paragraphSpan = createParagraphSpan(nestingLevel, alignmentApproach, AztecAttributes(attributes))
val paragraphSpan = createParagraphSpan(nestingLevel, alignmentRendering, AztecAttributes(attributes))
handleElement(output, opening, paragraphSpan)
return true
}
Expand All @@ -140,13 +140,13 @@ class AztecTagHandler(val context: Context, val plugins: List<IAztecPlugin> = Ar
return true
}
PREFORMAT -> {
val preformatSpan = createPreformatSpan(nestingLevel, alignmentApproach, AztecAttributes(attributes))
val preformatSpan = createPreformatSpan(nestingLevel, alignmentRendering, AztecAttributes(attributes))
handleElement(output, opening, preformatSpan)
return true
}
else -> {
if (tag.length == 2 && Character.toLowerCase(tag[0]) == 'h' && tag[1] >= '1' && tag[1] <= '6') {
handleElement(output, opening, createHeadingSpan(nestingLevel, tag, AztecAttributes(attributes), alignmentApproach))
handleElement(output, opening, createHeadingSpan(nestingLevel, tag, AztecAttributes(attributes), alignmentRendering))
return true
}
}
Expand Down
30 changes: 15 additions & 15 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

val DEFAULT_IMAGE_WIDTH = 800

val DEFAULT_ALIGNMENT_APPROACH = AlignmentApproach.SPAN_LEVEL
val DEFAULT_ALIGNMENT_RENDERING = AlignmentRendering.SPAN_LEVEL

var watchersNestingLevel: Int = 0

Expand Down Expand Up @@ -251,7 +251,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

var isInCalypsoMode = true
var isInGutenbergMode: Boolean = false
val alignmentApproach: AlignmentApproach
val alignmentRendering: AlignmentRendering

var consumeHistoryEvent: Boolean = false

Expand Down Expand Up @@ -337,22 +337,22 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

constructor(context: Context) : super(context) {
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
alignmentRendering = DEFAULT_ALIGNMENT_RENDERING
init(null)
}

constructor(context: Context, alignmentApproach: AlignmentApproach) : super(context) {
this.alignmentApproach = alignmentApproach
constructor(context: Context, alignmentRendering: AlignmentRendering) : super(context) {
this.alignmentRendering = alignmentRendering
init(null)
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
alignmentRendering = DEFAULT_ALIGNMENT_RENDERING
init(attrs)
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
alignmentRendering = DEFAULT_ALIGNMENT_RENDERING
init(attrs)
}

Expand Down Expand Up @@ -428,7 +428,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
getPreformatBackgroundAlpha(styles),
styles.getColor(R.styleable.AztecText_preformatColor, 0),
verticalParagraphMargin),
alignmentApproach
alignmentRendering
)

linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor(
Expand Down Expand Up @@ -603,9 +603,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
// will have the chance to run their "beforeTextChanged" and "onTextChanged" with the same string!

BlockElementWatcher(this)
.add(HeadingHandler(alignmentApproach))
.add(HeadingHandler(alignmentRendering))
.add(ListHandler())
.add(ListItemHandler(alignmentApproach))
.add(ListItemHandler(alignmentRendering))
.add(QuoteHandler())
.add(PreformatHandler())
.install(this)
Expand Down Expand Up @@ -1180,7 +1180,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

open fun fromHtml(source: String, isInit: Boolean = true) {
val builder = SpannableStringBuilder()
val parser = AztecParser(alignmentApproach, plugins)
val parser = AztecParser(alignmentRendering, plugins)

var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
Expand Down Expand Up @@ -1325,7 +1325,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

private fun parseHtml(content: Spannable, withCursorTag: Boolean): String {
val parser = AztecParser(alignmentApproach, plugins)
val parser = AztecParser(alignmentRendering, plugins)
val output: SpannableStringBuilder
try {
output = SpannableStringBuilder(content)
Expand Down Expand Up @@ -1571,7 +1571,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
// Convert selected text to html and add it to clipboard
fun copy(editable: Editable, start: Int, end: Int) {
val selectedText = editable.subSequence(start, end)
val parser = AztecParser(alignmentApproach, plugins)
val parser = AztecParser(alignmentRendering, plugins)
val output = SpannableStringBuilder(selectedText)

clearMetaSpans(output)
Expand Down Expand Up @@ -1637,7 +1637,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

if (clip.itemCount > 0) {
val textToPaste = if (asPlainText) clip.getItemAt(0).coerceToText(context).toString()
else clip.getItemAt(0).coerceToHtmlText(AztecParser(alignmentApproach, plugins))
else clip.getItemAt(0).coerceToHtmlText(AztecParser(alignmentRendering, plugins))

val oldHtml = toPlainHtml().replace("<aztec_cursor>", "")
val newHtml = oldHtml.replace(Constants.REPLACEMENT_MARKER_STRING, textToPaste + "<" + AztecCursorSpan.AZTEC_CURSOR_TAG + ">")
Expand Down Expand Up @@ -1755,7 +1755,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
val spanStart = text.getSpanStart(unknownHtmlSpan)

val textBuilder = SpannableStringBuilder()
textBuilder.append(AztecParser(alignmentApproach, plugins).fromHtml(source.getPureHtml(), context).trim())
textBuilder.append(AztecParser(alignmentRendering, plugins).fromHtml(source.getPureHtml(), context).trim())
setSelection(spanStart)

disableTextChangedListener()
Expand Down
Loading

0 comments on commit ea627b6

Please sign in to comment.