Skip to content

Commit

Permalink
Refactored so we dont need to expose things
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Nov 22, 2024
1 parent e4488cb commit f849418
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 395 deletions.
99 changes: 6 additions & 93 deletions richeditor-compose/api/android/richeditor-compose.api

Large diffs are not rendered by default.

99 changes: 6 additions & 93 deletions richeditor-compose/api/desktop/richeditor-compose.api

Large diffs are not rendered by default.

104 changes: 6 additions & 98 deletions richeditor-compose/api/richeditor-compose.klib.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import com.mohamedrejeb.richeditor.utils.isSpecifiedFieldsEquals
/**
* A rich span is a part of a rich paragraph.
*/
public class RichSpan(
internal class RichSpan(
internal val key: Int? = null,
public val children: MutableList<RichSpan> = mutableListOf(),
public var paragraph: RichParagraph,
public var parent: RichSpan? = null,
public var text: String = "",
public var textRange: TextRange = TextRange(start = 0, end = 0),
public var spanStyle: SpanStyle = SpanStyle(),
public var richSpanStyle: RichSpanStyle = RichSpanStyle.Default,
val children: MutableList<RichSpan> = mutableListOf(),
var paragraph: RichParagraph,
var parent: RichSpan? = null,
var text: String = "",
var textRange: TextRange = TextRange(start = 0, end = 0),
var spanStyle: SpanStyle = SpanStyle(),
var richSpanStyle: RichSpanStyle = RichSpanStyle.Default,
) {
/**
* Return the full text range of the rich span.
Expand Down Expand Up @@ -49,7 +49,7 @@ public class RichSpan(
*
* @return The full span style of the rich span
*/
public val fullSpanStyle: SpanStyle get() {
val fullSpanStyle: SpanStyle get() {
var spanStyle = this.spanStyle
var parent = this.parent

Expand All @@ -61,7 +61,7 @@ public class RichSpan(
return spanStyle
}

public val fullStyle: RichSpanStyle get() {
val fullStyle: RichSpanStyle get() {
var style = this.richSpanStyle
var parent = this.parent

Expand All @@ -73,7 +73,7 @@ public class RichSpan(
return style
}

public val before: RichSpan? get() {
val before: RichSpan? get() {
val parentChildren = parent?.children ?: paragraph.children
val index = parentChildren.indexOf(this)

Expand All @@ -95,7 +95,7 @@ public class RichSpan(
*
* @return The next rich span or null if the rich span is the last in the paragraph
*/
public val after: RichSpan? get() {
val after: RichSpan? get() {
if (children.isNotEmpty())
return children.first()

Expand Down Expand Up @@ -132,7 +132,7 @@ public class RichSpan(
*
* @return True if the rich span is the first in the paragraph, false otherwise
*/
public val isFirstInParagraph: Boolean get() {
val isFirstInParagraph: Boolean get() {
var current: RichSpan
var parent: RichSpan = this

Expand All @@ -151,7 +151,7 @@ public class RichSpan(
*
* @return True if the rich span is the last in the paragraph, false otherwise
*/
public val isLastInParagraph: Boolean get() {
val isLastInParagraph: Boolean get() {
var current: RichSpan
var parent: RichSpan = this

Expand All @@ -173,15 +173,15 @@ public class RichSpan(
*
* @return True if the rich span is empty, false otherwise
*/
public fun isEmpty(): Boolean = text.isEmpty() && isChildrenEmpty() && richSpanStyle !is RichSpanStyle.Image
fun isEmpty(): Boolean = text.isEmpty() && isChildrenEmpty() && richSpanStyle !is RichSpanStyle.Image

/**
* Check if the rich span is blank.
* A rich span is blank if its text is blank and its children are blank
*
* @return True if the rich span is blank, false otherwise
*/
public fun isBlank(): Boolean = text.isBlank() && isChildrenBlank() && richSpanStyle !is RichSpanStyle.Image
fun isBlank(): Boolean = text.isBlank() && isChildrenBlank() && richSpanStyle !is RichSpanStyle.Image

/**
* Check if the rich span children are empty
Expand Down Expand Up @@ -346,7 +346,7 @@ public class RichSpan(
* @return A pair of the offset and the rich span or null if the rich span is not found
*/
@OptIn(ExperimentalRichTextApi::class)
public fun getRichSpanByTextIndex(
fun getRichSpanByTextIndex(
textIndex: Int,
offset: Int = 0,
ignoreCustomFiltering: Boolean = false
Expand Down Expand Up @@ -400,7 +400,7 @@ public class RichSpan(
* @param offset The offset of the text range
* @return The rich span list
*/
public fun getRichSpanListByTextRange(
fun getRichSpanListByTextRange(
searchTextRange: TextRange,
offset: Int = 0,
): Pair<Int, List<RichSpan>> {
Expand Down Expand Up @@ -431,7 +431,7 @@ public class RichSpan(
/**
* Removes the current rich span from its parent and clears its text.
*/
public fun remove() {
fun remove() {
text = ""
parent?.children?.remove(this)
}
Expand All @@ -442,7 +442,7 @@ public class RichSpan(
* @param removeTextRange The text range to remove
* @return The rich span with the removed text range or null if the rich span is empty
*/
public fun removeTextRange(
fun removeTextRange(
removeTextRange: TextRange,
offset: Int,
): Pair<Int, RichSpan?> {
Expand Down Expand Up @@ -509,7 +509,7 @@ public class RichSpan(
* @param spanStyle The span style
* @return The closest parent rich span or null if not found
*/
public fun getClosestRichSpan(spanStyle: SpanStyle, newRichSpanStyle: RichSpanStyle): RichSpan? {
fun getClosestRichSpan(spanStyle: SpanStyle, newRichSpanStyle: RichSpanStyle): RichSpan? {
if (
spanStyle.isSpecifiedFieldsEquals(this.fullSpanStyle, strict = true) &&
newRichSpanStyle::class == richSpanStyle::class
Expand All @@ -523,14 +523,14 @@ public class RichSpan(
*
* @param newParagraph The new paragraph
*/
public fun updateChildrenParagraph(newParagraph: RichParagraph) {
fun updateChildrenParagraph(newParagraph: RichParagraph) {
children.fastForEach { childRichSpan ->
childRichSpan.paragraph = newParagraph
childRichSpan.updateChildrenParagraph(newParagraph)
}
}

public fun removeEmptyChildren() {
fun removeEmptyChildren() {
val toRemoveIndices = mutableListOf<Int>()

children.fastForEachIndexed { i, richSpan ->
Expand All @@ -545,7 +545,7 @@ public class RichSpan(
}
}

public fun copy(
fun copy(
newParagraph: RichParagraph = paragraph,
): RichSpan {
val newSpan = RichSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class RichTextState internal constructor(

internal var singleParagraphMode by mutableStateOf(false)

public var textLayoutResult: TextLayoutResult? by mutableStateOf(null)
internal var textLayoutResult: TextLayoutResult? by mutableStateOf(null)
private set

private var lastPressPosition: Offset? by mutableStateOf(null)
Expand Down Expand Up @@ -1734,24 +1734,24 @@ public class RichTextState internal constructor(

// If the new paragraph is empty apply style depending on the config
if (tempTextFieldValue.selection.collapsed && newParagraph.isEmpty()) {
val newParagraphFirstRichSpan = newParagraph.getFirstNonEmptyChild()

val isSelectionAtNewRichSpan =
newParagraphFirstRichSpan?.textRange?.min == tempTextFieldValue.selection.min - 1

// Check if the cursor is at the new paragraph
if (
(!config.preserveStyleOnEmptyLine || richSpan.paragraph.isEmpty()) &&
isSelectionAtNewRichSpan
) {
newParagraphFirstRichSpan.spanStyle = SpanStyle()
newParagraphFirstRichSpan.richSpanStyle = RichSpanStyle.Default
} else if (
config.preserveStyleOnEmptyLine &&
isSelectionAtNewRichSpan
) {
newParagraphFirstRichSpan.spanStyle = currentSpanStyle
newParagraphFirstRichSpan.richSpanStyle = currentRichSpanStyle
newParagraph.getFirstNonEmptyChild()?.let { newParagraphFirstRichSpan ->
val isSelectionAtNewRichSpan =
newParagraphFirstRichSpan.textRange.min == tempTextFieldValue.selection.min - 1

// Check if the cursor is at the new paragraph
if (
(!config.preserveStyleOnEmptyLine || richSpan.paragraph.isEmpty()) &&
isSelectionAtNewRichSpan
) {
newParagraphFirstRichSpan.spanStyle = SpanStyle()
newParagraphFirstRichSpan.richSpanStyle = RichSpanStyle.Default
} else if (
config.preserveStyleOnEmptyLine &&
isSelectionAtNewRichSpan
) {
newParagraphFirstRichSpan.spanStyle = currentSpanStyle
newParagraphFirstRichSpan.richSpanStyle = currentRichSpanStyle
}
}
}

Expand Down Expand Up @@ -2722,7 +2722,7 @@ public class RichTextState internal constructor(
return richSpan
}

public fun getRichSpanByOffset(offset: Offset): RichSpan? {
internal fun getRichSpanByOffset(offset: Offset): RichSpan? {
this.textLayoutResult?.let { textLayoutResult ->
val position = textLayoutResult.getOffsetForPosition(offset)
return getRichSpanByTextIndex(position, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import com.mohamedrejeb.richeditor.paragraph.type.ParagraphType
import com.mohamedrejeb.richeditor.paragraph.type.ParagraphType.Companion.startText
import com.mohamedrejeb.richeditor.ui.test.getRichTextStyleTreeRepresentation

public class RichParagraph(
internal val key: Int = 0,
internal val children: MutableList<RichSpan> = mutableListOf(),
internal var paragraphStyle: ParagraphStyle = DefaultParagraphStyle,
internal var type: ParagraphType = DefaultParagraph(),
internal class RichParagraph(
val key: Int = 0,
val children: MutableList<RichSpan> = mutableListOf(),
var paragraphStyle: ParagraphStyle = DefaultParagraphStyle,
var type: ParagraphType = DefaultParagraph(),
) {

@OptIn(ExperimentalRichTextApi::class)
public fun getRichSpanByTextIndex(
fun getRichSpanByTextIndex(
paragraphIndex: Int,
textIndex: Int,
offset: Int = 0,
Expand Down Expand Up @@ -64,7 +64,7 @@ public class RichParagraph(
}

@OptIn(ExperimentalRichTextApi::class)
public fun getRichSpanListByTextRange(
fun getRichSpanListByTextRange(
paragraphIndex: Int,
searchTextRange: TextRange,
offset: Int = 0,
Expand Down Expand Up @@ -101,7 +101,7 @@ public class RichParagraph(
return index to richSpanList
}

public fun removeTextRange(
fun removeTextRange(
textRange: TextRange,
offset: Int,
): RichParagraph? {
Expand Down Expand Up @@ -131,7 +131,7 @@ public class RichParagraph(
return this
}

public fun isEmpty(ignoreStartRichSpan: Boolean = true): Boolean {
fun isEmpty(ignoreStartRichSpan: Boolean = true): Boolean {
if (!ignoreStartRichSpan && !type.startRichSpan.isEmpty()) return false

if (children.isEmpty()) return true
Expand All @@ -141,9 +141,9 @@ public class RichParagraph(
return true
}

public fun isNotEmpty(ignoreStartRichSpan: Boolean = true): Boolean = !isEmpty(ignoreStartRichSpan)
fun isNotEmpty(ignoreStartRichSpan: Boolean = true): Boolean = !isEmpty(ignoreStartRichSpan)

public fun isBlank(ignoreStartRichSpan: Boolean = true): Boolean {
fun isBlank(ignoreStartRichSpan: Boolean = true): Boolean {
if (!ignoreStartRichSpan && !type.startRichSpan.isBlank()) return false

if (children.isEmpty()) return true
Expand All @@ -153,9 +153,9 @@ public class RichParagraph(
return true
}

public fun isNotBlank(ignoreStartRichSpan: Boolean = true): Boolean = !isBlank(ignoreStartRichSpan)
fun isNotBlank(ignoreStartRichSpan: Boolean = true): Boolean = !isBlank(ignoreStartRichSpan)

public fun getFirstNonEmptyChild(offset: Int = -1): RichSpan? {
fun getFirstNonEmptyChild(offset: Int = -1): RichSpan? {
children.fastForEach { richSpan ->
if (richSpan.text.isNotEmpty()) {
if (offset != -1)
Expand All @@ -181,7 +181,7 @@ public class RichParagraph(
/**
* Trim the rich paragraph
*/
public fun trim() {
fun trim() {
val isEmpty = trimStart()
if (!isEmpty)
trimEnd()
Expand All @@ -192,7 +192,7 @@ public class RichParagraph(
*
* @return True if the rich paragraph is empty after trimming, false otherwise
*/
public fun trimStart(): Boolean {
fun trimStart(): Boolean {
children.fastForEach { richSpan ->
val isEmpty = richSpan.trimStart()

Expand All @@ -208,7 +208,7 @@ public class RichParagraph(
*
* @return True if the rich paragraph is empty after trimming, false otherwise
*/
public fun trimEnd(): Boolean {
fun trimEnd(): Boolean {
children.fastForEachReversed { richSpan ->
val isEmpty = richSpan.trimEnd()

Expand All @@ -224,14 +224,14 @@ public class RichParagraph(
*
* @param newParagraph The new paragraph
*/
public fun updateChildrenParagraph(newParagraph: RichParagraph) {
fun updateChildrenParagraph(newParagraph: RichParagraph) {
children.fastForEach { childRichSpan ->
childRichSpan.paragraph = newParagraph
childRichSpan.updateChildrenParagraph(newParagraph)
}
}

public fun removeEmptyChildren() {
fun removeEmptyChildren() {
val toRemoveIndices = mutableListOf<Int>()

children.fastForEachIndexed { index, richSpan ->
Expand All @@ -246,7 +246,7 @@ public class RichParagraph(
}
}

public fun copy(): RichParagraph {
fun copy(): RichParagraph {
val newParagraph = RichParagraph(
paragraphStyle = paragraphStyle,
type = type.copy(),
Expand All @@ -269,7 +269,7 @@ public class RichParagraph(
return stringBuilder.toString()
}

public companion object {
public val DefaultParagraphStyle: ParagraphStyle = ParagraphStyle()
companion object {
val DefaultParagraphStyle = ParagraphStyle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import androidx.compose.ui.text.ParagraphStyle
import com.mohamedrejeb.richeditor.model.RichSpan
import com.mohamedrejeb.richeditor.model.RichTextConfig

public interface ParagraphType {
internal interface ParagraphType {

public fun getStyle(config: RichTextConfig): ParagraphStyle
fun getStyle(config: RichTextConfig): ParagraphStyle

public val startRichSpan: RichSpan
val startRichSpan: RichSpan

public fun getNextParagraphType(): ParagraphType
fun getNextParagraphType(): ParagraphType

public fun copy(): ParagraphType
fun copy(): ParagraphType

public companion object {
companion object {
public val ParagraphType.startText : String get() = startRichSpan.text
}
}
Loading

0 comments on commit f849418

Please sign in to comment.