-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEditorTextView.mm
More file actions
86 lines (71 loc) · 3.86 KB
/
Copy pathEditorTextView.mm
File metadata and controls
86 lines (71 loc) · 3.86 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
51
52
53
54
55
56
57
58
59
60
61
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
#import "EditorTextView.h"
#import "ReactNativeRichTextEditorView.h"
#import "StringExtension.h"
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#import "TextInsertionUtils.h"
@implementation EditorTextView
- (void)copy:(id)sender {
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
if(typedEditor == nullptr) { return; }
NSString *plainText = [typedEditor->textView.textStorage.string substringWithRange:typedEditor->textView.selectedRange];
NSString *escapedHtml = [NSString stringByEscapingHtml:[typedEditor->parser parseToHtmlFromRange:typedEditor->textView.selectedRange]];
NSAttributedString *attrStr = [typedEditor->textView.textStorage attributedSubstringFromRange:typedEditor->textView.selectedRange];
NSData *rtfData = [attrStr dataFromRange:NSMakeRange(0, attrStr.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
error:nullptr
];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setItems:@[@{
UTTypePlainText.identifier : plainText,
UTTypeHTML.identifier : escapedHtml,
UTTypeRTF.identifier : rtfData
}]];
}
- (void)paste:(id)sender {
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
if(typedEditor == nullptr) { return; }
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSArray<NSString *> *pasteboardTypes = pasteboard.pasteboardTypes;
NSRange currentRange = typedEditor->textView.selectedRange;
if([pasteboardTypes containsObject:UTTypeHTML.identifier]) {
// we try processing the html contents
NSString *htmlString;
id htmlValue = [pasteboard valueForPasteboardType:UTTypeHTML.identifier];
if([htmlValue isKindOfClass:[NSData class]]) {
htmlString = [[NSString alloc]initWithData:htmlValue encoding:NSUTF8StringEncoding];
} else if([htmlValue isKindOfClass:[NSString class]]) {
htmlString = htmlValue;
}
// unescape the html
htmlString = [NSString stringByUnescapingHtml:htmlString];
// validate it
NSString *initiallyProcessedHtml = [typedEditor->parser initiallyProcessHtml:htmlString];
if(initiallyProcessedHtml != nullptr) {
// valid html, let's apply it
currentRange.length > 0
? [typedEditor->parser replaceFromHtml:initiallyProcessedHtml range:currentRange]
: [typedEditor->parser insertFromHtml:initiallyProcessedHtml location:currentRange.location];
} else {
// fall back to plain text, otherwise do nothing
if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
currentRange.length > 0
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr editor:typedEditor]
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr editor:typedEditor];
}
}
} else if([pasteboardTypes containsObject:UTTypePlainText.identifier]) {
// just plain text
NSString *plainString = [pasteboard valueForPasteboardType:UTTypePlainText.identifier];
currentRange.length > 0
? [TextInsertionUtils replaceText:plainString inView:typedEditor->textView at:currentRange additionalAttributes:nullptr editor:typedEditor]
: [TextInsertionUtils insertText:plainString inView:typedEditor->textView at:currentRange.location additionalAttributes:nullptr editor:typedEditor];
}
}
- (void)cut:(id)sender {
ReactNativeRichTextEditorView *typedEditor = (ReactNativeRichTextEditorView *)_editor;
if(typedEditor == nullptr) { return; }
[self copy:sender];
[TextInsertionUtils replaceText:@"" inView:typedEditor->textView at:typedEditor->textView.selectedRange additionalAttributes:nullptr editor:typedEditor];
}
@end