Skip to content

Commit 7a2c2bd

Browse files
authored
chore: prepare android support (#102)
1 parent e49a5d3 commit 7a2c2bd

11 files changed

Lines changed: 497 additions & 459 deletions
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//
2-
// NitroTextComponentDescriptor.mm
3-
// Implementation for custom ComponentDescriptor
2+
// NitroTextComponentDescriptor.cpp
3+
// Shared implementation for custom ComponentDescriptor
44
//
55

6-
#import "NitroTextComponentDescriptor.hpp"
7-
#import <Foundation/Foundation.h>
6+
#include "NitroTextComponentDescriptor.hpp"
7+
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
88

99
using namespace facebook;
1010
using namespace margelo::nitro::nitrotext::views;

cpp/NitroTextShadowNode.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
#include "NitroTextShadowNode.hpp"
7+
#include "NitroTextUtil.hpp"
78

89
#include <cmath>
910
#include <optional>
@@ -366,14 +367,9 @@ react::Size NitroTextShadowNode::measureContent(
366367
// but leaving the scale here avoids over-adjusting and potential divergences.
367368
if (props.minimumFontScale.value.has_value())
368369
{
369-
#if defined(REACT_NATIVE_VERSION_MAJOR)
370-
#if (REACT_NATIVE_VERSION_MAJOR > 0) || \
371-
(REACT_NATIVE_VERSION_MAJOR == 0 && REACT_NATIVE_VERSION_MINOR >= 81)
370+
#if RN_VERSION_AT_LEAST(0, 81)
372371
paragraphAttributes.minimumFontScale =
373372
props.minimumFontScale.value.value();
374-
#else
375-
// React Native < 0.81 does not expose paragraphAttributes.minimumFontScale yet.
376-
#endif
377373
#endif
378374
}
379375

cpp/NitroTextUtil.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// NitroTextUtil.cpp
3+
// Utility functions for NitroText
4+
//
5+
6+
#include "NitroTextUtil.hpp"
7+
8+
// Currently only contains macros, so this file is mostly empty.
9+
// Kept for future utility functions if needed.
10+

cpp/NitroTextUtil.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// NitroTextUtil.hpp
3+
// Utility macros for React Native version checking
4+
//
5+
6+
#pragma once
7+
8+
#if __has_include(<cxxreact/ReactNativeVersion.h>)
9+
#include <cxxreact/ReactNativeVersion.h>
10+
#endif
11+
12+
/**
13+
* @spec RN_VERSION_AT_LEAST
14+
* @brief Checks if React Native version is >= (major.minor)
15+
* @param major Major version number (e.g., 0, 1, 2)
16+
* @param minor Minor version number (e.g., 81, 82, 83)
17+
* @return Non-zero if version >= (major.minor), 0 otherwise
18+
*
19+
* @example
20+
* ```cpp
21+
* #if RN_VERSION_AT_LEAST(0, 81)
22+
* // Code for RN >= 0.81
23+
* #endif
24+
* ```
25+
*/
26+
#if defined(REACT_NATIVE_VERSION_MAJOR)
27+
#define RN_VERSION_AT_LEAST(major, minor) \
28+
((REACT_NATIVE_VERSION_MAJOR > (major)) || \
29+
(REACT_NATIVE_VERSION_MAJOR == (major) && REACT_NATIVE_VERSION_MINOR >= (minor)))
30+
#else
31+
#define RN_VERSION_AT_LEAST(major, minor) 0
32+
#endif
33+

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default defineConfig([
5353
{
5454
"quoteProps": "consistent",
5555
"singleQuote": true,
56-
"tabWidth": 4,
56+
"tabWidth": 3,
5757
"trailingComma": "es5",
5858
"useTabs": false,
5959
"semi": false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"prettier": {
9393
"quoteProps": "consistent",
9494
"singleQuote": true,
95-
"tabWidth": 4,
95+
"tabWidth": 3,
9696
"trailingComma": "es5",
9797
"useTabs": false,
9898
"semi": false

src/nitro-text.tsx

Lines changed: 95 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import React, { useCallback } from 'react'
22
import {
3-
Platform,
4-
Text,
5-
type TextLayoutEvent,
6-
type TextProps,
7-
unstable_TextAncestorContext,
3+
Platform,
4+
Text,
5+
type TextLayoutEvent,
6+
type TextProps,
7+
unstable_TextAncestorContext,
88
} from 'react-native'
99

1010
import {
11-
callback,
12-
getHostComponent,
13-
type HybridRef,
11+
callback,
12+
getHostComponent,
13+
type HybridRef,
1414
} from 'react-native-nitro-modules'
1515
import NitroTextConfig from '../nitrogen/generated/shared/json/NitroTextConfig.json'
1616
import type { NitroTextMethods, NitroTextProps } from './specs/nitro-text.nitro'
@@ -19,123 +19,123 @@ import { flattenChildrenToFragments, styleToFragment } from './utils'
1919
export type NitroTextRef = HybridRef<NitroTextProps, NitroTextMethods>
2020

2121
const NitroTextView = getHostComponent<NitroTextProps, NitroTextMethods>(
22-
'NitroText',
23-
() => NitroTextConfig
22+
'NitroText',
23+
() => NitroTextConfig
2424
)
2525

2626
type NitroTextPropsWithEvents = Pick<
27-
NitroTextProps,
28-
'onTextLayout' | 'onPress' | 'onPressIn' | 'onPressOut' | 'menus'
27+
NitroTextProps,
28+
'onTextLayout' | 'onPress' | 'onPressIn' | 'onPressOut' | 'menus'
2929
> &
30-
Omit<TextProps, 'onTextLayout'>
30+
Omit<TextProps, 'onTextLayout'>
3131

3232
let TextAncestorContext = unstable_TextAncestorContext
3333
if (
34-
Platform.constants.reactNativeVersion.major > 0 ||
35-
(Platform.constants.reactNativeVersion.major === 0 &&
36-
Platform.constants.reactNativeVersion.minor < 81)
34+
Platform.constants.reactNativeVersion.major > 0 ||
35+
(Platform.constants.reactNativeVersion.major === 0 &&
36+
Platform.constants.reactNativeVersion.minor < 81)
3737
) {
38-
TextAncestorContext = require('react-native/Libraries/Text/TextAncestor')
38+
TextAncestorContext = require('react-native/Libraries/Text/TextAncestor')
3939
}
4040
export const NitroText = (props: NitroTextPropsWithEvents) => {
41-
const isInsideRNText = React.useContext(TextAncestorContext)
42-
const {
43-
children,
44-
style,
45-
selectable,
46-
selectionColor,
47-
onTextLayout,
48-
onPress,
49-
onPressIn,
50-
onPressOut,
51-
onLongPress,
52-
...rest
53-
} = props
41+
const isInsideRNText = React.useContext(TextAncestorContext)
42+
const {
43+
children,
44+
style,
45+
selectable,
46+
selectionColor,
47+
onTextLayout,
48+
onPress,
49+
onPressIn,
50+
onPressOut,
51+
onLongPress,
52+
...rest
53+
} = props
5454

55-
const isSimpleText =
56-
typeof children === 'string' || typeof children === 'number'
55+
const isSimpleText =
56+
typeof children === 'string' || typeof children === 'number'
5757

58-
const fragments = React.useMemo(() => {
59-
if (isSimpleText) return []
60-
return flattenChildrenToFragments(children, style)
61-
}, [children, style, isSimpleText])
58+
const fragments = React.useMemo(() => {
59+
if (isSimpleText) return []
60+
return flattenChildrenToFragments(children, style)
61+
}, [children, style, isSimpleText])
6262

63-
const onRNTextLayout = useCallback(
64-
(e: TextLayoutEvent) => {
65-
onTextLayout?.(e.nativeEvent)
66-
},
67-
[onTextLayout]
68-
)
63+
const onRNTextLayout = useCallback(
64+
(e: TextLayoutEvent) => {
65+
onTextLayout?.(e.nativeEvent)
66+
},
67+
[onTextLayout]
68+
)
6969

70-
if (isInsideRNText || Platform.OS === 'android') {
71-
return (
72-
<Text
73-
{...rest}
74-
selectionColor={selectionColor as any}
75-
onPress={onPress}
76-
onPressIn={onPressIn}
77-
onPressOut={onPressOut}
78-
onLongPress={onLongPress}
79-
selectable={selectable}
80-
style={style}
81-
onTextLayout={onRNTextLayout}
82-
>
83-
{children}
84-
</Text>
85-
)
86-
}
87-
88-
const topStyles = styleToFragment(style || undefined)
70+
if (isInsideRNText || Platform.OS === 'android') {
71+
return (
72+
<Text
73+
{...rest}
74+
selectionColor={selectionColor as any}
75+
onPress={onPress}
76+
onPressIn={onPressIn}
77+
onPressOut={onPressOut}
78+
onLongPress={onLongPress}
79+
selectable={selectable}
80+
style={style}
81+
onTextLayout={onRNTextLayout}
82+
>
83+
{children}
84+
</Text>
85+
)
86+
}
8987

90-
if (isSimpleText) {
91-
return (
92-
<NitroTextView
93-
{...rest}
94-
selectable={selectable}
95-
fontFamily={topStyles.fontFamily}
96-
selectionColor={selectionColor as string}
97-
text={String(children)}
98-
style={style}
99-
fontColor={topStyles.fontColor}
100-
textAlign={topStyles.textAlign}
101-
textTransform={topStyles.textTransform}
102-
fontSize={topStyles.fontSize}
103-
fontWeight={topStyles.fontWeight}
104-
fontStyle={topStyles.fontStyle}
105-
lineHeight={topStyles.lineHeight}
106-
letterSpacing={topStyles.letterSpacing}
107-
textDecorationLine={topStyles.textDecorationLine}
108-
textDecorationColor={topStyles.textDecorationColor}
109-
textDecorationStyle={topStyles.textDecorationStyle}
110-
onTextLayout={callback(onTextLayout)}
111-
onPress={callback(onPress)}
112-
onPressIn={callback(onPressIn)}
113-
onPressOut={callback(onPressOut)}
114-
/>
115-
)
116-
}
88+
const topStyles = styleToFragment(style || undefined)
11789

118-
return (
119-
<NitroTextView
90+
if (isSimpleText) {
91+
return (
92+
<NitroTextView
12093
{...rest}
12194
selectable={selectable}
122-
fragments={fragments}
12395
fontFamily={topStyles.fontFamily}
12496
selectionColor={selectionColor as string}
97+
text={String(children)}
12598
style={style}
12699
fontColor={topStyles.fontColor}
100+
textAlign={topStyles.textAlign}
101+
textTransform={topStyles.textTransform}
102+
fontSize={topStyles.fontSize}
127103
fontWeight={topStyles.fontWeight}
128104
fontStyle={topStyles.fontStyle}
105+
lineHeight={topStyles.lineHeight}
129106
letterSpacing={topStyles.letterSpacing}
130-
textAlign={topStyles.textAlign}
131-
textTransform={topStyles.textTransform}
132107
textDecorationLine={topStyles.textDecorationLine}
133108
textDecorationColor={topStyles.textDecorationColor}
134109
textDecorationStyle={topStyles.textDecorationStyle}
135110
onTextLayout={callback(onTextLayout)}
136111
onPress={callback(onPress)}
137112
onPressIn={callback(onPressIn)}
138113
onPressOut={callback(onPressOut)}
139-
/>
140-
)
114+
/>
115+
)
116+
}
117+
118+
return (
119+
<NitroTextView
120+
{...rest}
121+
selectable={selectable}
122+
fragments={fragments}
123+
fontFamily={topStyles.fontFamily}
124+
selectionColor={selectionColor as string}
125+
style={style}
126+
fontColor={topStyles.fontColor}
127+
fontWeight={topStyles.fontWeight}
128+
fontStyle={topStyles.fontStyle}
129+
letterSpacing={topStyles.letterSpacing}
130+
textAlign={topStyles.textAlign}
131+
textTransform={topStyles.textTransform}
132+
textDecorationLine={topStyles.textDecorationLine}
133+
textDecorationColor={topStyles.textDecorationColor}
134+
textDecorationStyle={topStyles.textDecorationStyle}
135+
onTextLayout={callback(onTextLayout)}
136+
onPress={callback(onPress)}
137+
onPressIn={callback(onPressIn)}
138+
onPressOut={callback(onPressOut)}
139+
/>
140+
)
141141
}

0 commit comments

Comments
 (0)