Repository navigation
fix(mobile): chat code blocks colour diff, kotlin, sql and other languages again #33680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+442
−89
Merged
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
17cb87e
fix(mobile): chat code blocks colour diff, kotlin, sql and other lang…
juliusmarminge a230081
fix(mobile): keep the fallback code block mounted while its fence str…
juliusmarminge 33151da
docs(mobile): say when a code fence counts as complete
juliusmarminge 558ee8d
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge 0c6994d
fix(mobile): mermaid and math fences keep their renderers ahead of th…
juliusmarminge f2f0112
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge bda955f
fix(mobile): highlighted code fences keep the long-press copy menu
juliusmarminge 25fea91
fix(mobile): highlighted code fences scroll sideways again on iOS
juliusmarminge e455e68
Merge remote-tracking branch 'origin/main' into pr-33680
juliusmarminge 0f0c652
test(mobile): pin which renderer each code fence gets
juliusmarminge 6c54d4a
docs(mobile): name the file the fence display names come from
juliusmarminge 6e5d2a0
fix(mobile): Android menus stay on screen when their anchor is taller…
juliusmarminge 63069af
fix(mobile): first long-press on a scrolled code block keeps the keyb…
juliusmarminge c6afa44
docs(mobile): say what keeping taps on the code scroller does
juliusmarminge 06c360d
fix(mobile): code block headers grow with large text instead of clipp…
juliusmarminge b00d15f
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge e0bc83b
refactor(mobile): drop the Android menu's duplicate root height
juliusmarminge b4ca413
Merge remote-tracking branch 'origin/main' into fix/issue-33638
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import type { ReactNode } from "react"; | ||
| import { Pressable, StyleSheet, Text, View } from "react-native"; | ||
|
|
||
| import { SymbolView, type AppSymbolName } from "../components/ui/AppSymbol"; | ||
| import { themeColorWithAlpha } from "../lib/mobileTheme"; | ||
| import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types"; | ||
|
|
||
| /** | ||
| * The native code block's border and header, for a fence drawn in React once it closes. | ||
| * The block slot already applies the code block's outer margins. | ||
| */ | ||
| export function CodeBlockFrame(props: { | ||
| readonly label: string; | ||
| readonly textStyle: NativeMarkdownTextStyle; | ||
| readonly actions: ReactNode; | ||
| readonly children: ReactNode; | ||
| }) { | ||
| const { textStyle } = props; | ||
| return ( | ||
| <View | ||
| style={[ | ||
| styles.block, | ||
| { | ||
| backgroundColor: textStyle.codeBlockBackgroundColor, | ||
| borderColor: textStyle.dividerColor, | ||
| }, | ||
| ]} | ||
| > | ||
| <View | ||
| style={[ | ||
| styles.header, | ||
| { borderBottomColor: themeColorWithAlpha(textStyle.codeColor, 0.2) }, | ||
| ]} | ||
| > | ||
| <Text | ||
| style={[ | ||
| styles.label, | ||
| { | ||
| color: themeColorWithAlpha(textStyle.codeColor, 0.6), | ||
| fontSize: textStyle.fontSize * 0.9 * 0.85, | ||
| }, | ||
| ]} | ||
| > | ||
| {props.label} | ||
| </Text> | ||
| <View style={styles.actions}>{props.actions}</View> | ||
| </View> | ||
| {props.children} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| /** A header icon; `active` draws it at full strength, as the native wrap toggle when on. */ | ||
| export function CodeBlockHeaderButton(props: { | ||
| readonly icon: AppSymbolName; | ||
| readonly label: string; | ||
| readonly textStyle: NativeMarkdownTextStyle; | ||
| readonly active?: boolean; | ||
| readonly onPress: () => void; | ||
| }) { | ||
| return ( | ||
| <Pressable | ||
| accessibilityRole="button" | ||
| accessibilityLabel={props.label} | ||
| hitSlop={10} | ||
| onPress={props.onPress} | ||
| > | ||
| <SymbolView | ||
| name={props.icon} | ||
| size={14} | ||
| tintColor={ | ||
| props.active | ||
| ? props.textStyle.codeColor | ||
| : themeColorWithAlpha(props.textStyle.codeColor, 0.6) | ||
| } | ||
| type="monochrome" | ||
| /> | ||
| </Pressable> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| block: { | ||
| borderRadius: 8, | ||
| borderWidth: StyleSheet.hairlineWidth, | ||
| overflow: "hidden", | ||
| }, | ||
| header: { | ||
| height: 32, | ||
| paddingHorizontal: 12, | ||
| flexDirection: "row", | ||
| alignItems: "center", | ||
| justifyContent: "space-between", | ||
| borderBottomWidth: StyleSheet.hairlineWidth, | ||
| }, | ||
| label: { fontWeight: "500" }, | ||
| actions: { flexDirection: "row", alignItems: "center", gap: 14 }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { useState } from "react"; | ||
| import { ScrollView, Text, View } from "react-native"; | ||
| import type { MarkdownStyle } from "react-native-enriched-markdown"; | ||
|
|
||
| import { useMarkdownCodeHighlight } from "../features/threads/markdownCodeHighlightState"; | ||
| import { copyTextWithHaptic } from "../lib/copyTextWithHaptic"; | ||
| import { resolveHighlightLanguage } from "../lib/highlightLanguage"; | ||
| import type { HighlightScheme } from "../lib/highlights"; | ||
| import { CodeBlockFrame, CodeBlockHeaderButton } from "./CodeBlockFrame"; | ||
| import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types"; | ||
|
|
||
| // Fences enriched-markdown colours itself: cpp/highlight/CodeBlockLanguages.cpp entries with a default grammar. | ||
| const NATIVE_HIGHLIGHT_FENCES = new Set([ | ||
| "bash", | ||
| "c", | ||
| "css", | ||
| "go", | ||
| "golang", | ||
| "html", | ||
| "java", | ||
| "javascript", | ||
| "js", | ||
| "json", | ||
| "jsx", | ||
| "markdown", | ||
| "md", | ||
| "py", | ||
| "python", | ||
| "rs", | ||
| "rust", | ||
| "sh", | ||
| "shell", | ||
| "ts", | ||
| "tsx", | ||
| "typescript", | ||
| "yaml", | ||
| "yml", | ||
| "zsh", | ||
| ]); | ||
|
|
||
| // The same file's header names that are not the fence capitalized. | ||
| const FENCE_DISPLAY_NAMES: Readonly<Record<string, string>> = { | ||
| cc: "C++", | ||
| cpp: "C++", | ||
| cxx: "C++", | ||
| cs: "C#", | ||
| csharp: "C#", | ||
| graphql: "GraphQL", | ||
| objc: "Objective-C", | ||
| objectivec: "Objective-C", | ||
| php: "PHP", | ||
| rb: "Ruby", | ||
| scss: "SCSS", | ||
| sql: "SQL", | ||
| toml: "TOML", | ||
| xml: "XML", | ||
| }; | ||
|
|
||
| /** The highlights language for a fence enriched-markdown leaves plain, or null to keep its block. */ | ||
| export function fallbackHighlightLanguage(fence: string): string | null { | ||
| const name = fence.toLowerCase(); | ||
| if (NATIVE_HIGHLIGHT_FENCES.has(name)) return null; | ||
| const language = resolveHighlightLanguage({ language: name }); | ||
| return language === "text" ? null : language; | ||
| } | ||
|
|
||
| /** A fence in a language enriched-markdown cannot colour, drawn like its native block. */ | ||
| export function HighlightedCodeBlock(props: { | ||
| readonly code: string; | ||
| readonly fence: string; | ||
| readonly language: string; | ||
| /** False only while the library withholds an unclosed fence (`streamingAnimation`); highlighting waits for it. */ | ||
| readonly complete: boolean; | ||
| readonly scheme: HighlightScheme; | ||
| readonly textStyle: NativeMarkdownTextStyle; | ||
| readonly codeStyle: NonNullable<MarkdownStyle["codeBlock"]>; | ||
| }) { | ||
| const { code, codeStyle, textStyle } = props; | ||
| const [wrapped, setWrapped] = useState(false); | ||
| const lines = useMarkdownCodeHighlight({ | ||
| code, | ||
| language: props.language, | ||
| theme: props.scheme, | ||
| enabled: props.complete, | ||
| }); | ||
| const fence = props.fence.toLowerCase(); | ||
| let offset = 0; | ||
| const text = ( | ||
| <Text | ||
| selectable | ||
| style={{ | ||
| color: codeStyle.color, | ||
| fontFamily: codeStyle.fontFamily, | ||
| fontSize: codeStyle.fontSize, | ||
| lineHeight: codeStyle.lineHeight, | ||
| }} | ||
| > | ||
| {lines?.flatMap((line, lineIndex) => { | ||
| const separator = lineIndex > 0 ? "\n" : ""; | ||
| offset += separator.length; | ||
| return [ | ||
| separator, | ||
| ...line.map((token) => { | ||
| const start = offset; | ||
| offset += token.content.length; | ||
| return ( | ||
| <Text key={start} style={token.color ? { color: token.color } : undefined}> | ||
| {token.content} | ||
| </Text> | ||
| ); | ||
| }), | ||
| ]; | ||
| }) ?? code} | ||
| </Text> | ||
| ); | ||
| const padding = { padding: codeStyle.padding }; | ||
| return ( | ||
| <CodeBlockFrame | ||
| label={FENCE_DISPLAY_NAMES[fence] ?? fence.charAt(0).toUpperCase() + fence.slice(1)} | ||
| textStyle={textStyle} | ||
| actions={ | ||
| <> | ||
| <CodeBlockHeaderButton | ||
| icon="text.alignleft" | ||
| label={wrapped ? "Disable line wrap" : "Wrap lines"} | ||
| textStyle={textStyle} | ||
| active={wrapped} | ||
| onPress={() => setWrapped((value) => !value)} | ||
| /> | ||
| <CodeBlockHeaderButton | ||
| icon="doc.on.doc" | ||
| label="Copy code" | ||
| textStyle={textStyle} | ||
| onPress={() => copyTextWithHaptic(code)} | ||
| /> | ||
| </> | ||
| } | ||
| > | ||
| {wrapped ? ( | ||
| <View style={padding}>{text}</View> | ||
| ) : ( | ||
| <ScrollView horizontal contentContainerStyle={padding}> | ||
| {text} | ||
| </ScrollView> | ||
| )} | ||
| </CodeBlockFrame> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.