Skip to content

Commit 09f8693

Browse files
authored
feat: migrate from delegate calls to properties (#121)
1 parent ae276f7 commit 09f8693

11 files changed

Lines changed: 35 additions & 34 deletions

ios/ReactNativeRichTextEditorView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
#import "EditorConfig.h"
55
#import "EditorParser.h"
66
#import "BaseStyleProtocol.h"
7+
#import "EditorTextView.h"
78

89
#ifndef ReactNativeRichTextEditorViewNativeComponent_h
910
#define ReactNativeRichTextEditorViewNativeComponent_h
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

1314
@interface ReactNativeRichTextEditorView : RCTViewComponentView {
14-
@public UITextView *textView;
15+
@public EditorTextView *textView;
1516
@public NSRange recentlyChangedRange;
1617
@public EditorConfig *config;
1718
@public EditorParser *parser;

ios/ReactNativeRichTextEditorView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#import "StyleHeaders.h"
1414
#import "WordsUtils.h"
1515
#import "LayoutManagerExtension.h"
16-
#import "EditorTextView.h"
1716

1817
using namespace facebook::react;
1918

@@ -137,6 +136,7 @@ - (void)setupTextView {
137136
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
138137
textView.textContainer.lineFragmentPadding = 0;
139138
textView.delegate = self;
139+
textView.editor = self;
140140
textView.layoutManager.editor = self;
141141
}
142142

ios/editorTextView/EditorTextView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
#import <UIkit/UIKit.h>
33

44
@interface EditorTextView : UITextView
5+
@property (nonatomic, weak) id editor;
56
@end

ios/editorTextView/EditorTextView.mm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@implementation EditorTextView
88

99
- (void)copy:(id)sender {
10-
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)self.delegate;
10+
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
1111
if(typedEditor == nullptr) { return; }
1212

1313
NSString *plainText = [typedEditor->textView.textStorage.string substringWithRange:typedEditor->textView.selectedRange];
@@ -27,7 +27,7 @@ - (void)copy:(id)sender {
2727
}
2828

2929
- (void)paste:(id)sender {
30-
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)self.delegate;
30+
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
3131
if(typedEditor == nullptr) { return; }
3232

3333
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
@@ -61,26 +61,26 @@ - (void)paste:(id)sender {
6161
if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
6262
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
6363
currentRange.length > 0
64-
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr]
65-
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr];
64+
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr editor:typedEditor]
65+
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr editor:typedEditor];
6666
}
6767
}
6868
} else if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
6969
// just plain text
7070

7171
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
7272
currentRange.length > 0
73-
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr]
74-
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr];
73+
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr editor:typedEditor]
74+
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr editor:typedEditor];
7575
}
7676
}
7777

7878
- (void)cut:(id)sender {
79-
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)self.delegate;
79+
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
8080
if(typedEditor == nullptr) { return; }
8181

8282
[self copy:sender];
83-
[TextInsertionUtils replaceText:@"" inView:typedEditor->textView at:typedEditor->textView.selectedRange additionalAttributes:nullptr];
83+
[TextInsertionUtils replaceText:@"" inView:typedEditor->textView at:typedEditor->textView.selectedRange additionalAttributes:nullptr editor:typedEditor];
8484
}
8585

8686
@end

