Skip to content

Commit b996947

Browse files
committed
Decouple alignment from gutenbergMode and initialize early
1 parent 667a448 commit b996947

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.wordpress.aztec
2+
3+
enum class AlignmentApproach { SPAN_LEVEL, VIEW_LEVEL }

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
152152

153153
val DEFAULT_IMAGE_WIDTH = 800
154154

155+
val DEFAULT_ALIGNMENT_APPROACH = AlignmentApproach.SPAN_LEVEL
156+
155157
var watchersNestingLevel: Int = 0
156158

157159
private fun getPlaceholderDrawableFromResID(context: Context, @DrawableRes drawableId: Int, maxImageWidthForVisualEditor: Int): BitmapDrawable {
@@ -248,12 +250,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
248250
var commentsVisible = resources.getBoolean(R.bool.comments_visible)
249251

250252
var isInCalypsoMode = true
251-
var alignmentApproach: AlignmentApproach = AlignmentApproach.SPAN_LEVEL
252-
var isInGutenbergMode = false
253-
set(value) {
254-
field = value
255-
alignmentApproach = if (value) AlignmentApproach.VIEW_LEVEL else AlignmentApproach.SPAN_LEVEL
256-
}
253+
var isInGutenbergMode: Boolean = false
254+
val alignmentApproach: AlignmentApproach
257255

258256
var consumeHistoryEvent: Boolean = false
259257

@@ -339,14 +337,22 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
339337
}
340338

341339
constructor(context: Context) : super(context) {
340+
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
341+
init(null)
342+
}
343+
344+
constructor(context: Context, alignmentApproach: AlignmentApproach) : super(context) {
345+
this.alignmentApproach = alignmentApproach
342346
init(null)
343347
}
344348

345349
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
350+
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
346351
init(attrs)
347352
}
348353

349354
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
355+
alignmentApproach = DEFAULT_ALIGNMENT_APPROACH
350356
init(attrs)
351357
}
352358

@@ -422,7 +428,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
422428
getPreformatBackgroundAlpha(styles),
423429
styles.getColor(R.styleable.AztecText_preformatColor, 0),
424430
verticalParagraphMargin),
425-
{ alignmentApproach }
431+
alignmentApproach
426432
)
427433

428434
linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(styles.getColor(
@@ -596,8 +602,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
596602
// NB: text change handler should not alter text before "afterTextChanged" is called otherwise not all watchers
597603
// will have the chance to run their "beforeTextChanged" and "onTextChanged" with the same string!
598604

599-
BlockElementWatcher(this, { alignmentApproach })
600-
.add(HeadingHandler { alignmentApproach })
605+
BlockElementWatcher(this)
606+
.add(HeadingHandler(alignmentApproach))
601607
.add(ListHandler())
602608
.add(ListItemHandler())
603609
.add(QuoteHandler())
@@ -1928,5 +1934,3 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19281934
}
19291935

19301936
}
1931-
1932-
enum class AlignmentApproach { SPAN_LEVEL, VIEW_LEVEL }

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BlockFormatter(editor: AztecText,
3939
private val quoteStyle: QuoteStyle,
4040
private val headerStyle: HeaderStyle,
4141
private val preformatStyle: PreformatStyle,
42-
private val getAlignmentApproach: () -> AlignmentApproach
42+
private val alignmentApproach: AlignmentApproach
4343
) : AztecFormatter(editor) {
4444
data class ListStyle(val indicatorColor: Int, val indicatorMargin: Int, val indicatorPadding: Int, val indicatorWidth: Int, val verticalPadding: Int)
4545
data class QuoteStyle(val quoteBackground: Int, val quoteColor: Int, val quoteBackgroundAlpha: Float, val quoteMargin: Int, val quotePadding: Int, val quoteWidth: Int, val verticalPadding: Int)
@@ -301,9 +301,9 @@ class BlockFormatter(editor: AztecText,
301301
AztecTextFormat.FORMAT_HEADING_3,
302302
AztecTextFormat.FORMAT_HEADING_4,
303303
AztecTextFormat.FORMAT_HEADING_5,
304-
AztecTextFormat.FORMAT_HEADING_6 -> return Arrays.asList(createHeadingSpan(nestingLevel, textFormat, attrs, getAlignmentApproach(), headerStyle))
305-
AztecTextFormat.FORMAT_PREFORMAT -> return Arrays.asList(createPreformatSpan(nestingLevel, getAlignmentApproach(), attrs, preformatStyle))
306-
else -> return Arrays.asList(createParagraphSpan(nestingLevel, getAlignmentApproach(), attrs))
304+
AztecTextFormat.FORMAT_HEADING_6 -> return Arrays.asList(createHeadingSpan(nestingLevel, textFormat, attrs, alignmentApproach, headerStyle))
305+
AztecTextFormat.FORMAT_PREFORMAT -> return Arrays.asList(createPreformatSpan(nestingLevel, alignmentApproach, attrs, preformatStyle))
306+
else -> return Arrays.asList(createParagraphSpan(nestingLevel, alignmentApproach, attrs))
307307
}
308308
}
309309

