Skip to content

Commit

Permalink
Fix characters addition
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 13, 2024
1 parent cac4960 commit d14563b
Showing 1 changed file with 115 additions and 76 deletions.
191 changes: 115 additions & 76 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 @@ -123,7 +122,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
if (!previousInsert.isNullOrEmpty() && previousInsert.last() == ' ') {
insert += ' '
}
val attributes = Attributes(
var 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,
Expand All @@ -132,6 +131,10 @@ class QuillTextManager(quillSpan: QuillSpan) {
list = if (span.style.contains(TextSpanStyle.BulletStyle)) ListType.bullet else null
)

if (insert == "\n") {
attributes = Attributes()
}

// Merge consecutive spans with the same attributes into one
if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes && attributes.list == null) {
groupedSpans.last().insert += insert
Expand Down Expand Up @@ -240,10 +243,91 @@ class QuillTextManager(quillSpan: QuillSpan) {
val fromIndex = selection.min
val toIndex = selection.max - 1

val selectedParts = quillTextSpans.filter { part ->
part.from < toIndex && part.to >= fromIndex && part.style.contains(style)
val selectedSpan = quillTextSpans.find {
it.from <= fromIndex && it.to >= toIndex
}
if (selectedSpan != null) {
if (fromIndex == selectedSpan.from && toIndex == selectedSpan.to) {
val index = quillTextSpans.indexOf(selectedSpan)
quillTextSpans[index] =
selectedSpan.copy(style = selectedSpan.style.filterNot { it == style })
} else {
if (fromIndex == selectedSpan.from && toIndex < selectedSpan.to) {
val index = quillTextSpans.indexOf(selectedSpan)
quillTextSpans.removeAt(index)
quillTextSpans.add(
index,
QuillTextSpan(
from = fromIndex,
to = toIndex,
style = selectedSpan.style.filterNot { it == style }
)
)
quillTextSpans.add(
index + 1,
QuillTextSpan(
from = toIndex + 1,
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,
style = selectedSpan.style.filterNot { it == style }
)
)
quillTextSpans.add(
index + 2,
QuillTextSpan(
from = toIndex + 1,
to = selectedSpan.to,
style = selectedSpan.style
)
)
} else if (fromIndex > 0) {
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,
style = selectedSpan.style.filterNot { it == style }
)
)
quillTextSpans.add(
index + 2,
QuillTextSpan(
from = toIndex + 1,
to = selectedSpan.to,
style = selectedSpan.style
)
)
}
}
}
removeStylesFromSelectedPart(selectedParts, fromIndex, toIndex, style)
updateText()
}
}
Expand Down Expand Up @@ -310,77 +394,6 @@ class QuillTextManager(quillSpan: QuillSpan) {
})
}

private fun removeStylesFromSelectedPart(
selectedParts: List<QuillTextSpan>,
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 == part.from && toIndex == part.to) {
quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style })
} 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 + 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
)
)
}
}
}

private fun applyStylesToSelectedText(style: TextSpanStyle) {
if (selection.collapsed) return

Expand Down Expand Up @@ -618,6 +631,32 @@ class QuillTextManager(quillSpan: QuillSpan) {
}
}
}
if (currentSpan == null) {
val lastSpan = quillTextSpans.lastOrNull()
if (lastSpan != null) {
val lastStyles = lastSpan.style
if (lastStyles == selectedStyles) {
quillTextSpans[quillTextSpans.lastIndex] =
lastSpan.copy(to = lastSpan.to + typedCharsCount)
} else {
quillTextSpans.add(
QuillTextSpan(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
}
} else {
quillTextSpans.add(
QuillTextSpan(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
}
}
}

private fun moveSpans(startTypeIndex: Int, by: Int) {
Expand Down

0 comments on commit d14563b

Please sign in to comment.