Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MarkdownTextPrimitive.tsx

109 lines3.5 KB

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: TextProps = {
13 allowFontScaling: true,
14 selectable: true,
15};
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 MarkdownTextPrimitiveProps = TextProps & {
28 uiTextView?: boolean;
29 /**
30 * Fired when the native text selection changes. Only fires on iOS when
31 * `uiTextView` is true. Note: fires on every selection-edge adjustment
32 * (e.g. dragging a selection handle), so consumers driving expensive work
33 * off this event should debounce.
34 */
35 onSelectionChange?: (event: SelectionChangeEvent) => void;
36};
37​
38function MarkdownTextPrimitiveChild({ style, children, ...rest }: MarkdownTextPrimitiveProps) {
39 const [isAncestor, rootStyle] = useTextAncestorContext();
40​
41 // Flatten the styles, and apply the root styles when needed
42 const flattenedStyle = React.useMemo(() => flattenStyles(rootStyle, style), [rootStyle, style]);
43 const contextValue = React.useMemo<[boolean, ViewStyle]>(
44 () => [true, flattenedStyle],
45 [flattenedStyle],
46 );
47 let childPosition = 0;
48 const nativeChildren = React.Children.toArray(children).map((child) => {
49 const position = childPosition;
50 childPosition += 1;
51​
52 if (React.isValidElement(child)) {
53 return child;
54 }
55 if (typeof child !== "string" && typeof child !== "number") {
56 return null;
57 }
58​
59 const text = child.toString();
60 return (
61 // @ts-expect-error The generated run props do not include inherited Text props.
62 <T3MarkdownTextRunNativeComponent
63 key={`text-${position}-${text.length}-${text}`}
64 style={flattenedStyle}
65 text={text}
66 {...rest}
67 />
68 );
69 });
70​
71 if (!isAncestor) {
72 return (
73 <TextAncestorContext.Provider value={contextValue}>
74 <T3MarkdownTextNativeComponent
75 {...textDefaults}
76 {...rest}
77 // ellipsizeMode={rest.ellipsizeMode ?? rest.lineBreakMode ?? 'tail'}
78 style={[flattenedStyle]}
79 // @ts-expect-error Weirdness
80 onPress={undefined}
81 onLongPress={undefined}
82 >
83 {nativeChildren}
84 </T3MarkdownTextNativeComponent>
85 </TextAncestorContext.Provider>
86 );
87 }
88​
89 return <>{nativeChildren}</>;
91​
92function MarkdownTextPrimitiveInner(props: MarkdownTextPrimitiveProps) {
93 const [isAncestor] = useTextAncestorContext();
94​
95 // Even if the uiTextView prop is set, we can still default to using
96 // normal selection (i.e. base RN text) if the text doesn't need to be
97 // selectable
98 if ((!props.selectable || !props.uiTextView) && !isAncestor) {
99 return <RNText {...props} />;
100 }
101 return <MarkdownTextPrimitiveChild {...props} />;
103​
104export function MarkdownTextPrimitive(props: MarkdownTextPrimitiveProps) {
105 if (Platform.OS !== "ios") {
106 return <RNText {...props} />;
107 }
108 return <MarkdownTextPrimitiveInner {...props} />;