Skip to content

Commit 373f92f

Browse files
committed
feat: add indicator inside onMentionDetected
1 parent 5a530ad commit 373f92f

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

android/src/main/java/com/swmansion/reactnativerichtexteditor/events/OnMentionDetectedEvent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.facebook.react.bridge.Arguments
44
import com.facebook.react.bridge.WritableMap
55
import com.facebook.react.uimanager.events.Event
66

7-
class OnMentionDetectedEvent(surfaceId: Int, viewId: Int, private val text: String, private val payload: String) :
7+
class OnMentionDetectedEvent(surfaceId: Int, viewId: Int, private val text: String, private val indicator: String, private val payload: String) :
88
Event<OnMentionDetectedEvent>(surfaceId, viewId) {
99

1010
override fun getEventName(): String {
@@ -14,6 +14,7 @@ class OnMentionDetectedEvent(surfaceId: Int, viewId: Int, private val text: Stri
1414
override fun getEventData(): WritableMap {
1515
val eventData: WritableMap = Arguments.createMap()
1616
eventData.putString("text", text)
17+
eventData.putString("indicator", indicator)
1718
eventData.putString("payload", payload)
1819
return eventData
1920
}

android/src/main/java/com/swmansion/reactnativerichtexteditor/utils/EditorSelection.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,22 @@ class EditorSelection(private val editorView: ReactNativeRichTextEditorView) {
231231
private fun emitMentionDetectedEvent(spannable: Spannable, span: EditorMentionSpan?, start: Int, end: Int) {
232232
val text = spannable.substring(start, end)
233233
val attributes = span?.getAttributes() ?: emptyMap()
234+
val indicator = span?.getIndicator() ?: ""
234235
val payload = JSONObject(attributes).toString()
235236

236-
if (text == previousMentionDetectedEvent["text"] && payload == previousMentionDetectedEvent["payload"]) return
237+
val previousText = previousMentionDetectedEvent["text"] ?: ""
238+
val previousPayload = previousMentionDetectedEvent["payload"] ?: ""
239+
val previousIndicator = previousMentionDetectedEvent["indicator"] ?: ""
240+
241+
if (text == previousText && payload == previousPayload && indicator == previousIndicator) return
237242

238243
previousMentionDetectedEvent.put("text", text)
239244
previousMentionDetectedEvent.put("payload", payload)
245+
previousMentionDetectedEvent.put("indicator", indicator)
240246

241247
val context = editorView.context as ReactContext
242248
val surfaceId = UIManagerHelper.getSurfaceId(context)
243249
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, editorView.id)
244-
dispatcher?.dispatchEvent(OnMentionDetectedEvent(surfaceId, editorView.id, text, payload))
250+
dispatcher?.dispatchEvent(OnMentionDetectedEvent(surfaceId, editorView.id, text, indicator, payload))
245251
}
246252
}

src/ReactNativeRichTextEditorViewNativeComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ export interface OnLinkDetected {
4242

4343
export interface OnMentionDetectedInternal {
4444
text: string;
45+
indicator: string;
4546
payload: string;
4647
}
4748

4849
export interface OnMentionDetected {
4950
text: string;
51+
indicator: string;
5052
attributes: Record<string, string>;
5153
}
5254

src/RichTextInput.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ export const RichTextInput = ({
303303
const handleMentionDetected = (
304304
e: NativeSyntheticEvent<OnMentionDetectedInternal>
305305
) => {
306-
const { text, payload } = e.nativeEvent;
307-
const parsedAttributes = JSON.parse(payload) as Record<string, string>;
308-
onMentionDetected?.({ text, attributes: parsedAttributes });
306+
const { text, indicator, payload } = e.nativeEvent;
307+
const attributes = JSON.parse(payload) as Record<string, string>;
308+
onMentionDetected?.({ text, indicator, attributes });
309309
};
310310

311311
return (

0 commit comments

Comments
 (0)