-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathApp.tsx
More file actions
347 lines (307 loc) · 10.2 KB
/
Copy pathApp.tsx
File metadata and controls
347 lines (307 loc) · 10.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { useRef, useState } from 'react';
import {
EnrichedTextInput,
type EnrichedTextInputInstance,
type OnKeyPressEvent,
type OnChangeTextEvent,
type OnChangeSelectionEvent,
type OnChangeStateEvent,
type FocusEvent,
type BlurEvent,
type EnrichedInputStyle,
type OnLinkDetected,
type OnPasteImagesEvent,
type OnSubmitEditing,
type OnChangeMentionEvent,
type OnMentionDetected,
} from 'react-native-enriched-html';
import { WEB_DEFAULT_HTML_STYLE } from './defaultHtmlStyle';
import type { NativeSyntheticEvent } from 'react-native';
import { EditorActions } from './components/EditorActions';
import { SetValueModal } from './components/SetValueModal';
import { ImageModal } from './components/ImageModal';
import { LinkModal } from './components/LinkModal';
import { HtmlOutputPanel } from './components/HtmlOutputPanel';
import './App.css';
import { Toolbar } from './components/Toolbar';
import { MentionPopup, type MentionItem } from './components/MentionPopup';
import { useUserMention } from './hooks/useUserMention';
import { useChannelMention } from './hooks/useChannelMention';
const DEFAULT_LINK_STATE: OnLinkDetected = {
text: '',
url: '',
start: 0,
end: 0,
};
function App() {
const ref = useRef<EnrichedTextInputInstance>(null);
const [currentHtml, setCurrentHtml] = useState('');
const [showHtmlOutput, setShowHtmlOutput] = useState(false);
const [isSetValueModalOpen, setIsSetValueModalOpen] = useState(false);
const [isChannelPopupOpen, setIsChannelPopupOpen] = useState(false);
const [isUserPopupOpen, setIsUserPopupOpen] = useState(false);
const [editorState, setEditorState] = useState<OnChangeStateEvent | null>(
null
);
const [selection, setSelection] = useState<OnChangeSelectionEvent | null>(
null
);
const [currentLink, setCurrentLink] =
useState<OnLinkDetected>(DEFAULT_LINK_STATE);
const [isLinkModalOpen, setIsLinkModalOpen] = useState(false);
const [isImageModalOpen, setIsImageModalOpen] = useState(false);
const isLinkActive = !!editorState?.link.isActive;
const hasLinkUrl = currentLink.url.length > 0;
const hasLinkSpan = currentLink.start !== 0 || currentLink.end !== 0;
const selectionInsideLink =
selection !== null &&
selection.start >= currentLink.start &&
selection.end <= currentLink.end;
const insideCurrentLink =
isLinkActive && hasLinkUrl && hasLinkSpan && selectionInsideLink;
const userMention = useUserMention();
const channelMention = useChannelMention();
const openUserMentionPopup = () => {
setIsUserPopupOpen(true);
};
const closeUserMentionPopup = () => {
setIsUserPopupOpen(false);
userMention.onMentionChange('');
};
const openChannelMentionPopup = () => {
setIsChannelPopupOpen(true);
};
const closeChannelMentionPopup = () => {
setIsChannelPopupOpen(false);
channelMention.onMentionChange('');
};
const handleStartMention = (indicator: string) => {
console.log('[EnrichedTextInput] Start mention', indicator);
if (indicator === '@') {
userMention.onMentionChange('');
openUserMentionPopup();
return;
}
channelMention.onMentionChange('');
openChannelMentionPopup();
};
const handleEndMention = (indicator: string) => {
console.log('[EnrichedTextInput] End mention', indicator);
if (indicator === '@') {
closeUserMentionPopup();
userMention.onMentionChange('');
return;
}
closeChannelMentionPopup();
channelMention.onMentionChange('');
};
const handleChangeMention = ({ indicator, text }: OnChangeMentionEvent) => {
console.log('[EnrichedTextInput] Change mention', indicator, text);
if (indicator === '@') {
userMention.onMentionChange(text);
if (!isUserPopupOpen) setIsUserPopupOpen(true);
} else {
channelMention.onMentionChange(text);
if (!isChannelPopupOpen) setIsChannelPopupOpen(true);
}
};
const handleUserMentionSelected = (item: MentionItem) => {
ref.current?.setMention('@', `@${item.name}`, {
id: item.id,
type: 'user',
});
closeUserMentionPopup();
};
const handleChannelMentionSelected = (item: MentionItem) => {
ref.current?.setMention('#', `#${item.name}`, {
id: item.id,
type: 'channel',
});
closeChannelMentionPopup();
};
const mentionPopoverOpen =
(isUserPopupOpen && userMention.data.length > 0) ||
(isChannelPopupOpen && channelMention.data.length > 0);
const handleOnMentionDetected = (e: OnMentionDetected) => {
console.log('[EnrichedTextInput] onMentionDetected event', e);
};
const handleFocus = (e: FocusEvent) => {
console.log('[EnrichedTextInput] onFocus', e.nativeEvent);
};
const handleBlur = (e: BlurEvent) => {
console.log('[EnrichedTextInput] onBlur', e.nativeEvent);
};
const handleKeyPress = (e: NativeSyntheticEvent<OnKeyPressEvent>) => {
console.log('[EnrichedTextInput] onKeyPress event', e.nativeEvent);
};
const handleOnChangeText = (e: NativeSyntheticEvent<OnChangeTextEvent>) => {
console.log('[EnrichedTextInput] onChangeText event', e.nativeEvent);
};
const handleOnChangeHtml = (e: NativeSyntheticEvent<{ value: string }>) => {
console.log('[EnrichedTextInput] onChangeHtml event', e.nativeEvent);
setCurrentHtml(e.nativeEvent.value);
};
const handleChangeSelection = (
e: NativeSyntheticEvent<OnChangeSelectionEvent>
) => {
console.log('[EnrichedTextInput] onChangeSelection event', e.nativeEvent);
setSelection(e.nativeEvent);
};
const openLinkModal = () => {
setIsLinkModalOpen(true);
};
const closeLinkModal = () => {
setIsLinkModalOpen(false);
};
const openImageModal = () => {
setIsImageModalOpen(true);
};
const closeImageModal = () => {
setIsImageModalOpen(false);
};
const submitImage = (url: string, width: number, height: number) => {
ref.current?.setImage(url, width, height);
};
const submitLink = (text: string, url: string) => {
if (!selection || url.length === 0) {
closeLinkModal();
return;
}
const newText = text.length > 0 ? text : url;
if (insideCurrentLink) {
ref.current?.setLink(currentLink.start, currentLink.end, newText, url);
} else {
ref.current?.setLink(selection.start, selection.end, newText, url);
}
closeLinkModal();
};
const handleChangeState = (e: NativeSyntheticEvent<OnChangeStateEvent>) => {
console.log('[EnrichedTextInput] onChangeState event', e.nativeEvent);
setEditorState(e.nativeEvent);
};
const handleSubmitEditing = (e: NativeSyntheticEvent<OnSubmitEditing>) => {
console.log('[EnrichedTextInput] onSubmitEditing event', e.nativeEvent);
};
const handleOnLinkDetected = (e: OnLinkDetected) => {
console.log('[EnrichedTextInput] onLinkDetected event', e);
setCurrentLink(e);
};
const handlePasteImages = (e: NativeSyntheticEvent<OnPasteImagesEvent>) => {
const DEFAULT_W = 80;
const DEFAULT_H = 80;
for (const image of e.nativeEvent.images) {
const w = image.width > 0 ? image.width : DEFAULT_W;
const h = image.height > 0 ? image.height : DEFAULT_H;
ref.current?.setImage(image.uri, w, h);
}
};
return (
<div className="container">
<h1 className="app-title">Enriched Text Input</h1>
<div
className={
mentionPopoverOpen
? 'editor-mention-host editor-mention-host--mention-open'
: 'editor-mention-host'
}
>
<EnrichedTextInput
ref={ref}
placeholder="Type something here..."
autoFocus
editable
scrollEnabled
autoCapitalize="sentences"
style={enrichedInputStyle}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyPress={handleKeyPress}
onChangeText={handleOnChangeText}
onChangeSelection={handleChangeSelection}
onChangeHtml={handleOnChangeHtml}
onChangeState={handleChangeState}
onSubmitEditing={handleSubmitEditing}
onLinkDetected={handleOnLinkDetected}
onPasteImages={handlePasteImages}
onStartMention={handleStartMention}
onChangeMention={handleChangeMention}
onEndMention={handleEndMention}
onMentionDetected={handleOnMentionDetected}
mentionIndicators={['@', '#']}
htmlStyle={WEB_DEFAULT_HTML_STYLE}
/>
<MentionPopup
variant="user"
data={userMention.data}
isOpen={isUserPopupOpen}
onItemPress={handleUserMentionSelected}
/>
<MentionPopup
variant="channel"
data={channelMention.data}
isOpen={isChannelPopupOpen}
onItemPress={handleChannelMentionSelected}
/>
</div>
<Toolbar
editorRef={ref}
state={editorState}
onOpenLinkModal={openLinkModal}
onOpenImageModal={openImageModal}
/>
<EditorActions
showHtmlOutput={showHtmlOutput}
onFocus={() => {
ref.current?.focus();
}}
onBlur={() => {
ref.current?.blur();
}}
onClear={() => {
ref.current?.setValue('');
}}
onToggleHtml={() => {
setShowHtmlOutput((prev) => !prev);
}}
onOpenSetValue={() => {
setIsSetValueModalOpen(true);
}}
/>
{showHtmlOutput && <HtmlOutputPanel html={currentHtml} />}
{isSetValueModalOpen && (
<SetValueModal
onSetValue={(value) => {
ref.current?.setValue(value);
}}
onClose={() => {
setIsSetValueModalOpen(false);
}}
/>
)}
{isLinkModalOpen && (
<LinkModal
editedText={
insideCurrentLink ? currentLink.text : (selection?.text ?? '')
}
editedUrl={insideCurrentLink ? currentLink.url : ''}
onSubmit={submitLink}
onClose={closeLinkModal}
/>
)}
{isImageModalOpen && (
<ImageModal onSubmit={submitImage} onClose={closeImageModal} />
)}
</div>
);
}
const enrichedInputStyle: EnrichedInputStyle = {
backgroundColor: 'gainsboro',
width: '100%',
marginVertical: 12,
maxHeight: 300,
paddingVertical: 12,
paddingHorizontal: 14,
borderRadius: 8,
fontSize: 18,
};
export default App;