Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Oct 16, 2023
1 parent 162d708 commit b1301d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.canopas.editor.ui.data

const val TYPE_IMAGE = "image"
const val TYPE_VIDEO = "video"
const val TYPE_TEXT = "text"


sealed interface EditorAttribute {
val type: String?

data class ImageAttribute(
val url: String, override val type: String = "image",
val url: String, override val type: String = TYPE_IMAGE,
) : EditorAttribute

data class VideoAttribute(
val url: String,
override val type: String = "video",
override val type: String = TYPE_VIDEO,
) : EditorAttribute

data class TextAttribute(
val content: RichTextState = RichTextState(), override val type: String = "text",
val content: RichTextState = RichTextState(), override val type: String = TYPE_TEXT,
) : EditorAttribute {

val isEmpty get() = content.text.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ class RichEditorState internal constructor(

fun hasStyle(style: SpanStyle): Boolean {
return attributes.filterIndexed { index, value ->
focusedAttributeIndex == index && value.type == "text"
focusedAttributeIndex == index && value.type == TYPE_TEXT
}.any { (it as TextAttribute).content.hasStyle(style) }
}

private fun getRichTexts(): List<TextAttribute> =
attributes.filter { it.type == "text" }.map { it as TextAttribute }
attributes.filter { it.type == TYPE_TEXT }.map { it as TextAttribute }

fun toggleStyle(style: SpanStyle): RichEditorState {
attributes.forEach { value ->
if (value.type == "text") {
if (value.type == TYPE_TEXT) {
((value as TextAttribute)).content.toggleStyle(style)
}
}
Expand All @@ -95,7 +95,7 @@ class RichEditorState internal constructor(

fun updateStyle(style: SpanStyle): RichEditorState {
attributes.forEach { value ->
if (value.type == "text") {
if (value.type == TYPE_TEXT) {
((value as TextAttribute)).content.updateStyle(style)
}
}
Expand All @@ -120,7 +120,7 @@ class RichEditorState internal constructor(
}

val value = attributes.last()
if (value.type == "text" && (value as TextAttribute).isEmpty) {
if (value.type == TYPE_TEXT && (value as TextAttribute).isEmpty) {
add(attribute, attributes.lastIndex)
return
}
Expand Down Expand Up @@ -168,7 +168,7 @@ class RichEditorState internal constructor(
}
if (upIndex != -1 && upIndex < attributes.size) {
val item = attributes[upIndex]
if (item.type != "text" && upIndex == focusedAttributeIndex) {
if (item.type != TYPE_TEXT && upIndex == focusedAttributeIndex) {
handleRemoveAndMerge(upIndex)
return
} else {
Expand All @@ -183,7 +183,7 @@ class RichEditorState internal constructor(
val nextItem = attributes.elementAtOrNull(index + 1) ?: return
clearFocus()
remove(index)
if (previousItem.type == "text" && nextItem.type == "text") {
if (previousItem.type == TYPE_TEXT && nextItem.type == TYPE_TEXT) {
if (!(nextItem as TextAttribute).isEmpty) {
(previousItem as TextAttribute).content.merge(nextItem.content)
} else {
Expand Down

0 comments on commit b1301d4

Please sign in to comment.