Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3MarkdownTextRun.mm

140 lines4.3 KB

1#import "T3MarkdownTextRun.h"
2#import "T3MarkdownText.h"
3#import "T3MarkdownTextRunComponentDescriptor.h"
4#import <react/renderer/components/T3MarkdownTextSpec/EventEmitters.h>
5#import <react/renderer/components/T3MarkdownTextSpec/Props.h>
6#import <react/renderer/components/T3MarkdownTextSpec/RCTComponentViewHelpers.h>
7#import "RCTFabricComponentsPlugins.h"
8#import "Utils.h"
9​
10using namespace facebook::react;
11​
12@interface T3MarkdownTextRun () <RCTT3MarkdownTextRunViewProtocol>
13​
14@end
15​
16@implementation T3MarkdownTextRun {
17 NSString * _text;
18 NSString * _contextMenuConfig;
20​
21+ (ComponentDescriptorProvider)componentDescriptorProvider
23 return concreteComponentDescriptorProvider<T3MarkdownTextRunComponentDescriptor>();
25​
26- (instancetype)initWithFrame:(CGRect)frame
28 if (self = [super initWithFrame:frame]) {
29 static const auto defaultProps = std::make_shared<const T3MarkdownTextRunProps>();
30 _props = defaultProps;
31 }
32 return self;
34​
35- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
37 const auto &oldViewProps = *std::static_pointer_cast<T3MarkdownTextRunProps const>(_props);
38 const auto &newViewProps = *std::static_pointer_cast<T3MarkdownTextRunProps const>(props);
39​
40 if (newViewProps.text != oldViewProps.text) {
41 NSString *text = [NSString stringWithUTF8String:newViewProps.text.c_str()];
42 _text = text;
43 }
44​
45 if (newViewProps.contextMenuConfig != oldViewProps.contextMenuConfig) {
46 _contextMenuConfig = [NSString stringWithUTF8String:newViewProps.contextMenuConfig.c_str()];
47 }
48​
49 [super updateProps:props oldProps:oldProps];
51​
52- (BOOL)hasContextMenu
54 return _contextMenuConfig.length > 0;
56​
57- (nullable UIMenu *)contextMenu
59 if (_contextMenuConfig.length == 0) {
60 return nil;
61 }
62​
63 NSData *data = [_contextMenuConfig dataUsingEncoding:NSUTF8StringEncoding];
64 NSDictionary *config = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
65 if (![config isKindOfClass:[NSDictionary class]]) {
66 return nil;
67 }
68​
69 NSArray *actionConfigs = config[@"actions"];
70 if (![actionConfigs isKindOfClass:[NSArray class]] || actionConfigs.count == 0) {
71 return nil;
72 }
73​
74 NSMutableArray<UIMenuElement *> *actions = [NSMutableArray arrayWithCapacity:actionConfigs.count];
75 __weak T3MarkdownTextRun *weakSelf = self;
76 for (NSDictionary *actionConfig in actionConfigs) {
77 if (![actionConfig isKindOfClass:[NSDictionary class]]) {
78 continue;
79 }
80 NSString *actionIdentifier = actionConfig[@"id"];
81 NSString *title = actionConfig[@"title"];
82 if (![actionIdentifier isKindOfClass:[NSString class]] ||
83 ![title isKindOfClass:[NSString class]]) {
84 continue;
85 }
86​
87 UIAction *action = [UIAction actionWithTitle:title
88 image:nil
89 identifier:actionIdentifier
90 handler:^(__kindof UIAction *selectedAction) {
91 [weakSelf onContextMenuAction:selectedAction.identifier];
92 }];
93 if ([actionConfig[@"disabled"] boolValue]) {
94 action.attributes = UIMenuElementAttributesDisabled;
95 }
96 [actions addObject:action];
97 }
98​
99 if (actions.count == 0) {
100 return nil;
101 }
102 NSString *title = [config[@"title"] isKindOfClass:[NSString class]] ? config[@"title"] : @"";
103 return [UIMenu menuWithTitle:title children:actions];
105​
106- (void)onContextMenuAction:(NSString *)actionIdentifier
108 if (_eventEmitter != nullptr) {
109 std::dynamic_pointer_cast<const facebook::react::T3MarkdownTextRunEventEmitter>(_eventEmitter)
110 ->onContextMenuAction(facebook::react::T3MarkdownTextRunEventEmitter::OnContextMenuAction{
111 static_cast<int>(self.tag),
112 actionIdentifier.UTF8String,
113 });
114 }
116​
117- (void)onPress {
118 if (_eventEmitter != nullptr) {
119 std::dynamic_pointer_cast<const facebook::react::T3MarkdownTextRunEventEmitter>(_eventEmitter)
120 ->onPress(facebook::react::T3MarkdownTextRunEventEmitter::OnPress{});
121 }
123​
124- (void)onLongPress {
125 if (_eventEmitter != nullptr) {
126 std::dynamic_pointer_cast<const facebook::react::T3MarkdownTextRunEventEmitter>(_eventEmitter)
127 ->onLongPress(facebook::react::T3MarkdownTextRunEventEmitter::OnLongPress{});
128 }
130​
131+ (BOOL)shouldBeRecycled {
132 return NO;
134​
135Class<RCTComponentViewProtocol> T3MarkdownTextRunCls(void)
137 return T3MarkdownTextRun.class;
139​
140@end