Skip to content

Commit

Permalink
WIP - Removed old files and passed first 3 test scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 6, 2024
1 parent 70c7cce commit acaf3be
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 747 deletions.
110 changes: 0 additions & 110 deletions app/src/main/assets/sample-data.json

This file was deleted.

This file was deleted.

This file was deleted.

68 changes: 40 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 @@ -93,7 +93,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
val quillGroupedSpans = quillTextSpans.groupBy {
it.from to it.to
}
val quillTextSpans = quillGroupedSpans.map {(fromTo, spanList) ->
val quillTextSpans = quillGroupedSpans.map { (fromTo, spanList) ->
val (from, to) = fromTo
val uniqueStyles = spanList.map { it.style }.flatten().distinct()
QuillTextSpan(from, to, uniqueStyles)
Expand All @@ -106,20 +106,22 @@ class QuillTextManager(quillSpan: QuillSpan) {
return@forEachIndexed
}
val nextSpan = quillTextSpans.getOrNull(index + 1)
val nextInsert = nextSpan?.let { editableText.substring(nextSpan.from, nextSpan.to + 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,
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
)

// Merge consecutive spans with the same attributes into one
if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes) {
if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes && attributes.list == null) {
groupedSpans.last().insert += insert
} else {
groupedSpans.add(Span(insert, attributes))
Expand All @@ -130,7 +132,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
}

private fun TextSpanStyle.headerLevel(): Int? {
return when(this) {
return when (this) {
TextSpanStyle.H1Style -> 1
TextSpanStyle.H2Style -> 2
TextSpanStyle.H3Style -> 3
Expand Down Expand Up @@ -398,14 +400,26 @@ class QuillTextManager(quillSpan: QuillSpan) {
}

if (startParts.isEmpty() && endParts.isEmpty() && selectedParts.isNotEmpty()) {
quillTextSpans.add(QuillTextSpan(from = fromIndex, to = toIndex - 1, style = listOf(style)))
quillTextSpans.add(
QuillTextSpan(
from = fromIndex,
to = toIndex - 1,
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) }
} else {
quillTextSpans.add(QuillTextSpan(from = fromIndex, to = toIndex - 1, style = listOf(style)))
quillTextSpans.add(
QuillTextSpan(
from = fromIndex,
to = toIndex - 1,
style = listOf(style)
)
)
}

updateText()
Expand Down Expand Up @@ -444,7 +458,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
currentStyles.clear()
}

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

moveSpans(startTypeIndex, typedCharsCount)

Expand All @@ -454,21 +468,23 @@ class QuillTextManager(quillSpan: QuillSpan) {

startParts.filter { it !in commonParts }
.forEach {
if (selectedStyles == it.style) {
if (selectedStyles.any { selectedStyle -> selectedStyle in it.style }) {
val index = quillTextSpans.indexOf(it)
quillTextSpans[index] = it.copy(to = it.to + typedCharsCount)
selectedStyles.forEach {style ->
if (style in it.style) {
selectedStyles.remove(style)
}
}
selectedStyles =
selectedStyles.filterNot { style -> style in it.style }.toMutableList()
}
}

endParts.filter { it !in commonParts }
.forEach { processSpan(it, typedCharsCount, startTypeIndex, selectedStyles, true) }
.forEach {
selectedStyles =
processSpan(it, typedCharsCount, startTypeIndex, selectedStyles, true)
}

commonParts.forEach { processSpan(it, typedCharsCount, startTypeIndex, selectedStyles) }
commonParts.forEach {
selectedStyles = processSpan(it, typedCharsCount, startTypeIndex, selectedStyles)
}

selectedStyles.forEach {
quillTextSpans.add(
Expand All @@ -487,19 +503,17 @@ class QuillTextManager(quillSpan: QuillSpan) {
startTypeIndex: Int,
selectedStyles: MutableList<TextSpanStyle>,
forward: Boolean = false
) {
): MutableList<TextSpanStyle> {

val newFromIndex = richTextSpan.from + typedChars
val newToIndex = richTextSpan.to + typedChars
var updatedSelectedStyle = selectedStyles

val index = quillTextSpans.indexOf(richTextSpan)
if (selectedStyles == richTextSpan.style) {
if (selectedStyles.any { it in richTextSpan.style }) {
quillTextSpans[index] = richTextSpan.copy(to = newToIndex)
selectedStyles.forEach { style ->
if (style in richTextSpan.style) {
selectedStyles.remove(style)
}
}
updatedSelectedStyle =
selectedStyles.filterNot { it in richTextSpan.style }.toMutableList()
} else {
if (forward) {
quillTextSpans[index] = richTextSpan.copy(
Expand All @@ -514,13 +528,11 @@ class QuillTextManager(quillSpan: QuillSpan) {
to = newToIndex
)
)
selectedStyles.forEach {style ->
if (style in richTextSpan.style) {
selectedStyles.remove(style)
}
}
updatedSelectedStyle =
selectedStyles.filterNot { it in richTextSpan.style }.toMutableList()
}
}
return updatedSelectedStyle
}

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

0 comments on commit acaf3be

Please sign in to comment.