Skip to content

Commit

Permalink
Add bullets support - stage 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Feb 21, 2024
1 parent 781f865 commit d74586b
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.canopas.editor.ui.data

import com.canopas.editor.ui.data.QuillTextManager.Companion.headerLevel
import com.canopas.editor.ui.data.QuillTextManager.Companion.isHeaderStyle
import com.canopas.editor.ui.model.Attributes
import com.canopas.editor.ui.model.ListType
import com.canopas.editor.ui.model.QuillSpan
import com.canopas.editor.ui.model.QuillTextSpan
import com.canopas.editor.ui.model.Span
import com.canopas.editor.ui.parser.QuillDefaultAdapter
import com.canopas.editor.ui.parser.QuillEditorAdapter
import com.canopas.editor.ui.utils.TextSpanStyle
Expand All @@ -21,7 +27,7 @@ class QuillEditorState internal constructor(
}

fun output(): String {
return adapter.decode(manager.richText)
return adapter.decode(getRichText())
}

fun reset() {
Expand All @@ -38,6 +44,79 @@ class QuillEditorState internal constructor(
manager.setStyle(style)
}

private fun getRichText() : QuillSpan {
val quillGroupedSpans = manager.quillTextSpans.groupBy { it.from to it.to }
val quillTextSpans =
quillGroupedSpans.map { (fromTo, spanList) ->
val (from, to) = fromTo
val uniqueStyles = spanList.map { it.style }.flatten().distinct()
QuillTextSpan(from, to, uniqueStyles)
}

val groupedSpans = mutableListOf<Span>()
quillTextSpans.forEachIndexed { index, span ->
var insert = manager.editableText.substring(span.from, span.to + 1)
if (insert == " " || insert == "") {
return@forEachIndexed
}
val nextSpan = quillTextSpans.getOrNull(index + 1)
val previousSpan = quillTextSpans.getOrNull(index - 1)
val nextInsert =
nextSpan?.let { manager.editableText.substring(nextSpan.from, nextSpan.to + 1) }
if (nextInsert == " " || nextInsert == "") {
insert += nextInsert
}
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,
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
)

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

if (
previousSpan?.style?.contains(TextSpanStyle.BulletStyle) == true &&
nextInsert == "\n" &&
!insert.contains("\n")
) {
insert += "\n"
}
if (
insert == "\n" &&
span.style.contains(TextSpanStyle.BulletStyle) &&
previousSpan?.style?.contains(TextSpanStyle.BulletStyle) == true &&
nextSpan?.style?.contains(TextSpanStyle.BulletStyle) == true
) {
return@forEachIndexed
}
insert = insert.replace("\u200B", "")
// Merge consecutive spans with the same attributes into one
if (
groupedSpans.isNotEmpty() &&
groupedSpans.last().attributes == attributes &&
(attributes.list == null ||
(groupedSpans.last().insert?.contains('\n') == false))
) {
groupedSpans.last().insert += insert
} else {
groupedSpans.add(Span(insert, attributes))
}
}

return QuillSpan(groupedSpans)
}

class Builder {
private var adapter: QuillEditorAdapter = QuillDefaultAdapter()
private var input: String = ""
Expand Down
Loading

0 comments on commit d74586b

Please sign in to comment.