Skip to content

Commit a75437a

Browse files
authored
feat: make pasting accept wider range of UTIs (#124)
1 parent cc9db06 commit a75437a

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

ios/editorTextView/EditorTextView.mm

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ - (void)copy:(id)sender {
2020

2121
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
2222
[pasteboard setItems:@[@{
23-
UTTypePlainText.identifier : plainText,
23+
UTTypeUTF8PlainText.identifier : plainText,
2424
UTTypeHTML.identifier : escapedHtml,
2525
UTTypeRTF.identifier : rtfData
2626
}]];
@@ -58,21 +58,41 @@ - (void)paste:(id)sender {
5858
: [typedEditor->parser insertFromHtml:initiallyProcessedHtml location:currentRange.location];
5959
} else {
6060
// fall back to plain text, otherwise do nothing
61-
if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
62-
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
63-
currentRange.length > 0
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];
66-
}
61+
[self tryHandlingPlainTextItemsIn:pasteboard range:currentRange editor:typedEditor];
6762
}
68-
} else if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
69-
// just plain text
63+
} else {
64+
[self tryHandlingPlainTextItemsIn:pasteboard range:currentRange editor:typedEditor];
65+
}
66+
67+
[typedEditor anyTextMayHaveBeenModified];
68+
}
69+
70+
- (void)tryHandlingPlainTextItemsIn:(UIPasteboard *)pasteboard range:(NSRange)range editor:(ReactNativeRichTextEditorView *)editor {
71+
NSArray *existingTypes = pasteboard.pasteboardTypes;
72+
NSArray *handledTypes = @[UTTypeUTF8PlainText.identifier, UTTypePlainText.identifier];
73+
NSString *plainText;
74+
75+
for(NSString *type in handledTypes) {
76+
if(![existingTypes containsObject:type]) {
77+
continue;
78+
}
79+
80+
id value = [pasteboard valueForPasteboardType:type];
81+
82+
if([value isKindOfClass:[NSData class]]) {
83+
plainText = [[NSString alloc]initWithData:value encoding:NSUTF8StringEncoding];
84+
} else if([value isKindOfClass:[NSString class]]) {
85+
plainText = (NSString *)value;
86+
}
87+
}
7088

71-
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
72-
currentRange.length > 0
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];
89+
if(!plainText) {
90+
return;
7591
}
92+
93+
range.length > 0
94+
? [TextInsertionUtils replaceText:plainText inView:editor->textView at:range additionalAttributes:nullptr editor:editor]
95+
: [TextInsertionUtils insertText:plainText inView:editor->textView at:range.location additionalAttributes:nullptr editor:editor];
7696
}
7797

7898
- (void)cut:(id)sender {
@@ -81,6 +101,8 @@ - (void)cut:(id)sender {
81101

82102
[self copy:sender];
83103
[TextInsertionUtils replaceText:@"" inView:typedEditor->textView at:typedEditor->textView.selectedRange additionalAttributes:nullptr editor:typedEditor];
104+
105+
[typedEditor anyTextMayHaveBeenModified];
84106
}
85107

86108
@end

0 commit comments

Comments
 (0)