@@ -331,7 +331,7 @@ class BlockFormatter(editor: AztecText,
331331
AztecTextFormat.FORMAT_HEADING_5,
332332
AztecTextFormat.FORMAT_HEADING_6 -> makeBlockSpan(AztecHeadingSpan::class, textFormat, nestingLevel, attrs)
333333
AztecTextFormat.FORMAT_PREFORMAT -> makeBlockSpan(AztecPreformatSpan::class, textFormat, nestingLevel, attrs)
334-
else -> createParagraphSpan(nestingLevel, getAlignmentApproach(), attrs)
334+
else -> createParagraphSpan(nestingLevel, alignmentApproach, attrs)
335335
}
336336
}
337337

@@ -342,9 +342,9 @@ class BlockFormatter(editor: AztecText,
342342
typeIsAssignableTo(AztecUnorderedListSpan::class) -> AztecUnorderedListSpan(nestingLevel, attrs, listStyle)
343343
typeIsAssignableTo(AztecListItemSpan::class) -> AztecListItemSpan(nestingLevel, attrs)
344344
typeIsAssignableTo(AztecQuoteSpan::class) -> AztecQuoteSpan(nestingLevel, attrs, quoteStyle)
345-
typeIsAssignableTo(AztecHeadingSpan::class) -> createHeadingSpan(nestingLevel, textFormat, attrs, getAlignmentApproach(), headerStyle)
346-
typeIsAssignableTo(AztecPreformatSpan::class) -> createPreformatSpan(nestingLevel, getAlignmentApproach(), attrs, preformatStyle)
347-
else -> createParagraphSpan(nestingLevel, getAlignmentApproach(), attrs)
345+
typeIsAssignableTo(AztecHeadingSpan::class) -> createHeadingSpan(nestingLevel, textFormat, attrs, alignmentApproach, headerStyle)
346+
typeIsAssignableTo(AztecPreformatSpan::class) -> createPreformatSpan(nestingLevel, alignmentApproach, attrs, preformatStyle)
347+
else -> createParagraphSpan(nestingLevel, alignmentApproach, attrs)
348348
}
349349
}
350350

@@ -698,7 +698,7 @@ class BlockFormatter(editor: AztecText,
698698
val lineLength = lineEnd - lineStart
699699
if (lineLength == 0) continue
700700

701-
HeadingHandler.cloneHeading(editableText, headingSpan, getAlignmentApproach(), lineStart, lineEnd)
701+
HeadingHandler.cloneHeading(editableText, headingSpan, alignmentApproach, lineStart, lineEnd)
702702
}
703703
}
704704

@@ -1053,7 +1053,7 @@ class BlockFormatter(editor: AztecText,
10531053
preformat.nestingLevel,
10541054
headingTextFormat,
10551055
preformat.attributes,
1056-
getAlignmentApproach())
1056+
alignmentApproach)
10571057
editableText.setSpan(headingSpan, spanStart, spanEnd, spanFlags)
10581058
editor.onSelectionChanged(start, end)
10591059
}

