@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
tsx 144 lines 4.64 KB
Raw
1 import { msg, useGT, useMessages } from "gt-react-native";
2 import { useState } from "react";
3 import { Pressable, ScrollView, Text } from "react-native";
4 import type { MarkdownStyle } from "react-native-enriched-markdown";
5
6 import { Menu } from "../components/ui/Menu";
7 import { useMarkdownCodeHighlight } from "../features/threads/markdownCodeHighlightState";
8 import { copyTextWithHaptic } from "../lib/copyTextWithHaptic";
9 import type { HighlightScheme } from "../lib/highlights";
10 import { CodeBlockFrame, CodeBlockHeaderButton } from "./CodeBlockFrame";
11 import { fencedCodeMarkdown } from "./codeBlockMarkdown";
12 import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types";
13
14 // Header names from enriched-markdown's CodeBlockLanguages.cpp that are not the fence capitalized.
15 const FENCE_DISPLAY_NAMES: Readonly<Record<string, string>> = {
16 cc: "C++",
17 cpp: "C++",
18 cxx: "C++",
19 cs: "C#",
20 csharp: "C#",
21 graphql: "GraphQL",
22 objc: "Objective-C",
23 objectivec: "Objective-C",
24 php: "PHP",
25 rb: "Ruby",
26 scss: "SCSS",
27 sql: "SQL",
28 toml: "TOML",
29 xml: "XML",
30 };
31
32 // The native block's long-press menu, with its labels and iOS symbols.
33 const CODE_MENU_ACTIONS = [
34 {
35 id: "copy-code",
36 title: msg("Copy", { $context: "verb, copies the code" }),
37 image: "doc.on.doc",
38 },
39 { id: "copy-markdown", title: msg("Copy as Markdown"), image: "doc.text" },
40 ];
41
42 /** A fence in a language enriched-markdown cannot colour, drawn like its native block. */
43 export function HighlightedCodeBlock(props: {
44 readonly code: string;
45 readonly fence: string;
46 readonly language: string;
47 /** False only while the library withholds an unclosed fence (`streamingAnimation`, which T3 never enables, so always true today); highlighting waits for it. */
48 readonly complete: boolean;
49 readonly scheme: HighlightScheme;
50 readonly textStyle: NativeMarkdownTextStyle;
51 readonly codeStyle: NonNullable<MarkdownStyle["codeBlock"]>;
52 }) {
53 const { code, codeStyle, textStyle } = props;
54 const gt = useGT();
55 const resolve = useMessages();
56 const [wrapped, setWrapped] = useState(false);
57 const lines = useMarkdownCodeHighlight({
58 code,
59 language: props.language,
60 theme: props.scheme,
61 enabled: props.complete,
62 });
63 const fence = props.fence.toLowerCase();
64 let offset = 0;
65 const text = (
66 // Not selectable: as in the native block, a long-press opens the copy menu instead.
67 <Text
68 style={{
69 color: codeStyle.color,
70 fontFamily: codeStyle.fontFamily,
71 fontSize: codeStyle.fontSize,
72 lineHeight: codeStyle.lineHeight,
73 }}
74 >
75 {lines?.flatMap((line, lineIndex) => {
76 const separator = lineIndex > 0 ? "\n" : "";
77 offset += separator.length;
78 return [
79 separator,
80 ...line.map((token) => {
81 const start = offset;
82 offset += token.content.length;
83 return (
84 <Text key={start} style={token.color ? { color: token.color } : undefined}>
85 {token.content}
86 </Text>
87 );
88 }),
89 ];
90 }) ?? code}
91 </Text>
92 );
93 // Inside the scroller: on iOS an ancestor JS responder disables a ScrollView's panning.
94 const content = (
95 <Menu
96 shouldOpenOnLongPress
97 actions={CODE_MENU_ACTIONS.map((action) => ({ ...action, title: resolve(action.title) }))}
98 style={{ flexGrow: 1 }}
99 onPressAction={({ nativeEvent }) => {
100 copyTextWithHaptic(
101 nativeEvent.event === "copy-markdown" ? fencedCodeMarkdown(code, props.fence) : code,
102 );
103 }}
104 >
105 <Pressable style={{ padding: codeStyle.padding }}>{text}</Pressable>
106 </Menu>
107 );
108 return (
109 <CodeBlockFrame
110 label={FENCE_DISPLAY_NAMES[fence] ?? fence.charAt(0).toUpperCase() + fence.slice(1)}
111 textStyle={textStyle}
112 actions={
113 <>
114 <CodeBlockHeaderButton
115 icon="text.alignleft"
116 label={wrapped ? gt("Disable line wrap") : gt("Wrap lines")}
117 textStyle={textStyle}
118 active={wrapped}
119 onPress={() => setWrapped((value) => !value)}
120 />
121 <CodeBlockHeaderButton
122 icon="doc.on.doc"
123 label={gt("Copy code")}
124 textStyle={textStyle}
125 onPress={() => copyTextWithHaptic(code)}
126 />
127 </>
128 }
129 >
130 {wrapped ? (
131 content
132 ) : (
133 <ScrollView
134 horizontal
135 // As in the thread feed: with the keyboard open, the first long-press reaches the copy menu.
136 keyboardShouldPersistTaps="always"
137 contentContainerStyle={{ flexGrow: 1 }}
138 >
139 {content}
140 </ScrollView>
141 )}
142 </CodeBlockFrame>
143 );
144 }