Skip to content

Commit

Permalink
WIP - Refactored add header and remove character logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 12, 2024
1 parent 10c95a5 commit eaefe20
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,14 @@ class QuillTextManager(quillSpan: QuillSpan) {
val fromIndex = selection.min
val toIndex = if (selection.collapsed) fromIndex else selection.max

val startIndex: Int = max(0, text.lastIndexOf("\n", fromIndex - 1))
var endIndex: Int = text.indexOf("\n", toIndex)

if (endIndex == -1) endIndex = text.length - 1
val selectedParts =
quillTextSpans.filter { endIndex >= it.to && startIndex <= it.from && it.style.any { it.isHeaderStyle() } }

quillTextSpans.removeAll(selectedParts)
quillTextSpans.add(
QuillTextSpan(
from = startIndex,
to = endIndex,
style = listOf(style)
)
)
val currentSpan = quillTextSpans.find {
it.from <= fromIndex && it.to >= toIndex
}
val index = quillTextSpans.indexOf(currentSpan)
quillTextSpans[index] =
currentSpan?.copy(style = currentSpan.style.filterNot { it.isHeaderStyle() } + listOf(
style
)) ?: return
updateText()
}

Expand Down Expand Up @@ -308,7 +301,9 @@ class QuillTextManager(quillSpan: QuillSpan) {
part.from < endIndex && part.to >= startIndex
}

quillTextSpans.removeAll(selectedParts.filter { it.style.any { it.isHeaderStyle() } })
quillTextSpans.removeAll(selectedParts.filter {
it.style.size == 1 && it.style.first().isHeaderStyle()
})
}

private fun removeStylesFromSelectedPart(
Expand Down

0 comments on commit eaefe20

Please sign in to comment.