81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
import { Pressable, ScrollView, View } from 'react-native'
|
|
import { Code, Pencil } from 'lucide-react-native'
|
|
import { MobileMarkdown } from '../components/MobileMarkdown'
|
|
import { colors } from '../theme/mobile-theme'
|
|
import {
|
|
MobileFilePreviewSourceText,
|
|
MobileFilePreviewTruncatedNote
|
|
} from './MobileFilePreviewSourceText'
|
|
import { filePreviewStyles as styles } from './mobile-file-preview-styles'
|
|
|
|
type Props = {
|
|
relativePath: string
|
|
content: string
|
|
truncated: boolean
|
|
byteLength: number
|
|
initialLine?: number
|
|
}
|
|
|
|
export function MobileFileMarkdownPreview({
|
|
relativePath,
|
|
content,
|
|
truncated,
|
|
byteLength,
|
|
initialLine
|
|
}: Props) {
|
|
const [mode, setMode] = useState<'preview' | 'source'>(() => (initialLine ? 'source' : 'preview'))
|
|
useEffect(() => {
|
|
setMode(initialLine ? 'source' : 'preview')
|
|
}, [initialLine, relativePath])
|
|
const previewSelected = mode === 'preview'
|
|
const sourceSelected = mode === 'source'
|
|
|
|
return (
|
|
<View style={styles.modeContainer}>
|
|
<View style={styles.modeToolbar}>
|
|
<Pressable
|
|
style={[styles.modeToggle, sourceSelected && styles.modeToggleActive]}
|
|
onPress={() => setMode('source')}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ selected: sourceSelected }}
|
|
accessibilityLabel="View Markdown source"
|
|
>
|
|
<Code
|
|
size={15}
|
|
color={sourceSelected ? colors.textPrimary : colors.textSecondary}
|
|
strokeWidth={2.2}
|
|
/>
|
|
</Pressable>
|
|
<Pressable
|
|
style={[styles.modeToggle, previewSelected && styles.modeToggleActive]}
|
|
onPress={() => setMode('preview')}
|
|
accessibilityRole="button"
|
|
accessibilityState={{ selected: previewSelected }}
|
|
accessibilityLabel="View rendered Markdown preview"
|
|
>
|
|
<Pencil
|
|
size={15}
|
|
color={previewSelected ? colors.textPrimary : colors.textSecondary}
|
|
strokeWidth={2.2}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
{mode === 'preview' ? (
|
|
<ScrollView style={styles.scroll} contentContainerStyle={styles.markdownContent}>
|
|
{truncated ? <MobileFilePreviewTruncatedNote byteLength={byteLength} /> : null}
|
|
<MobileMarkdown content={content} />
|
|
</ScrollView>
|
|
) : (
|
|
<MobileFilePreviewSourceText
|
|
relativePath={relativePath}
|
|
content={content}
|
|
truncated={truncated}
|
|
byteLength={byteLength}
|
|
initialLine={initialLine}
|
|
/>
|
|
)}
|
|
</View>
|
|
)
|
|
}
|