-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prototype(FastText): Add option to use TextBlock for simple text #1256
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f9c5b0
prototype(FastText): Add option to use TextBlock for simple text
rozele 4f6d246
fix(FastText): adds WPF support for RCTFastText
rozele 6d448f1
refactor(FastText): Renaming to RCTSimpleText
rozele 19c2f53
Change file names to SimpleText
rozele 84c3423
Remove dependency on Platform value
rozele 51c6bdb
Minor fix for variable name.
rozele File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @providesModule Text | ||
* @flow | ||
* @format | ||
*/ | ||
'use strict'; | ||
|
||
const React = require('React'); | ||
const ReactNative = require('ReactNative'); | ||
const ReactNativeViewAttributes = require('ReactNativeViewAttributes'); | ||
const TextPropTypes = require('TextPropTypes'); | ||
const Touchable = require('Touchable'); | ||
const UIManager = require('UIManager'); | ||
|
||
const createReactNativeComponentClass = require('createReactNativeComponentClass'); | ||
const mergeFast = require('mergeFast'); | ||
const processColor = require('processColor'); | ||
const {ViewContextTypes} = require('ViewContext'); | ||
|
||
import type {PressEvent} from 'CoreEventTypes'; | ||
import type {TextProps} from 'TextProps'; | ||
import type {ViewChildContext} from 'ViewContext'; | ||
|
||
type State = { | ||
isHighlighted: boolean, | ||
}; | ||
|
||
type RectOffset = { | ||
top: number, | ||
left: number, | ||
right: number, | ||
bottom: number, | ||
}; | ||
|
||
const PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30}; | ||
|
||
const viewConfig = { | ||
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, { | ||
isHighlighted: true, | ||
numberOfLines: true, | ||
ellipsizeMode: true, | ||
allowFontScaling: true, | ||
disabled: true, | ||
selectable: true, | ||
selectionColor: true, | ||
adjustsFontSizeToFit: true, | ||
minimumFontScale: true, | ||
textBreakStrategy: true, | ||
}), | ||
uiViewClassName: 'RCTText', | ||
}; | ||
|
||
const simpleViewConfig = { | ||
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, { | ||
isHighlighted: true, | ||
numberOfLines: true, | ||
ellipsizeMode: true, | ||
allowFontScaling: true, | ||
disabled: true, | ||
selectable: true, | ||
selectionColor: true, | ||
adjustsFontSizeToFit: true, | ||
minimumFontScale: true, | ||
textBreakStrategy: true, | ||
text: true, | ||
}), | ||
uiViewClassName: 'RCTSimpleText', | ||
}; | ||
|
||
/** | ||
* A React component for displaying text. | ||
* | ||
* See https://facebook.github.io/react-native/docs/text.html | ||
*/ | ||
class Text extends ReactNative.NativeComponent<TextProps, State> { | ||
static propTypes = TextPropTypes; | ||
static childContextTypes = ViewContextTypes; | ||
static contextTypes = ViewContextTypes; | ||
|
||
static defaultProps = { | ||
accessible: true, | ||
allowFontScaling: true, | ||
ellipsizeMode: 'tail', | ||
}; | ||
|
||
state = mergeFast(Touchable.Mixin.touchableGetInitialState(), { | ||
isHighlighted: false, | ||
}); | ||
|
||
viewConfig = viewConfig; | ||
|
||
getChildContext(): ViewChildContext { | ||
return { | ||
isInAParentText: true, | ||
}; | ||
} | ||
|
||
_handlers: ?Object; | ||
|
||
_hasPressHandler(): boolean { | ||
return !!this.props.onPress || !!this.props.onLongPress; | ||
} | ||
/** | ||
* These are assigned lazily the first time the responder is set to make plain | ||
* text nodes as cheap as possible. | ||
*/ | ||
touchableHandleActivePressIn: ?Function; | ||
touchableHandleActivePressOut: ?Function; | ||
touchableHandlePress: ?Function; | ||
touchableHandleLongPress: ?Function; | ||
touchableHandleResponderGrant: ?Function; | ||
touchableHandleResponderMove: ?Function; | ||
touchableHandleResponderRelease: ?Function; | ||
touchableHandleResponderTerminate: ?Function; | ||
touchableHandleResponderTerminationRequest: ?Function; | ||
touchableGetPressRectOffset: ?Function; | ||
|
||
render(): React.Element<any> { | ||
let newProps = this.props; | ||
if (this.props.onStartShouldSetResponder || this._hasPressHandler()) { | ||
if (!this._handlers) { | ||
this._handlers = { | ||
onStartShouldSetResponder: (): boolean => { | ||
const shouldSetFromProps = | ||
this.props.onStartShouldSetResponder && | ||
this.props.onStartShouldSetResponder(); | ||
const setResponder = shouldSetFromProps || this._hasPressHandler(); | ||
if (setResponder && !this.touchableHandleActivePressIn) { | ||
// Attach and bind all the other handlers only the first time a touch | ||
// actually happens. | ||
for (const key in Touchable.Mixin) { | ||
if (typeof Touchable.Mixin[key] === 'function') { | ||
(this: any)[key] = Touchable.Mixin[key].bind(this); | ||
} | ||
} | ||
this.touchableHandleActivePressIn = () => { | ||
if ( | ||
this.props.suppressHighlighting || | ||
!this._hasPressHandler() | ||
) { | ||
return; | ||
} | ||
this.setState({ | ||
isHighlighted: true, | ||
}); | ||
}; | ||
|
||
this.touchableHandleActivePressOut = () => { | ||
if ( | ||
this.props.suppressHighlighting || | ||
!this._hasPressHandler() | ||
) { | ||
return; | ||
} | ||
this.setState({ | ||
isHighlighted: false, | ||
}); | ||
}; | ||
|
||
this.touchableHandlePress = (e: PressEvent) => { | ||
this.props.onPress && this.props.onPress(e); | ||
}; | ||
|
||
this.touchableHandleLongPress = (e: PressEvent) => { | ||
this.props.onLongPress && this.props.onLongPress(e); | ||
}; | ||
|
||
this.touchableGetPressRectOffset = function(): RectOffset { | ||
return this.props.pressRetentionOffset || PRESS_RECT_OFFSET; | ||
}; | ||
} | ||
return setResponder; | ||
}, | ||
onResponderGrant: function(e: SyntheticEvent<>, dispatchID: string) { | ||
// $FlowFixMe TouchableMixin handlers couldn't actually be null | ||
this.touchableHandleResponderGrant(e, dispatchID); | ||
this.props.onResponderGrant && | ||
this.props.onResponderGrant.apply(this, arguments); | ||
}.bind(this), | ||
onResponderMove: function(e: SyntheticEvent<>) { | ||
// $FlowFixMe TouchableMixin handlers couldn't actually be null | ||
this.touchableHandleResponderMove(e); | ||
this.props.onResponderMove && | ||
this.props.onResponderMove.apply(this, arguments); | ||
}.bind(this), | ||
onResponderRelease: function(e: SyntheticEvent<>) { | ||
// $FlowFixMe TouchableMixin handlers couldn't actually be null | ||
this.touchableHandleResponderRelease(e); | ||
this.props.onResponderRelease && | ||
this.props.onResponderRelease.apply(this, arguments); | ||
}.bind(this), | ||
onResponderTerminate: function(e: SyntheticEvent<>) { | ||
// $FlowFixMe TouchableMixin handlers couldn't actually be null | ||
this.touchableHandleResponderTerminate(e); | ||
this.props.onResponderTerminate && | ||
this.props.onResponderTerminate.apply(this, arguments); | ||
}.bind(this), | ||
onResponderTerminationRequest: function(): boolean { | ||
// Allow touchable or props.onResponderTerminationRequest to deny | ||
// the request | ||
// $FlowFixMe TouchableMixin handlers couldn't actually be null | ||
var allowTermination = this.touchableHandleResponderTerminationRequest(); | ||
if (allowTermination && this.props.onResponderTerminationRequest) { | ||
allowTermination = this.props.onResponderTerminationRequest.apply( | ||
this, | ||
arguments, | ||
); | ||
} | ||
return allowTermination; | ||
}.bind(this), | ||
}; | ||
} | ||
newProps = { | ||
...this.props, | ||
...this._handlers, | ||
isHighlighted: this.state.isHighlighted, | ||
}; | ||
} | ||
if (newProps.selectionColor != null) { | ||
newProps = { | ||
...newProps, | ||
selectionColor: processColor(newProps.selectionColor), | ||
}; | ||
} | ||
if (Touchable.TOUCH_TARGET_DEBUG && newProps.onPress) { | ||
newProps = { | ||
...newProps, | ||
style: [this.props.style, {color: 'magenta'}], | ||
}; | ||
} | ||
if (this.context.isInAParentText) { | ||
return <RCTVirtualText {...newProps} />; | ||
} else { | ||
if (RCTSimpleText && typeof newProps.children === 'string') { | ||
let simpleProps = {}; | ||
Object.assign(simpleProps, newProps); | ||
simpleProps.text = simpleProps.children; | ||
delete simpleProps.children; | ||
return <RCTSimpleText {...simpleProps} />; | ||
} | ||
|
||
return <RCTText {...newProps} />; | ||
} | ||
} | ||
} | ||
|
||
var RCTText = createReactNativeComponentClass( | ||
viewConfig.uiViewClassName, | ||
() => viewConfig, | ||
); | ||
var RCTVirtualText = RCTText; | ||
var RCTSimpleText; | ||
|
||
if (UIManager.RCTVirtualText) { | ||
RCTVirtualText = createReactNativeComponentClass('RCTVirtualText', () => ({ | ||
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, { | ||
isHighlighted: true, | ||
}), | ||
uiViewClassName: 'RCTVirtualText', | ||
})); | ||
} | ||
|
||
if (UIManager.RCTSimpleText) { | ||
RCTSimpleText = createReactNativeComponentClass( | ||
simpleViewConfig.uiViewClassName, | ||
() => simpleViewConfig, | ||
); | ||
} | ||
|
||
module.exports = Text; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ | |
<Compile Include="Views\Scroll\ScrollView.cs" /> | ||
<Compile Include="Views\Slider\ReactSliderManager.cs" /> | ||
<Compile Include="Views\TextInput\PlaceholderAdorner.cs" /> | ||
<Compile Include="Views\Text\ReactSimpleTextViewManager.cs" /> | ||
<Compile Include="Views\Text\ReactTextCompoundView.cs" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rozele Where's the shadow node? [edit]: Oh, it's a dummy implementation for WPF? |
||
<Compile Include="Views\Text\ReactTextShadowNode.cs" /> | ||
<Compile Include="Views\Text\ReactTextViewManager.cs" /> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
ReactWindows/ReactNative.Net46/Views/Text/ReactSimpleTextViewManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Windows.Controls; | ||
|
||
namespace ReactNative.Views.Text | ||
{ | ||
/// <summary> | ||
/// The view manager for simple text views. | ||
/// </summary> | ||
public class ReactSimpleTextViewManager : ReactTextViewManager | ||
{ | ||
/// <summary> | ||
/// Name of the view manager. | ||
/// </summary> | ||
public override string Name | ||
{ | ||
get | ||
{ | ||
return "RCTSimpleText"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the number of children for the view. | ||
/// </summary> | ||
/// <param name="parent">The view parent.</param> | ||
/// <returns>The number of children.</returns> | ||
public override int GetChildCount(TextBlock parent) | ||
{ | ||
return 0; | ||
} | ||
|
||
/// <summary> | ||
/// Removes the children from the view. | ||
/// </summary> | ||
/// <param name="parent">The view parent.</param> | ||
public override void RemoveAllChildren(TextBlock parent) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be an array of strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reseul - not sure if we are supposed to handle the array of strings. I don't know what the concatenation handling should be, whether we just inject a space, newline, etc. Also, I imagine this can vary based on culture/i18n. I'd like to keep this as a single space for now and force the client to specifically concatenate string arrays if they want text perf optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rozele it's ok. We've been using this fast text for some months, but I'm not sure whether we have that case. If we do. we're going to reconcile on our end.