-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathReactNativeRichTextEditorViewNativeComponent.ts
More file actions
230 lines (210 loc) · 5.85 KB
/
Copy pathReactNativeRichTextEditorViewNativeComponent.ts
File metadata and controls
230 lines (210 loc) · 5.85 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
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
import type {
DirectEventHandler,
Float,
Int32,
UnsafeMixed,
} from 'react-native/Libraries/Types/CodegenTypes';
import type { ColorValue, HostComponent, ViewProps } from 'react-native';
import React from 'react';
export interface OnChangeTextEvent {
value: string;
}
export interface OnChangeHtmlEvent {
value: string;
}
export interface OnChangeStateEvent {
isBold: boolean;
isItalic: boolean;
isUnderline: boolean;
isStrikeThrough: boolean;
isInlineCode: boolean;
isH1: boolean;
isH2: boolean;
isH3: boolean;
isCodeBlock: boolean;
isBlockQuote: boolean;
isOrderedList: boolean;
isUnorderedList: boolean;
isLink: boolean;
isImage: boolean;
isMention: boolean;
}
export interface OnLinkDetected {
text: string;
url: string;
start: Int32;
end: Int32;
}
export interface OnMentionDetectedInternal {
text: string;
indicator: string;
payload: string;
}
export interface OnMentionDetected {
text: string;
indicator: string;
attributes: Record<string, string>;
}
export interface OnMentionEvent {
indicator: string;
text: UnsafeMixed;
}
export interface OnChangeSelectionEvent {
start: Int32;
end: Int32;
text: string;
}
export interface MentionStyleProperties {
color?: ColorValue;
backgroundColor?: ColorValue;
textDecorationLine?: 'underline' | 'none';
}
export interface RichTextStyleInternal {
h1?: {
fontSize?: Float;
};
h2?: {
fontSize?: Float;
};
h3?: {
fontSize?: Float;
};
blockquote?: {
borderColor?: ColorValue;
borderWidth?: Float;
gapWidth?: Float;
};
codeblock?: {
color?: ColorValue;
borderRadius?: Float;
backgroundColor?: ColorValue;
};
code?: {
color?: ColorValue;
backgroundColor?: ColorValue;
};
a?: {
color?: ColorValue;
textDecorationLine?: string;
};
// This is a workaround for the fact that codegen does not support Records.
// On native Android side this will become a ReadableMap, on native iOS we can work with a folly::dynamic object.
mention?: UnsafeMixed;
img?: {
width?: Float;
height?: Float;
};
ol?: {
gapWidth?: Float;
marginLeft?: Float;
};
ul?: {
bulletColor?: ColorValue;
bulletSize?: Float;
marginLeft?: Float;
gapWidth?: Float;
};
}
export interface NativeProps extends ViewProps {
// base props
autoFocus?: boolean;
editable?: boolean;
defaultValue?: string;
placeholder?: string;
placeholderTextColor?: ColorValue;
mentionIndicators: string[];
cursorColor?: ColorValue;
selectionColor?: ColorValue;
autoCapitalize?: string;
richTextStyle?: RichTextStyleInternal;
// event callbacks
onInputFocus?: DirectEventHandler<null>;
onInputBlur?: DirectEventHandler<null>;
onChangeText?: DirectEventHandler<OnChangeTextEvent>;
onChangeHtml?: DirectEventHandler<OnChangeHtmlEvent>;
onChangeState?: DirectEventHandler<OnChangeStateEvent>;
onLinkDetected?: DirectEventHandler<OnLinkDetected>;
onMentionDetected?: DirectEventHandler<OnMentionDetectedInternal>;
onMention?: DirectEventHandler<OnMentionEvent>;
onChangeSelection?: DirectEventHandler<OnChangeSelectionEvent>;
// Style related props - used for generating proper setters in component's manager
// These should not be passed as regular props
color?: ColorValue;
fontSize?: Float;
fontFamily?: string;
fontWeight?: string;
fontStyle?: string;
// Used for onChangeHtml event performance optimization
isOnChangeHtmlSet: boolean;
}
type ComponentType = HostComponent<NativeProps>;
interface NativeCommands {
// General commands
focus: (viewRef: React.ElementRef<ComponentType>) => void;
blur: (viewRef: React.ElementRef<ComponentType>) => void;
setValue: (viewRef: React.ElementRef<ComponentType>, text: string) => void;
// Text formatting commands
toggleBold: (viewRef: React.ElementRef<ComponentType>) => void;
toggleItalic: (viewRef: React.ElementRef<ComponentType>) => void;
toggleUnderline: (viewRef: React.ElementRef<ComponentType>) => void;
toggleStrikeThrough: (viewRef: React.ElementRef<ComponentType>) => void;
toggleInlineCode: (viewRef: React.ElementRef<ComponentType>) => void;
toggleH1: (viewRef: React.ElementRef<ComponentType>) => void;
toggleH2: (viewRef: React.ElementRef<ComponentType>) => void;
toggleH3: (viewRef: React.ElementRef<ComponentType>) => void;
toggleCodeBlock: (viewRef: React.ElementRef<ComponentType>) => void;
toggleBlockQuote: (viewRef: React.ElementRef<ComponentType>) => void;
toggleOrderedList: (viewRef: React.ElementRef<ComponentType>) => void;
toggleUnorderedList: (viewRef: React.ElementRef<ComponentType>) => void;
addLink: (
viewRef: React.ElementRef<ComponentType>,
start: Int32,
end: Int32,
text: string,
url: string
) => void;
addImage: (viewRef: React.ElementRef<ComponentType>, uri: string) => void;
startMention: (
viewRef: React.ElementRef<ComponentType>,
indicator: string
) => void;
addMention: (
viewRef: React.ElementRef<ComponentType>,
indicator: string,
text: string,
payload: string
) => void;
}
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
supportedCommands: [
// General commands
'focus',
'blur',
'setValue',
// Text formatting commands
'toggleBold',
'toggleItalic',
'toggleUnderline',
'toggleStrikeThrough',
'toggleInlineCode',
'toggleH1',
'toggleH2',
'toggleH3',
'toggleCodeBlock',
'toggleBlockQuote',
'toggleOrderedList',
'toggleUnorderedList',
'addLink',
'addImage',
'startMention',
'addMention',
],
});
export default codegenNativeComponent<NativeProps>(
'ReactNativeRichTextEditorView',
{
interfaceOnly: true,
}
) as HostComponent<NativeProps>;