|
1 | | -import { createContext, useContext, useEffect, useState } from "react"; |
| 1 | +import { createContext, memo, useContext, useMemo } from "react"; |
2 | 2 | import { Image, Platform, ScrollView, Text, useColorScheme, View } from "react-native"; |
3 | 3 | import type { MarkdownNode } from "react-native-nitro-markdown/headless"; |
4 | 4 |
|
5 | 5 | import { CopyTextButton } from "./CopyTextButton"; |
6 | 6 | import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive"; |
7 | | -import { nativeMarkdownDocumentRuns, nativeMarkdownListItemBlocks } from "./nativeMarkdownText"; |
8 | | -import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText.ios"; |
| 7 | +import { |
| 8 | + nativeMarkdownDocumentRuns, |
| 9 | + nativeMarkdownListItemBlocks, |
| 10 | + nativeMarkdownNodePosition, |
| 11 | +} from "./nativeMarkdownText"; |
| 12 | +import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText"; |
9 | 13 | import type { |
10 | 14 | MarkdownCodeHighlighter, |
11 | 15 | MarkdownHighlightedToken, |
12 | 16 | MarkdownImageRenderer, |
13 | 17 | NativeMarkdownTextStyle, |
14 | 18 | SelectableMarkdownSkill, |
15 | 19 | } from "./SelectableMarkdownText.types"; |
| 20 | +import { useHighlightedCode, type HighlightedCode } from "./useHighlightedCode"; |
16 | 21 |
|
17 | 22 | /** Set by SelectableMarkdownText so images anywhere in the block tree can use it. */ |
18 | 23 | export const MarkdownImageRendererContext = createContext<MarkdownImageRenderer | null>(null); |
19 | 24 |
|
20 | | -type HighlightedCode = ReadonlyArray<ReadonlyArray<MarkdownHighlightedToken>>; |
21 | | - |
22 | | -const highlightedCodeCache = new Map<string, HighlightedCode>(); |
23 | | -const highlightedCodePromiseCache = new Map<string, Promise<HighlightedCode>>(); |
24 | | -const HIGHLIGHTED_CODE_CACHE_LIMIT = 64; |
25 | 25 | const MONO_FONT_FAMILY = Platform.select({ |
26 | 26 | ios: "ui-monospace", |
27 | 27 | android: "monospace", |
28 | 28 | default: "monospace", |
29 | 29 | }); |
30 | 30 |
|
31 | 31 | function nodeKey(node: MarkdownNode, index: number): string { |
32 | | - return `${node.type}:${node.beg ?? index}:${node.end ?? index}`; |
| 32 | + return `${node.type}:${nativeMarkdownNodePosition(node, index)}`; |
33 | 33 | } |
34 | 34 |
|
35 | 35 | /** Code inside markdown scales with the base text size (12pt at the default 15pt body). */ |
@@ -67,174 +67,74 @@ function SelectableNode(props: { |
67 | 67 | ); |
68 | 68 | } |
69 | 69 |
|
70 | | -function codeHighlightCacheKey( |
71 | | - code: string, |
72 | | - language: string | undefined, |
73 | | - theme: "light" | "dark", |
74 | | -): string { |
75 | | - return `${theme}:${language ?? "text"}:${code}`; |
76 | | -} |
77 | | - |
78 | | -function cacheHighlightedCode(key: string, tokens: HighlightedCode): void { |
79 | | - highlightedCodeCache.delete(key); |
80 | | - highlightedCodeCache.set(key, tokens); |
81 | | - |
82 | | - while (highlightedCodeCache.size > HIGHLIGHTED_CODE_CACHE_LIMIT) { |
83 | | - const oldestKey = highlightedCodeCache.keys().next().value; |
84 | | - if (oldestKey === undefined) { |
85 | | - break; |
86 | | - } |
87 | | - highlightedCodeCache.delete(oldestKey); |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -function loadHighlightedCode( |
92 | | - code: string, |
93 | | - language: string | undefined, |
94 | | - theme: "light" | "dark", |
95 | | - highlightCode: MarkdownCodeHighlighter, |
96 | | -): Promise<HighlightedCode> { |
97 | | - const key = codeHighlightCacheKey(code, language, theme); |
98 | | - const cached = highlightedCodeCache.get(key); |
99 | | - if (cached) { |
100 | | - return Promise.resolve(cached); |
101 | | - } |
102 | | - |
103 | | - const pending = highlightedCodePromiseCache.get(key); |
104 | | - if (pending) { |
105 | | - return pending; |
106 | | - } |
107 | | - |
108 | | - const promise = highlightCode({ code, language, theme }) |
109 | | - .then((tokens) => { |
110 | | - cacheHighlightedCode(key, tokens); |
111 | | - highlightedCodePromiseCache.delete(key); |
112 | | - return tokens; |
113 | | - }) |
114 | | - .catch((error) => { |
115 | | - highlightedCodePromiseCache.delete(key); |
116 | | - throw error; |
117 | | - }); |
118 | | - highlightedCodePromiseCache.set(key, promise); |
119 | | - return promise; |
120 | | -} |
121 | | - |
122 | | -function useHighlightedCode( |
123 | | - code: string, |
124 | | - language: string | undefined, |
125 | | - theme: "light" | "dark", |
126 | | - highlightCode: MarkdownCodeHighlighter, |
127 | | -): HighlightedCode | null { |
128 | | - const key = codeHighlightCacheKey(code, language, theme); |
129 | | - const [highlighted, setHighlighted] = useState<{ |
130 | | - readonly key: string; |
131 | | - readonly tokens: HighlightedCode | null; |
132 | | - }>(() => ({ |
133 | | - key, |
134 | | - tokens: highlightedCodeCache.get(key) ?? null, |
135 | | - })); |
136 | | - |
137 | | - useEffect(() => { |
138 | | - let active = true; |
139 | | - const cached = highlightedCodeCache.get(key); |
140 | | - if (cached) { |
141 | | - cacheHighlightedCode(key, cached); |
142 | | - setHighlighted({ key, tokens: cached }); |
143 | | - return () => { |
144 | | - active = false; |
145 | | - }; |
146 | | - } |
147 | | - |
148 | | - void loadHighlightedCode(code, language, theme, highlightCode) |
149 | | - .then((tokens) => { |
150 | | - if (active) { |
151 | | - setHighlighted({ key, tokens }); |
152 | | - } |
153 | | - }) |
154 | | - .catch(() => { |
155 | | - if (active) { |
156 | | - setHighlighted({ key, tokens: null }); |
157 | | - } |
158 | | - }); |
159 | | - return () => { |
160 | | - active = false; |
161 | | - }; |
162 | | - }, [code, highlightCode, key, language, theme]); |
163 | | - |
164 | | - return highlighted.key === key ? highlighted.tokens : null; |
165 | | -} |
166 | | - |
167 | | -function HighlightedCodeText(props: { |
168 | | - readonly content: string; |
169 | | - readonly highlighted: HighlightedCode | null; |
170 | | - readonly textStyle: NativeMarkdownTextStyle; |
| 70 | +const HighlightedCodeLine = memo(function HighlightedCodeLine(props: { |
| 71 | + readonly tokens: ReadonlyArray<MarkdownHighlightedToken>; |
| 72 | + readonly color: string; |
| 73 | + readonly newline: boolean; |
171 | 74 | }) { |
172 | | - if (!props.highlighted) { |
173 | | - return ( |
| 75 | + let offset = 0; |
| 76 | + const children = []; |
| 77 | + for (const token of props.tokens) { |
| 78 | + if (!token.content) continue; |
| 79 | + children.push( |
174 | 80 | <MarkdownTextPrimitive |
175 | | - uiTextView |
176 | | - selectable |
| 81 | + key={offset} |
177 | 82 | style={{ |
178 | | - color: props.textStyle.codeColor, |
| 83 | + color: token.color ?? props.color, |
179 | 84 | fontFamily: MONO_FONT_FAMILY, |
180 | | - fontSize: codeBlockFontSize(props.textStyle), |
181 | | - lineHeight: codeBlockLineHeight(props.textStyle), |
| 85 | + fontStyle: token.fontStyle !== null && (token.fontStyle & 1) === 1 ? "italic" : "normal", |
| 86 | + fontWeight: token.fontStyle !== null && (token.fontStyle & 2) === 2 ? "700" : "400", |
182 | 87 | }} |
183 | 88 | > |
184 | | - {props.content} |
185 | | - </MarkdownTextPrimitive> |
| 89 | + {token.content} |
| 90 | + </MarkdownTextPrimitive>, |
186 | 91 | ); |
| 92 | + offset += token.content.length; |
187 | 93 | } |
188 | | - const highlighted = props.highlighted; |
189 | | - let sourceOffset = 0; |
190 | | - const keyOccurrences = new Map<string, number>(); |
191 | | - const keyedLines = highlighted.map((line) => { |
192 | | - const lineStart = sourceOffset; |
193 | | - const tokens = line.map((token) => { |
194 | | - const start = sourceOffset; |
195 | | - sourceOffset += token.content.length; |
196 | | - const signature = `${start}:${token.content}:${token.color ?? ""}:${token.fontStyle ?? ""}`; |
197 | | - const occurrence = keyOccurrences.get(signature) ?? 0; |
198 | | - keyOccurrences.set(signature, occurrence + 1); |
199 | | - return { key: `${signature}:${occurrence}`, token }; |
200 | | - }); |
201 | | - sourceOffset += 1; |
202 | | - return { |
203 | | - key: `line:${lineStart}:${line.map((token) => token.content).join("")}`, |
204 | | - tokens, |
205 | | - }; |
206 | | - }); |
| 94 | + return ( |
| 95 | + <MarkdownTextPrimitive> |
| 96 | + {children} |
| 97 | + {props.newline ? "\n" : ""} |
| 98 | + </MarkdownTextPrimitive> |
| 99 | + ); |
| 100 | +}); |
207 | 101 |
|
| 102 | +function HighlightedCodeText(props: { |
| 103 | + readonly content: string; |
| 104 | + readonly highlighted: HighlightedCode | null; |
| 105 | + readonly textStyle: NativeMarkdownTextStyle; |
| 106 | +}) { |
| 107 | + // The text root provides inherited styles through context. A new style object |
| 108 | + // would rerender every token even when its completed line is unchanged. |
| 109 | + const fontSize = codeBlockFontSize(props.textStyle); |
| 110 | + const lineHeight = codeBlockLineHeight(props.textStyle); |
| 111 | + const style = useMemo( |
| 112 | + () => ({ |
| 113 | + color: props.textStyle.codeColor, |
| 114 | + fontFamily: MONO_FONT_FAMILY, |
| 115 | + fontSize, |
| 116 | + lineHeight, |
| 117 | + }), |
| 118 | + [props.textStyle.codeColor, fontSize, lineHeight], |
| 119 | + ); |
| 120 | + let offset = 0; |
| 121 | + const lines = []; |
| 122 | + if (props.highlighted) { |
| 123 | + for (const tokens of props.highlighted) { |
| 124 | + lines.push( |
| 125 | + <HighlightedCodeLine |
| 126 | + key={offset} |
| 127 | + tokens={tokens} |
| 128 | + color={props.textStyle.codeColor} |
| 129 | + newline={lines.length + 1 < props.highlighted.length} |
| 130 | + />, |
| 131 | + ); |
| 132 | + offset += tokens.reduce((length, token) => length + token.content.length, 0) + 1; |
| 133 | + } |
| 134 | + } |
208 | 135 | return ( |
209 | | - <MarkdownTextPrimitive |
210 | | - uiTextView |
211 | | - selectable |
212 | | - style={{ |
213 | | - color: props.textStyle.codeColor, |
214 | | - fontFamily: MONO_FONT_FAMILY, |
215 | | - fontSize: codeBlockFontSize(props.textStyle), |
216 | | - lineHeight: codeBlockLineHeight(props.textStyle), |
217 | | - }} |
218 | | - > |
219 | | - {keyedLines.map((line, lineIndex) => ( |
220 | | - <MarkdownTextPrimitive key={line.key}> |
221 | | - {line.tokens.map(({ key, token }) => ( |
222 | | - <MarkdownTextPrimitive |
223 | | - key={key} |
224 | | - style={{ |
225 | | - color: token.color ?? props.textStyle.codeColor, |
226 | | - fontFamily: MONO_FONT_FAMILY, |
227 | | - fontStyle: |
228 | | - token.fontStyle !== null && (token.fontStyle & 1) === 1 ? "italic" : "normal", |
229 | | - fontWeight: token.fontStyle !== null && (token.fontStyle & 2) === 2 ? "700" : "400", |
230 | | - }} |
231 | | - > |
232 | | - {token.content} |
233 | | - </MarkdownTextPrimitive> |
234 | | - ))} |
235 | | - {lineIndex + 1 < keyedLines.length ? "\n" : ""} |
236 | | - </MarkdownTextPrimitive> |
237 | | - ))} |
| 136 | + <MarkdownTextPrimitive uiTextView selectable style={style}> |
| 137 | + {props.highlighted ? lines : props.content} |
238 | 138 | </MarkdownTextPrimitive> |
239 | 139 | ); |
240 | 140 | } |
|
0 commit comments