react-native-typerich 2.2.3 → 2.2.4
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.
|
@@ -60,82 +60,77 @@
|
|
|
60
60
|
/// setText(text) - diff-based with proper cursor tracking, non-undoable
|
|
61
61
|
- (void)setText:(NSString *)text
|
|
62
62
|
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
[tv scrollRangeToVisible:tv.selectedRange];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
[owner dispatchSelectionChangeIfNeeded];
|
|
138
|
-
});
|
|
63
|
+
UITextView *tv = _textView;
|
|
64
|
+
TypeRichTextInputView *owner = _owner;
|
|
65
|
+
if (!tv || !owner) return;
|
|
66
|
+
|
|
67
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
68
|
+
if (tv.markedTextRange) return;
|
|
69
|
+
|
|
70
|
+
NSString *newText = text ?: @"";
|
|
71
|
+
NSString *currentText = tv.text ?: @"";
|
|
72
|
+
|
|
73
|
+
// if text is same, do nothing.
|
|
74
|
+
if ([currentText isEqualToString:newText]) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
owner.blockEmitting = YES;
|
|
79
|
+
|
|
80
|
+
NSRange oldSelection = tv.selectedRange;
|
|
81
|
+
NSRange diffRange = [self calculateDiffRange:currentText newText:newText];
|
|
82
|
+
|
|
83
|
+
NSInteger newLength = newText.length - (currentText.length - diffRange.length);
|
|
84
|
+
NSString *replacementText = (newLength > 0)
|
|
85
|
+
? [newText substringWithRange:NSMakeRange(diffRange.location, newLength)]
|
|
86
|
+
: @"";
|
|
87
|
+
|
|
88
|
+
UITextPosition *start = [tv positionFromPosition:tv.beginningOfDocument offset:diffRange.location];
|
|
89
|
+
UITextPosition *end = [tv positionFromPosition:tv.beginningOfDocument offset:NSMaxRange(diffRange)];
|
|
90
|
+
|
|
91
|
+
if (start && end) {
|
|
92
|
+
UITextRange *range = [tv textRangeFromPosition:start toPosition:end];
|
|
93
|
+
|
|
94
|
+
// text replacement
|
|
95
|
+
[tv replaceRange:range withText:replacementText];
|
|
96
|
+
|
|
97
|
+
// if the user is typing at the end of the selection,
|
|
98
|
+
// and we just inserted text that matches what the system expected,
|
|
99
|
+
// we should only jump the cursor if the JS-provided state differs
|
|
100
|
+
// from the current internal state.
|
|
101
|
+
|
|
102
|
+
NSInteger delta = replacementText.length - diffRange.length;
|
|
103
|
+
BOOL cursorWasAtEnd = (oldSelection.location == currentText.length);
|
|
104
|
+
|
|
105
|
+
if (cursorWasAtEnd) {
|
|
106
|
+
tv.selectedRange = NSMakeRange(newText.length, 0);
|
|
107
|
+
} else {
|
|
108
|
+
// If the change happened at or before the cursor, shift it
|
|
109
|
+
if (diffRange.location <= oldSelection.location) {
|
|
110
|
+
NSInteger newPos = oldSelection.location + delta;
|
|
111
|
+
newPos = MAX(0, MIN(newPos, newText.length));
|
|
112
|
+
|
|
113
|
+
// IMPORTANT: Only set the range if it's actually different.
|
|
114
|
+
// Setting selectedRange unnecessarily resets the keyboard's internal state.
|
|
115
|
+
if (tv.selectedRange.location != newPos) {
|
|
116
|
+
tv.selectedRange = NSMakeRange(newPos, 0);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
tv.text = newText;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
owner.blockEmitting = NO;
|
|
125
|
+
[owner updatePlaceholderVisibilityFromCommand];
|
|
126
|
+
|
|
127
|
+
if (tv.scrollEnabled) {
|
|
128
|
+
[owner invalidateTextLayoutFromCommand];
|
|
129
|
+
[tv scrollRangeToVisible:tv.selectedRange];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
[owner dispatchSelectionChangeIfNeeded];
|
|
133
|
+
});
|
|
139
134
|
}
|
|
140
135
|
|
|
141
136
|
// Helper: Calculate minimal diff range between two strings
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-typerich",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.4",
|
|
4
4
|
"description": "Drop in TextInput replacement with inbuilt support for Image Pasting and Gboard stickers",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|