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,18 +1,22 @@
1
- #import "StyleHeaders.h"
1
+ #import "ColorExtension.h"
2
2
  #import "EnrichedTextInputView.h"
3
3
  #import "OccurenceUtils.h"
4
4
  #import "ParagraphsUtils.h"
5
+ #import "StyleHeaders.h"
5
6
  #import "TextInsertionUtils.h"
6
- #import "ColorExtension.h"
7
7
 
8
8
  @implementation BlockQuoteStyle {
9
9
  EnrichedTextInputView *_input;
10
10
  NSArray *_stylesToExclude;
11
11
  }
12
12
 
13
- + (StyleType)getStyleType { return BlockQuote; }
13
+ + (StyleType)getStyleType {
14
+ return BlockQuote;
15
+ }
14
16
 
15
- + (BOOL)isParagraphStyle { return YES; }
17
+ + (BOOL)isParagraphStyle {
18
+ return YES;
19
+ }
16
20
 
17
21
  - (instancetype)initWithInput:(id)input {
18
22
  self = [super init];
@@ -23,118 +27,158 @@
23
27
 
24
28
  - (CGFloat)getHeadIndent {
25
29
  // rectangle width + gap
26
- return [_input->config blockquoteBorderWidth] + [_input->config blockquoteGapWidth];
30
+ return [_input->config blockquoteBorderWidth] +
31
+ [_input->config blockquoteGapWidth];
27
32
  }
28
33
 
29
34
  // the range will already be the full paragraph/s range
30
35
  - (void)applyStyle:(NSRange)range {
31
36
  BOOL isStylePresent = [self detectStyle:range];
32
- if(range.length >= 1) {
33
- isStylePresent ? [self removeAttributes:range] : [self addAttributes:range];
37
+ if (range.length >= 1) {
38
+ isStylePresent ? [self removeAttributes:range]
39
+ : [self addAttributes:range withTypingAttr:YES];
34
40
  } else {
35
41
  isStylePresent ? [self removeTypingAttributes] : [self addTypingAttributes];
36
42
  }
37
43
  }
38
44
 
39
- - (void)addAttributes:(NSRange)range {
40
- NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:range];
41
- // if we fill empty lines with zero width spaces, we need to offset later ranges
45
+ - (void)addAttributes:(NSRange)range withTypingAttr:(BOOL)withTypingAttr {
46
+ NSArray *paragraphs =
47
+ [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView
48
+ range:range];
49
+ // if we fill empty lines with zero width spaces, we need to offset later
50
+ // ranges
42
51
  NSInteger offset = 0;
43
52
  NSRange preModificationRange = _input->textView.selectedRange;
44
-
53
+
45
54
  // to not emit any space filling selection/text changes
46
55
  _input->blockEmitting = YES;
47
-
48
- for(NSValue *value in paragraphs) {
49
- NSRange pRange = NSMakeRange([value rangeValue].location + offset, [value rangeValue].length);
50
-
51
- // length 0 with first line, length 1 and newline with some empty lines in the middle
52
- if(pRange.length == 0 ||
53
- (pRange.length == 1 &&
54
- [[NSCharacterSet newlineCharacterSet] characterIsMember: [_input->textView.textStorage.string characterAtIndex:pRange.location]])
55
- ) {
56
- [TextInsertionUtils insertText:@"\u200B" at:pRange.location additionalAttributes:nullptr input:_input withSelection:NO];
56
+
57
+ for (NSValue *value in paragraphs) {
58
+ NSRange pRange = NSMakeRange([value rangeValue].location + offset,
59
+ [value rangeValue].length);
60
+
61
+ // length 0 with first line, length 1 and newline with some empty lines in
62
+ // the middle
63
+ if (pRange.length == 0 ||
64
+ (pRange.length == 1 &&
65
+ [[NSCharacterSet newlineCharacterSet]
66
+ characterIsMember:[_input->textView.textStorage.string
67
+ characterAtIndex:pRange.location]])) {
68
+ [TextInsertionUtils insertText:@"\u200B"
69
+ at:pRange.location
70
+ additionalAttributes:nullptr
71
+ input:_input
72
+ withSelection:NO];
57
73
  pRange = NSMakeRange(pRange.location, pRange.length + 1);
58
74
  offset += 1;
59
75
  }
60
-
61
- [_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:pRange options:0
62
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
63
- NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
64
- pStyle.headIndent = [self getHeadIndent];
65
- pStyle.firstLineHeadIndent = [self getHeadIndent];
66
- [_input->textView.textStorage addAttribute:NSParagraphStyleAttributeName value:pStyle range:range];
67
- }
68
- ];
76
+
77
+ [_input->textView.textStorage
78
+ enumerateAttribute:NSParagraphStyleAttributeName
79
+ inRange:pRange
80
+ options:0
81
+ usingBlock:^(id _Nullable value, NSRange range,
82
+ BOOL *_Nonnull stop) {
83
+ NSMutableParagraphStyle *pStyle =
84
+ [(NSParagraphStyle *)value mutableCopy];
85
+ pStyle.headIndent = [self getHeadIndent];
86
+ pStyle.firstLineHeadIndent = [self getHeadIndent];
87
+ [_input->textView.textStorage
88
+ addAttribute:NSParagraphStyleAttributeName
89
+ value:pStyle
90
+ range:range];
91
+ }];
69
92
  }
70
-
93
+
71
94
  // back to emitting
72
95
  _input->blockEmitting = NO;
73
-
74
- if(preModificationRange.length == 0) {
75
- // fix selection if only one line was possibly made a list and filled with a space
96
+
97
+ if (preModificationRange.length == 0) {
98
+ // fix selection if only one line was possibly made a list and filled with a
99
+ // space
76
100
  _input->textView.selectedRange = preModificationRange;
77
101
  } else {
78
102
  // in other cases, fix the selection with newly made offsets
79
- _input->textView.selectedRange = NSMakeRange(preModificationRange.location, preModificationRange.length + offset);
103
+ _input->textView.selectedRange = NSMakeRange(
104
+ preModificationRange.location, preModificationRange.length + offset);
80
105
  }
81
-
106
+
82
107
  // also add typing attributes
83
- NSMutableDictionary *typingAttrs = [_input->textView.typingAttributes mutableCopy];
84
- NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
85
- pStyle.headIndent = [self getHeadIndent];
86
- pStyle.firstLineHeadIndent = [self getHeadIndent];
87
- typingAttrs[NSParagraphStyleAttributeName] = pStyle;
88
- _input->textView.typingAttributes = typingAttrs;
108
+ if (withTypingAttr) {
109
+ NSMutableDictionary *typingAttrs =
110
+ [_input->textView.typingAttributes mutableCopy];
111
+ NSMutableParagraphStyle *pStyle =
112
+ [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
113
+ pStyle.headIndent = [self getHeadIndent];
114
+ pStyle.firstLineHeadIndent = [self getHeadIndent];
115
+ typingAttrs[NSParagraphStyleAttributeName] = pStyle;
116
+ _input->textView.typingAttributes = typingAttrs;
117
+ }
89
118
  }
90
119
 
91
120
  // does pretty much the same as addAttributes
92
121
  - (void)addTypingAttributes {
93
- [self addAttributes:_input->textView.selectedRange];
122
+ [self addAttributes:_input->textView.selectedRange withTypingAttr:YES];
94
123
  }
95
124
 
96
125
  - (void)removeAttributes:(NSRange)range {
97
- NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:range];
98
-
99
- for(NSValue *value in paragraphs) {
126
+ NSArray *paragraphs =
127
+ [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView
128
+ range:range];
129
+
130
+ for (NSValue *value in paragraphs) {
100
131
  NSRange pRange = [value rangeValue];
101
- [_input->textView.textStorage enumerateAttribute:NSParagraphStyleAttributeName inRange:pRange options:0
102
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
103
- NSMutableParagraphStyle *pStyle = [(NSParagraphStyle *)value mutableCopy];
104
- pStyle.headIndent = 0;
105
- pStyle.firstLineHeadIndent = 0;
106
- [_input->textView.textStorage addAttribute:NSParagraphStyleAttributeName value:pStyle range:range];
107
- }
108
- ];
132
+ [_input->textView.textStorage
133
+ enumerateAttribute:NSParagraphStyleAttributeName
134
+ inRange:pRange
135
+ options:0
136
+ usingBlock:^(id _Nullable value, NSRange range,
137
+ BOOL *_Nonnull stop) {
138
+ NSMutableParagraphStyle *pStyle =
139
+ [(NSParagraphStyle *)value mutableCopy];
140
+ pStyle.headIndent = 0;
141
+ pStyle.firstLineHeadIndent = 0;
142
+ [_input->textView.textStorage
143
+ addAttribute:NSParagraphStyleAttributeName
144
+ value:pStyle
145
+ range:range];
146
+ }];
109
147
  }
110
-
148
+
111
149
  // also remove typing attributes
112
- NSMutableDictionary *typingAttrs = [_input->textView.typingAttributes mutableCopy];
113
- NSMutableParagraphStyle *pStyle = [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
150
+ NSMutableDictionary *typingAttrs =
151
+ [_input->textView.typingAttributes mutableCopy];
152
+ NSMutableParagraphStyle *pStyle =
153
+ [typingAttrs[NSParagraphStyleAttributeName] mutableCopy];
114
154
  pStyle.headIndent = 0;
115
155
  pStyle.firstLineHeadIndent = 0;
116
156
  typingAttrs[NSParagraphStyleAttributeName] = pStyle;
117
157
  _input->textView.typingAttributes = typingAttrs;
118
158
  }
119
159
 
120
- // needed for the sake of style conflicts, needs to do exactly the same as removeAttribtues
160
+ // needed for the sake of style conflicts, needs to do exactly the same as
161
+ // removeAttribtues
121
162
  - (void)removeTypingAttributes {
122
163
  [self removeAttributes:_input->textView.selectedRange];
123
164
  }
124
165
 
125
166
  - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
126
- if([self detectStyle:_input->textView.selectedRange] && text.length == 0) {
167
+ if ([self detectStyle:_input->textView.selectedRange] && text.length == 0) {
127
168
  // backspace while the style is active
128
-
129
- NSRange paragraphRange = [_input->textView.textStorage.string paragraphRangeForRange:_input->textView.selectedRange];
130
-
131
- if(NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0))) {
169
+
170
+ NSRange paragraphRange = [_input->textView.textStorage.string
171
+ paragraphRangeForRange:_input->textView.selectedRange];
172
+
173
+ if (NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0))) {
132
174
  // a backspace on the very first input's line quote
133
- // it doesn't run textVieDidChange so we need to manually remove attributes
175
+ // it doesn't run textVieDidChange so we need to manually remove
176
+ // attributes
134
177
  [self removeAttributes:paragraphRange];
135
178
  return YES;
136
- } else if(range.location == paragraphRange.location - 1) {
137
- // same case in other lines; here, the removed range location will be exactly 1 less than paragraph range location
179
+ } else if (range.location == paragraphRange.location - 1) {
180
+ // same case in other lines; here, the removed range location will be
181
+ // exactly 1 less than paragraph range location
138
182
  [self removeAttributes:paragraphRange];
139
183
  return YES;
140
184
  }
@@ -142,79 +186,106 @@
142
186
  return NO;
143
187
  }
144
188
 
145
- - (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
189
+ - (BOOL)styleCondition:(id _Nullable)value:(NSRange)range {
146
190
  NSParagraphStyle *pStyle = (NSParagraphStyle *)value;
147
- return pStyle != nullptr && pStyle.headIndent == [self getHeadIndent] && pStyle.firstLineHeadIndent == [self getHeadIndent] && pStyle.textLists.count == 0;
191
+ return pStyle != nullptr && pStyle.headIndent == [self getHeadIndent] &&
192
+ pStyle.firstLineHeadIndent == [self getHeadIndent] &&
193
+ pStyle.textLists.count == 0;
148
194
  }
149
195
 
150
196
  - (BOOL)detectStyle:(NSRange)range {
151
- if(range.length >= 1) {
152
- return [OccurenceUtils detect:NSParagraphStyleAttributeName withInput:_input inRange:range
153
- withCondition: ^BOOL(id _Nullable value, NSRange range) {
154
- return [self styleCondition:value :range];
155
- }
156
- ];
197
+ if (range.length >= 1) {
198
+ return [OccurenceUtils detect:NSParagraphStyleAttributeName
199
+ withInput:_input
200
+ inRange:range
201
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
202
+ return [self styleCondition:value:range];
203
+ }];
157
204
  } else {
158
- return [OccurenceUtils detect:NSParagraphStyleAttributeName withInput:_input atIndex:range.location checkPrevious:YES
159
- withCondition:^BOOL(id _Nullable value, NSRange range) {
160
- return [self styleCondition:value :range];
161
- }
162
- ];
205
+ return [OccurenceUtils detect:NSParagraphStyleAttributeName
206
+ withInput:_input
207
+ atIndex:range.location
208
+ checkPrevious:YES
209
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
210
+ return [self styleCondition:value:range];
211
+ }];
163
212
  }
164
213
  }
165
214
 
166
215
  - (BOOL)anyOccurence:(NSRange)range {
167
- return [OccurenceUtils any:NSParagraphStyleAttributeName withInput:_input inRange:range
168
- withCondition:^BOOL(id _Nullable value, NSRange range) {
169
- return [self styleCondition:value :range];
170
- }
171
- ];
216
+ return [OccurenceUtils any:NSParagraphStyleAttributeName
217
+ withInput:_input
218
+ inRange:range
219
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
220
+ return [self styleCondition:value:range];
221
+ }];
172
222
  }
173
223
 
174
224
  - (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
175
- return [OccurenceUtils all:NSParagraphStyleAttributeName withInput:_input inRange:range
176
- withCondition:^BOOL(id _Nullable value, NSRange range) {
177
- return [self styleCondition:value :range];
178
- }
179
- ];
225
+ return [OccurenceUtils all:NSParagraphStyleAttributeName
226
+ withInput:_input
227
+ inRange:range
228
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
229
+ return [self styleCondition:value:range];
230
+ }];
180
231
  }
181
232
 
182
233
  // general checkup correcting blockquote color
183
- // since links, mentions and inline code affects coloring, the checkup gets done only outside of them
234
+ // since links, mentions and inline code affects coloring, the checkup gets done
235
+ // only outside of them
184
236
  - (void)manageBlockquoteColor {
185
- if([[_input->config blockquoteColor] isEqualToColor:[_input->config primaryColor]]) {
237
+ if ([[_input->config blockquoteColor]
238
+ isEqualToColor:[_input->config primaryColor]]) {
186
239
  return;
187
240
  }
188
-
189
- NSRange wholeRange = NSMakeRange(0, _input->textView.textStorage.string.length);
190
-
191
- NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView range:wholeRange];
192
- for(NSValue *pValue in paragraphs) {
241
+
242
+ NSRange wholeRange =
243
+ NSMakeRange(0, _input->textView.textStorage.string.length);
244
+
245
+ NSArray *paragraphs =
246
+ [ParagraphsUtils getSeparateParagraphsRangesIn:_input->textView
247
+ range:wholeRange];
248
+ for (NSValue *pValue in paragraphs) {
193
249
  NSRange paragraphRange = [pValue rangeValue];
194
- NSArray *properRanges = [OccurenceUtils getRangesWithout:_stylesToExclude withInput:_input inRange:paragraphRange];
195
-
196
- for(NSValue *value in properRanges) {
250
+ NSArray *properRanges = [OccurenceUtils getRangesWithout:_stylesToExclude
251
+ withInput:_input
252
+ inRange:paragraphRange];
253
+
254
+ for (NSValue *value in properRanges) {
197
255
  NSRange currRange = [value rangeValue];
198
256
  BOOL selfDetected = [self detectStyle:currRange];
199
-
200
- [_input->textView.textStorage enumerateAttribute:NSForegroundColorAttributeName inRange:currRange options:0
201
- usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
202
- UIColor *newColor = nullptr;
203
- BOOL colorApplied = [(UIColor *)value isEqualToColor:[_input->config blockquoteColor]];
204
-
205
- if(colorApplied && !selfDetected) {
206
- newColor = [_input->config primaryColor];
207
- } else if(!colorApplied && selfDetected) {
208
- newColor = [_input->config blockquoteColor];
209
- }
210
-
211
- if(newColor != nullptr) {
212
- [_input->textView.textStorage addAttribute:NSForegroundColorAttributeName value:newColor range:currRange];
213
- [_input->textView.textStorage addAttribute:NSUnderlineColorAttributeName value:newColor range:currRange];
214
- [_input->textView.textStorage addAttribute:NSStrikethroughColorAttributeName value:newColor range:currRange];
215
- }
216
- }
217
- ];
257
+
258
+ [_input->textView.textStorage
259
+ enumerateAttribute:NSForegroundColorAttributeName
260
+ inRange:currRange
261
+ options:0
262
+ usingBlock:^(id _Nullable value, NSRange range,
263
+ BOOL *_Nonnull stop) {
264
+ UIColor *newColor = nullptr;
265
+ BOOL colorApplied = [(UIColor *)value
266
+ isEqualToColor:[_input->config blockquoteColor]];
267
+
268
+ if (colorApplied && !selfDetected) {
269
+ newColor = [_input->config primaryColor];
270
+ } else if (!colorApplied && selfDetected) {
271
+ newColor = [_input->config blockquoteColor];
272
+ }
273
+
274
+ if (newColor != nullptr) {
275
+ [_input->textView.textStorage
276
+ addAttribute:NSForegroundColorAttributeName
277
+ value:newColor
278
+ range:currRange];
279
+ [_input->textView.textStorage
280
+ addAttribute:NSUnderlineColorAttributeName
281
+ value:newColor
282
+ range:currRange];
283
+ [_input->textView.textStorage
284
+ addAttribute:NSStrikethroughColorAttributeName
285
+ value:newColor
286
+ range:currRange];
287
+ }
288
+ }];
218
289
  }
219
290
  }
220
291
  }
@@ -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 BoldStyle {
7
7
  EnrichedTextInputView *_input;
8
8
  }
9
9
 
10
- + (StyleType)getStyleType { return Bold; }
10
+ + (StyleType)getStyleType {
11
+ return Bold;
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 setBold];
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 setBold];
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 setBold];
48
60
  _input->textView.typingAttributes = newTypingAttrs;
