react-native-enriched 0.2.0 → 0.2.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 (107) hide show
  1. package/README.md +1 -5
  2. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedTextInputViewManagerDelegate.java +3 -0
  3. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedTextInputViewManagerInterface.java +1 -0
  4. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/EventEmitters.cpp +10 -0
  5. package/android/generated/jni/react/renderer/components/RNEnrichedTextInputViewSpec/EventEmitters.h +7 -0
  6. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt +92 -0
  7. package/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewManager.kt +6 -0
  8. package/android/src/main/java/com/swmansion/enriched/events/MentionHandler.kt +1 -1
  9. package/android/src/main/java/com/swmansion/enriched/events/OnRequestHtmlResultEvent.kt +33 -0
  10. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedBlockQuoteSpan.kt +6 -0
  11. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedBoldSpan.kt +6 -0
  12. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedCodeBlockSpan.kt +6 -0
  13. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH1Span.kt +6 -0
  14. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH2Span.kt +6 -0
  15. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedH3Span.kt +6 -0
  16. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedImageSpan.kt +5 -0
  17. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedInlineCodeSpan.kt +6 -0
  18. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedItalicSpan.kt +5 -0
  19. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedLinkSpan.kt +6 -0
  20. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedMentionSpan.kt +6 -0
  21. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedOrderedListSpan.kt +6 -0
  22. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedSpans.kt +9 -3
  23. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedStrikeThroughSpan.kt +5 -0
  24. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedUnderlineSpan.kt +5 -0
  25. package/android/src/main/java/com/swmansion/enriched/spans/EnrichedUnorderedListSpan.kt +6 -0
  26. package/android/src/main/java/com/swmansion/enriched/spans/interfaces/EnrichedSpan.kt +4 -0
  27. package/android/src/main/java/com/swmansion/enriched/styles/HtmlStyle.kt +78 -0
  28. package/android/src/main/java/com/swmansion/enriched/styles/ParagraphStyles.kt +80 -4
  29. package/android/src/main/java/com/swmansion/enriched/utils/EnrichedParser.java +8 -0
  30. package/android/src/main/new_arch/RNEnrichedTextInputViewSpec.cpp +6 -6
  31. package/android/src/main/new_arch/RNEnrichedTextInputViewSpec.h +6 -6
  32. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputComponentDescriptor.h +19 -19
  33. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputMeasurementManager.cpp +40 -51
  34. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputMeasurementManager.h +13 -15
  35. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputShadowNode.cpp +23 -21
  36. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputShadowNode.h +35 -36
  37. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputState.cpp +4 -4
  38. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/EnrichedTextInputState.h +13 -14
  39. package/android/src/main/new_arch/react/renderer/components/RNEnrichedTextInputViewSpec/conversions.h +12 -13
  40. package/ios/EnrichedTextInputView.h +25 -13
  41. package/ios/EnrichedTextInputView.mm +872 -581
  42. package/ios/attachments/ImageAttachment.h +10 -0
  43. package/ios/attachments/ImageAttachment.mm +34 -0
  44. package/ios/attachments/MediaAttachment.h +23 -0
  45. package/ios/attachments/MediaAttachment.mm +31 -0
  46. package/ios/config/InputConfig.h +6 -6
  47. package/ios/config/InputConfig.mm +39 -33
  48. package/ios/generated/RNEnrichedTextInputViewSpec/EventEmitters.cpp +10 -0
  49. package/ios/generated/RNEnrichedTextInputViewSpec/EventEmitters.h +7 -0
  50. package/ios/generated/RNEnrichedTextInputViewSpec/RCTComponentViewHelpers.h +21 -0
  51. package/ios/inputParser/InputParser.h +5 -5
  52. package/ios/inputParser/InputParser.mm +789 -378
  53. package/ios/inputTextView/InputTextView.h +1 -1
  54. package/ios/inputTextView/InputTextView.mm +100 -59
  55. package/ios/internals/EnrichedTextInputViewComponentDescriptor.h +11 -9
  56. package/ios/internals/EnrichedTextInputViewShadowNode.h +28 -25
  57. package/ios/internals/EnrichedTextInputViewShadowNode.mm +45 -40
  58. package/ios/internals/EnrichedTextInputViewState.h +3 -1
  59. package/ios/styles/BlockQuoteStyle.mm +189 -118
  60. package/ios/styles/BoldStyle.mm +95 -63
  61. package/ios/styles/CodeBlockStyle.mm +204 -128
  62. package/ios/styles/H1Style.mm +10 -4
  63. package/ios/styles/H2Style.mm +10 -4
  64. package/ios/styles/H3Style.mm +10 -4
  65. package/ios/styles/HeadingStyleBase.mm +129 -84
  66. package/ios/styles/ImageStyle.mm +75 -73
  67. package/ios/styles/InlineCodeStyle.mm +148 -85
  68. package/ios/styles/ItalicStyle.mm +76 -52
  69. package/ios/styles/LinkStyle.mm +348 -227
  70. package/ios/styles/MentionStyle.mm +363 -246
  71. package/ios/styles/OrderedListStyle.mm +171 -106
  72. package/ios/styles/StrikethroughStyle.mm +52 -35
  73. package/ios/styles/UnderlineStyle.mm +68 -46
  74. package/ios/styles/UnorderedListStyle.mm +169 -106
  75. package/ios/utils/BaseStyleProtocol.h +2 -2
  76. package/ios/utils/ColorExtension.mm +7 -5
  77. package/ios/utils/FontExtension.mm +42 -27
  78. package/ios/utils/LayoutManagerExtension.h +1 -1
  79. package/ios/utils/LayoutManagerExtension.mm +280 -170
  80. package/ios/utils/MentionParams.h +0 -1
  81. package/ios/utils/MentionStyleProps.h +1 -1
  82. package/ios/utils/MentionStyleProps.mm +27 -20
  83. package/ios/utils/OccurenceUtils.h +42 -42
  84. package/ios/utils/OccurenceUtils.mm +142 -119
  85. package/ios/utils/ParagraphAttributesUtils.h +6 -2
  86. package/ios/utils/ParagraphAttributesUtils.mm +115 -71
  87. package/ios/utils/ParagraphsUtils.h +2 -1
  88. package/ios/utils/ParagraphsUtils.mm +40 -26
  89. package/ios/utils/StringExtension.h +1 -1
  90. package/ios/utils/StringExtension.mm +19 -16
  91. package/ios/utils/StyleHeaders.h +27 -15
  92. package/ios/utils/TextInsertionUtils.h +13 -2
  93. package/ios/utils/TextInsertionUtils.mm +38 -20
  94. package/ios/utils/WordsUtils.h +2 -1
  95. package/ios/utils/WordsUtils.mm +32 -22
  96. package/ios/utils/ZeroWidthSpaceUtils.h +3 -1
  97. package/ios/utils/ZeroWidthSpaceUtils.mm +145 -79
  98. package/lib/module/EnrichedTextInput.js +39 -1
  99. package/lib/module/EnrichedTextInput.js.map +1 -1
  100. package/lib/module/EnrichedTextInputNativeComponent.ts +11 -0
  101. package/lib/typescript/src/EnrichedTextInput.d.ts +1 -0
  102. package/lib/typescript/src/EnrichedTextInput.d.ts.map +1 -1
  103. package/lib/typescript/src/EnrichedTextInputNativeComponent.d.ts +6 -0
  104. package/lib/typescript/src/EnrichedTextInputNativeComponent.d.ts.map +1 -1
  105. package/package.json +8 -1
  106. package/src/EnrichedTextInput.tsx +45 -0
  107. package/src/EnrichedTextInputNativeComponent.ts +11 -0
