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

SelectableMarkdownText.tsx

121 lines3.9 KB

1import { useMemo } from "react";
2import { View } from "react-native";
3import { parseMarkdownWithOptions } from "react-native-nitro-markdown/headless";
4​
5import {
6 nativeMarkdownChunkSpacing,
7 nativeMarkdownDocumentChunks,
8 nativeMarkdownDocumentRuns,
9 nativeMarkdownWithPreservedSoftBreaks,
10} from "./nativeMarkdownText";
11import { MarkdownImageRendererContext, NativeMarkdownBlock } from "./NativeMarkdownBlock";
12import {
13 MarkdownContextClipboardContext,
14 MarkdownFileContextMenuContext,
15 NativeMarkdownSelectableText,
16 type MarkdownFileContextMenuHandlers,
17} from "./NativeMarkdownSelectableText";
18import type {
19 SelectableMarkdownSkill,
20 SelectableMarkdownTextProps,
21} from "./SelectableMarkdownText.types";
22​
23const EMPTY_SKILLS: ReadonlyArray<SelectableMarkdownSkill> = [];
24​
25export type {
26 MarkdownCodeHighlighter,
27 MarkdownHighlightedToken,
28 MarkdownImageRenderer,
29 MarkdownImageRequest,
30 NativeMarkdownTextStyle,
31 SelectableMarkdownSkill,
32 SelectableMarkdownTextProps,
33} from "./SelectableMarkdownText.types";
34​
35export function hasNativeSelectableMarkdownText(): boolean {
36 return true;
38​
39export function SelectableMarkdownText({
40 markdown,
41 contextClipboardFragment,
42 skills = EMPTY_SKILLS,
43 textStyle,
44 highlightCode,
45 preserveSoftBreaks = false,
46 onLinkPress,
47 fileContextMenu,
48 onFileContextMenuAction,
49 renderImage,
50 marginTop = 0,
51 marginBottom = 0,
52}: SelectableMarkdownTextProps) {
53 const chunks = useMemo(() => {
54 const parsedDocument = parseMarkdownWithOptions(markdown, {
55 gfm: true,
56 html: true,
57 math: false,
58 });
59 const document = preserveSoftBreaks
60 ? nativeMarkdownWithPreservedSoftBreaks(parsedDocument)
61 : parsedDocument;
62 return nativeMarkdownDocumentChunks(document).map((chunk) =>
63 chunk.kind === "selectable"
64 ? {
65 ...chunk,
66 runs: nativeMarkdownDocumentRuns(chunk.node, skills),
67 }
68 : chunk,
69 );
70 }, [markdown, preserveSoftBreaks, skills]);
71​
72 const fileContextMenuHandlers = useMemo<MarkdownFileContextMenuHandlers | null>(
73 () =>
74 fileContextMenu && onFileContextMenuAction
75 ? { fileContextMenu, onFileContextMenuAction }
76 : null,
77 [fileContextMenu, onFileContextMenuAction],
78 );
79​
80 return (
81 <MarkdownContextClipboardContext.Provider value={contextClipboardFragment ?? ""}>
82 <MarkdownImageRendererContext.Provider value={renderImage ?? null}>
83 <MarkdownFileContextMenuContext.Provider value={fileContextMenuHandlers}>
84 {/* A percentage width here creates a cyclic intrinsic measurement inside
85 shrink-to-fit containers such as user-message bubbles. Yoga then gives
86 the native text node an unbounded second pass and the parent only clips
87 the resulting single-line width instead of reflowing it. */}
88 <View style={{ flexShrink: 1, minWidth: 0, marginTop, marginBottom }}>
89 {chunks.map((chunk, index) => {
90 const content =
91 chunk.kind === "rich" ? (
92 <NativeMarkdownBlock
93 node={chunk.node}
94 skills={skills}
95 textStyle={textStyle}
96 highlightCode={highlightCode}
97 onLinkPress={onLinkPress}
98 />
99 ) : (
100 <NativeMarkdownSelectableText
101 runs={chunk.runs}
102 textStyle={textStyle}
103 onLinkPress={onLinkPress}
104 />
105 );
106​
107 return (
108 <View
109 key={chunk.key}
110 style={{ paddingTop: nativeMarkdownChunkSpacing(chunks[index - 1], chunk) }}
111 >
112 {content}
113 </View>
114 );
115 })}
116 </View>
117 </MarkdownFileContextMenuContext.Provider>
118 </MarkdownImageRendererContext.Provider>
119 </MarkdownContextClipboardContext.Provider>
120 );