-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTextInsertionUtils.mm
More file actions
50 lines (43 loc) · 2.25 KB
/
Copy pathTextInsertionUtils.mm
File metadata and controls
50 lines (43 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#import "TextInsertionUtils.h"
#import "UIView+React.h"
#import "EditorManager.h"
#import "ReactNativeRichTextEditorView.h"
@implementation TextInsertionUtils
+ (void)insertText:(NSString*)text inView:(UITextView*)textView at:(NSInteger)index additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs {
NSMutableDictionary<NSAttributedStringKey, id> *copiedAttrs = [[NSMutableDictionary<NSAttributedStringKey, id> alloc ] init];
if(index == textView.textStorage.length) {
if(index > 0) {
// if we are at the end of the text, we want to use previous style, but only if the index won't be -1 then
copiedAttrs = [textView.textStorage attributesAtIndex:index - 1 effectiveRange:nullptr].mutableCopy;
}
} else {
copiedAttrs = [textView.textStorage attributesAtIndex:index effectiveRange:nullptr].mutableCopy;
}
if([copiedAttrs count] == 0) {
copiedAttrs = textView.typingAttributes.mutableCopy;
}
if(additionalAttrs != nullptr) {
[copiedAttrs addEntriesFromDictionary: additionalAttrs];
}
NSAttributedString *newAttrStr = [[NSAttributedString alloc] initWithString:text attributes:copiedAttrs];
[textView.textStorage insertAttributedString:newAttrStr atIndex:index];
[textView reactFocus];
textView.selectedRange = NSMakeRange(index + text.length, 0);
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)[EditorManager sharedManager].currentEditor;
if(typedEditor != nullptr) {
typedEditor->recentlyChangedRange = NSMakeRange(index, text.length);
}
}
+ (void)replaceText:(NSString*)text inView:(UITextView*)textView at:(NSRange)range additionalAttributes:(NSDictionary<NSAttributedStringKey, id>*)additionalAttrs {
[textView.textStorage replaceCharactersInRange:range withString:text];
if(additionalAttrs != nullptr) {
[textView.textStorage addAttributes:additionalAttrs range:NSMakeRange(range.location, [text length])];
}
[textView reactFocus];
textView.selectedRange = NSMakeRange(range.location + text.length, 0);
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)[EditorManager sharedManager].currentEditor;
if(typedEditor != nullptr) {
typedEditor->recentlyChangedRange = NSMakeRange(range.location, text.length);
}
}
@end