react-native-enriched 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +875 -0
  3. package/ReactNativeEnriched.podspec +27 -0
  4. package/android/build.gradle +101 -0
  5. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedTextInputViewManagerDelegate.java +146 -0
  6. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedTextInputViewManagerInterface.java +55 -0
  7. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/ComponentDescriptors.cpp +22 -0
  8. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/ComponentDescriptors.h +24 -0
  9. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/EventEmitters.cpp +118 -0
  10. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/EventEmitters.h +95 -0
  11. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/Props.cpp +128 -0
  12. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/Props.h +577 -0
  13. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/ShadowNodes.cpp +17 -0
  14. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/ShadowNodes.h +23 -0
  15. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/States.cpp +16 -0
  16. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/States.h +20 -0
  17. package/android/gradle.properties +5 -0
  18. package/android/src/main/AndroidManifest.xml +3 -0
  19. package/android/src/main/AndroidManifestNew.xml +2 -0
  20. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt +535 -0
  21. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewLayoutManager.kt +64 -0
  22. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewManager.kt +292 -0
  23. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewPackage.kt +19 -0
  24. package/android/src/main/java/com/swmansion/enriched/events/MentionHandler.kt +40 -0
  25. package/android/src/main/java/com/swmansion/enriched/events/OnChangeHtmlEvent.kt +28 -0
  26. package/android/src/main/java/com/swmansion/enriched/events/OnChangeSelectionEvent.kt +29 -0
  27. package/android/src/main/java/com/swmansion/enriched/events/OnChangeStateEvent.kt +24 -0
  28. package/android/src/main/java/com/swmansion/enriched/events/OnChangeTextEvent.kt +30 -0
  29. package/android/src/main/java/com/swmansion/enriched/events/OnInputBlurEvent.kt +27 -0
  30. package/android/src/main/java/com/swmansion/enriched/events/OnInputFocusEvent.kt +27 -0
  31. package/android/src/main/java/com/swmansion/enriched/events/OnLinkDetectedEvent.kt +30 -0
  32. package/android/src/main/java/com/swmansion/enriched/events/OnMentionDetectedEvent.kt +29 -0
  33. package/android/src/main/java/com/swmansion/enriched/events/OnMentionEvent.kt +33 -0
  34. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedBlockQuoteSpan.kt +34 -0
  35. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedBoldSpan.kt +10 -0
  36. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedCodeBlockSpan.kt +38 -0
  37. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH1Span.kt +17 -0
  38. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH2Span.kt +17 -0
  39. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH3Span.kt +17 -0
  40. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedImageSpan.kt +41 -0
  41. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedInlineCodeSpan.kt +16 -0
  42. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedItalicSpan.kt +10 -0
  43. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedLinkSpan.kt +24 -0
  44. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedMentionSpan.kt +36 -0
  45. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedOrderedListSpan.kt +71 -0
  46. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedSpans.kt +111 -0
  47. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedStrikeThroughSpan.kt +9 -0
  48. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedUnderlineSpan.kt +9 -0
  49. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedUnorderedListSpan.kt +49 -0
  50. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedBlockSpan.kt +4 -0
  51. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedHeadingSpan.kt +4 -0
  52. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedInlineSpan.kt +4 -0
  53. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedParagraphSpan.kt +4 -0
  54. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedSpan.kt +4 -0
  55. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedZeroWidthSpaceSpan.kt +5 -0
  56. package/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt +227 -0
  57. package/android/src/main/java/com/swmansion/enriched/styles/InlineStyles.kt +146 -0
  58. package/android/src/main/java/com/swmansion/enriched/styles/ListStyles.kt +173 -0
  59. package/android/src/main/java/com/swmansion/enriched/styles/ParagraphStyles.kt +186 -0
  60. package/android/src/main/java/com/swmansion/enriched/styles/ParametrizedStyles.kt +223 -0
  61. package/android/src/main/java/com/swmansion/enriched/utils/EnrichedParser.java +857 -0
  62. package/android/src/main/java/com/swmansion/enriched/utils/EnrichedSelection.kt +285 -0
  63. package/android/src/main/java/com/swmansion/enriched/utils/EnrichedSpanState.kt +204 -0
  64. package/android/src/main/java/com/swmansion/enriched/utils/Utils.kt +91 -0
  65. package/android/src/main/java/com/swmansion/enriched/watchers/EnrichedSpanWatcher.kt +73 -0
  66. package/android/src/main/java/com/swmansion/enriched/watchers/EnrichedTextWatcher.kt +51 -0
  67. package/android/src/main/new_arch/CMakeLists.txt +56 -0
  68. package/android/src/main/new_arch/RNEnrichedTextInputViewSpec.cpp +22 -0
  69. package/android/src/main/new_arch/RNEnrichedTextInputViewSpec.h +26 -0
  70. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputComponentDescriptor.h +35 -0
  71. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputMeasurementManager.cpp +51 -0
  72. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputMeasurementManager.h +26 -0
  73. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputShadowNode.cpp +34 -0
  74. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputShadowNode.h +54 -0
  75. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputState.cpp +9 -0
  76. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputState.h +25 -0
  77. package/ios/EnrichedTextInputView.h +33 -0
  78. package/ios/EnrichedTextInputView.mm +1190 -0
  79. package/ios/EnrichedTextInputViewManager.mm +13 -0
  80. package/ios/config/InputConfig.h +67 -0
  81. package/ios/config/InputConfig.mm +382 -0
  82. package/ios/generated/RNEnrichedTextInputViewSpec/ComponentDescriptors.cpp +22 -0
  83. package/ios/generated/RNEnrichedTextInputViewSpec/ComponentDescriptors.h +24 -0
  84. package/ios/generated/RNEnrichedTextInputViewSpec/EventEmitters.cpp +118 -0
  85. package/ios/generated/RNEnrichedTextInputViewSpec/EventEmitters.h +95 -0
  86. package/ios/generated/RNEnrichedTextInputViewSpec/Props.cpp +128 -0
  87. package/ios/generated/RNEnrichedTextInputViewSpec/Props.h +577 -0
  88. package/ios/generated/RNEnrichedTextInputViewSpec/RCTComponentViewHelpers.h +384 -0
  89. package/ios/generated/RNEnrichedTextInputViewSpec/ShadowNodes.cpp +17 -0
  90. package/ios/generated/RNEnrichedTextInputViewSpec/ShadowNodes.h +23 -0
  91. package/ios/generated/RNEnrichedTextInputViewSpec/States.cpp +16 -0
  92. package/ios/generated/RNEnrichedTextInputViewSpec/States.h +20 -0
  93. package/ios/inputParser/InputParser.h +11 -0
  94. package/ios/inputParser/InputParser.mm +659 -0
  95. package/ios/inputTextView/InputTextView.h +6 -0
  96. package/ios/inputTextView/InputTextView.mm +115 -0
  97. package/ios/internals/EnrichedTextInputViewComponentDescriptor.h +17 -0
  98. package/ios/internals/EnrichedTextInputViewShadowNode.h +40 -0
  99. package/ios/internals/EnrichedTextInputViewShadowNode.mm +83 -0
  100. package/ios/internals/EnrichedTextInputViewState.cpp +10 -0
  101. package/ios/internals/EnrichedTextInputViewState.h +20 -0
  102. package/ios/styles/BlockQuoteStyle.mm +248 -0
  103. package/ios/styles/BoldStyle.mm +122 -0
  104. package/ios/styles/H1Style.mm +10 -0
  105. package/ios/styles/H2Style.mm +10 -0
  106. package/ios/styles/H3Style.mm +10 -0
  107. package/ios/styles/HeadingStyleBase.mm +144 -0
  108. package/ios/styles/InlineCodeStyle.mm +163 -0
  109. package/ios/styles/ItalicStyle.mm +110 -0
  110. package/ios/styles/LinkStyle.mm +463 -0
  111. package/ios/styles/MentionStyle.mm +476 -0
  112. package/ios/styles/OrderedListStyle.mm +225 -0
  113. package/ios/styles/StrikethroughStyle.mm +80 -0
  114. package/ios/styles/UnderlineStyle.mm +112 -0
  115. package/ios/styles/UnorderedListStyle.mm +225 -0
  116. package/ios/utils/BaseStyleProtocol.h +16 -0
  117. package/ios/utils/ColorExtension.h +6 -0
  118. package/ios/utils/ColorExtension.mm +27 -0
  119. package/ios/utils/FontExtension.h +13 -0
  120. package/ios/utils/FontExtension.mm +91 -0
  121. package/ios/utils/LayoutManagerExtension.h +6 -0
  122. package/ios/utils/LayoutManagerExtension.mm +171 -0
  123. package/ios/utils/LinkData.h +9 -0
  124. package/ios/utils/LinkData.mm +4 -0
  125. package/ios/utils/MentionParams.h +9 -0
  126. package/ios/utils/MentionParams.mm +4 -0
  127. package/ios/utils/MentionStyleProps.h +13 -0
  128. package/ios/utils/MentionStyleProps.mm +56 -0
  129. package/ios/utils/OccurenceUtils.h +37 -0
  130. package/ios/utils/OccurenceUtils.mm +124 -0
  131. package/ios/utils/ParagraphsUtils.h +7 -0
  132. package/ios/utils/ParagraphsUtils.mm +54 -0
  133. package/ios/utils/StringExtension.h +15 -0
  134. package/ios/utils/StringExtension.mm +57 -0
  135. package/ios/utils/StyleHeaders.h +74 -0
  136. package/ios/utils/StylePair.h +9 -0
  137. package/ios/utils/StylePair.mm +4 -0
  138. package/ios/utils/StyleTypeEnum.h +22 -0
  139. package/ios/utils/TextDecorationLineEnum.h +6 -0
  140. package/ios/utils/TextDecorationLineEnum.mm +4 -0
  141. package/ios/utils/TextInsertionUtils.h +6 -0
  142. package/ios/utils/TextInsertionUtils.mm +48 -0
  143. package/ios/utils/WordsUtils.h +6 -0
  144. package/ios/utils/WordsUtils.mm +88 -0
  145. package/ios/utils/ZeroWidthSpaceUtils.h +7 -0
  146. package/ios/utils/ZeroWidthSpaceUtils.mm +164 -0
  147. package/lib/module/EnrichedTextInput.js +191 -0
  148. package/lib/module/EnrichedTextInput.js.map +1 -0
  149. package/lib/module/EnrichedTextInputNativeComponent.ts +235 -0
  150. package/lib/module/index.js +4 -0
  151. package/lib/module/index.js.map +1 -0
  152. package/lib/module/normalizeHtmlStyle.js +141 -0
  153. package/lib/module/normalizeHtmlStyle.js.map +1 -0
  154. package/lib/module/package.json +1 -0
  155. package/lib/typescript/package.json +1 -0
  156. package/lib/typescript/src/EnrichedTextInput.d.ts +113 -0
  157. package/lib/typescript/src/EnrichedTextInput.d.ts.map +1 -0
  158. package/lib/typescript/src/EnrichedTextInputNativeComponent.d.ts +160 -0
  159. package/lib/typescript/src/EnrichedTextInputNativeComponent.d.ts.map +1 -0
  160. package/lib/typescript/src/index.d.ts +3 -0
  161. package/lib/typescript/src/index.d.ts.map +1 -0
  162. package/lib/typescript/src/normalizeHtmlStyle.d.ts +4 -0
  163. package/lib/typescript/src/normalizeHtmlStyle.d.ts.map +1 -0
  164. package/package.json +172 -1
  165. package/react-native.config.js +13 -0
  166. package/src/EnrichedTextInput.tsx +358 -0
  167. package/src/EnrichedTextInputNativeComponent.ts +235 -0
  168. package/src/index.tsx +9 -0
  169. package/src/normalizeHtmlStyle.ts +188 -0
