Skip to content

Commit 65cbb5f

Browse files
authored
fix(android): align mixed nested list indents with iOS (#3)
1 parent 3824d11 commit 65cbb5f

14 files changed

Lines changed: 537 additions & 13 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
appId: swmansion.enriched.markdown.example
2+
tags:
3+
- block
4+
- advanced
5+
- list
6+
- task_list
7+
- text
8+
- android-only
9+
---
10+
- launchApp:
11+
permissions: {}
12+
13+
- waitForAnimationToEnd
14+
15+
- runFlow:
16+
file: '../../../../subflows/move_to_playground.yaml'
17+
18+
- assertVisible:
19+
id: playground-screen
20+
21+
- runFlow:
22+
file: '../../../subflows/set_enriched_text_value.yaml'
23+
env:
24+
VALUE: |
25+
1. Ordered parent
26+
- Bullet child
27+
- Second bullet child
28+
2. Following ordered sibling
29+
30+
- Bullet parent
31+
1. Ordered child
32+
2. Second ordered child
33+
- Following bullet sibling
34+
35+
- scrollUntilVisible:
36+
element:
37+
id: blur-button
38+
direction: UP
39+
visibilityPercentage: 100
40+
41+
- tapOn:
42+
id: blur-button
43+
44+
- scroll
45+
46+
- waitForAnimationToEnd
47+
48+
- runFlow:
49+
file: '../../../subflows/capture_or_assert_screenshot.yaml'
50+
env:
51+
SCREENSHOT_NAME: 'mixed_nested_list_markers_bullet_ordered_display'
52+
53+
- scrollUntilVisible:
54+
element:
55+
id: set-markdown-button
56+
direction: UP
57+
visibilityPercentage: 100
58+
59+
- runFlow:
60+
file: '../../../subflows/set_enriched_text_value.yaml'
61+
env:
62+
VALUE: |
63+
- Bullet parent
64+
- [ ] Unchecked task child
65+
- [x] Checked task child
66+
67+
1. Ordered parent
68+
- [ ] Unchecked task child
69+
- [x] Checked task child
70+
71+
- [ ] Unchecked task parent
72+
- Bullet child
73+
- [x] Checked task parent
74+
- Second bullet child
75+
76+
- scrollUntilVisible:
77+
element:
78+
id: blur-button
79+
direction: UP
80+
visibilityPercentage: 100
81+
82+
- tapOn:
83+
id: blur-button
84+
85+
- scroll
86+
87+
- waitForAnimationToEnd
88+
89+
- runFlow:
90+
file: '../../../subflows/capture_or_assert_screenshot.yaml'
91+
env:
92+
SCREENSHOT_NAME: 'mixed_nested_list_markers_tasks_display'

‎.maestro/enrichedMarkdownText/flows/basic/block_elements/mixed_nested_list_test.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ tags:
2525
1. Django
2626
2. FastAPI
2727
28+
- runFlow:
29+
when:
30+
platform: Android
31+
commands:
32+
- scroll
33+
- waitForAnimationToEnd
34+
2835
- runFlow:
2936
file: '../../../subflows/capture_or_assert_screenshot.yaml'
3037
env:
116 Bytes
Loading
346 Bytes
Loading
48.3 KB
Loading
63.6 KB
Loading

‎packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/BlockStyleContext.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BlockStyleContext {
4040
var blockquoteDepth = 0
4141
var listDepth = 0
4242
var listType: ListType? = null
43+
var listItemMarkerWidth: Float? = null
4344
var listItemNumber = 0
4445
var taskItemCount = 0
4546
var accumulatedIndent = 0
@@ -151,6 +152,7 @@ class BlockStyleContext {
151152
listDepth = 0
152153
listType = null
153154
listItemNumber = 0
155+
listItemMarkerWidth = null
154156
taskItemCount = 0
155157
accumulatedIndent = 0
156158
listItemStarts.clear()

‎packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/ListItemRenderer.kt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ListItemRenderer(
3838

3939
val depth = styleContext.listDepth - 1
4040
val listStyle = config.style.listStyle
41+
val parentMarkerWidth = styleContext.listItemMarkerWidth
4142

4243
fun makeSpan(drawsMarker: Boolean): BaseListSpan =
4344
if (isTask) {
@@ -50,15 +51,16 @@ class ListItemRenderer(
5051
taskIndex = taskIndex,
5152
isChecked = isChecked,
5253
drawsMarker = drawsMarker,
54+
parentMarkerWidth = parentMarkerWidth,
5355
)
5456
} else {
5557
when (listType) {
5658
BlockStyleContext.ListType.UNORDERED -> {
57-
UnorderedListSpan(listStyle, depth, factory.context, factory.styleCache, drawsMarker)
59+
UnorderedListSpan(listStyle, depth, factory.context, factory.styleCache, drawsMarker, parentMarkerWidth)
5860
}
5961

6062
BlockStyleContext.ListType.ORDERED -> {
61-
OrderedListSpan(listStyle, depth, factory.context, factory.styleCache, drawsMarker).apply {
63+
OrderedListSpan(listStyle, depth, factory.context, factory.styleCache, drawsMarker, parentMarkerWidth).apply {
6264
setItemNumber(styleContext.listItemNumber)
6365
}
6466
}
@@ -70,9 +72,11 @@ class ListItemRenderer(
7072
val prevIndent = styleContext.accumulatedIndent
7173
styleContext.accumulatedIndent = prevIndent + markerSpan.getLeadingMargin(false)
7274
try {
75+
styleContext.listItemMarkerWidth = markerSpan.getBaselineMarkerWidth()
7376
factory.renderChildren(node, builder, onLinkPress, onLinkLongPress)
7477
} finally {
7578
styleContext.accumulatedIndent = prevIndent
79+
styleContext.listItemMarkerWidth = parentMarkerWidth
7680
}
7781

7882
if (builder.length == start || builder.substring(start).isBlank()) return

‎packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/spans/BaseListSpan.kt‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class BaseListSpan(
2323
protected val marginLeft: Float,
2424
protected val gapWidth: Float,
2525
internal val drawsMarker: Boolean = true,
26+
internal val parentMarkerWidth: Float? = null,
2627
) : MetricAffectingSpan(),
2728
LeadingMarginSpan {
2829
private var cachedText: CharSequence? = null
@@ -38,17 +39,26 @@ abstract class BaseListSpan(
3839

3940
protected abstract fun getMarkerWidth(): Float
4041

42+
/** The marker column a child measures its offset against. */
43+
internal open fun getBaselineMarkerWidth(): Float = getMarkerWidth()
44+
4145
override fun updateMeasureState(textPaint: TextPaint) = applyTextStyle(textPaint)
4246

4347
override fun updateDrawState(textPaint: TextPaint) = applyTextStyle(textPaint)
4448

45-
override fun getLeadingMargin(first: Boolean): Int =
46-
if (depth == 0) {
47-
val effectiveGap = gapWidth.coerceAtLeast(DEFAULT_MIN_GAP)
48-
(getMarkerWidth() + effectiveGap).toInt()
49-
} else {
50-
marginLeft.toInt()
51-
}
49+
override fun getLeadingMargin(first: Boolean): Int {
50+
val effectiveGap = gapWidth.coerceAtLeast(DEFAULT_MIN_GAP)
51+
val markerWidth = getMarkerWidth()
52+
if (depth == 0) return (markerWidth + effectiveGap).toInt()
53+
54+
val parentWidth = parentMarkerWidth ?: getBaselineMarkerWidth()
55+
if (parentWidth == markerWidth) return marginLeft.toInt()
56+
57+
// Subtract truncated absolute indents so mixed columns do not accumulate their own rounding.
58+
val indent = (depth * marginLeft + markerWidth + effectiveGap).toInt()
59+
val parentIndent = ((depth - 1) * marginLeft + parentWidth + effectiveGap).toInt()
60+
return indent - parentIndent
61+
}
5262

5363
override fun drawLeadingMargin(
5464
canvas: Canvas,

‎packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/spans/OrderedListSpan.kt‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class OrderedListSpan(
1919
context: Context,
2020
styleCache: SpanStyleCache,
2121
drawsMarker: Boolean = true,
22+
parentMarkerWidth: Float? = null,
2223
) : BaseListSpan(
2324
depth = depth,
2425
context = context,
@@ -33,6 +34,7 @@ class OrderedListSpan(
3334
marginLeft = listStyle.marginLeft,
3435
gapWidth = listStyle.gapWidth,
3536
drawsMarker = drawsMarker,
37+
parentMarkerWidth = parentMarkerWidth,
3638
) {
3739
companion object {
3840
private val sharedMarkerPaint = TextPaint().apply { isAntiAlias = true }
@@ -45,17 +47,16 @@ class OrderedListSpan(
4547
applyStyles(null, ReactConstants.UNSET, fontWeight, fontFamily, context.assets)
4648
}
4749

50+
private val markerColumnWidth = listStyle.effectiveMarkerWidth(configureMarkerPaint().measureText("99."))
51+
4852
private fun configureMarkerPaint(): TextPaint =
4953
sharedMarkerPaint.apply {
5054
textSize = listStyle.fontSize
5155
color = listStyle.markerColor
5256
typeface = markerTypeface
5357
}
5458

55-
override fun getMarkerWidth(): Float {
56-
val paint = configureMarkerPaint()
57-
return listStyle.effectiveMarkerWidth(paint.measureText("99."))
58-
}
59+
override fun getMarkerWidth(): Float = markerColumnWidth
5960

6061
var itemNumber: Int = 1
6162
private set

0 commit comments

Comments
 (0)