1+ import * as Hash from "effect/Hash" ;
2+ import { File , Paths } from "expo-file-system" ;
3+ import * as Haptics from "expo-haptics" ;
14import type { DiagramType } from "pele/lazy" ;
2- import { Suspense , use , useState } from "react" ;
3- import { Platform , Pressable , StyleSheet , Text , View } from "react-native" ;
4- import { SvgXml } from "react-native-svg" ;
5+ import { Suspense , use , useRef , useState } from "react" ;
6+ import { PixelRatio , Platform , Pressable , StyleSheet , Text , View } from "react-native" ;
7+ import { parse , Svg , SvgXml } from "react-native-svg" ;
58
9+ import { MediaImagePreview } from "../components/MediaImagePreview" ;
610import { SymbolView , type AppSymbolName } from "../components/ui/AppSymbol" ;
711import { copyTextWithHaptic } from "../lib/copyTextWithHaptic" ;
8- import { mermaidSvgForNative } from "../lib/mermaidSvg" ;
12+ import { mermaidSvgForNative , withDiagramBackground } from "../lib/mermaidSvg" ;
913import { themeColorWithAlpha } from "../lib/mobileTheme" ;
1014import type { NativeMarkdownTextStyle } from "./SelectableMarkdownText.types" ;
1115
@@ -90,6 +94,22 @@ async function renderDiagram(source: string): Promise<Diagram> {
9094 }
9195}
9296
97+ // The image viewer zooms a bitmap: about twice a phone's screen leaves room to zoom while
98+ // bounding the memory one image takes.
99+ const EXPORT_LONGEST_SIDE = 3072 ;
100+
101+ /** Named by content, so a theme change never shows a stale image cached under the same URI. */
102+ async function writeDiagramImage ( xml : string , base64 : string ) : Promise < string > {
103+ const file = new File ( Paths . cache , "t3-diagrams" , `${ ( Hash . string ( xml ) >>> 0 ) . toString ( 36 ) } .png` ) ;
104+ file . create ( { intermediates : true , overwrite : true } ) ;
105+ await file . write ( base64 , { encoding : "base64" } ) ;
106+ return file . uri ;
107+ }
108+
109+ type Expansion =
110+ | { readonly status : "exporting" }
111+ | { readonly status : "open" ; readonly uri : string } ;
112+
93113type MermaidCodeBlockProps = {
94114 readonly code : string ;
95115 readonly textStyle : NativeMarkdownTextStyle ;
@@ -111,6 +131,8 @@ function RenderedMermaidBlock(props: MermaidCodeBlockProps) {
111131function MermaidBlock ( props : MermaidCodeBlockProps & { readonly diagram : Diagram | null } ) {
112132 const { code, textStyle, diagram } = props ;
113133 const [ showCode , setShowCode ] = useState ( false ) ;
134+ const [ expansion , setExpansion ] = useState < Expansion | null > ( null ) ;
135+ const exportSvg = useRef < Svg > ( null ) ;
114136 const secondaryColor = themeColorWithAlpha ( textStyle . codeColor , 0.6 ) ;
115137 const codeFontSize = textStyle . fontSize * 0.9 ;
116138 const svg =
@@ -128,6 +150,41 @@ function MermaidBlock(props: MermaidCodeBlockProps & { readonly diagram: Diagram
128150 "--pele-font-mono" : MONOSPACE ,
129151 } )
130152 : null ;
153+ const exportXml =
154+ expansion ?. status === "exporting" && svg
155+ ? withDiagramBackground ( svg , textStyle . codeBlockBackgroundColor )
156+ : null ;
157+ const exportTree = exportXml ? parse ( exportXml ) : null ;
158+ const exportScale =
159+ diagram ?. status === "ready" ? EXPORT_LONGEST_SIDE / Math . max ( diagram . width , diagram . height ) : 0 ;
160+ // In points. iOS draws the copy at its own size and the screen scale, Android at the
161+ // requested pixels, so the copy is laid out at the export size for both.
162+ const exportSize =
163+ diagram ?. status === "ready"
164+ ? {
165+ width : ( diagram . width * exportScale ) / PixelRatio . get ( ) ,
166+ height : ( diagram . height * exportScale ) / PixelRatio . get ( ) ,
167+ }
168+ : null ;
169+
170+ // Runs once the hidden export copy has laid out.
171+ const exportDiagram = ( ) => {
172+ if ( ! exportSvg . current || ! exportXml || ! exportSize ) return ;
173+ const pixels = Platform . OS === "ios" ? 1 : PixelRatio . get ( ) ;
174+ exportSvg . current . toDataURL (
175+ ( base64 ) => {
176+ // iOS wraps the encoding in line feeds.
177+ writeDiagramImage ( exportXml , base64 . replace ( / \s / g, "" ) ) . then (
178+ ( uri ) => setExpansion ( { status : "open" , uri } ) ,
179+ ( ) => setExpansion ( null ) ,
180+ ) ;
181+ } ,
182+ {
183+ width : Math . round ( exportSize . width * pixels ) ,
184+ height : Math . round ( exportSize . height * pixels ) ,
185+ } ,
186+ ) ;
187+ } ;
131188
132189 // The header copies the native code block's, which this block replaces once the fence closes.
133190 return (
@@ -176,18 +233,23 @@ function MermaidBlock(props: MermaidCodeBlockProps & { readonly diagram: Diagram
176233 // In a row the width resolves first, so the height follows the shrunk width;
177234 // a column with maxWidth would size the height from the full width.
178235 < View style = { styles . diagramRow } >
179- < View
180- accessible
181- accessibilityRole = "image"
236+ < Pressable
237+ accessibilityRole = "imagebutton"
182238 accessibilityLabel = "Mermaid diagram"
239+ accessibilityHint = "Opens the diagram full screen"
240+ onPress = { ( ) => {
241+ if ( expansion ) return ;
242+ void Haptics . selectionAsync ( ) ;
243+ setExpansion ( { status : "exporting" } ) ;
244+ } }
183245 style = { {
184246 flexBasis : diagram . width ,
185247 flexShrink : 1 ,
186248 aspectRatio : diagram . width / diagram . height ,
187249 } }
188250 >
189251 < SvgXml xml = { svg } width = "100%" height = "100%" />
190- </ View >
252+ </ Pressable >
191253 </ View >
192254 ) : (
193255 < Text
@@ -203,6 +265,35 @@ function MermaidBlock(props: MermaidCodeBlockProps & { readonly diagram: Diagram
203265 </ Text >
204266 ) }
205267 </ View >
268+ { expansion ?. status === "exporting" && exportTree && exportSize ? (
269+ < View pointerEvents = "none" style = { [ styles . exportCopy , exportSize ] } onLayout = { exportDiagram } >
270+ < Svg ref = { exportSvg } { ...exportTree . props } width = "100%" height = "100%" >
271+ { exportTree . children }
272+ </ Svg >
273+ </ View >
274+ ) : null }
275+ { expansion ?. status === "open" ? (
276+ < MediaImagePreview
277+ source = { {
278+ kind : "image" ,
279+ selectedIndex : 0 ,
280+ images : [
281+ {
282+ kind : "image" ,
283+ uri : expansion . uri ,
284+ name : "Mermaid diagram" ,
285+ mimeType : "image/png" ,
286+ actionsSource : {
287+ uri : expansion . uri ,
288+ name : "Mermaid diagram.png" ,
289+ mimeType : "image/png" ,
290+ } ,
291+ } ,
292+ ] ,
293+ } }
294+ onRequestClose = { ( ) => setExpansion ( null ) }
295+ />
296+ ) : null }
206297 </ View >
207298 ) ;
208299}
@@ -245,4 +336,5 @@ const styles = StyleSheet.create({
245336 content : { padding : 12 } ,
246337 diagramRow : { flexDirection : "row" , justifyContent : "center" } ,
247338 error : { marginBottom : 8 } ,
339+ exportCopy : { position : "absolute" , opacity : 0 } ,
248340} ) ;
0 commit comments