From eaefe20d38b5a694b75536ac0d4e7ae1d0e876fa Mon Sep 17 00:00:00 2001 From: cp-megh Date: Mon, 12 Feb 2024 18:10:30 +0530 Subject: [PATCH] WIP - Refactored add header and remove character logic --- .../editor/ui/data/QuillTextManager.kt | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt b/editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt index f91bcdb..deb4613 100644 --- a/editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt +++ b/editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt @@ -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() } @@ -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(