Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Mar 7, 2024
1 parent e85286d commit 91c4849
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
94 changes: 55 additions & 39 deletions editor/src/main/java/com/canopas/editor/ui/data/QuillTextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class QuillTextManager(quillSpan: QuillSpan) {
getRichSpanListByTextRange(selection).distinct()
}

updateSpanStyle(currentStyles)
}

private fun updateSpanStyle(currentStyles: List<TextSpanStyle>) {
val currentSpan = quillTextSpans.findLast {
it.from <= selection.min - 2 && it.to >= selection.min - 2 && it.style.contains(
TextSpanStyle.BulletStyle
Expand Down Expand Up @@ -395,48 +399,60 @@ class QuillTextManager(quillSpan: QuillSpan) {
when {
span.style == selectedStyles -> {
if (isBulletStyle && newValue.getOrNull(startTypeIndex) == '\n') {
if (newValue.getOrNull(startTypeIndex - 1) != '\n' && startTypeIndex == to) {
quillTextSpans.add(
index + 1,
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
quillTextSpans.add(
index + 2,
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = selectedStyles
)
)
} else {
if (startTypeIndex in (from + 1) until to) {
val newSpans = mutableListOf<QuillTextSpan>()
newSpans.add(span.copy(to = startTypeIndex - 1, style = styles))
newSpans.add(
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
if (newValue.getOrNull(startTypeIndex - 1) != '\n') {
when (startTypeIndex) {
to -> {
quillTextSpans.add(
index + 1,
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
)
newSpans.add(
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = styles
quillTextSpans.add(
index + 2,
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = selectedStyles
)
)
)
quillTextSpans.removeAt(index)
quillTextSpans.addAll(index, newSpans)
} else {
val updatedSpan = span.copy(to = to + typedCharsCount, style = selectedStyles)
quillTextSpans[index] = updatedSpan
quillTextSpans.add(index + 1, updatedSpan)
}

in (from + 1) until to -> {
val newSpans = mutableListOf<QuillTextSpan>()
newSpans.add(span.copy(to = startTypeIndex - 1, style = styles))
newSpans.add(
span.copy(
from = startTypeIndex,
to = startTypeIndex + typedCharsCount - 1,
style = selectedStyles
)
)
newSpans.add(
span.copy(
from = startTypeIndex + typedCharsCount,
to = to + typedCharsCount,
style = styles
)
)
quillTextSpans.removeAt(index)
quillTextSpans.addAll(index, newSpans)
}

else -> {
val updatedSpan =
span.copy(to = to + typedCharsCount, style = selectedStyles)
quillTextSpans[index] = updatedSpan
quillTextSpans.add(index + 1, updatedSpan)
}
}
} else {
quillTextSpans[index] =
span.copy(to = to + typedCharsCount, style = styles.filterNot {
it == TextSpanStyle.BulletStyle
})
}
} else {
quillTextSpans[index] = span.copy(to = to + typedCharsCount, style = styles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type

class QuillRichTextStateAdapter : JsonSerializer<Span>, JsonDeserializer<Span> {
class RichTextStateAdapter : JsonSerializer<Span>, JsonDeserializer<Span> {
override fun serialize(
src: Span?,
typeOfSrc: Type?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import com.google.gson.GsonBuilder

class QuillJsonEditorParser : QuillEditorAdapter {
class TestEditorParser : QuillEditorAdapter {

private val gson: Gson = GsonBuilder()
.registerTypeAdapter(Span::class.java, QuillRichTextStateAdapter())
.registerTypeAdapter(Span::class.java, RichTextStateAdapter())
.create()

override fun encode(input: String): QuillSpan {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.text.Editable
import androidx.compose.ui.text.TextRange
import androidx.test.core.app.ApplicationProvider
import com.canopas.editor.MainCoroutineRule
import com.canopas.editor.jsonparser.QuillJsonEditorParser
import com.canopas.editor.jsonparser.TestEditorParser
import com.canopas.editor.ui.data.QuillEditorState
import com.canopas.editor.ui.model.QuillTextSpan
import com.canopas.editor.ui.utils.TextSpanStyle
Expand Down Expand Up @@ -35,7 +35,7 @@ class QuillTextManagerTest {
editableInstance = Editable.Factory.getInstance()
quillEditorState = QuillEditorState.Builder()
.setInput(input)
.adapter(QuillJsonEditorParser())
.adapter(TestEditorParser())
.build()
sampleSpansListSize = quillEditorState.manager.quillTextSpans.size
}
Expand Down

0 comments on commit 91c4849

Please sign in to comment.