ios/parser/EditorParser.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ - (void)replaceFromHtml:(NSString * _Nonnull)html range:(NSRange)range {
354354
NSArray *stylesInfo = (NSArray *)processingResult[1];
355355

356356
// we can use ready replace util
357-
[TextInsertionUtils replaceText:plainText inView:_editor->textView at:range additionalAttributes:nil];
357+
[TextInsertionUtils replaceText:plainText inView:_editor->textView at:range additionalAttributes:nil editor:_editor];
358358

359359
[self applyProcessedStyles:stylesInfo offsetFromBeginning:range.location];
360360
[_editor anyTextMayHaveBeenModified];
@@ -366,7 +366,7 @@ - (void)insertFromHtml:(NSString * _Nonnull)html location:(NSInteger)location {
366366
NSArray *stylesInfo = (NSArray *)processingResult[1];
367367

368368
// same here, insertion utils got our back
369-
[TextInsertionUtils insertText:plainText inView:_editor->textView at:location additionalAttributes:nil];
369+
[TextInsertionUtils insertText:plainText inView:_editor->textView at:location additionalAttributes:nil editor:_editor];
370370

371371
[self applyProcessedStyles:stylesInfo offsetFromBeginning:location];
372372
[_editor anyTextMayHaveBeenModified];

ios/styles/LinkStyle.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ - (void)addLink:(NSString*)text url:(NSString*)url range:(NSRange)range manual:(
140140

141141
if(range.length == 0) {
142142
// insert link
143-
[TextInsertionUtils insertText:text inView:_editor->textView at:range.location additionalAttributes:newAttrs];
143+
[TextInsertionUtils insertText:text inView:_editor->textView at:range.location additionalAttributes:newAttrs editor:_editor];
144144
} else if([currentText isEqualToString:text]) {
145145
// apply link attributes
146146
[_editor->textView.textStorage addAttributes:newAttrs range:range];
@@ -152,7 +152,7 @@ - (void)addLink:(NSString*)text url:(NSString*)url range:(NSRange)range manual:(
152152
}
153153
} else {
154154
// replace text with link
155-
[TextInsertionUtils replaceText:text inView:_editor->textView at:range additionalAttributes:newAttrs];
155+
[TextInsertionUtils replaceText:text inView:_editor->textView at:range additionalAttributes:newAttrs editor:_editor];
156156
}
157157

158158
// mandatory connected links check

ios/styles/MentionStyle.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ - (void)addMention:(NSString *)indicator text:(NSString *)text attributes:(NSStr
164164
// add a single space after the mention
165165
NSString *newText = [NSString stringWithFormat:@"%@ ", text];
166166
NSRange rangeToBeReplaced = [_activeMentionRange rangeValue];
167-
[TextInsertionUtils replaceText:newText inView: _editor->textView at:rangeToBeReplaced additionalAttributes:nullptr];
167+
[TextInsertionUtils replaceText:newText inView: _editor->textView at:rangeToBeReplaced additionalAttributes:nullptr editor:_editor];
168168

169169
// THEN, add the attributes to not apply them on the space
170170
[_editor->textView.textStorage addAttributes:newAttrs range:NSMakeRange(rangeToBeReplaced.location, text.length)];
@@ -230,9 +230,9 @@ - (void)startMentionWithIndicator:(NSString *)indicator {
230230
NSRange newSelect = NSMakeRange(currentRange.location + finalString.length + (addSpaceAfter ? -1 : 0), 0);
231231

232232
if(currentRange.length == 0) {
233-
[TextInsertionUtils insertText:finalString inView:_editor->textView at:currentRange.location additionalAttributes:nullptr];
233+
[TextInsertionUtils insertText:finalString inView:_editor->textView at:currentRange.location additionalAttributes:nullptr editor:_editor];
234234
} else {
235-
[TextInsertionUtils replaceText:finalString inView:_editor->textView at:currentRange additionalAttributes:nullptr];
235+
[TextInsertionUtils replaceText:finalString inView:_editor->textView at:currentRange additionalAttributes:nullptr editor:_editor];
236236
}
237237

238238
[_editor->textView reactFocus];

ios/styles/OrderedListStyle.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (void)addAttributes:(NSRange)range {
4949
NSRange fixedRange = NSMakeRange([value rangeValue].location + offset, [value rangeValue].length);
5050

5151
if(fixedRange.length == 0) {
52-
[TextInsertionUtils insertText:@" " inView:_editor->textView at:fixedRange.location additionalAttributes:nullptr];
52+
[TextInsertionUtils insertText:@" " inView:_editor->textView at:fixedRange.location additionalAttributes:nullptr editor:_editor];
5353
fixedRange = NSMakeRange(fixedRange.location, 1);
5454
offset += 1;
5555
}
@@ -144,7 +144,7 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
144144

145145
// if there is only a space left we should also remove it as it's apple's placholder for empty lists
146146
if([[_editor->textView.textStorage.string substringWithRange:paragraphRange] isEqualToString:@" "]) {
147-
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:paragraphRange additionalAttributes:nullptr];
147+
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:paragraphRange additionalAttributes:nullptr editor:_editor];
148148
return YES;
149149
}
150150
}
@@ -166,7 +166,7 @@ - (BOOL)tryHandlingListShorcutInRange:(NSRange)range replacementText:(NSString *
166166
}
167167

168168
// remove the number
169-
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:NSMakeRange(paragraphRange.location, 1) additionalAttributes:nullptr];
169+
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:NSMakeRange(paragraphRange.location, 1) additionalAttributes:nullptr editor:_editor];
170170

171171
if(prevEmitHtml) {
172172
_editor->emitHtml = YES;

ios/styles/UnorderedListStyle.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (void)addAttributes:(NSRange)range {
4949
NSRange fixedRange = NSMakeRange([value rangeValue].location + offset, [value rangeValue].length);
5050

5151
if(fixedRange.length == 0) {
52-
[TextInsertionUtils insertText:@" " inView:_editor->textView at:fixedRange.location additionalAttributes:nullptr];
52+
[TextInsertionUtils insertText:@" " inView:_editor->textView at:fixedRange.location additionalAttributes:nullptr editor:_editor];
5353
fixedRange = NSMakeRange(fixedRange.location, 1);
5454
offset += 1;
5555
}
@@ -144,7 +144,7 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
144144

145145
// if there is only a space left we should also remove it as it's apple's placholder for empty lists
146146
if([[_editor->textView.textStorage.string substringWithRange:paragraphRange] isEqualToString:@" "]) {
147-
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:paragraphRange additionalAttributes:nullptr];
147+
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:paragraphRange additionalAttributes:nullptr editor:_editor];
148148
return YES;
149149
}
150150
}
@@ -166,7 +166,7 @@ - (BOOL)tryHandlingListShorcutInRange:(NSRange)range replacementText:(NSString *
166166
}
167167

168168
// remove the dash
169-
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:NSMakeRange(paragraphRange.location, 1) additionalAttributes:nullptr];
169+
[TextInsertionUtils replaceText:@"" inView:_editor->textView at:NSMakeRange(paragraphRange.location, 1) additionalAttributes:nullptr editor:_editor];
170170

171171
if(prevEmitHtml) {
172172
_editor->emitHtml = YES;

ios/utils/TextInsertionUtils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <UIKit/UIKit.h>
2+
#import "ReactNativeRichTextEditorView.h"
23

34
@interface TextInsertionUtils : NSObject
4-
+ (void)insertText:(NSString*)text inView:(UITextView*)textView at:(NSInteger)index additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs;
5-
+ (void)replaceText:(NSString*)text inView:(UITextView*)textView at:(NSRange)range additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs;
5+
+ (void)insertText:(NSString*)text inView:(UITextView*)textView at:(NSInteger)index additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs editor:(ReactNativeRichTextEditorView *)editor;
6+
+ (void)replaceText:(NSString*)text inView:(UITextView*)textView at:(NSRange)range additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs editor:(ReactNativeRichTextEditorView *)editor;
67
@end

0 commit comments

Comments
 (0)