-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathuseMentionTextInput.ts
More file actions
160 lines (139 loc) · 5.75 KB
/
useMentionTextInput.ts
File metadata and controls
160 lines (139 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { type RefObject, useEffect, useRef, useState } from 'react';
import type { NativeSyntheticEvent, TextInput, TextInputSelectionChangeEventData } from 'react-native';
import { Platform } from 'react-native';
import { SendbirdFileMessage, SendbirdUserMessage, replace, useFreshCallback } from '@sendbird/uikit-utils';
import type { MentionedUser } from '../types';
import { useSendbirdChat } from './useContext';
export interface UseMentionTextInputParams {
messageToEdit?: SendbirdUserMessage | SendbirdFileMessage;
}
export interface UseMentionTextInputReturn {
textInputRef: RefObject<TextInput | null>;
selection: { start: number; end: number };
onSelectionChange: (e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => void;
text: string;
onChangeText: (text: string, addedMentionedUser?: MentionedUser) => void;
mentionedUsers: MentionedUser[];
}
// Note: The selection change with the keyboard cursor might not work properly with RTL languages
const useMentionTextInput = (params: UseMentionTextInputParams): UseMentionTextInputReturn => {
const { mentionManager, sbOptions } = useSendbirdChat();
const mentionedUsersRef = useRef<MentionedUser[]>([]);
const textInputRef = useRef<TextInput | null>(null);
const [text, setText] = useState('');
const [selection, setSelection] = useState({ start: 0, end: 0 });
// TODO: Refactor text edit logic more clearly
useEffect(() => {
if (
mentionManager.shouldUseMentionedMessageTemplate(
params.messageToEdit,
sbOptions.uikit.groupChannel.channel.enableMention,
)
) {
const result = mentionManager.templateToTextAndMentionedUsers(
params.messageToEdit && 'mentionedMessageTemplate' in params.messageToEdit
? params.messageToEdit.mentionedMessageTemplate ?? ''
: '',
params.messageToEdit?.mentionedUsers ?? [],
);
mentionedUsersRef.current = result.mentionedUsers;
setText(result.mentionedText);
} else {
mentionedUsersRef.current = [];
if (params.messageToEdit?.isUserMessage()) {
setText(params.messageToEdit.message);
}
}
}, [params.messageToEdit]);
const onChangeText = useFreshCallback((_nextText: string, addedMentionedUser?: MentionedUser) => {
const prevText = text;
let nextText = _nextText;
let offset = nextText.length - prevText.length;
// Text clear
if (nextText === '') {
mentionedUsersRef.current = [];
}
// Text add
else if (offset > 0) {
/** Add mentioned user **/
if (addedMentionedUser) mentionedUsersRef.current.push(addedMentionedUser);
/** Reconcile mentioned users range on the right side of the selection **/
mentionedUsersRef.current = mentionManager.reconcileRangeOfMentionedUsers(
offset,
selection.end,
mentionedUsersRef.current,
);
}
// Text remove
else if (offset < 0) {
// Ranged remove
if (selection.start !== selection.end) {
/** Filter mentioned users in selection range **/
const { filtered, lastSelection } = mentionManager.removeMentionedUsersInSelection(
selection,
mentionedUsersRef.current,
);
/** Reconcile mentioned users range on the right side of the selection **/
mentionedUsersRef.current = mentionManager.reconcileRangeOfMentionedUsers(
offset,
Math.max(selection.end, lastSelection),
filtered,
);
}
// Single remove
else {
/** Find mentioned user who ranges in removed selection **/
const foundIndex = mentionedUsersRef.current.findIndex((it) =>
mentionManager.rangeHelpers.overlaps(it.range, selection, 'underMore'),
);
/** If found, remove from the mentioned user list and remove remainder text **/
if (foundIndex > -1) {
const it = mentionedUsersRef.current[foundIndex];
const remainderLength = it.range.end - it.range.start + offset;
offset = -remainderLength + offset;
nextText = replace(nextText, it.range.start, it.range.start + remainderLength, '');
mentionedUsersRef.current.splice(foundIndex, 1);
}
/** Reconcile mentioned users range on the right side of the selection **/
mentionedUsersRef.current = mentionManager.reconcileRangeOfMentionedUsers(
offset,
selection.end,
mentionedUsersRef.current,
);
}
}
setText(nextText);
});
return {
textInputRef,
selection,
onSelectionChange: useFreshCallback((e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
const nativeSelection = { ...e.nativeEvent.selection };
// NOTE: To synchronize call onSelectionChange after onChangeText called on each platform.
setTimeout(() => {
const mentionedUser = mentionedUsersRef.current.find((it) =>
mentionManager.rangeHelpers.overlaps(it.range, nativeSelection),
);
// Selection should be blocked if changed into mentioned area
if (mentionedUser) {
const selectionBlock = { start: mentionedUser.range.start, end: mentionedUser.range.end };
textInputRef.current?.setNativeProps({ selection: selectionBlock });
// BUG: setNativeProps called again when invoked onChangeText
// https://github.com/facebook/react-native/issues/33520
if (Platform.OS === 'android') {
setTimeout(() => {
textInputRef.current?.setNativeProps({ selection: { start: 0 } });
}, 250);
}
setSelection(selectionBlock);
} else {
setSelection(nativeSelection);
}
}, 10);
}),
text,
onChangeText,
mentionedUsers: mentionedUsersRef.current,
};
};
export default useMentionTextInput;