Open History to see the last commit
1import React from "react";
2import { Platform, StyleSheet, Text as RNText, type TextProps, type ViewStyle } from "react-native";
3import T3MarkdownTextRunNativeComponent from "./T3MarkdownTextRunNativeComponent";
4import T3MarkdownTextNativeComponent from "./T3MarkdownTextNativeComponent";
5import { flattenStyles } from "./util";
6
7const TextAncestorContext = React.createContext<[boolean, ViewStyle]>([
8 false,
9 StyleSheet.create({}),
10]);
11
12const textDefaults = {
13 allowFontScaling: true,
14 selectable: true,
15} satisfies TextProps;
16
17const useTextAncestorContext = () => React.useContext(TextAncestorContext);
18
19/**
20 * Event fired by `onSelectionChange`. `start`/`end` are 0-based UTF-16 indices
21 * into the rendered string. `start === end` means the selection was cleared.
22 */
23export type SelectionChangeEvent = {
24 nativeEvent: { target: number; start: number; end: number };
25};
26
27export type ContextMenuActionEvent = {
28 nativeEvent: { target: number; actionIdentifier: string };
29};
30
31/**
32 * `onTextLayout` is not offered: the native view reports plain line strings
33 * while the React Native Text fallback reports measured `TextLayoutLine`s.
34 */
35export type MarkdownTextPrimitiveProps = Omit<TextProps, "onTextLayout"> & {
36 uiTextView?: boolean;
37 contextMenuConfig?: string;
38 onContextMenuAction?: (event: ContextMenuActionEvent) => void;
39 /**
40 * Fired when the native text selection changes. Only fires on iOS when
41 * `uiTextView` is true. Note: fires on every selection-edge adjustment
42 * (e.g. dragging a selection handle), so consumers driving expensive work
43 * off this event should debounce.
44 */
45 onSelectionChange?: (event: SelectionChangeEvent) => void;
46};
47
48function MarkdownTextPrimitiveChild({ style, children, ...rest }: MarkdownTextPrimitiveProps) {
49 const [isAncestor, rootStyle] = useTextAncestorContext();
50
51 // Flatten the styles, and apply the root styles when needed
52 const flattenedStyle = React.useMemo(() => flattenStyles(rootStyle, style), [rootStyle, style]);
53 const contextValue = React.useMemo<[boolean, ViewStyle]>(
54 () => [true, flattenedStyle],
55 [flattenedStyle],
56 );
57 let childPosition = 0;
58 const nativeChildren = React.Children.toArray(children).map((child) => {
59 const position = childPosition;
60 childPosition += 1;
61
62 if (React.isValidElement(child)) {
63 return child;
64 }
65 if (typeof child !== "string" && typeof child !== "number") {
66 return null;
67 }
68
69 const text = child.toString();
70 return (
71 // @ts-expect-error The generated run props do not include inherited Text props.
72 <T3MarkdownTextRunNativeComponent
73 key={`text-${position}-${text.length}-${text}`}
74 style={flattenedStyle}
75 text={text}
76 {...rest}
77 />
78 );
79 });
80
81 if (!isAncestor) {
82 // Press handlers are delivered by the text runs; the container never sees them.
83 const { onPress: _onPress, onLongPress: _onLongPress, ...containerProps } = rest;
84 return (
85 <TextAncestorContext.Provider value={contextValue}>
86 <T3MarkdownTextNativeComponent
87 {...textDefaults}
88 {...containerProps}
89 style={[flattenedStyle]}
90 >
91 {nativeChildren}
92 </T3MarkdownTextNativeComponent>
93 </TextAncestorContext.Provider>
94 );
95 }
96
97 return <>{nativeChildren}</>;
98}
99
100function MarkdownTextPrimitiveInner(props: MarkdownTextPrimitiveProps) {
101 const [isAncestor] = useTextAncestorContext();
102
103 // Even if the uiTextView prop is set, we can still default to using
104 // normal selection (i.e. base RN text) if the text doesn't need to be
105 // selectable
106 if ((!props.selectable || !props.uiTextView) && !isAncestor) {
107 return <RNText {...props} />;
108 }
109 return <MarkdownTextPrimitiveChild {...props} />;
110}
111
112export function MarkdownTextPrimitive(props: MarkdownTextPrimitiveProps) {
113 if (Platform.OS !== "ios") {
114 return <RNText {...props} />;
115 }
116 return <MarkdownTextPrimitiveInner {...props} />;
117}