@@ -1,17 +1,21 @@
1
- #import "StyleHeaders.h"
1
+ #import "ColorExtension.h"
2
2
  #import "EnrichedTextInputView.h"
3
3
  #import "FontExtension.h"
4
4
  #import "OccurenceUtils.h"
5
5
  #import "ParagraphsUtils.h"
6
- #import "ColorExtension.h"
6
+ #import "StyleHeaders.h"
7
7
 
8
8
  @implementation InlineCodeStyle {
9
9
  EnrichedTextInputView *_input;
10
10
  }
11
11
 
12
- + (StyleType)getStyleType { return InlineCode; }
12
+ + (StyleType)getStyleType {
13
+ return InlineCode;
14
+ }
13
15
 
14
- + (BOOL)isParagraphStyle { return NO; }
16
+ + (BOOL)isParagraphStyle {
17
+ return NO;
18
+ }
15
19
 
16
20
  - (instancetype)initWithInput:(id)input {
17
21
  self = [super init];
@@ -21,48 +25,76 @@
21
25
 
22
26
  - (void)applyStyle:(NSRange)range {
23
27
  BOOL isStylePresent = [self detectStyle:range];
24
- if(range.length >= 1) {
25
- isStylePresent ? [self removeAttributes:range] : [self addAttributes:range];
28
+ if (range.length >= 1) {
29
+ isStylePresent ? [self removeAttributes:range]
30
+ : [self addAttributes:range withTypingAttr:YES];
26
31
  } else {
27
32
  isStylePresent ? [self removeTypingAttributes] : [self addTypingAttributes];
28
33
  }
29
34
  }
30
35
 
31
- - (void)addAttributes:(NSRange)range {
36
+ - (void)addAttributes:(NSRange)range withTypingAttr:(BOOL)withTypingAttr {
32
37
  // we don't want to apply inline code to newline characters, it looks bad
33
- NSArray *nonNewlineRanges = [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
34
-
35
- for(NSValue *value in nonNewlineRanges) {
38
+ NSArray *nonNewlineRanges =
39
+ [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
40
+
41
+ for (NSValue *value in nonNewlineRanges) {
36
42
  NSRange currentRange = [value rangeValue];
37
43
  [_input->textView.textStorage beginEditing];
38
44
 
39
- [_input->textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4] range:currentRange];
40
- [_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
41
- [_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
42
- [_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config inlineCodeFgColor] range:currentRange];
43
- [_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:currentRange options:0
44
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
45
- UIFont *font = (UIFont *)value;
46
- if(font != nullptr) {
47
- UIFont *newFont = [[[_input->config monospacedFont] withFontTraits:font] setSize:font.pointSize];
48
- [_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
49
- }
50
- }
51
- ];
52
-
45
+ [_input->textView.textStorage
46
+ addAttribute:NSBackgroundColorAttributeName
47
+ value:[[_input->config inlineCodeBgColor]
48
+ colorWithAlphaIfNotTransparent:0.4]
49
+ range:currentRange];
50
+ [_input->textView.textStorage
51
+ addAttribute:NSForegroundColorAttributeName
52
+ value:[_input->config inlineCodeFgColor]
53
+ range:currentRange];
54
+ [_input->textView.textStorage
55
+ addAttribute:NSUnderlineColorAttributeName
56
+ value:[_input->config inlineCodeFgColor]
57
+ range:currentRange];
58
+ [_input->textView.textStorage
59
+ addAttribute:NSStrikethroughColorAttributeName
60
+ value:[_input->config inlineCodeFgColor]
61
+ range:currentRange];
62
+ [_input->textView.textStorage
63
+ enumerateAttribute:NSFontAttributeName
64
+ inRange:currentRange
65
+ options:0
66
+ usingBlock:^(id _Nullable value, NSRange range,
67
+ BOOL *_Nonnull stop) {
68
+ UIFont *font = (UIFont *)value;
69
+ if (font != nullptr) {
70
+ UIFont *newFont = [[[_input->config monospacedFont]
71
+ withFontTraits:font] setSize:font.pointSize];
72
+ [_input->textView.textStorage
73
+ addAttribute:NSFontAttributeName
74
+ value:newFont
75
+ range:range];
76
+ }
77
+ }];
78
+
53
79
  [_input->textView.textStorage endEditing];
54
80
  }
55
81
  }
56
82
 
57
83
  - (void)addTypingAttributes {
58
- NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
59
- newTypingAttrs[NSBackgroundColorAttributeName] = [[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4];
60
- newTypingAttrs[NSForegroundColorAttributeName] = [_input->config inlineCodeFgColor];
61
- newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config inlineCodeFgColor];
62
- newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config inlineCodeFgColor];
63
- UIFont* currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
64
- if(currentFont != nullptr) {
65
- newTypingAttrs[NSFontAttributeName] = [[[_input->config monospacedFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
84
+ NSMutableDictionary *newTypingAttrs =
85
+ [_input->textView.typingAttributes mutableCopy];
86
+ newTypingAttrs[NSBackgroundColorAttributeName] =
87
+ [[_input->config inlineCodeBgColor] colorWithAlphaIfNotTransparent:0.4];
88
+ newTypingAttrs[NSForegroundColorAttributeName] =
89
+ [_input->config inlineCodeFgColor];
90
+ newTypingAttrs[NSUnderlineColorAttributeName] =
91
+ [_input->config inlineCodeFgColor];
92
+ newTypingAttrs[NSStrikethroughColorAttributeName] =
93
+ [_input->config inlineCodeFgColor];
94
+ UIFont *currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
95
+ if (currentFont != nullptr) {
96
+ newTypingAttrs[NSFontAttributeName] = [[[_input->config monospacedFont]
97
+ withFontTraits:currentFont] setSize:currentFont.pointSize];
66
98
  }
67
99
  _input->textView.typingAttributes = newTypingAttrs;
68
100
  }
@@ -70,100 +102,131 @@
70
102
  - (void)removeAttributes:(NSRange)range {
71
103
  [_input->textView.textStorage beginEditing];
72
104
 
73
- [_input->textView.textStorage removeAttribute:NSBackgroundColorAttributeName range:range];
74
- [_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:[_input->config primaryColor] range:range];
75
- [_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:[_input->config primaryColor] range:range];
76
- [_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:[_input->config primaryColor] range:range];
77
- [_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
78
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
79
- UIFont *font = (UIFont *)value;
80
- if(font != nullptr) {
81
- UIFont *newFont = [[[_input->config primaryFont] withFontTraits:font] setSize:font.pointSize];
82
- [_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
83
- }
84
- }
85
- ];
86
-
105
+ [_input->textView.textStorage removeAttribute:NSBackgroundColorAttributeName
106
+ range:range];
107
+ [_input->textView.textStorage addAttribute:NSForegroundColorAttributeName
108
+ value:[_input->config primaryColor]
109
+ range:range];
110
+ [_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName
111
+ value:[_input->config primaryColor]
112
+ range:range];
113
+ [_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName
114
+ value:[_input->config primaryColor]
115
+ range:range];
116
+ [_input->textView.textStorage
117
+ enumerateAttribute:NSFontAttributeName
118
+ inRange:range
119
+ options:0
120
+ usingBlock:^(id _Nullable value, NSRange range,
121
+ BOOL *_Nonnull stop) {
122
+ UIFont *font = (UIFont *)value;
123
+ if (font != nullptr) {
124
+ UIFont *newFont = [[[_input->config primaryFont]
125
+ withFontTraits:font] setSize:font.pointSize];
126
+ [_input->textView.textStorage addAttribute:NSFontAttributeName
127
+ value:newFont
128
+ range:range];
129
+ }
130
+ }];
131
+
87
132
  [_input->textView.textStorage endEditing];
88
133
  }
89
134
 
90
135
  - (void)removeTypingAttributes {
91
- NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
136
+ NSMutableDictionary *newTypingAttrs =
137
+ [_input->textView.typingAttributes mutableCopy];
92
138
  [newTypingAttrs removeObjectForKey:NSBackgroundColorAttributeName];
93
- newTypingAttrs[NSForegroundColorAttributeName] = [_input->config primaryColor];
139
+ newTypingAttrs[NSForegroundColorAttributeName] =
140
+ [_input->config primaryColor];
94
141
  newTypingAttrs[NSUnderlineColorAttributeName] = [_input->config primaryColor];
95
- newTypingAttrs[NSStrikethroughColorAttributeName] = [_input->config primaryColor];
96
- UIFont* currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
97
- if(currentFont != nullptr) {
98
- newTypingAttrs[NSFontAttributeName] = [[[_input->config primaryFont] withFontTraits:currentFont] setSize:currentFont.pointSize];
142
+ newTypingAttrs[NSStrikethroughColorAttributeName] =
143
+ [_input->config primaryColor];
144
+ UIFont *currentFont = (UIFont *)newTypingAttrs[NSFontAttributeName];
145
+ if (currentFont != nullptr) {
146
+ newTypingAttrs[NSFontAttributeName] = [[[_input->config primaryFont]
147
+ withFontTraits:currentFont] setSize:currentFont.pointSize];
99
148
  }
100
149
  _input->textView.typingAttributes = newTypingAttrs;
101
150
  }
102
151
 
103
152
  // making sure no newlines get inline code style, it looks bad
104
153
  - (void)handleNewlines {
105
- for(int i = 0; i < _input->textView.textStorage.string.length; i++) {
106
- if([[NSCharacterSet newlineCharacterSet] characterIsMember:[_input->textView.textStorage.string characterAtIndex:i]]) {
154
+ for (int i = 0; i < _input->textView.textStorage.string.length; i++) {
155
+ if ([[NSCharacterSet newlineCharacterSet]
156
+ characterIsMember:[_input->textView.textStorage.string
157
+ characterAtIndex:i]]) {
107
158
  NSRange mockRange = NSMakeRange(0, 0);
108
- // can't use detect style because it intentionally doesn't take newlines into consideration
109
- UIColor *bgColor = [_input->textView.textStorage attribute:NSBackgroundColorAttributeName atIndex:i effectiveRange:&mockRange];
110
- if([self styleCondition:bgColor :NSMakeRange(i, 1)]) {
159
+ // can't use detect style because it intentionally doesn't take newlines
160
+ // into consideration
161
+ UIColor *bgColor =
162
+ [_input->textView.textStorage attribute:NSBackgroundColorAttributeName
163
+ atIndex:i
164
+ effectiveRange:&mockRange];
165
+ if ([self styleCondition:bgColor:NSMakeRange(i, 1)]) {
111
166
  [self removeAttributes:NSMakeRange(i, 1)];
112
167
  }
113
168
  }
114
169
  }
115
170
  }
116
171
 
117
- // emojis don't retain monospace font attribute so we check for the background color if there is no mention
118
- - (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
172
+ // emojis don't retain monospace font attribute so we check for the background
173
+ // color if there is no mention
174
+ - (BOOL)styleCondition:(id _Nullable)value:(NSRange)range {
119
175
  UIColor *bgColor = (UIColor *)value;
120
176
  MentionStyle *mStyle = _input->stylesDict[@([MentionStyle getStyleType])];
121
177
  return bgColor != nullptr && mStyle != nullptr && ![mStyle detectStyle:range];
122
178
  }
123
179
 
124
180
  - (BOOL)detectStyle:(NSRange)range {
125
- if(range.length >= 1) {
181
+ if (range.length >= 1) {
126
182
  // detect only in non-newline characters
127
- NSArray *nonNewlineRanges = [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
128
- if(nonNewlineRanges.count == 0) {
183
+ NSArray *nonNewlineRanges =
184
+ [ParagraphsUtils getNonNewlineRangesIn:_input->textView range:range];
185
+ if (nonNewlineRanges.count == 0) {
129
186
  return NO;
130
187
  }
131
-
188
+
132
189
  BOOL detected = YES;
133
- for(NSValue *value in nonNewlineRanges) {
190
+ for (NSValue *value in nonNewlineRanges) {
134
191
  NSRange currentRange = [value rangeValue];
135
- BOOL currentDetected = [OccurenceUtils detect:NSBackgroundColorAttributeName withInput:_input inRange:currentRange
136
- withCondition: ^BOOL(id _Nullable value, NSRange range) {
137
- return [self styleCondition:value :range];
138
- }
139
- ];
192
+ BOOL currentDetected =
193
+ [OccurenceUtils detect:NSBackgroundColorAttributeName
194
+ withInput:_input
195
+ inRange:currentRange
196
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
197
+ return [self styleCondition:value:range];
198
+ }];
140
199
  detected = detected && currentDetected;
141
200
  }
142
-
201
+
143
202
  return detected;
144
203
  } else {
145
- return [OccurenceUtils detect:NSBackgroundColorAttributeName withInput:_input atIndex:range.location checkPrevious:NO
146
- withCondition:^BOOL(id _Nullable value, NSRange range) {
147
- return [self styleCondition:value :range];
148
- }
149
- ];
204
+ return [OccurenceUtils detect:NSBackgroundColorAttributeName
205
+ withInput:_input
206
+ atIndex:range.location
207
+ checkPrevious:NO
208
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
209
+ return [self styleCondition:value:range];
210
+ }];
150
211
  }
151
212
  }
152
213
 
153
214
  - (BOOL)anyOccurence:(NSRange)range {
154
- return [OccurenceUtils any:NSBackgroundColorAttributeName withInput:_input inRange:range
155
- withCondition:^BOOL(id _Nullable value, NSRange range) {
156
- return [self styleCondition:value :range];
157
- }
158
- ];
215
+ return [OccurenceUtils any:NSBackgroundColorAttributeName
216
+ withInput:_input
217
+ inRange:range
218
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
219
+ return [self styleCondition:value:range];
220
+ }];
159
221
  }
160
222
 
161
223
  - (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
162
- return [OccurenceUtils all:NSBackgroundColorAttributeName withInput:_input inRange:range
163
- withCondition:^BOOL(id _Nullable value, NSRange range) {
164
- return [self styleCondition:value :range];
165
- }
166
- ];
224
+ return [OccurenceUtils all:NSBackgroundColorAttributeName
225
+ withInput:_input
226
+ inRange:range
227
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
228
+ return [self styleCondition:value:range];
229
+ }];
167
230
  }
168
231
 
169
232
  @end
@@ -1,15 +1,19 @@
1
- #import "StyleHeaders.h"
2
1
  #import "EnrichedTextInputView.h"
3
2
  #import "FontExtension.h"
4
3
  #import "OccurenceUtils.h"
4
+ #import "StyleHeaders.h"
5
5
 
6
6
  @implementation ItalicStyle {
7
7
  EnrichedTextInputView *_input;
8
8
  }
9
9
 
10
- + (StyleType)getStyleType { return Italic; }
10
+ + (StyleType)getStyleType {
11
+ return Italic;
12
+ }
11
13
 
12
- + (BOOL)isParagraphStyle { return NO; }
14
+ + (BOOL)isParagraphStyle {
15
+ return NO;
16
+ }
13
17
 
14
18
  - (instancetype)initWithInput:(id)input {
15
19
  self = [super init];
@@ -19,31 +23,39 @@
19
23
 
20
24
  - (void)applyStyle:(NSRange)range {
21
25
  BOOL isStylePresent = [self detectStyle:range];
22
- if(range.length >= 1) {
23
- isStylePresent ? [self removeAttributes:range] : [self addAttributes:range];
26
+ if (range.length >= 1) {
27
+ isStylePresent ? [self removeAttributes:range]
28
+ : [self addAttributes:range withTypingAttr:YES];
24
29
  } else {
25
30
  isStylePresent ? [self removeTypingAttributes] : [self addTypingAttributes];
26
31
  }
27
32
  }
28
33
 
29
- - (void)addAttributes:(NSRange)range {
34
+ - (void)addAttributes:(NSRange)range withTypingAttr:(BOOL)withTypingAttr {
30
35
  [_input->textView.textStorage beginEditing];
31
- [_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
32
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
33
- UIFont *font = (UIFont *)value;
34
- if(font != nullptr) {
35
- UIFont *newFont = [font setItalic];
36
- [_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
37
- }
38
- }
39
- ];
36
+ [_input->textView.textStorage
37
+ enumerateAttribute:NSFontAttributeName
38
+ inRange:range
39
+ options:0
40
+ usingBlock:^(id _Nullable value, NSRange range,
41
+ BOOL *_Nonnull stop) {
42
+ UIFont *font = (UIFont *)value;
43
+ if (font != nullptr) {
44
+ UIFont *newFont = [font setItalic];
45
+ [_input->textView.textStorage addAttribute:NSFontAttributeName
46
+ value:newFont
47
+ range:range];
48
+ }
49
+ }];
40
50
  [_input->textView.textStorage endEditing];
41
51
  }
42
52
 
43
53
  - (void)addTypingAttributes {
44
- UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
45
- if(currentFontAttr != nullptr) {
46
- NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
54
+ UIFont *currentFontAttr =
55
+ (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
56
+ if (currentFontAttr != nullptr) {
57
+ NSMutableDictionary *newTypingAttrs =
58
+ [_input->textView.typingAttributes mutableCopy];
47
59
  newTypingAttrs[NSFontAttributeName] = [currentFontAttr setItalic];
48
60
  _input->textView.typingAttributes = newTypingAttrs;
49
61
  }
@@ -51,62 +63,74 @@
51
63
 
52
64
  - (void)removeAttributes:(NSRange)range {
53
65
  [_input->textView.textStorage beginEditing];
54
- [_input->textView.textStorage enumerateAttribute:NSFontAttributeName inRange:range options:0
55
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
56
- UIFont *font = (UIFont *)value;
57
- if(font != nullptr) {
58
- UIFont *newFont = [font removeItalic];
59
- [_input->textView.textStorage addAttribute:NSFontAttributeName value:newFont range:range];
60
- }
61
- }
62
- ];
66
+ [_input->textView.textStorage
67
+ enumerateAttribute:NSFontAttributeName
68
+ inRange:range
69
+ options:0
70
+ usingBlock:^(id _Nullable value, NSRange range,
71
+ BOOL *_Nonnull stop) {
72
+ UIFont *font = (UIFont *)value;
73
+ if (font != nullptr) {
74
+ UIFont *newFont = [font removeItalic];
75
+ [_input->textView.textStorage addAttribute:NSFontAttributeName
76
+ value:newFont
77
+ range:range];
78
+ }
79
+ }];
63
80
  [_input->textView.textStorage endEditing];
64
81
  }
65
82
 
66
83
  - (void)removeTypingAttributes {
67
- UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
68
- if(currentFontAttr != nullptr) {
69
- NSMutableDictionary *newTypingAttrs = [_input->textView.typingAttributes mutableCopy];
84
+ UIFont *currentFontAttr =
85
+ (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
86
+ if (currentFontAttr != nullptr) {
87
+ NSMutableDictionary *newTypingAttrs =
88
+ [_input->textView.typingAttributes mutableCopy];
70
89
  newTypingAttrs[NSFontAttributeName] = [currentFontAttr removeItalic];
71
90
  _input->textView.typingAttributes = newTypingAttrs;
72
91
  }
73
92
  }
74
93
 
75
- - (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
94
+ - (BOOL)styleCondition:(id _Nullable)value:(NSRange)range {
76
95
  UIFont *font = (UIFont *)value;
77
96
  return font != nullptr && [font isItalic];
78
97
  }
79
98
 
80
99
  - (BOOL)detectStyle:(NSRange)range {
81
- if(range.length >= 1) {
82
- return [OccurenceUtils detect:NSFontAttributeName withInput:_input inRange:range
83
- withCondition: ^BOOL(id _Nullable value, NSRange range) {
84
- return [self styleCondition:value :range];
85
- }
86
- ];
100
+ if (range.length >= 1) {
101
+ return [OccurenceUtils detect:NSFontAttributeName
102
+ withInput:_input
103
+ inRange:range
104
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
105
+ return [self styleCondition:value:range];
106
+ }];
87
107
  } else {
88
- return [OccurenceUtils detect:NSFontAttributeName withInput:_input atIndex:range.location checkPrevious:NO
89
- withCondition:^BOOL(id _Nullable value, NSRange range) {
90
- return [self styleCondition:value :range];
91
- }
92
- ];
108
+ return [OccurenceUtils detect:NSFontAttributeName
109
+ withInput:_input
110
+ atIndex:range.location
111
+ checkPrevious:NO
112
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
113
+ return [self styleCondition:value:range];
114
+ }];
93
115
  }
94
116
  }
95
117
 
96
118
  - (BOOL)anyOccurence:(NSRange)range {
97
- return [OccurenceUtils any:NSFontAttributeName withInput:_input inRange:range
98
- withCondition:^BOOL(id _Nullable value, NSRange range) {
99
- return [self styleCondition:value :range];
100
- }
101
- ];
119
+ return [OccurenceUtils any:NSFontAttributeName
120
+ withInput:_input
121
+ inRange:range
122
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
123
+ return [self styleCondition:value:range];
124
+ }];
102
125
  }
103
126
 
104
127
  - (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
105
- return [OccurenceUtils all:NSFontAttributeName withInput:_input inRange:range
106
- withCondition:^BOOL(id _Nullable value, NSRange range) {
107
- return [self styleCondition:value :range];
108
- }
109
- ];
128
+ return [OccurenceUtils all:NSFontAttributeName
129
+ withInput:_input
130
+ inRange:range
131
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
132
+ return [self styleCondition:value:range];
133
+ }];
110
134
  }
111
135
 
112
136
  @end