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 fc5f5fb..325822b 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,7 +6,6 @@ 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 @@ -123,7 +122,7 @@ class QuillTextManager(quillSpan: QuillSpan) { if (!previousInsert.isNullOrEmpty() && previousInsert.last() == ' ') { insert += ' ' } - val attributes = Attributes( + var attributes = Attributes( header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() } ?.headerLevel() else null, bold = if (span.style.contains(TextSpanStyle.BoldStyle)) true else null, @@ -132,6 +131,10 @@ class QuillTextManager(quillSpan: QuillSpan) { list = if (span.style.contains(TextSpanStyle.BulletStyle)) ListType.bullet else null ) + if (insert == "\n") { + attributes = Attributes() + } + // Merge consecutive spans with the same attributes into one if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes && attributes.list == null) { groupedSpans.last().insert += insert @@ -240,10 +243,91 @@ class QuillTextManager(quillSpan: QuillSpan) { val fromIndex = selection.min val toIndex = selection.max - 1 - val selectedParts = quillTextSpans.filter { part -> - part.from < toIndex && part.to >= fromIndex && part.style.contains(style) + val selectedSpan = quillTextSpans.find { + it.from <= fromIndex && it.to >= toIndex + } + if (selectedSpan != null) { + if (fromIndex == selectedSpan.from && toIndex == selectedSpan.to) { + val index = quillTextSpans.indexOf(selectedSpan) + quillTextSpans[index] = + selectedSpan.copy(style = selectedSpan.style.filterNot { it == style }) + } else { + if (fromIndex == selectedSpan.from && toIndex < selectedSpan.to) { + val index = quillTextSpans.indexOf(selectedSpan) + quillTextSpans.removeAt(index) + quillTextSpans.add( + index, + QuillTextSpan( + from = fromIndex, + to = toIndex, + style = selectedSpan.style.filterNot { it == style } + ) + ) + quillTextSpans.add( + index + 1, + QuillTextSpan( + from = toIndex + 1, + to = selectedSpan.to, + style = selectedSpan.style + ) + ) + } else if (fromIndex > selectedSpan.from && toIndex < selectedSpan.to) { + val index = quillTextSpans.indexOf(selectedSpan) + quillTextSpans.removeAt(index) + quillTextSpans.add( + index, + QuillTextSpan( + from = selectedSpan.from, + to = fromIndex - 1, + style = selectedSpan.style + ) + ) + quillTextSpans.add( + index + 1, + QuillTextSpan( + from = fromIndex, + to = toIndex, + style = selectedSpan.style.filterNot { it == style } + ) + ) + quillTextSpans.add( + index + 2, + QuillTextSpan( + from = toIndex + 1, + to = selectedSpan.to, + style = selectedSpan.style + ) + ) + } else if (fromIndex > 0) { + val index = quillTextSpans.indexOf(selectedSpan) + quillTextSpans.removeAt(index) + quillTextSpans.add( + index, + QuillTextSpan( + from = selectedSpan.from, + to = fromIndex - 1, + style = selectedSpan.style + ) + ) + quillTextSpans.add( + index + 1, + QuillTextSpan( + from = fromIndex, + to = toIndex, + style = selectedSpan.style.filterNot { it == style } + ) + ) + quillTextSpans.add( + index + 2, + QuillTextSpan( + from = toIndex + 1, + to = selectedSpan.to, + style = selectedSpan.style + ) + ) + } + } } - removeStylesFromSelectedPart(selectedParts, fromIndex, toIndex, style) updateText() } } @@ -310,77 +394,6 @@ class QuillTextManager(quillSpan: QuillSpan) { }) } - private fun removeStylesFromSelectedPart( - selectedParts: List, - 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 == part.from && toIndex == part.to) { - quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style }) - } 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, - style = part.style.filterNot { it == style } - ) - ) - quillTextSpans.add( - index + 2, - QuillTextSpan( - from = toIndex + 1, - to = part.to, - style = part.style - ) - ) - } else if (fromIndex > 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, - style = part.style.filterNot { it == style } - ) - ) - quillTextSpans.add( - index + 2, - QuillTextSpan( - from = toIndex + 1, - to = part.to, - style = part.style - ) - ) - } - } - } - private fun applyStylesToSelectedText(style: TextSpanStyle) { if (selection.collapsed) return @@ -618,6 +631,32 @@ class QuillTextManager(quillSpan: QuillSpan) { } } } + if (currentSpan == null) { + val lastSpan = quillTextSpans.lastOrNull() + if (lastSpan != null) { + val lastStyles = lastSpan.style + if (lastStyles == selectedStyles) { + quillTextSpans[quillTextSpans.lastIndex] = + lastSpan.copy(to = lastSpan.to + typedCharsCount) + } else { + quillTextSpans.add( + QuillTextSpan( + from = startTypeIndex, + to = startTypeIndex + typedCharsCount - 1, + style = selectedStyles + ) + ) + } + } else { + quillTextSpans.add( + QuillTextSpan( + from = startTypeIndex, + to = startTypeIndex + typedCharsCount - 1, + style = selectedStyles + ) + ) + } + } } private fun moveSpans(startTypeIndex: Int, by: Int) {