Skip to content

Commit 5a1089d

Browse files
committed
Refactor add character logic
1 parent eaefe20 commit 5a1089d

File tree

3 files changed

+142
-98
lines changed

3 files changed

+142
-98
lines changed
+82-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,84 @@
11
{
2-
"spans": [{"insert":"Android Quill","attributes":{"bold":true,"header":1}},{"insert":"\n","attributes":{}},{"insert":"\nRich text editor for Android","attributes":{"bold":true,"header":2}},{"insert":"\n","attributes":{}},{"insert":"Quill component for Android\n","attributes":{"header":3,"italic":true}},{"insert":"Bullet Journal :\nTrack personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders","attributes":{"bold":true}},{"insert":"\nShare your tasks and notes with teammates, and see changes as they happen in real-time, across all devices\n","attributes":{}},{"insert":"Splitting bills with friends can never be easier.","attributes":{"list":"bullet"}},{"insert":"\n","attributes":{}},{"insert":"Testing span addition to the editor.","attributes":{"list":"bullet"}},{"insert":"\n","attributes":{}},{"insert":"Start creating a group and invite your friends to join.","attributes":{"list":"bullet"}},{"insert":"\n","attributes":{}},{"insert":"Create a BuJo of Ledger type to see expense or balance summary.","attributes":{"bold":true,"list":"bullet"}},{"insert":"\n","attributes":{}}]
2+
"spans": [
3+
{
4+
"insert": "Android Quill Editor",
5+
"attributes": {
6+
"bold": true,
7+
"header": 1
8+
}
9+
},
10+
{
11+
"insert": "\n",
12+
"attributes": {}
13+
},
14+
{
15+
"insert": "\nRich text editor for Android",
16+
"attributes": {
17+
"bold": true,
18+
"header": 2
19+
}
20+
},
21+
{
22+
"insert": "\n",
23+
"attributes": {}
24+
},
25+
{
26+
"insert": "Quill component for Android\n",
27+
"attributes": {
28+
"header": 3,
29+
"italic": true
30+
}
31+
},
32+
{
33+
"insert": "Bullet Journal :\nTrack personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders",
34+
"attributes": {
35+
"bold": true
36+
}
37+
},
38+
{
39+
"insert": "\nShare your tasks and notes with teammates, and see changes as they happen in real-time, across all devices\n",
40+
"attributes": {}
41+
},
42+
{
43+
"insert": "Splitting bills with friends can never be easier.",
44+
"attributes": {
45+
"list": "bullet"
46+
}
47+
},
48+
{
49+
"insert": "\n",
50+
"attributes": {}
51+
},
52+
{
53+
"insert": "Testing span addition to the editor.",
54+
"attributes": {
55+
"list": "bullet"
56+
}
57+
},
58+
{
59+
"insert": "\n",
60+
"attributes": {}
61+
},
62+
{
63+
"insert": "Start creating a group and invite your friends to join.",
64+
"attributes": {
65+
"list": "bullet"
66+
}
67+
},
68+
{
69+
"insert": "\n",
70+
"attributes": {}
71+
},
72+
{
73+
"insert": "Create a BuJo of Ledger type to see expense or balance summary.",
74+
"attributes": {
75+
"bold": true,
76+
"list": "bullet"
77+
}
78+
},
79+
{
80+
"insert": "\n",
81+
"attributes": {}
82+
}
83+
]
384
}

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '8.2.1' apply false
4-
id 'com.android.library' version '8.2.1' apply false
3+
id 'com.android.application' version '8.2.2' apply false
4+
id 'com.android.library' version '8.2.2' apply false
55
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
66
id 'io.github.gradle-nexus.publish-plugin' version "1.3.0"
77
}

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

+58-95
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ class QuillTextManager(quillSpan: QuillSpan) {
108108
val nextSpan = quillTextSpans.getOrNull(index + 1)
109109
val nextInsert =
110110
nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) }
111-
if (insert.last() != ' ' && insert.last() != '\n' && nextInsert != null &&
112-
nextInsert.first() != ' ' && nextInsert.first() != '\n'
113-
) {
114-
insert += " "
115-
}
116111
if (nextInsert == " " || nextInsert == "") {
117112
insert += nextInsert
118113
}
@@ -372,7 +367,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
372367
val toIndex = selection.max
373368

