Skip to content

Commit

Permalink
WIP - Updated AGP dependency and fixed add characters logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 12, 2024
1 parent acaf3be commit 10c95a5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 159 deletions.
101 changes: 1 addition & 100 deletions app/src/main/assets/android-quill-sample.json
Original file line number Diff line number Diff line change
@@ -1,102 +1,3 @@
{
"spans": [
{
"insert": "Android Quill",
"attributes": {
"bold": true,
"header": 1
}
},
{
"insert": "\n"
},
{
"insert": "\nRich",
"attributes": {
"bold": true,
"header": 2
}
},
{
"insert": " text ",
"attributes": {
"header": 2
}
},
{
"insert": "editor for Android",
"attributes": {
"bold": true,
"header": 2
}
},
{
"insert": "\n"
},
{
"insert": "Quill component for Android\n",
"attributes": {
"header": 3,
"italic": true
}
},
{
"insert": "Bullet Journal",
"attributes": {
"bold": true
}
},
{
"insert": ":\nTrack personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders",
"attributes": {
"bold": true
}
},
{
"insert": "\n"
},
{
"insert": "Share your tasks and notes with teammates, and see changes as they happen in real-time, across all devices",
"attributes": {}
},
{
"insert": "\n"
},
{
"insert": "Splitting bills with friends can never be easier.",
"attributes": {
"list": "bullet"
}
},
{
"insert": "\n"
},
{
"insert": "Testing span addition to the editor.",
"attributes": {
"list": "bullet"
}
},
{
"insert": "\n"
},
{
"insert": "Start creating a group and invite your friends to join.",
"attributes": {
"list": "bullet"
}
},
{
"insert": "\n"
},
{
"insert": "Create a BuJo of Ledger type to see expense or balance summary.",
"attributes": {
"list": "bullet"
}
},
{
"insert": "\n"
}
]
"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":{}}]
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'com.android.application' version '8.2.1' apply false
id 'com.android.library' version '8.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'io.github.gradle-nexus.publish-plugin' version "1.3.0"
}
Expand Down
187 changes: 131 additions & 56 deletions editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
val endIndex = fromIndex + (span.insert?.length ?: 0) - 1

attributes?.let {
val style = mutableListOf<TextSpanStyle>()
val textSpanStyles = mutableListOf<TextSpanStyle>()

if (it.header != null) {
when (it.header) {
Expand All @@ -43,30 +43,30 @@ class QuillTextManager(quillSpan: QuillSpan) {
5 -> TextSpanStyle.H5Style
6 -> TextSpanStyle.H6Style
else -> null
}?.let { it1 -> style.add(it1) }
}?.let { headerStyle -> textSpanStyles.add(headerStyle) }
}

if (it.bold == true) {
style.add(TextSpanStyle.BoldStyle)
textSpanStyles.add(TextSpanStyle.BoldStyle)
}

if (it.italic == true) {
style.add(TextSpanStyle.ItalicStyle)
textSpanStyles.add(TextSpanStyle.ItalicStyle)
}

if (it.underline == true) {
style.add(TextSpanStyle.UnderlineStyle)
textSpanStyles.add(TextSpanStyle.UnderlineStyle)
}

if (it.list == ListType.bullet) {
style.add(TextSpanStyle.BulletStyle)
textSpanStyles.add(TextSpanStyle.BulletStyle)
}

quillTextSpans.add(
QuillTextSpan(
from = fromIndex,
to = endIndex,
style = style
style = textSpanStyles
)
)
} ?: run {
Expand Down Expand Up @@ -108,6 +108,11 @@ class QuillTextManager(quillSpan: QuillSpan) {
val nextSpan = quillTextSpans.getOrNull(index + 1)
val nextInsert =
nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) }
if (insert.last() != ' ' && insert.last() != '\n' && nextInsert != null &&
nextInsert.first() != ' ' && nextInsert.first() != '\n'
) {
insert += " "
}
if (nextInsert == " " || nextInsert == "") {
insert += nextInsert
}
Expand All @@ -131,18 +136,6 @@ class QuillTextManager(quillSpan: QuillSpan) {
return QuillSpan(groupedSpans)
}

private fun TextSpanStyle.headerLevel(): Int? {
return when (this) {
TextSpanStyle.H1Style -> 1
TextSpanStyle.H2Style -> 2
TextSpanStyle.H3Style -> 3
TextSpanStyle.H4Style -> 4
TextSpanStyle.H5Style -> 5
TextSpanStyle.H6Style -> 6
else -> null
}
}

