Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
appId: swmansion.enriched.markdown.example
tags:
- block
- advanced
- list
- text
---
- launchApp:
permissions: {}

- waitForAnimationToEnd

- runFlow:
file: '../../../../subflows/move_to_playground.yaml'

- assertVisible:
id: playground-screen

- runFlow:
file: '../../../subflows/set_enriched_text_value.yaml'
env:
VALUE: |
1. First short item
2. Second short item
3. Third short item

- runFlow:
when:
platform: Android
commands:
- scrollUntilVisible:
element:
id: blur-button
direction: UP
visibilityPercentage: 100
- tapOn:
id: blur-button

- scrollUntilVisible:
element:
id: preview-container
direction: DOWN
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/capture_or_assert_screenshot.yaml'
env:
SCREENSHOT_NAME: 'ordered_list_wide_markers_short_control_display'

- scrollUntilVisible:
element:
id: set-markdown-button
direction: UP
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/set_enriched_text_value.yaml'
env:
VALUE: |
98. Before the boundary
99. Two-digit marker
100. Three-digit marker
101. After the boundary

- runFlow:
when:
platform: Android
commands:
- scrollUntilVisible:
element:
id: blur-button
direction: UP
visibilityPercentage: 100
- tapOn:
id: blur-button

- scrollUntilVisible:
element:
id: preview-container
direction: DOWN
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/capture_or_assert_screenshot.yaml'
env:
SCREENSHOT_NAME: 'ordered_list_wide_markers_hundreds_display'

- scrollUntilVisible:
element:
id: set-markdown-button
direction: UP
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/set_enriched_text_value.yaml'
env:
VALUE: |
998. Before the boundary
999. Three-digit marker
1000. Four-digit marker
1001. After the boundary

- runFlow:
when:
platform: Android
commands:
- scrollUntilVisible:
element:
id: blur-button
direction: UP
visibilityPercentage: 100
- tapOn:
id: blur-button

- scrollUntilVisible:
element:
id: preview-container
direction: DOWN
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/capture_or_assert_screenshot.yaml'
env:
SCREENSHOT_NAME: 'ordered_list_wide_markers_thousands_display'

- scrollUntilVisible:
element:
id: set-markdown-button
direction: UP
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/set_enriched_text_value.yaml'
env:
VALUE: |
1000. Wide parent
1. Nested first item
1. Deeper nested item
2. Nested second item
1001. Next wide parent

- runFlow:
when:
platform: Android
commands:
- scrollUntilVisible:
element:
id: blur-button
direction: UP
visibilityPercentage: 100
- tapOn:
id: blur-button

- scrollUntilVisible:
element:
id: preview-container
direction: DOWN
visibilityPercentage: 100

- runFlow:
file: '../../../subflows/capture_or_assert_screenshot.yaml'
env:
SCREENSHOT_NAME: 'ordered_list_wide_markers_nested_display'
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
appId: swmansion.enriched.markdown.ios.example
tags:
- block
- list
- text
---
- launchApp

- runFlow:
file: '../../../../subflows/move_to_playground.yaml'

- runFlow:
file: '../../../subflows/set_enriched_text_value.yaml'
env:
VALUE: |
1. One
2. Two
3. Three
4. Four
5. Five
6. Six
7. Seven
8. Eight
9. Nine
10. Ten
11. Eleven
12. Twelve
- Nested under a two-digit item