aztec/src/main/kotlin/org/wordpress/aztec/handlers/HeadingHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.wordpress.aztec.spans.AztecHeadingSpan
77
import org.wordpress.aztec.spans.createHeadingSpan
88
import org.wordpress.aztec.watchers.TextDeleter
99

10-
class HeadingHandler(private val getAlignmentApproach: () -> AlignmentApproach) : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.java) {
10+
class HeadingHandler(private val alignmentApproach: AlignmentApproach) : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.java) {
1111
override fun handleNewlineAtStartOfBlock() {
1212
// we got a newline at the start of the block. Let's just push the block after the newline
1313
block.start = newlineIndex + 1
@@ -46,7 +46,7 @@ class HeadingHandler(private val getAlignmentApproach: () -> AlignmentApproach)
4646
// not add a new block after it
4747
} else {
4848
// newline added at some position inside the block. Let's split the block into two
49-
cloneHeading(text, block.span, getAlignmentApproach(), newlineIndex + 1, block.end)
49+
cloneHeading(text, block.span, alignmentApproach, newlineIndex + 1, block.end)
5050
}
5151

5252
block.end = newlineIndex + 1

aztec/src/main/kotlin/org/wordpress/aztec/watchers/BlockElementWatcher.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import org.wordpress.aztec.util.SpanWrapper
1515
import java.lang.ref.WeakReference
1616
import java.util.ArrayList
1717

18-
open class BlockElementWatcher(aztecText: AztecText, val getAlignmentApproach: () -> AlignmentApproach) : TextWatcher {
18+
open class BlockElementWatcher(aztecText: AztecText) : TextWatcher {
1919
val handlers = ArrayList<TextChangeHandler>()
2020

2121
interface TextChangeHandler {
2222
fun handleTextChanged(text: Spannable, inputStart: Int, count: Int, nestingLevel: Int, isReplay: Boolean)
2323
}
2424

2525
private val aztecTextRef: WeakReference<AztecText?> = WeakReference(aztecText)
26+
private val alignmentApproach: AlignmentApproach = aztecText.alignmentApproach
2627

2728
override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
2829
if (count > 0) {
@@ -46,7 +47,7 @@ open class BlockElementWatcher(aztecText: AztecText, val getAlignmentApproach: (
4647
spanWrapper.span.nestingLevel,
4748
spanWrapper.span.TAG,
4849
spanWrapper.span.attributes,
49-
getAlignmentApproach(),
50+
alignmentApproach,
5051
spanWrapper.span.headerStyle)
5152
spannable.setSpan(headingSpan, deleteEnd - 1, deleteEnd, spanWrapper.flags)
5253
}

wordpress-comments/src/main/java/org/wordpress/aztec/plugins/wpcomments/HiddenGutenbergPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HiddenGutenbergPlugin @JvmOverloads constructor(private val aztecText: Azt
2222

2323
init {
2424
aztecText?.let {
25-
BlockElementWatcher(aztecText, { aztecText.alignmentApproach })
25+
BlockElementWatcher(aztecText)
2626
.add(GutenbergCommentHandler())
2727
.install(aztecText)
2828
}

wordpress-shortcodes/src/main/java/org/wordpress/aztec/plugins/shortcodes/watchers/CaptionWatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.wordpress.aztec.plugins.shortcodes.spans.CaptionShortcodeSpan
77
import org.wordpress.aztec.util.SpanWrapper
88
import org.wordpress.aztec.watchers.BlockElementWatcher
99

10-
class CaptionWatcher(private val aztecText: AztecText) : BlockElementWatcher(aztecText, { aztecText.alignmentApproach }) {
10+
class CaptionWatcher(private val aztecText: AztecText) : BlockElementWatcher(aztecText) {
1111

1212
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
1313
super.onTextChanged(s, start, before, count)

0 commit comments

Comments
 (0)