Skip to content

Commit eaefe20

Browse files
committed
WIP - Refactored add header and remove character logic
1 parent 10c95a5 commit eaefe20

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt

+11-16
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,14 @@ class QuillTextManager(quillSpan: QuillSpan) {
266266
val fromIndex = selection.min
267267
val toIndex = if (selection.collapsed) fromIndex else selection.max
268268

269-
val startIndex: Int = max(0, text.lastIndexOf("\n", fromIndex - 1))
270-
var endIndex: Int = text.indexOf("\n", toIndex)
271-
272-
if (endIndex == -1) endIndex = text.length - 1
273-
val selectedParts =
274-
quillTextSpans.filter { endIndex >= it.to && startIndex <= it.from && it.style.any { it.isHeaderStyle() } }
275-
276-
quillTextSpans.removeAll(selectedParts)
277-
quillTextSpans.add(
278-
QuillTextSpan(
279-
from = startIndex,
280-
to = endIndex,
281-
style = listOf(style)
282-
)
283-
)
269+
val currentSpan = quillTextSpans.find {
270+
it.from <= fromIndex && it.to >= toIndex
271+
}
272+
val index = quillTextSpans.indexOf(currentSpan)
273+
quillTextSpans[index] =
274+
currentSpan?.copy(style = currentSpan.style.filterNot { it.isHeaderStyle() } + listOf(
275+
style
276+
)) ?: return
284277
updateText()
285278
}
286279

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

311-
quillTextSpans.removeAll(selectedParts.filter { it.style.any { it.isHeaderStyle() } })
304+
quillTextSpans.removeAll(selectedParts.filter {
305+
it.style.size == 1 && it.style.first().isHeaderStyle()
306+
})
312307
}
313308

314309
private fun removeStylesFromSelectedPart(

0 commit comments

Comments
 (0)