@@ -6,6 +6,7 @@ import android.text.style.BulletSpan
6
6
import android.text.style.RelativeSizeSpan
7
7
import android.text.style.StyleSpan
8
8
import android.text.style.UnderlineSpan
9
+ import android.util.Log
9
10
import androidx.compose.runtime.mutableStateListOf
10
11
import androidx.compose.ui.text.TextRange
11
12
import com.canopas.editor.ui.model.Attributes
@@ -108,9 +109,20 @@ class QuillTextManager(quillSpan: QuillSpan) {
108
109
val nextSpan = quillTextSpans.getOrNull(index + 1 )
109
110
val nextInsert =
110
111
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
+ }
111
120
if (nextInsert == " " || nextInsert == " " ) {
112
121
insert + = nextInsert
113
122
}
123
+ if (! previousInsert.isNullOrEmpty() && previousInsert.last() == ' ' ) {
124
+ insert + = ' '
125
+ }
114
126
val attributes = Attributes (
115
127
header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() }
116
128
?.headerLevel() else null ,
@@ -202,14 +214,11 @@ class QuillTextManager(quillSpan: QuillSpan) {
202
214
203
215
private fun getRichSpanListByTextRange (selection : TextRange ): List <TextSpanStyle > {
204
216
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)
213
222
}
214
223
return matchingSpans
215
224
}
@@ -306,24 +315,36 @@ class QuillTextManager(quillSpan: QuillSpan) {
306
315
fromIndex : Int , toIndex : Int , style : TextSpanStyle
307
316
) {
308
317
318
+ Log .e(
319
+ " XXX" ,
320
+ " Selected parts: $selectedParts \n From index: $fromIndex \n To index: $toIndex \n Style: $style "
321
+ )
309
322
selectedParts.forEach { part ->
310
323
val index = quillTextSpans.indexOf(part)
311
324
if (index !in quillTextSpans.indices) return @forEach
312
325
313
- if (fromIndex == 0 && toIndex == part.to) {
326
+ if (fromIndex == part.from && toIndex == part.to) {
314
327
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 ) {
316
329
quillTextSpans.removeAt(index)
317
330
quillTextSpans.add(
318
331
index,
332
+ QuillTextSpan (
333
+ from = part.from,
334
+ to = fromIndex - 1 ,
335
+ style = part.style
336
+ )
337
+ )
338
+ quillTextSpans.add(
339
+ index + 1 ,
319
340
QuillTextSpan (
320
341
from = fromIndex,
321
342
to = toIndex,
322
343
style = part.style.filterNot { it == style }
323
344
)
324
345
)
325
346
quillTextSpans.add(
326
- index + 1 ,
347
+ index + 2 ,
327
348
QuillTextSpan (
328
349
from = toIndex + 1 ,
329
350
to = part.to,
@@ -487,71 +508,114 @@ class QuillTextManager(quillSpan: QuillSpan) {
487
508
currentStyles.clear()
488
509
}
489
510
490
- val selectedStyles = currentStyles.distinct().toMutableList()
511
+ val selectedStyles = currentStyles.distinct()
491
512
492
513
moveSpans(startTypeIndex, typedCharsCount)
493
514
494
515
val currentSpan = quillTextSpans.find {
495
516
it.from <= startTypeIndex && it.to >= startTypeIndex
496
517
}
497
518
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 -> {
509
535
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
+ )
516
544
)
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)
518
585
quillTextSpans.add(
519
586
index + 1 ,
520
- currentSpan .copy(
587
+ span .copy(
521
588
from = startTypeIndex + typedCharsCount,
522
- to = currentSpan. to + typedCharsCount,
589
+ to = to + typedCharsCount,
523
590
style = styles
524
591
)
525
592
)
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 -> {
527
600
quillTextSpans.removeAt(index)
601
+ quillTextSpans.add(index, span.copy(to = startTypeIndex - 1 , style = styles))
528
602
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
534
608
)
535
609
)
536
610
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,
541
615
style = styles
542
616
)
543
617
)
544
- } else if (startTypeIndex > currentSpan.from && currentSpan.to > startTypeIndex) {
545
- quillTextSpans[index] = currentSpan.copy(
546
- to = currentSpan.to + typedCharsCount,
547
- style = styles
548
- )
549
618
}
550
- } else {
551
- quillTextSpans[index] = currentSpan.copy(
552
- to = currentSpan.to + typedCharsCount,
553
- style = styles
554
- )
555
619
}
556
620
}
557
621
}
0 commit comments