Skip to content
Open
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
Expand Up @@ -103,8 +103,16 @@ class InlineStyles(

fun afterTextChanged(
s: Editable,
startCursorPosition: Int,
endCursorPosition: Int,
) {
if (endCursorPosition > startCursorPosition) {
Comment thread
exploIF marked this conversation as resolved.
for ((style, config) in EnrichedSpans.inlineSpans) {
if (view.spanState?.getStart(style) != null) continue
splitSpanOnInsertion(s, config.clazz, startCursorPosition, endCursorPosition)
}
}

for ((style, config) in EnrichedSpans.inlineSpans) {
val start = view.spanState?.getStart(style) ?: continue
var end = endCursorPosition
Expand All @@ -117,6 +125,22 @@ class InlineStyles(

setSpan(s, config.clazz, start, end)
}

val isBackspace = endCursorPosition == startCursorPosition
if (!isBackspace) {
return
}

// Collapse same-type inline spans that ended up adjacent to each other after deletion.
// Without this the HTML output would emit separate tags like <b>...</b><b>...</b>.
for ((_, config) in EnrichedSpans.inlineSpans) {
for (span in s.getSpans(startCursorPosition, startCursorPosition, config.clazz)) {
val spanStart = s.getSpanStart(span)
val spanEnd = s.getSpanEnd(span)
if (spanStart < 0 || spanEnd < 0) continue
setSpan(s, config.clazz, spanStart, spanEnd)
}
}
}

fun toggleStyle(name: String) {
Expand All @@ -143,6 +167,33 @@ class InlineStyles(
view.selection.validateStyles()
}

private fun <T> splitSpanOnInsertion(
spannable: Spannable,
type: Class<T>,
insertStart: Int,
insertEnd: Int,
) {
val spans = spannable.getSpans(insertStart, insertEnd, type)
for (span in spans) {
val spanStart = spannable.getSpanStart(span)
val spanEnd = spannable.getSpanEnd(span)
if (spanStart < 0 || spanEnd < 0) continue

spannable.removeSpan(span)

if (spanStart < insertStart) {
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(spanStart, insertStart)
val left = type.getDeclaredConstructor(HtmlStyle::class.java).newInstance(view.htmlStyle)
spannable.setSpan(left, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if (spanEnd > insertEnd) {
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(insertEnd, spanEnd)
val right = type.getDeclaredConstructor(HtmlStyle::class.java).newInstance(view.htmlStyle)
spannable.setSpan(right, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}

fun removeStyle(
name: String,
start: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class EnrichedTextWatcher(
}

private fun applyStyles(s: Editable) {
view.inlineStyles?.afterTextChanged(s, endCursorPosition)
view.inlineStyles?.afterTextChanged(s, startCursorPosition, endCursorPosition)
view.paragraphStyles?.afterTextChanged(s, endCursorPosition, previousTextLength)
view.listStyles?.afterTextChanged(s, endCursorPosition, previousTextLength)
view.parametrizedStyles?.afterTextChanged(s, startCursorPosition, endCursorPosition)
Expand Down
Loading