- runFlow:
file: '../../../subflows/capture_or_assert_screenshot.yaml'
env:
SCREENSHOT_NAME: 'ordered_list_wide_markers_display'
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/docs/ios/api-reference/style-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Ordered and unordered lists, and their markers. Takes the [shared modifiers](#sh
| `.bulletColor(_:)` | `Color \| semantic` | `.secondary` | The dot drawn for an unordered item |
| `.bulletSize(_:)` | `CGFloat` | `6` | Diameter of that dot, in points |
| `.markerColor(_:)` | `Color \| semantic` | `.secondary` | The `1.` `2.` `3.` of an ordered item |
| `.markerMinWidth(_:)` | `CGFloat` | `0` | Minimum width reserved for the marker column, so numbers past `9.` do not shift the text. `0` sizes the column to each marker |
| `.markerMinWidth(_:)` | `CGFloat` | `0` | Minimum width reserved for the marker column. An ordered list's column also grows to fit its widest number, so its markers are never clipped |
| `.gapWidth(_:)` | `CGFloat` | `12` | Space between the marker and the item text. Clamped to a minimum of `4` |
| `.marginLeading(_:)` | `CGFloat` | `24` | Indent added per nesting level |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class BlockStyleContext {
var listDepth = 0
var listType: ListType? = null
var listItemNumber = 0
var listMarkerWidth = 0f
var ancestorMarkerOffset = 0f
var listItemMarkerExtra = 0f
var taskItemCount = 0

private val orderedListItemNumbers = ArrayDeque<Int>()
Expand Down Expand Up @@ -149,6 +152,9 @@ class BlockStyleContext {
listDepth = 0
listType = null
listItemNumber = 0
listMarkerWidth = 0f
ancestorMarkerOffset = 0f
listItemMarkerExtra = 0f
taskItemCount = 0
orderedListItemNumbers.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,8 @@ class ListItemRenderer(
// indices, matching the top-down order of `- [ ]` markers in the source.
val taskIndex = if (isTask) styleContext.taskItemCount++ else -1

factory.renderChildren(node, builder, onLinkPress, onLinkLongPress)

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

while (builder.length > start && builder.last() == '\n') {
builder.delete(builder.length - 1, builder.length)
}
builder.append("\n")

val depth = styleContext.listDepth - 1
val listStyle = config.style.listStyle
val itemEnd = builder.length
val span =
if (isTask) {
TaskListSpan(
Expand All @@ -59,21 +49,52 @@ class ListItemRenderer(
styleCache = factory.styleCache,
taskIndex = taskIndex,
isChecked = isChecked,
ancestorMarkerOffset = styleContext.ancestorMarkerOffset,
)
} else {
when (listType) {
BlockStyleContext.ListType.UNORDERED -> {
UnorderedListSpan(listStyle, depth, factory.context, factory.styleCache)
UnorderedListSpan(
listStyle,
depth,
factory.context,
factory.styleCache,
styleContext.ancestorMarkerOffset,
)
}

BlockStyleContext.ListType.ORDERED -> {
OrderedListSpan(listStyle, depth, factory.context, factory.styleCache).apply {
OrderedListSpan(
listStyle,
depth,
factory.context,
factory.styleCache,
styleContext.listMarkerWidth,
styleContext.ancestorMarkerOffset,
).apply {
setItemNumber(styleContext.listItemNumber)
}
}
}
}

// Nested lists inside this item shift by its extra marker width.
val previousMarkerExtra = styleContext.listItemMarkerExtra
styleContext.listItemMarkerExtra = (span as? OrderedListSpan)?.markerExtraWidth ?: 0f
try {
factory.renderChildren(node, builder, onLinkPress, onLinkLongPress)
} finally {
styleContext.listItemMarkerExtra = previousMarkerExtra
}

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

while (builder.length > start && builder.last() == '\n') {
builder.delete(builder.length - 1, builder.length)
}
builder.append("\n")

val itemEnd = builder.length
builder.setSpan(span, start, itemEnd, SPAN_FLAGS_LIST_ITEM)

if (isChecked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.swmansion.enriched.markdown.renderer
import android.text.SpannableStringBuilder
import com.swmansion.enriched.markdown.parser.MarkdownASTNode
import com.swmansion.enriched.markdown.spans.MarginBottomSpan
import com.swmansion.enriched.markdown.spans.OrderedListSpan
import com.swmansion.enriched.markdown.styles.ListStyle
import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE
import com.swmansion.enriched.markdown.utils.text.span.applyLineHeightSkippingImages
import com.swmansion.enriched.markdown.utils.text.span.applyMarginTop
Expand All @@ -24,6 +26,13 @@ class ListRenderer(

val contextManager = ListContextManager(factory.blockStyleContext)
val entryState = contextManager.enterList(listType, listStyle)
val styleContext = factory.blockStyleContext
val previousMarkerWidth = styleContext.listMarkerWidth
val previousMarkerOffset = styleContext.ancestorMarkerOffset
if (entryState.previousDepth > 0) {
styleContext.ancestorMarkerOffset += styleContext.listItemMarkerExtra
}
styleContext.listMarkerWidth = if (isOrdered) measureMarkerColumn(node, listStyle, factory) else 0f

// For top-level lists, insert marginTop spacer before rendering content
if (entryState.previousDepth == 0) {
Expand All @@ -41,6 +50,8 @@ class ListRenderer(
try {
factory.renderChildren(node, builder, onLinkPress, onLinkLongPress)
} finally {
styleContext.listMarkerWidth = previousMarkerWidth
styleContext.ancestorMarkerOffset = previousMarkerOffset
contextManager.exitList(entryState)
}

Expand All @@ -49,6 +60,25 @@ class ListRenderer(
}
}

/** Widest marker this list draws; lists under 100 items keep the `99.` column. */
private fun measureMarkerColumn(
node: MarkdownASTNode,
listStyle: ListStyle,
factory: RendererFactory,
): Float {
val marker = OrderedListSpan(listStyle, 0, factory.context, factory.styleCache)
var number = 1
var width = marker.measureMarkerWidth(99)
for (item in node.children) {
if (item.type != MarkdownASTNode.NodeType.ListItem) continue
if (number >= 100 && item.attributes["isTask"] != "true") {
width = maxOf(width, marker.measureMarkerWidth(number))
}
number++
}
return width
}

private fun applyListSpacing(
builder: SpannableStringBuilder,
start: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class BaseListSpan(
protected val blockStyle: BlockStyle,
protected val marginLeft: Float,
protected val gapWidth: Float,
internal val ancestorMarkerOffset: Float = 0f,
) : MetricAffectingSpan(),
LeadingMarginSpan {
private var cachedText: CharSequence? = null
Expand Down
Loading