Skip to content

Commit

Permalink
WIP - Phase I of managing spans completed
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 6, 2024
1 parent 6fa7452 commit 70c7cce
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
90 changes: 62 additions & 28 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,7 +6,6 @@ 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 @@ -101,16 +100,30 @@ class QuillTextManager(quillSpan: QuillSpan) {
}

val groupedSpans = mutableListOf<Span>()
quillTextSpans.forEach { span ->
val insert = editableText.substring(span.from, span.to + 1)
quillTextSpans.forEachIndexed { index, span ->
var insert = editableText.substring(span.from, span.to + 1)
if (insert == " " || insert == "") {
return@forEachIndexed
}
val nextSpan = quillTextSpans.getOrNull(index + 1)
val nextInsert = nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 1) }
if (nextInsert == " " || nextInsert == "") {
insert += nextInsert
}
val attributes = Attributes(
header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() }?.headerLevel() else null,
bold = if (span.style.contains(TextSpanStyle.BoldStyle)) true else null,
italic = if (span.style.contains(TextSpanStyle.ItalicStyle)) true else null,
underline = if (span.style.contains(TextSpanStyle.UnderlineStyle)) true else null,
list = if (span.style.contains(TextSpanStyle.BulletStyle)) ListType.bullet else null
)
groupedSpans.add(Span(insert, attributes))

// Merge consecutive spans with the same attributes into one
if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes) {
groupedSpans.last().insert += insert
} else {
groupedSpans.add(Span(insert, attributes))
}
}

return QuillSpan(groupedSpans)
Expand Down Expand Up @@ -226,12 +239,11 @@ class QuillTextManager(quillSpan: QuillSpan) {

if (!selection.collapsed) {
val fromIndex = selection.min
val toIndex = selection.max
val toIndex = selection.max - 1

val selectedParts = quillTextSpans.filter { part ->
part.from < toIndex && part.to >= fromIndex && part.style.contains(style)
}
Log.d("XXX", "selectedParts $selectedParts")
removeStylesFromSelectedPart(selectedParts, fromIndex, toIndex, style)
updateText()
}
Expand Down Expand Up @@ -313,31 +325,53 @@ class QuillTextManager(quillSpan: QuillSpan) {
val index = quillTextSpans.indexOf(part)
if (index !in quillTextSpans.indices) return@forEach

quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
QuillTextSpan(
from = part.from,
to = fromIndex - 1,
style = part.style
if (fromIndex == 0 && toIndex == part.to) {
quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style })
} else if (fromIndex == 0 && toIndex < part.to && toIndex > 0) {
quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
QuillTextSpan(
from = fromIndex,
to = toIndex,
style = part.style.filterNot { it == style }
)
)
)
quillTextSpans.add(
index + 1,
QuillTextSpan(
from = fromIndex,
to = toIndex,
style = part.style.filterNot { it == style }
quillTextSpans.add(
index + 1,
QuillTextSpan(
from = toIndex + 1,
to = part.to,
style = part.style
)
)
)
quillTextSpans.add(
index + 2,
QuillTextSpan(
from = toIndex + 1,
to = part.to,
style = part.style
} else if (fromIndex > 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 + 2,
QuillTextSpan(
from = toIndex + 1,
to = part.to,
style = part.style
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data class QuillSpan(
)

data class Span(
val insert: String?,
var insert: String?,
val attributes: Attributes? = null
)

Expand Down

0 comments on commit 70c7cce

Please sign in to comment.