forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReactTextViewManager.java
210 lines (180 loc) · 7.21 KB
/
ReactTextViewManager.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.text;
import android.content.Context;
import android.text.Spannable;
import androidx.annotation.Nullable;
import com.facebook.react.R;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableNativeMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.common.mapbuffer.ReadableMapBuffer;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.IViewManagerWithChildren;
import com.facebook.react.uimanager.ReactAccessibilityDelegate;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.StateWrapper;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.yoga.YogaMeasureMode;
import java.util.HashMap;
import java.util.Map;
/**
* Concrete class for {@link ReactTextAnchorViewManager} which represents view managers of anchor
* {@code <Text>} nodes.
*/
@ReactModule(name = ReactTextViewManager.REACT_CLASS)
public class ReactTextViewManager
extends ReactTextAnchorViewManager<ReactTextView, ReactTextShadowNode>
implements IViewManagerWithChildren {
private static final short TX_STATE_KEY_ATTRIBUTED_STRING = 0;
private static final short TX_STATE_KEY_PARAGRAPH_ATTRIBUTES = 1;
// used for text input
private static final short TX_STATE_KEY_HASH = 2;
private static final short TX_STATE_KEY_MOST_RECENT_EVENT_COUNT = 3;
@VisibleForTesting public static final String REACT_CLASS = "RCTText";
protected @Nullable ReactTextViewManagerCallback mReactTextViewManagerCallback;
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public ReactTextView createViewInstance(ThemedReactContext context) {
return new ReactTextView(context);
}
@Override
public void updateExtraData(ReactTextView view, Object extraData) {
ReactTextUpdate update = (ReactTextUpdate) extraData;
Spannable spannable = update.getText();
if (update.containsImages()) {
TextInlineImageSpan.possiblyUpdateInlineImageSpans(spannable, view);
}
view.setText(update);
// If this text view contains any clickable spans, set a view tag and reset the accessibility
// delegate so that these can be picked up by the accessibility system.
ReactClickableSpan[] clickableSpans =
spannable.getSpans(0, update.getText().length(), ReactClickableSpan.class);
if (clickableSpans.length > 0) {
view.setTag(
R.id.accessibility_links,
new ReactAccessibilityDelegate.AccessibilityLinks(clickableSpans, spannable));
ReactAccessibilityDelegate.resetDelegate(
view, view.isFocusable(), view.getImportantForAccessibility());
}
}
@Override
public ReactTextShadowNode createShadowNodeInstance() {
return new ReactTextShadowNode();
}
public ReactTextShadowNode createShadowNodeInstance(
@Nullable ReactTextViewManagerCallback reactTextViewManagerCallback) {
return new ReactTextShadowNode(reactTextViewManagerCallback);
}
@Override
public Class<ReactTextShadowNode> getShadowNodeClass() {
return ReactTextShadowNode.class;
}
@Override
protected void onAfterUpdateTransaction(ReactTextView view) {
super.onAfterUpdateTransaction(view);
view.updateView();
}
public boolean needsCustomLayoutForChildren() {
return true;
}
@Override
public Object updateState(
ReactTextView view, ReactStylesDiffMap props, @Nullable StateWrapper stateWrapper) {
if (stateWrapper == null) {
return null;
}
if (ReactFeatureFlags.isMapBufferSerializationEnabled()) {
ReadableMapBuffer stateMapBuffer = stateWrapper.getStateDataMapBuffer();
if (stateMapBuffer != null) {
return getReactTextUpdate(view, props, stateMapBuffer);
}
}
ReadableNativeMap state = stateWrapper.getStateData();
if (state == null) {
return null;
}
ReadableMap attributedString = state.getMap("attributedString");
ReadableMap paragraphAttributes = state.getMap("paragraphAttributes");
Spannable spanned =
TextLayoutManager.getOrCreateSpannableForText(
view.getContext(), attributedString, mReactTextViewManagerCallback);
view.setSpanned(spanned);
int textBreakStrategy =
TextAttributeProps.getTextBreakStrategy(paragraphAttributes.getString("textBreakStrategy"));
return new ReactTextUpdate(
spanned,
state.hasKey("mostRecentEventCount") ? state.getInt("mostRecentEventCount") : -1,
false, // TODO add this into local Data
TextAttributeProps.getTextAlignment(props, TextLayoutManager.isRTL(attributedString)),
textBreakStrategy,
TextAttributeProps.getJustificationMode(props));
}
private Object getReactTextUpdate(
ReactTextView view, ReactStylesDiffMap props, ReadableMapBuffer state) {
ReadableMapBuffer attributedString = state.getMapBuffer(TX_STATE_KEY_ATTRIBUTED_STRING);
ReadableMapBuffer paragraphAttributes = state.getMapBuffer(TX_STATE_KEY_PARAGRAPH_ATTRIBUTES);
Spannable spanned =
TextLayoutManagerMapBuffer.getOrCreateSpannableForText(
view.getContext(), attributedString, mReactTextViewManagerCallback);
view.setSpanned(spanned);
int textBreakStrategy =
TextAttributeProps.getTextBreakStrategy(
paragraphAttributes.getString(TextLayoutManagerMapBuffer.PA_KEY_TEXT_BREAK_STRATEGY));
return new ReactTextUpdate(
spanned,
-1, // UNUSED FOR TEXT
false, // TODO add this into local Data
TextAttributeProps.getTextAlignment(
props, TextLayoutManagerMapBuffer.isRTL(attributedString)),
textBreakStrategy,
TextAttributeProps.getJustificationMode(props));
}
@Override
public @Nullable Map getExportedCustomDirectEventTypeConstants() {
@Nullable
Map<String, Object> baseEventTypeConstants = super.getExportedCustomDirectEventTypeConstants();
Map<String, Object> eventTypeConstants =
baseEventTypeConstants == null ? new HashMap<String, Object>() : baseEventTypeConstants;
eventTypeConstants.putAll(
MapBuilder.of(
"topTextLayout", MapBuilder.of("registrationName", "onTextLayout"),
"topInlineViewLayout", MapBuilder.of("registrationName", "onInlineViewLayout")));
return eventTypeConstants;
}
@Override
public long measure(
Context context,
ReadableMap localData,
ReadableMap props,
ReadableMap state,
float width,
YogaMeasureMode widthMode,
float height,
YogaMeasureMode heightMode,
@Nullable float[] attachmentsPositions) {
return TextLayoutManager.measureText(
context,
localData,
props,
width,
widthMode,
height,
heightMode,
mReactTextViewManagerCallback,
attachmentsPositions);
}
@Override
public void setPadding(ReactTextView view, int left, int top, int right, int bottom) {
view.setPadding(left, top, right, bottom);
}
}