@@ -0,0 +1,235 @@
1
+ import { codegenNativeComponent, codegenNativeCommands } from 'react-native';
2
+ import type {
3
+ DirectEventHandler,
4
+ Float,
5
+ Int32,
6
+ UnsafeMixed,
7
+ } from 'react-native/Libraries/Types/CodegenTypes';
8
+ import type { ColorValue, HostComponent, ViewProps } from 'react-native';
9
+ import React from 'react';
10
+
11
+ export interface OnChangeTextEvent {
12
+ value: string;
13
+ }
14
+
15
+ export interface OnChangeHtmlEvent {
16
+ value: string;
17
+ }
18
+
19
+ export interface OnChangeStateEvent {
20
+ isBold: boolean;
21
+ isItalic: boolean;
22
+ isUnderline: boolean;
23
+ isStrikeThrough: boolean;
24
+ isInlineCode: boolean;
25
+ isH1: boolean;
26
+ isH2: boolean;
27
+ isH3: boolean;
28
+ isCodeBlock: boolean;
29
+ isBlockQuote: boolean;
30
+ isOrderedList: boolean;
31
+ isUnorderedList: boolean;
32
+ isLink: boolean;
33
+ isImage: boolean;
34
+ isMention: boolean;
35
+ }
36
+
37
+ export interface OnLinkDetected {
38
+ text: string;
39
+ url: string;
40
+ start: Int32;
41
+ end: Int32;
42
+ }
43
+
44
+ export interface OnMentionDetectedInternal {
45
+ text: string;
46
+ indicator: string;
47
+ payload: string;
48
+ }
49
+
50
+ export interface OnMentionDetected {
51
+ text: string;
52
+ indicator: string;
53
+ attributes: Record<string, string>;
54
+ }
55
+
56
+ export interface OnMentionEvent {
57
+ indicator: string;
58
+ text: UnsafeMixed;
59
+ }
60
+
61
+ export interface OnChangeSelectionEvent {
62
+ start: Int32;
63
+ end: Int32;
64
+ text: string;
65
+ }
66
+
67
+ export interface MentionStyleProperties {
68
+ color?: ColorValue;
69
+ backgroundColor?: ColorValue;
70
+ textDecorationLine?: 'underline' | 'none';
71
+ }
72
+
73
+ export interface HtmlStyleInternal {
74
+ h1?: {
75
+ fontSize?: Float;
76
+ bold?: boolean;
77
+ };
78
+ h2?: {
79
+ fontSize?: Float;
80
+ bold?: boolean;
81
+ };
82
+ h3?: {
83
+ fontSize?: Float;
84
+ bold?: boolean;
85
+ };
86
+ blockquote?: {
87
+ borderColor?: ColorValue;
88
+ borderWidth?: Float;
89
+ gapWidth?: Float;
90
+ color?: ColorValue;
91
+ };
92
+ codeblock?: {
93
+ color?: ColorValue;
94
+ borderRadius?: Float;
95
+ backgroundColor?: ColorValue;
96
+ };
97
+ code?: {
98
+ color?: ColorValue;
99
+ backgroundColor?: ColorValue;
100
+ };
101
+ a?: {
102
+ color?: ColorValue;
103
+ textDecorationLine?: string;
104
+ };
105
+ // This is a workaround for the fact that codegen does not support Records.
106
+ // On native Android side this will become a ReadableMap, on native iOS we can work with a folly::dynamic object.
107
+ mention?: UnsafeMixed;
108
+ img?: {
109
+ width?: Float;
110
+ height?: Float;
111
+ };
112
+ ol?: {
113
+ gapWidth?: Float;
114
+ marginLeft?: Float;
115
+ markerFontWeight?: string;
116
+ markerColor?: ColorValue;
117
+ };
118
+ ul?: {
119
+ bulletColor?: ColorValue;
120
+ bulletSize?: Float;
121
+ marginLeft?: Float;
122
+ gapWidth?: Float;
123
+ };
124
+ }
125
+
126
+ export interface NativeProps extends ViewProps {
127
+ // base props
128
+ autoFocus?: boolean;
129
+ editable?: boolean;
130
+ defaultValue?: string;
131
+ placeholder?: string;
132
+ placeholderTextColor?: ColorValue;
133
+ mentionIndicators: string[];
134
+ cursorColor?: ColorValue;
135
+ selectionColor?: ColorValue;
136
+ autoCapitalize?: string;
137
+ htmlStyle?: HtmlStyleInternal;
138
+
139
+ // event callbacks
140
+ onInputFocus?: DirectEventHandler<null>;
141
+ onInputBlur?: DirectEventHandler<null>;
142
+ onChangeText?: DirectEventHandler<OnChangeTextEvent>;
143
+ onChangeHtml?: DirectEventHandler<OnChangeHtmlEvent>;
144
+ onChangeState?: DirectEventHandler<OnChangeStateEvent>;
145
+ onLinkDetected?: DirectEventHandler<OnLinkDetected>;
146
+ onMentionDetected?: DirectEventHandler<OnMentionDetectedInternal>;
147
+ onMention?: DirectEventHandler<OnMentionEvent>;
148
+ onChangeSelection?: DirectEventHandler<OnChangeSelectionEvent>;
149
+
150
+ // Style related props - used for generating proper setters in component's manager
151
+ // These should not be passed as regular props
152
+ color?: ColorValue;
153
+ fontSize?: Float;
154
+ fontFamily?: string;
155
+ fontWeight?: string;
156
+ fontStyle?: string;
157
+
158
+ // Used for onChangeHtml event performance optimization
159
+ isOnChangeHtmlSet: boolean;
160
+
161
+ // Experimental
162
+ androidExperimentalSynchronousEvents: boolean;
163
+ }
164
+
165
+ type ComponentType = HostComponent<NativeProps>;
166
+
167
+ interface NativeCommands {
168
+ // General commands
169
+ focus: (viewRef: React.ElementRef<ComponentType>) => void;
170
+ blur: (viewRef: React.ElementRef<ComponentType>) => void;
171
+ setValue: (viewRef: React.ElementRef<ComponentType>, text: string) => void;
172
+
173
+ // Text formatting commands
174
+ toggleBold: (viewRef: React.ElementRef<ComponentType>) => void;
175
+ toggleItalic: (viewRef: React.ElementRef<ComponentType>) => void;
176
+ toggleUnderline: (viewRef: React.ElementRef<ComponentType>) => void;
177
+ toggleStrikeThrough: (viewRef: React.ElementRef<ComponentType>) => void;
178
+ toggleInlineCode: (viewRef: React.ElementRef<ComponentType>) => void;
179
+ toggleH1: (viewRef: React.ElementRef<ComponentType>) => void;
180
+ toggleH2: (viewRef: React.ElementRef<ComponentType>) => void;
181
+ toggleH3: (viewRef: React.ElementRef<ComponentType>) => void;
182
+ toggleCodeBlock: (viewRef: React.ElementRef<ComponentType>) => void;
183
+ toggleBlockQuote: (viewRef: React.ElementRef<ComponentType>) => void;
184
+ toggleOrderedList: (viewRef: React.ElementRef<ComponentType>) => void;
185
+ toggleUnorderedList: (viewRef: React.ElementRef<ComponentType>) => void;
186
+ addLink: (
187
+ viewRef: React.ElementRef<ComponentType>,
188
+ start: Int32,
189
+ end: Int32,
190
+ text: string,
191
+ url: string
192
+ ) => void;
193
+ addImage: (viewRef: React.ElementRef<ComponentType>, uri: string) => void;
194
+ startMention: (
195
+ viewRef: React.ElementRef<ComponentType>,
196
+ indicator: string
197
+ ) => void;
198
+ addMention: (
199
+ viewRef: React.ElementRef<ComponentType>,
200
+ indicator: string,
201
+ text: string,
202
+ payload: string
203
+ ) => void;
204
+ }
205
+
206
+ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
207
+ supportedCommands: [
208
+ // General commands
209
+ 'focus',
210
+ 'blur',
211
+ 'setValue',
212
+
213
+ // Text formatting commands
214
+ 'toggleBold',
215
+ 'toggleItalic',
216
+ 'toggleUnderline',
217
+ 'toggleStrikeThrough',
218
+ 'toggleInlineCode',
219
+ 'toggleH1',
220
+ 'toggleH2',
221
+ 'toggleH3',
222
+ 'toggleCodeBlock',
223
+ 'toggleBlockQuote',
224
+ 'toggleOrderedList',
225
+ 'toggleUnorderedList',
226
+ 'addLink',
227
+ 'addImage',
228
+ 'startMention',
229
+ 'addMention',
230
+ ],
231
+ });
232
+
233
+ export default codegenNativeComponent<NativeProps>('EnrichedTextInputView', {
234
+ interfaceOnly: true,
235
+ }) as HostComponent<NativeProps>;
package/src/index.tsx ADDED
@@ -0,0 +1,9 @@
1
+ export * from './EnrichedTextInput';
2
+ export type {
3
+ OnChangeTextEvent,
4
+ OnChangeHtmlEvent,
5
+ OnChangeStateEvent,
6
+ OnLinkDetected,
7
+ OnMentionDetected,
8
+ OnChangeSelectionEvent,
9
+ } from './EnrichedTextInputNativeComponent';
@@ -0,0 +1,188 @@
1
+ import type { HtmlStyle } from './EnrichedTextInput';
2
+ import { type ColorValue, processColor } from 'react-native';
3
+ import type {
4
+ MentionStyleProperties,
5
+ HtmlStyleInternal,
6
+ } from './EnrichedTextInputNativeComponent';
7
+
8
+ const defaultStyle: Required<HtmlStyle> = {
9
+ h1: {
10
+ fontSize: 32,
11
+ bold: false,
12
+ },
13
+ h2: {
14
+ fontSize: 24,
15
+ bold: false,
16
+ },
17
+ h3: {
18
+ fontSize: 20,
19
+ bold: false,
20
+ },
21
+ blockquote: {
22
+ borderColor: 'darkgray',
23
+ borderWidth: 4,
24
+ gapWidth: 16,
25
+ color: undefined,
26
+ },
27
+ codeblock: {
28
+ color: 'black',
29
+ borderRadius: 8,
30
+ backgroundColor: 'darkgray',
31
+ },
32
+ code: {
33
+ color: 'red',
34
+ backgroundColor: 'darkgray',
35
+ },
36
+ a: {
37
+ color: 'blue',
38
+ textDecorationLine: 'underline',
39
+ },
40
+ mention: {
41
+ color: 'blue',
42
+ backgroundColor: 'yellow',
43
+ textDecorationLine: 'underline',
44
+ },
45
+ img: {
46
+ width: 80,
47
+ height: 80,
48
+ },
49
+ ol: {
50
+ gapWidth: 16,
51
+ marginLeft: 16,
52
+ markerFontWeight: undefined,
53
+ markerColor: undefined,
54
+ },
55
+ ul: {
56
+ bulletColor: 'black',
57
+ bulletSize: 8,
58
+ marginLeft: 16,
59
+ gapWidth: 16,
60
+ },
61
+ };
62
+
63
+ const isMentionStyleRecord = (
64
+ mentionStyle: HtmlStyle['mention']
65
+ ): mentionStyle is Record<string, MentionStyleProperties> => {
66
+ if (
67
+ mentionStyle &&
68
+ typeof mentionStyle === 'object' &&
69
+ !Array.isArray(mentionStyle)
70
+ ) {
71
+ const keys = Object.keys(mentionStyle);
72
+
73
+ return (
74
+ keys.length > 0 &&
75
+ keys.every(
76
+ (key) =>
77
+ typeof (mentionStyle as Record<string, unknown>)[key] === 'object' &&
78
+ (mentionStyle as Record<string, unknown>)[key] !== null
79
+ )
80
+ );
81
+ }
82
+ return false;
83
+ };
84
+
85
+ const convertToHtmlStyleInternal = (
86
+ style: HtmlStyle,
87
+ mentionIndicators: string[]
88
+ ): HtmlStyleInternal => {
89
+ const mentionStyles: Record<string, MentionStyleProperties> = {};
90
+
91
+ mentionIndicators.forEach((indicator) => {
92
+ mentionStyles[indicator] = {
93
+ ...defaultStyle.mention,
94
+ ...(isMentionStyleRecord(style.mention)
95
+ ? (style.mention[indicator] ?? style.mention.default ?? {})
96
+ : style.mention),
97
+ };
98
+ });
99
+
100
+ let markerFontWeight: string | undefined;
101
+ if (style.ol?.markerFontWeight) {
102
+ if (typeof style.ol?.markerFontWeight === 'number') {
103
+ markerFontWeight = String(style.ol?.markerFontWeight);
104
+ } else if (typeof style.ol?.markerFontWeight === 'string') {
105
+ markerFontWeight = style.ol?.markerFontWeight;
106
+ }
107
+ }
108
+
109
+ const olStyles = {
110
+ ...style.ol,
111
+ markerFontWeight: markerFontWeight,
112
+ };
113
+
114
+ return {
115
+ ...style,
116
+ mention: mentionStyles,
117
+ ol: olStyles,
118
+ };
119
+ };
120
+
121
+ const assignDefaultValues = (style: HtmlStyleInternal): HtmlStyleInternal => {
122
+ const merged: Record<string, any> = { ...defaultStyle };
123
+
124
+ for (const key in style) {
125
+ if (key === 'mention') {
126
+ merged[key] = {
127
+ ...(style.mention as object),
128
+ };
129
+
130
+ continue;
131
+ }
132
+
133
+ merged[key] = {
134
+ ...defaultStyle[key as keyof HtmlStyle],
135
+ ...(style[key as keyof HtmlStyle] as object),
136
+ };
137
+ }
138
+
139
+ return merged;
140
+ };
141
+
142
+ const parseStyle = (name: string, value: unknown) => {
143
+ if (name !== 'color' && !name.endsWith('Color')) {
144
+ return value;
145
+ }
146
+
147
+ return processColor(value as ColorValue);
148
+ };
149
+
150
+ const parseColors = (style: HtmlStyleInternal): HtmlStyleInternal => {
151
+ const finalStyle: Record<string, any> = {};
152
+
153
+ for (const [tagName, tagStyle] of Object.entries(style)) {
154
+ const tagStyles: Record<string, any> = {};
155
+
156
+ if (tagName === 'mention') {
157
+ for (const [indicator, mentionStyle] of Object.entries(tagStyle)) {
158
+ tagStyles[indicator] = {};
159
+
160
+ for (const [styleName, styleValue] of Object.entries(
161
+ mentionStyle as MentionStyleProperties
162
+ )) {
163
+ tagStyles[indicator][styleName] = parseStyle(styleName, styleValue);
164
+ }
165
+ }
166
+
167
+ finalStyle[tagName] = tagStyles;
168
+ continue;
169
+ }
170
+
171
+ for (const [styleName, styleValue] of Object.entries(tagStyle)) {
172
+ tagStyles[styleName] = parseStyle(styleName, styleValue);
173
+ }
174
+
175
+ finalStyle[tagName] = tagStyles;
176
+ }
177
+
178
+ return finalStyle;
179
+ };
180
+
181
+ export const normalizeHtmlStyle = (
182
+ style: HtmlStyle,
183
+ mentionIndicators: string[]
184
+ ): HtmlStyleInternal => {
185
+ const converted = convertToHtmlStyleInternal(style, mentionIndicators);
186
+ const withDefaults = assignDefaultValues(converted);
187
+ return parseColors(withDefaults);
188
+ };