Skip to content

Commit

Permalink
Use-Case Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 13, 2024
1 parent f1a58ff commit cac4960
Showing 1 changed file with 116 additions and 52 deletions.
168 changes: 116 additions & 52 deletions editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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
Expand Down Expand Up @@ -108,9 +109,20 @@ class QuillTextManager(quillSpan: QuillSpan) {
val nextSpan = quillTextSpans.getOrNull(index + 1)
val nextInsert =
nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) }
val previousSpan = quillTextSpans.getOrNull(index - 1)
val previousInsert =
previousSpan?.let {
editableText.substring(
previousSpan.from,
previousSpan.to + 1
)
}
if (nextInsert == " " || nextInsert == "") {
insert += nextInsert
}
if (!previousInsert.isNullOrEmpty() && previousInsert.last() == ' ') {
insert += ' '
}
val attributes = Attributes(
header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() }
?.headerLevel() else null,
Expand Down Expand Up @@ -202,14 +214,11 @@ class QuillTextManager(quillSpan: QuillSpan) {

private fun getRichSpanListByTextRange(selection: TextRange): List<TextSpanStyle> {
val matchingSpans = mutableListOf<TextSpanStyle>()

for (part in quillTextSpans) {
val partRange = TextRange(part.from, part.to.coerceAtLeast(0))
if (selection.overlaps(partRange)) {
part.style.let {
matchingSpans.addAll(it)
}
}
val currentSpan = quillTextSpans.find {
it.from <= selection.min && it.to >= selection.min
}
if (currentSpan != null) {
matchingSpans.addAll(currentSpan.style)
}
return matchingSpans
}
Expand Down Expand Up @@ -306,24 +315,36 @@ class QuillTextManager(quillSpan: QuillSpan) {
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 == 0 && toIndex == part.to) {
if (fromIndex == part.from && toIndex == part.to) {
quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style })
} else if (fromIndex == 0 && toIndex < part.to && toIndex > 0) {
} 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 + 1,
index + 2,
QuillTextSpan(
from = toIndex + 1,
to = part.to,
Expand Down Expand Up @@ -487,71 +508,114 @@ class QuillTextManager(quillSpan: QuillSpan) {
currentStyles.clear()
}

val selectedStyles = currentStyles.distinct().toMutableList()
val selectedStyles = currentStyles.distinct()

moveSpans(startTypeIndex, typedCharsCount)

val currentSpan = quillTextSpans.find {
it.from <= startTypeIndex && it.to >= startTypeIndex
}

if (currentSpan != null) {
val index = quillTextSpans.indexOf(currentSpan)
val styles = (currentSpan.style + selectedStyles).distinct()
if (!currentSpan.style.contains(TextSpanStyle.BulletStyle)) {
if (startTypeIndex == currentSpan.from && currentSpan.to == startTypeIndex) {
quillTextSpans[index] = currentSpan.copy(
from = currentSpan.from,
to = currentSpan.to + typedCharsCount,
style = styles
)
} else if (startTypeIndex == currentSpan.from && currentSpan.to > startTypeIndex) {
currentSpan?.let { span ->
val index = quillTextSpans.indexOf(span)
val styles = (span.style + selectedStyles).distinct()

val from = span.from
val to = span.to
// TODO: Add support for bullet style
val isBulletStyle = TextSpanStyle.BulletStyle in styles

when {
span.style == selectedStyles -> {
val updatedSpan = span.copy(to = to + typedCharsCount, style = styles)
quillTextSpans[index] = updatedSpan
}

span.style != selectedStyles -> {
quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
currentSpan.copy(
from = currentSpan.from,
to = startTypeIndex + typedCharsCount - 1,
style = styles
if (startTypeIndex == from) {
quillTextSpans.add(
index,
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
)
quillTextSpans.add(
index + 1,
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = span.style
)
)
} else {
quillTextSpans.add(
index,
span.copy(to = startTypeIndex - 1, style = span.style)
)
quillTextSpans.add(
index + 1,
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount,
style = selectedStyles
)
)
quillTextSpans.add(
index + 2,
span.copy(
from = startTypeIndex + typedCharsCount + 1,
to = to + typedCharsCount,
style = span.style
)
)
}
}

startTypeIndex == from && to == startTypeIndex -> {
quillTextSpans[index] =
span.copy(to = to + typedCharsCount, style = selectedStyles)
}

startTypeIndex == from && to > startTypeIndex -> {
quillTextSpans[index] =
span.copy(to = startTypeIndex + typedCharsCount - 1, style = selectedStyles)
quillTextSpans.add(
index + 1,
currentSpan.copy(
span.copy(
from = startTypeIndex + typedCharsCount,
to = currentSpan.to + typedCharsCount,
to = to + typedCharsCount,
style = styles
)
)
} else if (startTypeIndex > currentSpan.from && currentSpan.to == startTypeIndex) {
}

startTypeIndex > from && to == startTypeIndex -> {
quillTextSpans[index] = span.copy(to = to + typedCharsCount, style = styles)
}

startTypeIndex in (from + 1) until to -> {
quillTextSpans.removeAt(index)
quillTextSpans.add(index, span.copy(to = startTypeIndex - 1, style = styles))
quillTextSpans.add(
index,
currentSpan.copy(
from = currentSpan.from,
to = startTypeIndex - 1,
style = styles
index + 1,
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
quillTextSpans.add(
index + 1,
currentSpan.copy(
from = startTypeIndex,
to = currentSpan.to + typedCharsCount,
index + 2,
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = styles
)
)
} else if (startTypeIndex > currentSpan.from && currentSpan.to > startTypeIndex) {
quillTextSpans[index] = currentSpan.copy(
to = currentSpan.to + typedCharsCount,
style = styles
)
}
} else {
quillTextSpans[index] = currentSpan.copy(
to = currentSpan.to + typedCharsCount,
style = styles
)
}
}
}
Expand Down

0 comments on commit cac4960

Please sign in to comment.