49
61
  }
@@ -51,77 +63,97 @@
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 removeBold];
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 removeBold];
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 removeBold];
71
90
  _input->textView.typingAttributes = newTypingAttrs;
72
91
  }
73
92
  }
74
93
 
75
94
  - (BOOL)boldHeadingConflictsInRange:(NSRange)range type:(StyleType)type {
76
- if(type == H1) {
77
- if(![_input->config h1Bold]) { return NO; }
78
- } else if(type == H2) {
79
- if(![_input->config h2Bold]) { return NO; }
80
- } else if(type == H3) {
81
- if(![_input->config h3Bold]) { return NO; }
95
+ if (type == H1) {
96
+ if (![_input->config h1Bold]) {
97
+ return NO;
98
+ }
99
+ } else if (type == H2) {
100
+ if (![_input->config h2Bold]) {
101
+ return NO;
102
+ }
103
+ } else if (type == H3) {
104
+ if (![_input->config h3Bold]) {
105
+ return NO;
106
+ }
82
107
  }
83
-
108
+
84
109
  id<BaseStyleProtocol> headingStyle = _input->stylesDict[@(type)];
85
- return range.length > 0
86
- ? [headingStyle anyOccurence:range]
87
- : [headingStyle detectStyle:range];
110
+ return range.length > 0 ? [headingStyle anyOccurence:range]
111
+ : [headingStyle detectStyle:range];
88
112
  }
89
113
 
90
- - (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
114
+ - (BOOL)styleCondition:(id _Nullable)value:(NSRange)range {
91
115
  UIFont *font = (UIFont *)value;
92
- return font != nullptr && [font isBold] && ![self boldHeadingConflictsInRange:range type:H1] && ![self boldHeadingConflictsInRange:range type:H2] && ![self boldHeadingConflictsInRange:range type:H3];
116
+ return font != nullptr && [font isBold] &&
117
+ ![self boldHeadingConflictsInRange:range type:H1] &&
118
+ ![self boldHeadingConflictsInRange:range type:H2] &&
119
+ ![self boldHeadingConflictsInRange:range type:H3];
93
120
  }
94
121
 
95
122
  - (BOOL)detectStyle:(NSRange)range {
96
- if(range.length >= 1) {
97
- return [OccurenceUtils detect:NSFontAttributeName withInput:_input inRange:range
98
- withCondition: ^BOOL(id _Nullable value, NSRange range) {
99
- return [self styleCondition:value :range];
100
- }
101
- ];
123
+ if (range.length >= 1) {
124
+ return [OccurenceUtils detect:NSFontAttributeName
125
+ withInput:_input
126
+ inRange:range
127
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
128
+ return [self styleCondition:value:range];
129
+ }];
102
130
  } else {
103
- return [OccurenceUtils detect:NSFontAttributeName withInput:_input atIndex:range.location checkPrevious:NO
104
- withCondition:^BOOL(id _Nullable value, NSRange range) {
105
- return [self styleCondition:value :range];
106
- }
107
- ];
131
+ return [OccurenceUtils detect:NSFontAttributeName
132
+ withInput:_input
133
+ atIndex:range.location
134
+ checkPrevious:NO
135
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
136
+ return [self styleCondition:value:range];
137
+ }];
108
138
  }
109
139
  }
110
140
 
111
141
  - (BOOL)anyOccurence:(NSRange)range {
112
- return [OccurenceUtils any:NSFontAttributeName withInput:_input inRange:range
113
- withCondition:^BOOL(id _Nullable value, NSRange range) {
114
- return [self styleCondition:value :range];
115
- }
116
- ];
142
+ return [OccurenceUtils any:NSFontAttributeName
143
+ withInput:_input
144
+ inRange:range
145
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
146
+ return [self styleCondition:value:range];
147
+ }];
117
148
  }
118
149
 
119
150
  - (NSArray<StylePair *> *_Nullable)findAllOccurences:(NSRange)range {
120
- return [OccurenceUtils all:NSFontAttributeName withInput:_input inRange:range
121
- withCondition:^BOOL(id _Nullable value, NSRange range) {
122
- return [self styleCondition:value :range];
123
- }
124
- ];
151
+ return [OccurenceUtils all:NSFontAttributeName
152
+ withInput:_input
153
+ inRange:range
154
+ withCondition:^BOOL(id _Nullable value, NSRange range) {
155
+ return [self styleCondition:value:range];
156
+ }];
125
157
  }
126
158
 
127
159
  @end