Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class EnrichedTextInputView : AppCompatEditText {
val paragraphStyles: ParagraphStyles? = ParagraphStyles(this)
val listStyles: ListStyles? = ListStyles(this)
val parametrizedStyles: ParametrizedStyles? = ParametrizedStyles(this)
var isSettingValue: Boolean = false
// Sometimes setting up style triggers many changes in sequence
// Eg. removing conflicting styles -> changing text -> applying spans
// In such scenario we want to prevent from handling side effects (eg. onTextChanged)
var isDuringTransaction: Boolean = false
var isRemovingMany: Boolean = false

val mentionHandler: MentionHandler? = MentionHandler(this)
Expand Down Expand Up @@ -233,7 +236,7 @@ class EnrichedTextInputView : AppCompatEditText {

fun setValue(value: CharSequence?) {
if (value == null) return
isSettingValue = true
isDuringTransaction = true

val newText = parseText(value)
setText(newText)
Expand All @@ -244,7 +247,7 @@ class EnrichedTextInputView : AppCompatEditText {
// Scroll to the last line of text
setSelection(text?.length ?: 0)

isSettingValue = false
isDuringTransaction = false
}

fun setAutoFocus(autoFocus: Boolean) {
Expand Down Expand Up @@ -452,13 +455,13 @@ class EnrichedTextInputView : AppCompatEditText {
val end = selection?.end ?: 0
val lengthBefore = text?.length ?: 0

isSettingValue = true
isDuringTransaction = true
val targetRange = getTargetRange(name)
val removed = removeStyle(style, targetRange.first, targetRange.second)
if (removed) {
spanState?.setStart(style, null)
}
isSettingValue = false
isDuringTransaction = false

val lengthAfter = text?.length ?: 0
val charactersRemoved = lengthBefore - lengthAfter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class MentionHandler(private val view: EnrichedTextInputView) {
private var previousText: String? = null
private var previousIndicator: String? = null

fun reset() {
previousText = null
previousIndicator = null
}

fun endMention() {
val indicator = previousIndicator
if (indicator == null) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
}

val start = mentionStart ?: return
view.isDuringTransaction = true
spannable.replace(start, selectionEnd, text)

val span = EnrichedMentionSpan(text, indicator, attributes, view.htmlStyle)
Expand All @@ -213,6 +214,8 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
spannable.insert(safeEnd, " ")
}

view.isDuringTransaction = false
view.mentionHandler?.reset()
view.selection.validateStyles()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EnrichedSelection(private val view: EnrichedTextInputView) {
val finalStart = newStart.coerceAtMost(newEnd).coerceAtLeast(0).coerceAtMost(textLength)
val finalEnd = newEnd.coerceAtLeast(newStart).coerceAtLeast(0).coerceAtMost(textLength)

if (isZeroWidthSelection(finalStart, finalEnd) && !view.isSettingValue) {
if (isZeroWidthSelection(finalStart, finalEnd) && !view.isDuringTransaction) {
view.setSelection(finalStart + 1)
shouldValidateStyles = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class EnrichedTextWatcher(private val view: EnrichedTextInputView) : TextWatcher
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
endCursorPosition = start + count
view.layoutManager.measureSize(s ?: "")
view.isRemovingMany = !view.isSettingValue && before > count + 1
view.isRemovingMany = !view.isDuringTransaction && before > count + 1
}

override fun afterTextChanged(s: Editable?) {
if (s == null) return
emitEvents(s)

if (view.isSettingValue) return
if (view.isDuringTransaction) return
applyStyles(s)
}

Expand Down
9 changes: 8 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ export default function App() {
};

const handleStartMention = (indicator: string) => {
indicator === '@' ? openUserMentionPopup() : openChannelMentionPopup();
if (indicator === '@') {
userMention.onMentionChange('');
openUserMentionPopup();
return;
}

channelMention.onMentionChange('');
openChannelMentionPopup();
};

const handleEndMention = (indicator: string) => {
Expand Down
Loading