374369
val selectedSpan = quillTextSpans.find {
375-
it.from <= fromIndex && it.to >= toIndex
370+
it.from <= fromIndex && (it.to + 1) >= toIndex
376371
}
377372
if (selectedSpan != null) {
378373
if (fromIndex == selectedSpan.from && toIndex < selectedSpan.to) {
@@ -421,7 +416,9 @@ class QuillTextManager(quillSpan: QuillSpan) {
421416
style = selectedSpan.style
422417
)
423418
)
424-
} else if (fromIndex > selectedSpan.from && toIndex == selectedSpan.to) {
419+
} else if (fromIndex > selectedSpan.from &&
420+
(toIndex == selectedSpan.to || toIndex == (selectedSpan.to + 1))
421+
) {
425422
val index = quillTextSpans.indexOf(selectedSpan)
426423
quillTextSpans.removeAt(index)
427424
quillTextSpans.add(
@@ -490,107 +487,73 @@ class QuillTextManager(quillSpan: QuillSpan) {
490487
currentStyles.clear()
491488
}
492489

493-
var selectedStyles = currentStyles.distinct().toMutableList()
490+
val selectedStyles = currentStyles.distinct().toMutableList()
494491

495492
moveSpans(startTypeIndex, typedCharsCount)
496493

497-
val startParts = quillTextSpans.filter { startTypeIndex - 1 in it.from..it.to }
498-
val endParts = quillTextSpans.filter { startTypeIndex in it.from..it.to }
499-
val commonParts = startParts.intersect(endParts.toSet())
500-
501-
startParts.filter { it !in commonParts }
502-
.forEach {
503-
if (selectedStyles.any { selectedStyle -> selectedStyle in it.style }) {
504-
val index = quillTextSpans.indexOf(it)
505-
quillTextSpans[index] = it.copy(to = it.to + typedCharsCount)
506-
selectedStyles =
507-
selectedStyles.filterNot { style -> style in it.style }.toMutableList()
508-
}
509-
}
510-
511-
endParts.filter { it !in commonParts }
512-
.forEach {
513-
selectedStyles =
514-
processSpan(it, typedCharsCount, startTypeIndex, selectedStyles, true)
515-
}
516-
517-
commonParts.forEach {
518-
selectedStyles = processSpan(it, typedCharsCount, startTypeIndex, selectedStyles)
494+
val currentSpan = quillTextSpans.find {
495+
it.from <= startTypeIndex && it.to >= startTypeIndex
519496
}
520497

521-
selectedStyles.forEach { style ->
522-
val currentSpan = quillTextSpans.find {
523-
it.from <= startTypeIndex && it.to >= startTypeIndex
524-
}
525-
if (currentSpan != null) {
526-
val index = quillTextSpans.indexOf(currentSpan)
527-
quillTextSpans.removeAt(index)
528-
quillTextSpans.add(
529-
index,
530-
currentSpan.copy(
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(
531504
from = currentSpan.from,
532-
to = startTypeIndex - 1,
533-
style = currentSpan.style
505+
to = currentSpan.to + typedCharsCount,
506+
style = styles
534507
)
535-
)
536-
quillTextSpans.add(
537-
index + 1,
538-
currentSpan.copy(
539-
from = startTypeIndex,
540-
to = startTypeIndex + typedCharsCount - 1,
541-
style = currentSpan.style + listOf(style)
508+
} else if (startTypeIndex == currentSpan.from && currentSpan.to > startTypeIndex) {
509+
quillTextSpans.removeAt(index)
510+
quillTextSpans.add(
511+
index,
512+
currentSpan.copy(
513+
from = currentSpan.from,
514+
to = startTypeIndex + typedCharsCount - 1,
515+
style = styles
516+
)
542517
)
543-
)
544-
quillTextSpans.add(
545-
index + 2,
546-
currentSpan.copy(
547-
from = startTypeIndex + typedCharsCount,
548-
to = currentSpan.to,
549-
style = currentSpan.style
518+
quillTextSpans.add(
519+
index + 1,
520+
currentSpan.copy(
521+
from = startTypeIndex + typedCharsCount,
522+
to = currentSpan.to + typedCharsCount,
523+
style = styles
524+
)
550525
)
551-
)
552-
}
553-
}
554-
}
555-
556-
private fun processSpan(
557-
richTextSpan: QuillTextSpan,
558-
typedChars: Int,
559-
startTypeIndex: Int,
560-
selectedStyles: MutableList<TextSpanStyle>,
561-
forward: Boolean = false
562-
): MutableList<TextSpanStyle> {
563-
564-
val newFromIndex = richTextSpan.from + typedChars
565-
val newToIndex = richTextSpan.to + typedChars
566-
var updatedSelectedStyle = selectedStyles
567-
568-
val index = quillTextSpans.indexOf(richTextSpan)
569-
if (selectedStyles.any { it in richTextSpan.style }) {
570-
quillTextSpans[index] = richTextSpan.copy(to = newToIndex)
571-
updatedSelectedStyle =
572-
selectedStyles.filterNot {
573-
it in richTextSpan.style
574-
}.toMutableList()
575-
} else {
576-
if (forward) {
577-
quillTextSpans[index] = richTextSpan.copy(
578-
from = newFromIndex,
579-
to = newToIndex
580-
)
581-
} else {
582-
quillTextSpans[index] = richTextSpan.copy(to = startTypeIndex - 1)
583-
quillTextSpans.add(
584-
index + 1, richTextSpan.copy(
585-
from = startTypeIndex + typedChars,
586-
to = newToIndex
526+
} else if (startTypeIndex > currentSpan.from && currentSpan.to == startTypeIndex) {
527+
quillTextSpans.removeAt(index)
528+
quillTextSpans.add(
529+
index,
530+
currentSpan.copy(
531+
from = currentSpan.from,
532+
to = startTypeIndex - 1,
533+
style = styles
534+
)
587535
)
536+
quillTextSpans.add(
537+
index + 1,
538+
currentSpan.copy(
539+
from = startTypeIndex,
540+
to = currentSpan.to + typedCharsCount,
541+
style = styles
542+
)
543+
)
544+
} else if (startTypeIndex > currentSpan.from && currentSpan.to > startTypeIndex) {
545+
quillTextSpans[index] = currentSpan.copy(
546+
to = currentSpan.to + typedCharsCount,
547+
style = styles
548+
)
549+
}
550+
} else {
551+
quillTextSpans[index] = currentSpan.copy(
552+
to = currentSpan.to + typedCharsCount,
553+
style = styles
588554
)
589-
updatedSelectedStyle =
590-
selectedStyles.filterNot { it in richTextSpan.style }.toMutableList()
591555
}
592556
}
593-
return updatedSelectedStyle
594557
}
595558

596559
private fun moveSpans(startTypeIndex: Int, by: Int) {

0 commit comments

Comments
 (0)