Fix richtext link rendering

zio/stable
Paul Frazee 2022-11-18 11:37:25 -06:00
parent 2f9dd131f9
commit 368286ad67
2 changed files with 44 additions and 16 deletions

View File

@ -1,6 +1,12 @@
import React from 'react' import React from 'react'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {StyleProp, Text, TouchableOpacity, ViewStyle} from 'react-native' import {
StyleProp,
Text,
TouchableOpacity,
TextStyle,
ViewStyle,
} from 'react-native'
import {useStores} from '../../../state' import {useStores} from '../../../state'
import {LinkActionsModel} from '../../../state/models/shell-ui' import {LinkActionsModel} from '../../../state/models/shell-ui'
@ -21,6 +27,7 @@ export const Link = observer(function Link({
store.nav.navigate(href) store.nav.navigate(href)
} }
const onLongPress = () => { const onLongPress = () => {
store.shell.closeModal() // close any active modals
store.nav.newTab(href, title) store.nav.newTab(href, title)
// store.shell.openModal(new LinkActionsModel(href, title || href)) // store.shell.openModal(new LinkActionsModel(href, title || href))
} }
@ -34,3 +41,30 @@ export const Link = observer(function Link({
</TouchableOpacity> </TouchableOpacity>
) )
}) })
export const TextLink = observer(function Link({
style,
href,
title,
text,
}: {
style?: StyleProp<TextStyle>
href: string
title?: string
text: string
}) {
const store = useStores()
const onPress = () => {
store.shell.closeModal() // close any active modals
store.nav.navigate(href)
}
const onLongPress = () => {
store.shell.closeModal() // close any active modals
store.nav.newTab(href, title)
}
return (
<Text style={style} onPress={onPress} onLongPress={onLongPress}>
{text}
</Text>
)
})

View File

@ -1,6 +1,6 @@
import React from 'react' import React from 'react'
import {Text, TextStyle, StyleProp} from 'react-native' import {Text, TextStyle, StyleProp, View} from 'react-native'
import {Link} from './Link' import {TextLink} from './Link'
import {s} from '../../lib/styles' import {s} from '../../lib/styles'
type TextSlice = {start: number; end: number} type TextSlice = {start: number; end: number}
@ -30,26 +30,20 @@ export function RichText({
let key = 0 let key = 0
for (const segment of segments) { for (const segment of segments) {
if (typeof segment === 'string') { if (typeof segment === 'string') {
els.push( els.push(segment)
<Text key={key} style={style}>
{segment}
</Text>,
)
} else { } else {
els.push( els.push(
<Link <TextLink
key={key} key={key}
title={segment.text} text={segment.text}
href={`/profile/${segment.entity.value}`}> href={`/profile/${segment.entity.value}`}
<Text key={key} style={[style, s.blue3]}> style={[style, s.blue3]}
{segment.text} />,
</Text>
</Link>,
) )
} }
key++ key++
} }
return <>{els}</> return <Text style={style}>{els}</Text>
} }
function sortByIndex(a: Entity, b: Entity) { function sortByIndex(a: Entity, b: Entity) {