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

NativeMarkdownSelectableText.ios.tsx

210 lines6.8 KB

1import { Image, Linking, type TextStyle, useColorScheme } from "react-native";
2​
3import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive";
4import { markdownFileIconSource } from "./markdownFileIcons";
5import type { NativeMarkdownTextRun } from "./nativeMarkdownText";
6import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";
7​
8const EXTERNAL_LINK_PREFIX = "◉ ";
9const INLINE_ATTACHMENT_PREFIX = "\uFFFC\u00A0";
10const SKILL_ICON_PLACEHOLDER = "\uFFFC";
11const PARAGRAPH_STYLE_ENCODING_OFFSET = 1000;
12​
13function runKeySignature(run: NativeMarkdownTextRun): string {
14 return [
15 run.text,
16 run.bold,
17 run.italic,
18 run.strikethrough,
19 run.code,
20 run.href,
21 run.externalHost,
22 run.fileIcon,
23 run.skillName,
24 run.skillLabel,
25 run.role,
26 run.headingLevel,
27 run.depth,
28 run.spacing,
29 run.firstLineHeadIndent,
30 run.headIndent,
31 run.paragraphSpacing,
32 ].join(":");
34​
35function runStyle(run: NativeMarkdownTextRun, textStyle: NativeMarkdownTextStyle): TextStyle {
36 const isFile = run.fileIcon != null;
37 const isSkill = run.skillName != null;
38 const headingLevel = Math.max(1, Math.min(6, run.headingLevel ?? 1));
39 const headingFontSize = [22, 19, 17, 16, 15, 15][headingLevel - 1] ?? 15;
40 const isHeading = run.role === "heading";
41 const isCodeBlock = run.role === "code-block" || run.role === "code-language";
42 const hasParagraphStyle = run.headIndent !== undefined;
43 const textDecorationLine = run.strikethrough
44 ? "line-through"
45 : run.href && !isFile
46 ? "underline"
47 : "none";
48​
49 return {
50 color: isFile
51 ? textStyle.fileTextColor
52 : isSkill
53 ? textStyle.skillTextColor
54 : run.href
55 ? textStyle.linkColor
56 : isHeading
57 ? textStyle.strongColor
58 : run.role === "quote-marker"
59 ? textStyle.quoteMarkerColor
60 : run.role === "divider"
61 ? textStyle.dividerColor
62 : run.role === "code-language"
63 ? textStyle.mutedColor
64 : run.role === "list-marker"
65 ? textStyle.mutedColor
66 : isCodeBlock
67 ? textStyle.codeColor
68 : run.code
69 ? textStyle.inlineCodeColor
70 : run.bold
71 ? textStyle.strongColor
72 : textStyle.color,
73 fontFamily:
74 isFile || isSkill
75 ? textStyle.boldFontFamily
76 : run.code || isCodeBlock
77 ? "ui-monospace"
78 : isHeading
79 ? textStyle.headingFontFamily
80 : run.bold
81 ? textStyle.boldFontFamily
82 : textStyle.fontFamily,
83 fontSize:
84 run.role === "spacer"
85 ? (run.spacing ?? 10)
86 : run.role === "list-break"
87 ? textStyle.fontSize
88 : isHeading
89 ? headingFontSize
90 : run.role === "code-language"
91 ? 11
92 : run.code || isCodeBlock
93 ? Math.max(12, textStyle.fontSize - 2)
94 : textStyle.fontSize,
95 lineHeight:
96 run.role === "spacer"
97 ? (run.spacing ?? 10)
98 : run.role === "list-break"
99 ? textStyle.lineHeight + (run.spacing ?? 0)
100 : isHeading
101 ? Math.max(headingFontSize + 6, 20)
102 : isCodeBlock
103 ? 18
104 : textStyle.lineHeight,
105 fontStyle: run.italic ? "italic" : "normal",
106 fontWeight: isHeading || run.bold || isFile || isSkill ? "700" : "400",
107 textDecorationLine,
108 backgroundColor: isCodeBlock ? textStyle.codeBlockBackgroundColor : undefined,
109 ...(hasParagraphStyle
110 ? {
111 shadowColor: "transparent",
112 shadowOffset: {
113 width: run.firstLineHeadIndent ?? 0,
114 height: run.headIndent,
115 },
116 shadowRadius: PARAGRAPH_STYLE_ENCODING_OFFSET + (run.paragraphSpacing ?? 0),
117 }
118 : {}),
119 };
121​
122export function NativeMarkdownSelectableText(props: {
123 readonly runs: ReadonlyArray<NativeMarkdownTextRun>;
124 readonly textStyle: NativeMarkdownTextStyle;
125 readonly onLinkPress?: (href: string) => void;
126}) {
127 const colorScheme = useColorScheme();
128 const occurrences = new Map<string, number>();
129 const prefixedExternalLinks = new Set<string>();
130 const keyedRuns = props.runs.map((run) => {
131 const signature = runKeySignature(run);
132 const occurrence = occurrences.get(signature) ?? 0;
133 occurrences.set(signature, occurrence + 1);
134​
135 let text = run.text;
136 if (run.fileIcon) {
137 text = `${INLINE_ATTACHMENT_PREFIX}${text}`;
138 } else if (run.skillName && run.skillLabel) {
139 text = `${SKILL_ICON_PLACEHOLDER}\u00A0${run.skillLabel}`;
140 } else if (run.externalHost && run.href && !prefixedExternalLinks.has(run.href)) {
141 prefixedExternalLinks.add(run.href);
142 text = `${EXTERNAL_LINK_PREFIX}${text}`;
143 }
144​
145 return { key: `${signature}:${occurrence}`, run, text };
146 });
147 // T3MarkdownText only rebuilds its attributed string during native layout. A
148 // color-only child update can otherwise leave the previous appearance cached.
149 const appearanceKey = [
150 colorScheme ?? "unspecified",
151 props.textStyle.color,
152 props.textStyle.strongColor,
153 props.textStyle.mutedColor,
154 props.textStyle.linkColor,
155 props.textStyle.inlineCodeColor,
156 props.textStyle.codeColor,
157 props.textStyle.codeBackgroundColor,
158 props.textStyle.codeBlockBackgroundColor,
159 props.textStyle.fileTextColor,
160 props.textStyle.skillTextColor,
161 props.textStyle.quoteMarkerColor,
162 props.textStyle.dividerColor,
163 ].join(":");
164​
165 return (
166 <MarkdownTextPrimitive
167 key={appearanceKey}
168 uiTextView
169 selectable
170 style={{
171 flexShrink: 1,
172 minWidth: 0,
173 color: props.textStyle.color,
174 fontFamily: props.textStyle.fontFamily,
175 fontSize: props.textStyle.fontSize,
176 lineHeight: props.textStyle.lineHeight,
177 }}
178 >
179 {keyedRuns.map(({ key, run, text }) => {
180 const href = run.href;
181 return (
182 <MarkdownTextPrimitive
183 key={key}
184 nativeID={
185 run.fileIcon
186 ? `t3-file:${Image.resolveAssetSource(markdownFileIconSource(run.fileIcon)).uri}`
187 : run.skillName
188 ? "t3-skill:sf:cube"
189 : undefined
190 }
191 style={runStyle(run, props.textStyle)}
192 onPress={
193 href
194 ? () => {
195 if (props.onLinkPress) {
196 props.onLinkPress(href);
197 } else {
198 void Linking.openURL(href);
199 }
200 }
201 : undefined
202 }
203 >
204 {text}
205 </MarkdownTextPrimitive>
206 );
207 })}
208 </MarkdownTextPrimitive>
209 );