internal fun setEditable(editable: Editable) {
editable.append(editableText)
this.editable = editable
Expand Down Expand Up @@ -216,7 +209,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
val matchingSpans = mutableListOf<TextSpanStyle>()

for (part in quillTextSpans) {
val partRange = TextRange(part.from, part.to)
val partRange = TextRange(part.from, part.to.coerceAtLeast(0))
if (selection.overlaps(partRange)) {
part.style.let {
matchingSpans.addAll(it)
Expand Down Expand Up @@ -383,35 +376,80 @@ class QuillTextManager(quillSpan: QuillSpan) {
val fromIndex = selection.min
val toIndex = selection.max

val selectedParts = quillTextSpans.filter { part ->
part.from < toIndex && part.to >= fromIndex
val selectedSpan = quillTextSpans.find {
it.from <= fromIndex && it.to >= toIndex
}
val startParts = quillTextSpans.filter { fromIndex - 1 in it.from..it.to }
val endParts = quillTextSpans.filter { toIndex in it.from..it.to }

val updateToIndex: (QuillTextSpan, Int) -> Unit = { part, index ->
val partIndex = quillTextSpans.indexOf(part)
quillTextSpans[partIndex] = part.copy(to = index)
}

val updateFromIndex: (QuillTextSpan, Int) -> Unit = { part, index ->
val partIndex = quillTextSpans.indexOf(part)
quillTextSpans[partIndex] = part.copy(from = index)
}

if (startParts.isEmpty() && endParts.isEmpty() && selectedParts.isNotEmpty()) {
quillTextSpans.add(
QuillTextSpan(
from = fromIndex,
to = toIndex - 1,
style = listOf(style)
if (selectedSpan != null) {
if (fromIndex == selectedSpan.from && toIndex < selectedSpan.to) {
val index = quillTextSpans.indexOf(selectedSpan)
quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
QuillTextSpan(
from = fromIndex,
to = toIndex - 1,
style = selectedSpan.style + listOf(style)
)
)
)
} else if (startParts.map { it.style }.any { it.contains(style) }) {
startParts.filter { it.style == style }.forEach { updateToIndex(it, toIndex - 1) }
} else if (endParts.map { it.style }.any { it.contains(style) }) {
endParts.filter { it.style == style }
.forEach { part -> updateFromIndex(part, fromIndex) }
quillTextSpans.add(
index + 1,
QuillTextSpan(
from = toIndex,
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 - 1,
style = selectedSpan.style + listOf(style)
)
)
quillTextSpans.add(
index + 2,
QuillTextSpan(
from = toIndex,
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 - 1,
style = selectedSpan.style + listOf(style)
)
)
} else {
val index = quillTextSpans.indexOf(selectedSpan)
quillTextSpans[index] =
selectedSpan.copy(style = selectedSpan.style + listOf(style))
}
} else {
quillTextSpans.add(
QuillTextSpan(
Expand All @@ -421,7 +459,6 @@ class QuillTextManager(quillSpan: QuillSpan) {
)
)
}

updateText()
}

Expand Down Expand Up @@ -486,14 +523,38 @@ class QuillTextManager(quillSpan: QuillSpan) {
selectedStyles = processSpan(it, typedCharsCount, startTypeIndex, selectedStyles)
}

selectedStyles.forEach {
quillTextSpans.add(
QuillTextSpan(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = listOf(it)
selectedStyles.forEach { style ->
val currentSpan = quillTextSpans.find {
it.from <= startTypeIndex && it.to >= startTypeIndex
}
if (currentSpan != null) {
val index = quillTextSpans.indexOf(currentSpan)
quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
currentSpan.copy(
from = currentSpan.from,
to = startTypeIndex - 1,
style = currentSpan.style
)
)
)
quillTextSpans.add(
index + 1,
currentSpan.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = currentSpan.style + listOf(style)
)
)
quillTextSpans.add(
index + 2,
currentSpan.copy(
from = startTypeIndex + typedCharsCount,
to = currentSpan.to,
style = currentSpan.style
)
)
}
}
}

Expand All @@ -513,7 +574,9 @@ class QuillTextManager(quillSpan: QuillSpan) {
if (selectedStyles.any { it in richTextSpan.style }) {
quillTextSpans[index] = richTextSpan.copy(to = newToIndex)
updatedSelectedStyle =
selectedStyles.filterNot { it in richTextSpan.style }.toMutableList()
selectedStyles.filterNot {
it in richTextSpan.style
}.toMutableList()
} else {
if (forward) {
quillTextSpans[index] = richTextSpan.copy(
Expand Down Expand Up @@ -635,6 +698,18 @@ class QuillTextManager(quillSpan: QuillSpan) {
return headers.contains(this)
}

private fun TextSpanStyle.headerLevel(): Int? {
return when (this) {
TextSpanStyle.H1Style -> 1
TextSpanStyle.H2Style -> 2
TextSpanStyle.H3Style -> 3
TextSpanStyle.H4Style -> 4
TextSpanStyle.H5Style -> 5
TextSpanStyle.H6Style -> 6
else -> null
}
}

internal inline fun <reified T> Editable.removeSpans() {
val allSpans = getSpans(0, length, T::class.java)
for (span in allSpans) {
Expand Down
Loading

0 comments on commit 10c95a5

Please sign in to comment.