mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
8131b7bb7b
Summary: This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349. It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`. The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` . There are still a few breaking changes which I hope will be acceptable: - `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path. - ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967. - ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823. - Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled. Still to do: - ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302. - I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? � ## Changelog [iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks. Pull Request resolved: https://github.com/facebook/react-native/pull/25619 Test Plan: ### FB ``` buck build catalyst ``` ### Sample Project Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed. You can see that it works with these steps: 1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git` 2. `git checkout fix-frameworks-subspecs` 3. `cd ios && pod install` 4. `cd .. && react-native run-ios` The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again. ### RNTesterPods `RNTesterPodsPods` can now work with or without `use_frameworks!`. 1. Go to the `RNTester` directory and run `pod install`. 2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine. 3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again. 4. Run the tests again and see that it still works with frameworks enabled. Reviewed By: PeteTheHeat Differential Revision: D16465247 Pulled By: PeteTheHeat fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
269 lines
7.4 KiB
Objective-C
269 lines
7.4 KiB
Objective-C
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import <React/RCTTextView.h>
|
|
|
|
#import <MobileCoreServices/UTCoreTypes.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
#import <React/UIView+React.h>
|
|
|
|
#import <React/RCTTextShadowView.h>
|
|
|
|
@implementation RCTTextView
|
|
{
|
|
CAShapeLayer *_highlightLayer;
|
|
UILongPressGestureRecognizer *_longPressGestureRecognizer;
|
|
|
|
NSArray<UIView *> *_Nullable _descendantViews;
|
|
NSTextStorage *_Nullable _textStorage;
|
|
CGRect _contentFrame;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
if (self = [super initWithFrame:frame]) {
|
|
self.isAccessibilityElement = YES;
|
|
self.accessibilityTraits |= UIAccessibilityTraitStaticText;
|
|
self.opaque = NO;
|
|
self.contentMode = UIViewContentModeRedraw;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
NSString *superDescription = super.description;
|
|
NSRange semicolonRange = [superDescription rangeOfString:@";"];
|
|
NSString *replacement = [NSString stringWithFormat:@"; reactTag: %@; text: %@", self.reactTag, _textStorage.string];
|
|
return [superDescription stringByReplacingCharactersInRange:semicolonRange withString:replacement];
|
|
}
|
|
|
|
- (void)setSelectable:(BOOL)selectable
|
|
{
|
|
if (_selectable == selectable) {
|
|
return;
|
|
}
|
|
|
|
_selectable = selectable;
|
|
|
|
if (_selectable) {
|
|
[self enableContextMenu];
|
|
}
|
|
else {
|
|
[self disableContextMenu];
|
|
}
|
|
}
|
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
{
|
|
// Text looks super weird if its frame is animated.
|
|
// This disables the frame animation, without affecting opacity, etc.
|
|
[UIView performWithoutAnimation:^{
|
|
[super reactSetFrame:frame];
|
|
}];
|
|
}
|
|
|
|
- (void)didUpdateReactSubviews
|
|
{
|
|
// Do nothing, as subviews are managed by `setTextStorage:` method
|
|
}
|
|
|
|
- (void)setTextStorage:(NSTextStorage *)textStorage
|
|
contentFrame:(CGRect)contentFrame
|
|
descendantViews:(NSArray<UIView *> *)descendantViews
|
|
{
|
|
_textStorage = textStorage;
|
|
_contentFrame = contentFrame;
|
|
|
|
// FIXME: Optimize this.
|
|
for (UIView *view in _descendantViews) {
|
|
[view removeFromSuperview];
|
|
}
|
|
|
|
_descendantViews = descendantViews;
|
|
|
|
for (UIView *view in descendantViews) {
|
|
[self addSubview:view];
|
|
}
|
|
|
|
[self setNeedsDisplay];
|
|
}
|
|
|
|
- (void)drawRect:(CGRect)rect
|
|
{
|
|
if (!_textStorage) {
|
|
return;
|
|
}
|
|
|
|
|
|
NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
|
|
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
|
|
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin];
|
|
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin];
|
|
|
|
__block UIBezierPath *highlightPath = nil;
|
|
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange
|
|
actualGlyphRange:NULL];
|
|
[_textStorage enumerateAttribute:RCTTextAttributesIsHighlightedAttributeName
|
|
inRange:characterRange
|
|
options:0
|
|
usingBlock:
|
|
^(NSNumber *value, NSRange range, __unused BOOL *stop) {
|
|
if (!value.boolValue) {
|
|
return;
|
|
}
|
|
|
|
[layoutManager enumerateEnclosingRectsForGlyphRange:range
|
|
withinSelectedGlyphRange:range
|
|
inTextContainer:textContainer
|
|
usingBlock:
|
|
^(CGRect enclosingRect, __unused BOOL *anotherStop) {
|
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(enclosingRect, -2, -2) cornerRadius:2];
|
|
if (highlightPath) {
|
|
[highlightPath appendPath:path];
|
|
} else {
|
|
highlightPath = path;
|
|
}
|
|
}
|
|
];
|
|
}];
|
|
|
|
if (highlightPath) {
|
|
if (!_highlightLayer) {
|
|
_highlightLayer = [CAShapeLayer layer];
|
|
_highlightLayer.fillColor = [UIColor colorWithWhite:0 alpha:0.25].CGColor;
|
|
[self.layer addSublayer:_highlightLayer];
|
|
}
|
|
_highlightLayer.position = _contentFrame.origin;
|
|
_highlightLayer.path = highlightPath.CGPath;
|
|
} else {
|
|
[_highlightLayer removeFromSuperlayer];
|
|
_highlightLayer = nil;
|
|
}
|
|
}
|
|
|
|
|
|
- (NSNumber *)reactTagAtPoint:(CGPoint)point
|
|
{
|
|
NSNumber *reactTag = self.reactTag;
|
|
|
|
CGFloat fraction;
|
|
NSLayoutManager *layoutManager = _textStorage.layoutManagers.firstObject;
|
|
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
|
|
NSUInteger characterIndex = [layoutManager characterIndexForPoint:point
|
|
inTextContainer:textContainer
|
|
fractionOfDistanceBetweenInsertionPoints:&fraction];
|
|
|
|
// If the point is not before (fraction == 0.0) the first character and not
|
|
// after (fraction == 1.0) the last character, then the attribute is valid.
|
|
if (_textStorage.length > 0 && (fraction > 0 || characterIndex > 0) && (fraction < 1 || characterIndex < _textStorage.length - 1)) {
|
|
reactTag = [_textStorage attribute:RCTTextAttributesTagAttributeName atIndex:characterIndex effectiveRange:NULL];
|
|
}
|
|
|
|
return reactTag;
|
|
}
|
|
|
|
- (void)didMoveToWindow
|
|
{
|
|
[super didMoveToWindow];
|
|
|
|
if (!self.window) {
|
|
self.layer.contents = nil;
|
|
if (_highlightLayer) {
|
|
[_highlightLayer removeFromSuperlayer];
|
|
_highlightLayer = nil;
|
|
}
|
|
} else if (_textStorage) {
|
|
[self setNeedsDisplay];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Accessibility
|
|
|
|
- (NSString *)accessibilityLabel
|
|
{
|
|
NSString *superAccessibilityLabel = [super accessibilityLabel];
|
|
if (superAccessibilityLabel) {
|
|
return superAccessibilityLabel;
|
|
}
|
|
return _textStorage.string;
|
|
}
|
|
|
|
#pragma mark - Context Menu
|
|
|
|
- (void)enableContextMenu
|
|
{
|
|
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
|
|
[self addGestureRecognizer:_longPressGestureRecognizer];
|
|
}
|
|
|
|
- (void)disableContextMenu
|
|
{
|
|
[self removeGestureRecognizer:_longPressGestureRecognizer];
|
|
_longPressGestureRecognizer = nil;
|
|
}
|
|
|
|
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
|
|
{
|
|
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
|
|
#if !TARGET_OS_TV && !TARGET_OS_UIKITFORMAC
|
|
UIMenuController *menuController = [UIMenuController sharedMenuController];
|
|
|
|
if (menuController.isMenuVisible) {
|
|
return;
|
|
}
|
|
|
|
if (!self.isFirstResponder) {
|
|
[self becomeFirstResponder];
|
|
}
|
|
|
|
[menuController setTargetRect:self.bounds inView:self];
|
|
[menuController setMenuVisible:YES animated:YES];
|
|
#endif
|
|
}
|
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
{
|
|
return _selectable;
|
|
}
|
|
|
|
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
|
|
{
|
|
if (_selectable && action == @selector(copy:)) {
|
|
return YES;
|
|
}
|
|
|
|
return [self.nextResponder canPerformAction:action withSender:sender];
|
|
}
|
|
|
|
- (void)copy:(id)sender
|
|
{
|
|
#if !TARGET_OS_TV
|
|
NSAttributedString *attributedText = _textStorage;
|
|
|
|
NSMutableDictionary *item = [NSMutableDictionary new];
|
|
|
|
NSData *rtf = [attributedText dataFromRange:NSMakeRange(0, attributedText.length)
|
|
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
|
|
error:nil];
|
|
|
|
if (rtf) {
|
|
[item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
|
|
}
|
|
|
|
[item setObject:attributedText.string forKey:(id)kUTTypeUTF8PlainText];
|
|
|
|
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
|
pasteboard.items = @[item];
|
|
#endif
|
|
}
|
|
|
|
@end
|