Skip to content
Merged
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
@@ -1,6 +1,7 @@
package com.swmansion.enriched.markdown.renderer

import android.text.SpannableStringBuilder
import android.text.Spanned
import com.swmansion.enriched.markdown.parser.MarkdownASTNode
import com.swmansion.enriched.markdown.spans.HeadingSpan
import com.swmansion.enriched.markdown.utils.common.layout.isLayoutRTL
Expand Down Expand Up @@ -37,14 +38,16 @@ class HeadingRenderer(
val contentLength = end - start

if (contentLength > 0) {
// Set after the children, so top priority makes it apply first and the
// inline spans inside the heading (code, links, strong, emphasis) win.
builder.setSpan(
HeadingSpan(
level,
config.style,
),
start,
end,
SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or Spanned.SPAN_PRIORITY,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have SPAN_FLAGS_CONTAINER_BACKGROUND constant that is equal to Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or Spanned.SPAN_PRIORITY. Could you rename it something more generic like SPAN_FLAGS_EXCULSIVE_EXCLUSIVE_PRIORITY and reuse it here?

)

builder.setSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.swmansion.enriched.markdown.spans.OrderedListSpan
import com.swmansion.enriched.markdown.spans.TaskListSpan
import com.swmansion.enriched.markdown.spans.UnorderedListSpan
import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE
import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_LIST_ITEM

class ListItemRenderer(
private val config: RendererConfig,
Expand Down Expand Up @@ -73,7 +74,7 @@ class ListItemRenderer(
}
}

builder.setSpan(span, start, itemEnd, SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE)
builder.setSpan(span, start, itemEnd, SPAN_FLAGS_LIST_ITEM)

if (isChecked) {
applyCheckedDecorations(builder, start, itemEnd, depth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ const val SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE = SpannableString.SPAN_EXCLUSIVE_EXCLUS
*/
const val SPAN_FLAGS_CONTAINER_BACKGROUND =
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or Spanned.SPAN_PRIORITY

/**
* `SPAN_EXCLUSIVE_EXCLUSIVE` with a span priority just above the default.
*
* List item spans are set after the item's children, so at the default
* priority they apply after inline spans such as `CodeSpan` and replace their
* font and size. This priority applies them first, while staying below
* [SPAN_FLAGS_CONTAINER_BACKGROUND] so a blockquote still draws its leading
* margin before a list's.
*/
const val SPAN_FLAGS_LIST_ITEM =
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or (1 shl Spanned.SPAN_PRIORITY_SHIFT)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.swmansion.enriched.markdown.renderer

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.AlignmentSpan
import com.swmansion.enriched.markdown.parser.MarkdownASTNode
import com.swmansion.enriched.markdown.spans.HeadingSpan
Expand Down Expand Up @@ -40,14 +41,16 @@ class HeadingRenderer(
val contentLength = end - start

if (contentLength > 0) {
// Set after the children, so top priority makes it apply first and the
// inline spans inside the heading (code, links, strong, emphasis) win.
builder.setSpan(
HeadingSpan(
level,
config.style,
),
start,
end,
SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or Spanned.SPAN_PRIORITY,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

)

builder.setSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.swmansion.enriched.markdown.spans.OrderedListSpan
import com.swmansion.enriched.markdown.spans.TaskListSpan
import com.swmansion.enriched.markdown.spans.UnorderedListSpan
import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE
import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_LIST_ITEM

class ListItemRenderer(
private val config: RendererConfig,
Expand Down Expand Up @@ -109,7 +110,7 @@ class ListItemRenderer(
if (isFirstSegment && plainAnchor) markerSpan else makeSpan(drawsMarker = false),
pos,
cbStart,
SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE,
SPAN_FLAGS_LIST_ITEM,
)
isFirstSegment = false
}
Expand All @@ -120,7 +121,7 @@ class ListItemRenderer(
if (isFirstSegment && plainAnchor) markerSpan else makeSpan(drawsMarker = false),
pos,
itemEnd,
SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE,
SPAN_FLAGS_LIST_ITEM,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ object TaskListTapUtils {
for (old in targetSpans) {
val start = spannable.getSpanStart(old)
val end = spannable.getSpanEnd(old)
// Keep the original flags so the list item priority survives the toggle
val flags = spannable.getSpanFlags(old)
spannable.removeSpan(old)
spannable.setSpan(toggledCopy(old), start, end, SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(toggledCopy(old), start, end, flags)
}

// The marker may be drawn by an anchor span wrapping an unattached copy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ const val SPAN_FLAGS_CONTAINER_BACKGROUND =
*/
const val SPAN_FLAGS_LINE_HEIGHT_PRIORITY =
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or Spanned.SPAN_PRIORITY

/**
* `SPAN_EXCLUSIVE_EXCLUSIVE` with a span priority just above the default.
*
* List item spans are set after the item's children, so at the default
* priority they apply after inline spans such as `CodeSpan` and replace their
* font and size. This priority applies them first, while staying below
* [SPAN_FLAGS_CONTAINER_BACKGROUND] so a blockquote still draws its leading
* margin before a list's.
*/
const val SPAN_FLAGS_LIST_ITEM =
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE or (1 shl Spanned.SPAN_PRIORITY_SHIFT)
Loading