Skip to content

Commit cac4960

Browse files
committed
Use-Case Fixes
1 parent f1a58ff commit cac4960

File tree

1 file changed

+116
-52
lines changed

1 file changed

+116
-52
lines changed

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

+116-52
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.text.style.BulletSpan
66
import android.text.style.RelativeSizeSpan
77
import android.text.style.StyleSpan
88
import android.text.style.UnderlineSpan
9+
import android.util.Log
910
import androidx.compose.runtime.mutableStateListOf
1011
import androidx.compose.ui.text.TextRange
1112
import com.canopas.editor.ui.model.Attributes
@@ -108,9 +109,20 @@ class QuillTextManager(quillSpan: QuillSpan) {
108109
val nextSpan = quillTextSpans.getOrNull(index + 1)
109110
val nextInsert =
110111
nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) }
112+
val previousSpan = quillTextSpans.getOrNull(index - 1)
113+
val previousInsert =
114+
previousSpan?.let {
115+
editableText.substring(
116+
previousSpan.from,
117+
previousSpan.to + 1
118+
)
119+
}
111120
if (nextInsert == " " || nextInsert == "") {
112121
insert += nextInsert
113122
}
123+
if (!previousInsert.isNullOrEmpty() && previousInsert.last() == ' ') {
124+
insert += ' '
125+
}
114126
val attributes = Attributes(
115127
header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() }
116128
?.headerLevel() else null,
@@ -202,14 +214,11 @@ class QuillTextManager(quillSpan: QuillSpan) {
202214

203215
private fun getRichSpanListByTextRange(selection: TextRange): List<TextSpanStyle> {
204216
val matchingSpans = mutableListOf<TextSpanStyle>()
205-
206-
for (part in quillTextSpans) {
207-
val partRange = TextRange(part.from, part.to.coerceAtLeast(0))
208-
if (selection.overlaps(partRange)) {
209-
part.style.let {
210-
matchingSpans.addAll(it)
211-
}
212-
}
217+
val currentSpan = quillTextSpans.find {
218+
it.from <= selection.min && it.to >= selection.min
219+
}
220+
if (currentSpan != null) {
221+
matchingSpans.addAll(currentSpan.style)
213222
}
214223
return matchingSpans
215224
}
@@ -306,24 +315,36 @@ class QuillTextManager(quillSpan: QuillSpan) {
306315
fromIndex: Int, toIndex: Int, style: TextSpanStyle
307316
) {
308317

318+
Log.e(
319+
"XXX",
320+
"Selected parts: $selectedParts\nFrom index: $fromIndex\nTo index: $toIndex\nStyle: $style"
321+
)
309322
selectedParts.forEach { part ->
310323
val index = quillTextSpans.indexOf(part)
311324
if (index !in quillTextSpans.indices) return@forEach
312325

313-
if (fromIndex == 0 && toIndex == part.to) {
326+
if (fromIndex == part.from && toIndex == part.to) {
314327
quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style })
315-
} else if (fromIndex == 0 && toIndex < part.to && toIndex > 0) {
328+
} else if (fromIndex >= part.from && toIndex < part.to && toIndex > 0) {
316329
quillTextSpans.removeAt(index)
317330
quillTextSpans.add(
318331
index,
332+
QuillTextSpan(
333+
from = part.from,
334+
to = fromIndex - 1,
335+
style = part.style
336+
)
337+
)
338+
quillTextSpans.add(
339+
index + 1,
319340
QuillTextSpan(
320341
from = fromIndex,
321342
to = toIndex,
322343
style = part.style.filterNot { it == style }
323344
)
324345
)
325346
quillTextSpans.add(
326-
index + 1,
347+
index + 2,
327348
QuillTextSpan(
328349
from = toIndex + 1,
329350
to = part.to,
@@ -487,71 +508,114 @@ class QuillTextManager(quillSpan: QuillSpan) {
487508
currentStyles.clear()
488509
}
489510

490-
val selectedStyles = currentStyles.distinct().toMutableList()
511+
val selectedStyles = currentStyles.distinct()
491512

492513
moveSpans(startTypeIndex, typedCharsCount)
493514

494515
val currentSpan = quillTextSpans.find {
495516
it.from <= startTypeIndex && it.to >= startTypeIndex
496517
}
497518

498-
if (currentSpan != null) {
499-
val index = quillTextSpans.indexOf(currentSpan)
500-
val styles = (currentSpan.style + selectedStyles).distinct()
501-
if (!currentSpan.style.contains(TextSpanStyle.BulletStyle)) {
502-
if (startTypeIndex == currentSpan.from && currentSpan.to == startTypeIndex) {
503-
quillTextSpans[index] = currentSpan.copy(
504-
from = currentSpan.from,
505-
to = currentSpan.to + typedCharsCount,
506-
style = styles
507-
)
508-
} else if (startTypeIndex == currentSpan.from && currentSpan.to > startTypeIndex) {
519+
currentSpan?.let { span ->
520+
val index = quillTextSpans.indexOf(span)
521+
val styles = (span.style + selectedStyles).distinct()
522+
523+
val from = span.from
524+
val to = span.to
525+
// TODO: Add support for bullet style
526+
val isBulletStyle = TextSpanStyle.BulletStyle in styles
527+
528+
when {
529+
span.style == selectedStyles -> {
530+
val updatedSpan = span.copy(to = to + typedCharsCount, style = styles)
531+
quillTextSpans[index] = updatedSpan
532+
}
533+
534+
span.style != selectedStyles -> {
509535
quillTextSpans.removeAt(index)
510-
quillTextSpans.add(
511-
index,
512-
currentSpan.copy(
513-
from = currentSpan.from,
514-
to = startTypeIndex + typedCharsCount - 1,
515-
style = styles
536+
if (startTypeIndex == from) {
537+
quillTextSpans.add(
538+
index,
539+
span.copy(
540+
from = startTypeIndex,
541+
to = startTypeIndex + typedCharsCount - 1,
542+
style = selectedStyles
543+
)
516544
)
517-
)
545+
quillTextSpans.add(
546+
index + 1,
547+
span.copy(
548+
from = startTypeIndex + typedCharsCount,
549+
to = to + typedCharsCount,
550+
style = span.style
551+
)
552+
)
553+
} else {
554+
quillTextSpans.add(
555+
index,
556+
span.copy(to = startTypeIndex - 1, style = span.style)
557+
)
558+
quillTextSpans.add(
559+
index + 1,
560+
span.copy(
561+
from = startTypeIndex,
562+
to = startTypeIndex + typedCharsCount,
563+
style = selectedStyles
564+
)
565+
)
566+
quillTextSpans.add(
567+
index + 2,
568+
span.copy(
569+
from = startTypeIndex + typedCharsCount + 1,
570+
to = to + typedCharsCount,
571+
style = span.style
572+
)
573+
)
574+
}
575+
}
576+
577+
startTypeIndex == from && to == startTypeIndex -> {
578+
quillTextSpans[index] =
579+
span.copy(to = to + typedCharsCount, style = selectedStyles)
580+
}
581+
582+
startTypeIndex == from && to > startTypeIndex -> {
583+
quillTextSpans[index] =
584+
span.copy(to = startTypeIndex + typedCharsCount - 1, style = selectedStyles)
518585
quillTextSpans.add(
519586
index + 1,
520-
currentSpan.copy(
587+
span.copy(
521588
from = startTypeIndex + typedCharsCount,
522-
to = currentSpan.to + typedCharsCount,
589+
to = to + typedCharsCount,
523590
style = styles
524591
)
525592
)
526-
} else if (startTypeIndex > currentSpan.from && currentSpan.to == startTypeIndex) {
593+
}
594+
595+
startTypeIndex > from && to == startTypeIndex -> {
596+
quillTextSpans[index] = span.copy(to = to + typedCharsCount, style = styles)
597+
}
598+
599+
startTypeIndex in (from + 1) until to -> {
527600
quillTextSpans.removeAt(index)
601+
quillTextSpans.add(index, span.copy(to = startTypeIndex - 1, style = styles))
528602
quillTextSpans.add(
529-
index,
530-
currentSpan.copy(
531-
from = currentSpan.from,
532-
to = startTypeIndex - 1,
533-
style = styles
603+
index + 1,
604+
span.copy(
605+
from = startTypeIndex,
606+
to = startTypeIndex + typedCharsCount - 1,
607+
style = selectedStyles
534608
)
535609
)
536610
quillTextSpans.add(
537-
index + 1,
538-
currentSpan.copy(
539-
from = startTypeIndex,
540-
to = currentSpan.to + typedCharsCount,
611+
index + 2,
612+
span.copy(
613+
from = startTypeIndex + typedCharsCount,
614+
to = to + typedCharsCount,
541615
style = styles
542616
)
543617
)
544-
} else if (startTypeIndex > currentSpan.from && currentSpan.to > startTypeIndex) {
545-
quillTextSpans[index] = currentSpan.copy(
546-
to = currentSpan.to + typedCharsCount,
547-
style = styles
548-
)
549618
}
550-
} else {
551-
quillTextSpans[index] = currentSpan.copy(
552-
to = currentSpan.to + typedCharsCount,
553-
style = styles
554-
)
555619
}
556620
}
557621
}

0 commit comments

Comments
 (0)