Skip to content

Commit 3082b48

Browse files
authored
android - fix emitting html change event (#67)
1 parent 8295946 commit 3082b48

7 files changed

Lines changed: 19 additions & 14 deletions

File tree

android/src/main/java/com/swmansion/reactnativerichtexteditor/ReactNativeRichTextEditorView.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
5252

5353
val mentionHandler: MentionHandler? = MentionHandler(this)
5454
var richTextStyle: RichTextStyle = RichTextStyle(this, null)
55+
var spanWatcher: EditorSpanWatcher? = null
5556

5657
var fontSize: Float? = null
5758
private var autoFocus = false
@@ -345,6 +346,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
345346
private fun addSpanWatcher(watcher: EditorSpanWatcher) {
346347
val spannable = text as Spannable
347348
spannable.setSpan(watcher, 0, spannable.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
349+
spanWatcher = watcher
348350
}
349351

350352
fun verifyAndToggleStyle(name: String) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package com.swmansion.reactnativerichtexteditor.spans.interfaces
22

3-
interface EditorHeadingSpan {
3+
interface EditorHeadingSpan : EditorSpan {
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package com.swmansion.reactnativerichtexteditor.spans.interfaces
22

3-
interface EditorInlineSpan {
3+
interface EditorInlineSpan : EditorSpan {
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package com.swmansion.reactnativerichtexteditor.spans.interfaces
22

3-
interface EditorParagraphSpan {
3+
interface EditorParagraphSpan : EditorSpan {
44
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.swmansion.reactnativerichtexteditor.spans.interfaces
2+
3+
interface EditorSpan {
4+
}

android/src/main/java/com/swmansion/reactnativerichtexteditor/watchers/EditorSpanWatcher.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.swmansion.reactnativerichtexteditor.ReactNativeRichTextEditorView
99
import com.swmansion.reactnativerichtexteditor.events.OnChangeHtmlEvent
1010
import com.swmansion.reactnativerichtexteditor.spans.EditorOrderedListSpan
1111
import com.swmansion.reactnativerichtexteditor.spans.interfaces.EditorHeadingSpan
12+
import com.swmansion.reactnativerichtexteditor.spans.interfaces.EditorSpan
1213
import com.swmansion.reactnativerichtexteditor.utils.EditorParser
1314
import com.swmansion.reactnativerichtexteditor.utils.getSafeSpanBoundaries
1415

@@ -18,13 +19,13 @@ class EditorSpanWatcher(private val editorView: ReactNativeRichTextEditorView) :
1819
override fun onSpanAdded(text: Spannable, what: Any, start: Int, end: Int) {
1920
updateNextLineLayout(what, text, end)
2021
updateUnorderedListSpans(what, text, end)
21-
emitEvent(text)
22+
emitEvent(text, what)
2223
}
2324

2425
override fun onSpanRemoved(text: Spannable, what: Any, start: Int, end: Int) {
2526
updateNextLineLayout(what, text, end)
2627
updateUnorderedListSpans(what, text, end)
27-
emitEvent(text)
28+
emitEvent(text, what)
2829
}
2930

3031
override fun onSpanChanged(text: Spannable, what: Any, ostart: Int, oend: Int, nstart: Int, nend: Int) {
@@ -50,7 +51,10 @@ class EditorSpanWatcher(private val editorView: ReactNativeRichTextEditorView) :
5051
}
5152
}
5253

53-
private fun emitEvent(s: Spannable) {
54+
fun emitEvent(s: Spannable, what: Any?) {
55+
// Emit event only if we change one of ours spans
56+
if (what != null && what !is EditorSpan) return;
57+
5458
val html = EditorParser.toHtml(s, EditorParser.TO_HTML_PARAGRAPH_LINES_INDIVIDUAL)
5559
if (html == previousHtml) return;
5660

android/src/main/java/com/swmansion/reactnativerichtexteditor/watchers/EditorTextWatcher.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.text.TextWatcher
55
import com.facebook.react.bridge.ReactContext
66
import com.facebook.react.uimanager.UIManagerHelper
77
import com.swmansion.reactnativerichtexteditor.ReactNativeRichTextEditorView
8-
import com.swmansion.reactnativerichtexteditor.events.OnChangeHtmlEvent
98
import com.swmansion.reactnativerichtexteditor.events.OnChangeTextEvent
109

1110
class EditorTextWatcher(private val editorView: ReactNativeRichTextEditorView) : TextWatcher {
@@ -24,7 +23,7 @@ class EditorTextWatcher(private val editorView: ReactNativeRichTextEditorView) :
2423
override fun afterTextChanged(s: Editable?) {
2524
if (s == null || editorView.isSettingValue) return
2625

27-
emitEvent(s)
26+
emitEvents(s)
2827
applyStyles(s)
2928
}
3029

@@ -35,15 +34,11 @@ class EditorTextWatcher(private val editorView: ReactNativeRichTextEditorView) :
3534
editorView.parametrizedStyles?.afterTextChanged(s, endCursorPosition)
3635
}
3736

38-
private fun emitEvent(s: Editable) {
37+
private fun emitEvents(s: Editable) {
3938
val context = editorView.context as ReactContext
4039
val surfaceId = UIManagerHelper.getSurfaceId(context)
4140
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, editorView.id)
4241
dispatcher?.dispatchEvent(OnChangeTextEvent(surfaceId, editorView.id, s))
43-
44-
// For empty text we have to manually emit HTML event from TextWatcher instead of SpanWatcher
45-
if (s.isEmpty()) {
46-
dispatcher?.dispatchEvent(OnChangeHtmlEvent(surfaceId, editorView.id, ""))
47-
}
42+
editorView.spanWatcher?.emitEvent(s, null)
4843
}
4944
}

0 commit comments

Comments
 (0)