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 59b6c7e..fc5f5fb 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 @@ -6,6 +6,7 @@ import android.text.style.BulletSpan import android.text.style.RelativeSizeSpan import android.text.style.StyleSpan import android.text.style.UnderlineSpan +import android.util.Log import androidx.compose.runtime.mutableStateListOf import androidx.compose.ui.text.TextRange import com.canopas.editor.ui.model.Attributes @@ -108,9 +109,20 @@ class QuillTextManager(quillSpan: QuillSpan) { val nextSpan = quillTextSpans.getOrNull(index + 1) val nextInsert = nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) } + val previousSpan = quillTextSpans.getOrNull(index - 1) + val previousInsert = + previousSpan?.let { + editableText.substring( + previousSpan.from, + previousSpan.to + 1 + ) + } if (nextInsert == " " || nextInsert == "") { insert += nextInsert } + if (!previousInsert.isNullOrEmpty() && previousInsert.last() == ' ') { + insert += ' ' + } val attributes = Attributes( header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() } ?.headerLevel() else null, @@ -202,14 +214,11 @@ class QuillTextManager(quillSpan: QuillSpan) { private fun getRichSpanListByTextRange(selection: TextRange): List { val matchingSpans = mutableListOf() - - for (part in quillTextSpans) { - val partRange = TextRange(part.from, part.to.coerceAtLeast(0)) - if (selection.overlaps(partRange)) { - part.style.let { - matchingSpans.addAll(it) - } - } + val currentSpan = quillTextSpans.find { + it.from <= selection.min && it.to >= selection.min + } + if (currentSpan != null) { + matchingSpans.addAll(currentSpan.style) } return matchingSpans } @@ -306,16 +315,28 @@ class QuillTextManager(quillSpan: QuillSpan) { fromIndex: Int, toIndex: Int, style: TextSpanStyle ) { + Log.e( + "XXX", + "Selected parts: $selectedParts\nFrom index: $fromIndex\nTo index: $toIndex\nStyle: $style" + ) selectedParts.forEach { part -> val index = quillTextSpans.indexOf(part) if (index !in quillTextSpans.indices) return@forEach - if (fromIndex == 0 && toIndex == part.to) { + if (fromIndex == part.from && toIndex == part.to) { quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style }) - } else if (fromIndex == 0 && toIndex < part.to && toIndex > 0) { + } else if (fromIndex >= part.from && toIndex < part.to && toIndex > 0) { quillTextSpans.removeAt(index) quillTextSpans.add( index, + QuillTextSpan( + from = part.from, + to = fromIndex - 1, + style = part.style + ) + ) + quillTextSpans.add( + index + 1, QuillTextSpan( from = fromIndex, to = toIndex, @@ -323,7 +344,7 @@ class QuillTextManager(quillSpan: QuillSpan) { ) ) quillTextSpans.add( - index + 1, + index + 2, QuillTextSpan( from = toIndex + 1, to = part.to, @@ -487,7 +508,7 @@ class QuillTextManager(quillSpan: QuillSpan) { currentStyles.clear() } - val selectedStyles = currentStyles.distinct().toMutableList() + val selectedStyles = currentStyles.distinct() moveSpans(startTypeIndex, typedCharsCount) @@ -495,63 +516,106 @@ class QuillTextManager(quillSpan: QuillSpan) { it.from <= startTypeIndex && it.to >= startTypeIndex } - if (currentSpan != null) { - val index = quillTextSpans.indexOf(currentSpan) - val styles = (currentSpan.style + selectedStyles).distinct() - if (!currentSpan.style.contains(TextSpanStyle.BulletStyle)) { - if (startTypeIndex == currentSpan.from && currentSpan.to == startTypeIndex) { - quillTextSpans[index] = currentSpan.copy( - from = currentSpan.from, - to = currentSpan.to + typedCharsCount, - style = styles - ) - } else if (startTypeIndex == currentSpan.from && currentSpan.to > startTypeIndex) { + currentSpan?.let { span -> + val index = quillTextSpans.indexOf(span) + val styles = (span.style + selectedStyles).distinct() + + val from = span.from + val to = span.to + // TODO: Add support for bullet style + val isBulletStyle = TextSpanStyle.BulletStyle in styles + + when { + span.style == selectedStyles -> { + val updatedSpan = span.copy(to = to + typedCharsCount, style = styles) + quillTextSpans[index] = updatedSpan + } + + span.style != selectedStyles -> { quillTextSpans.removeAt(index) - quillTextSpans.add( - index, - currentSpan.copy( - from = currentSpan.from, - to = startTypeIndex + typedCharsCount - 1, - style = styles + if (startTypeIndex == from) { + quillTextSpans.add( + index, + span.copy( + from = startTypeIndex, + to = startTypeIndex + typedCharsCount - 1, + style = selectedStyles + ) ) - ) + quillTextSpans.add( + index + 1, + span.copy( + from = startTypeIndex + typedCharsCount, + to = to + typedCharsCount, + style = span.style + ) + ) + } else { + quillTextSpans.add( + index, + span.copy(to = startTypeIndex - 1, style = span.style) + ) + quillTextSpans.add( + index + 1, + span.copy( + from = startTypeIndex, + to = startTypeIndex + typedCharsCount, + style = selectedStyles + ) + ) + quillTextSpans.add( + index + 2, + span.copy( + from = startTypeIndex + typedCharsCount + 1, + to = to + typedCharsCount, + style = span.style + ) + ) + } + } + + startTypeIndex == from && to == startTypeIndex -> { + quillTextSpans[index] = + span.copy(to = to + typedCharsCount, style = selectedStyles) + } + + startTypeIndex == from && to > startTypeIndex -> { + quillTextSpans[index] = + span.copy(to = startTypeIndex + typedCharsCount - 1, style = selectedStyles) quillTextSpans.add( index + 1, - currentSpan.copy( + span.copy( from = startTypeIndex + typedCharsCount, - to = currentSpan.to + typedCharsCount, + to = to + typedCharsCount, style = styles ) ) - } else if (startTypeIndex > currentSpan.from && currentSpan.to == startTypeIndex) { + } + + startTypeIndex > from && to == startTypeIndex -> { + quillTextSpans[index] = span.copy(to = to + typedCharsCount, style = styles) + } + + startTypeIndex in (from + 1) until to -> { quillTextSpans.removeAt(index) + quillTextSpans.add(index, span.copy(to = startTypeIndex - 1, style = styles)) quillTextSpans.add( - index, - currentSpan.copy( - from = currentSpan.from, - to = startTypeIndex - 1, - style = styles + index + 1, + span.copy( + from = startTypeIndex, + to = startTypeIndex + typedCharsCount - 1, + style = selectedStyles ) ) quillTextSpans.add( - index + 1, - currentSpan.copy( - from = startTypeIndex, - to = currentSpan.to + typedCharsCount, + index + 2, + span.copy( + from = startTypeIndex + typedCharsCount, + to = to + typedCharsCount, style = styles ) ) - } else if (startTypeIndex > currentSpan.from && currentSpan.to > startTypeIndex) { - quillTextSpans[index] = currentSpan.copy( - to = currentSpan.to + typedCharsCount, - style = styles - ) } - } else { - quillTextSpans[index] = currentSpan.copy( - to = currentSpan.to + typedCharsCount, - style = styles - ) } } }