Skip to content

Commit

Permalink
Extract some helper methods in ReactBaseTextShadowNode
Browse files Browse the repository at this point in the history
...for consistency with the de-duplicated code
  • Loading branch information
cubuspl42 committed Sep 25, 2023
1 parent 05b5fd5 commit fa79677
Showing 1 changed file with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,41 +237,12 @@ private static void buildSpannedFromShadowNode(
supportsInlineViews,
inlineViews,
sb.length());
} else if (child instanceof ReactTextInlineImageShadowNode) {
// We make the image take up 1 character in the span and put a corresponding character into
// the text so that the image doesn't run over any following text.
sb.append(INLINE_VIEW_PLACEHOLDER);
ops.add(
new SetSpanOperation(
sb.length() - INLINE_VIEW_PLACEHOLDER.length(),
sb.length(),
((ReactTextInlineImageShadowNode) child).buildInlineImageSpan()));
} else if (child instanceof ReactTextInlineImageShadowNode inlineImageChild) {
addInlineImageSpan(ops, sb, inlineImageChild);
} else if (supportsInlineViews) {
int reactTag = child.getReactTag();
YogaValue widthValue = child.getStyleWidth();
YogaValue heightValue = child.getStyleHeight();

float width;
float height;
if (widthValue.unit != YogaUnit.POINT || heightValue.unit != YogaUnit.POINT) {
// If the measurement of the child isn't calculated, we calculate the layout for the
// view using Yoga
child.calculateLayout();
width = child.getLayoutWidth();
height = child.getLayoutHeight();
} else {
width = widthValue.value;
height = heightValue.value;
}

// We make the inline view take up 1 character in the span and put a corresponding character
// into
// the text so that the inline view doesn't run over any following text.
sb.append(INLINE_VIEW_PLACEHOLDER);
addInlineViewPlaceholderSpan(ops, sb, child);

TextLayoutUtils.addInlineViewPlaceholderSpan(ops, sb, reactTag, width, height);

inlineViews.put(reactTag, child);
inlineViews.put(child.getReactTag(), child);
} else {
throw new IllegalViewOperationException(
"Unexpected view type nested under a <Text> or <TextInput> node: " + child.getClass());
Expand All @@ -287,6 +258,40 @@ private static void buildSpannedFromShadowNode(
}
}

private static void addInlineImageSpan(List<SetSpanOperation> ops, SpannableStringBuilder sb,
ReactTextInlineImageShadowNode child) {
// We make the image take up 1 character in the span and put a corresponding character into
// the text so that the image doesn't run over any following text.
sb.append(INLINE_VIEW_PLACEHOLDER);
ops.add(new SetSpanOperation(sb.length() - INLINE_VIEW_PLACEHOLDER.length(), sb.length(),
child.buildInlineImageSpan()));
}

private static void addInlineViewPlaceholderSpan(List<SetSpanOperation> ops, SpannableStringBuilder sb,
ReactShadowNode child) {
YogaValue widthValue = child.getStyleWidth();
YogaValue heightValue = child.getStyleHeight();

float width;
float height;
if (widthValue.unit != YogaUnit.POINT || heightValue.unit != YogaUnit.POINT) {
// If the measurement of the child isn't calculated, we calculate the layout for the
// view using Yoga
child.calculateLayout();
width = child.getLayoutWidth();
height = child.getLayoutHeight();
} else {
width = widthValue.value;
height = heightValue.value;
}

// We make the inline view take up 1 character in the span and put a corresponding character into the text so that
// the inline view doesn't run over any following text.
sb.append(INLINE_VIEW_PLACEHOLDER);

TextLayoutUtils.addInlineViewPlaceholderSpan(ops, sb, child.getReactTag(), width, height);
}

// `nativeViewHierarchyOptimizer` can be `null` as long as `supportsInlineViews` is `false`.
protected Spannable spannedFromShadowNode(
ReactBaseTextShadowNode textShadowNode,
Expand Down

0 comments on commit fa79677

Please sign in to comment.