mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 07:37:12 +01:00
Show base64 images
This commit is contained in:
@@ -43,9 +43,6 @@ class ExpandableTextCutOffCalculator {
|
||||
newString.lastIndexOf(' ').let { if (it < 0) content.length else it }
|
||||
val firstNewLineBeforeCut =
|
||||
newString.lastIndexOf('\n').let { if (it < 0) content.length else it }
|
||||
if (maxOf(firstSpaceBeforeCut, firstNewLineBeforeCut) == content.length && content.length > SHORT_TEXT_LENGTH) {
|
||||
return SHORT_TEXT_LENGTH
|
||||
}
|
||||
|
||||
return maxOf(firstSpaceBeforeCut, firstNewLineBeforeCut)
|
||||
} else {
|
||||
|
||||
@@ -81,6 +81,21 @@ class RichTextParser {
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseBase64Images(content: String): LinkedHashSet<String> {
|
||||
val regex = "data:image/(${imageExtensions.joinToString(separator = "|") { it } });base64,[a-zA-Z0-9+/]+={0,2}"
|
||||
val pattern = Pattern.compile(regex)
|
||||
val matcher = pattern.matcher(content)
|
||||
|
||||
val base64Images = mutableListOf<String>()
|
||||
|
||||
// Find all matches and add them to the list
|
||||
while (matcher.find()) {
|
||||
base64Images.add(matcher.group())
|
||||
}
|
||||
|
||||
return base64Images.mapTo(LinkedHashSet(base64Images.size)) { it }
|
||||
}
|
||||
|
||||
fun parseValidUrls(content: String): LinkedHashSet<String> {
|
||||
val urls = UrlDetector(content, UrlDetectorOptions.Default).detect()
|
||||
|
||||
@@ -112,13 +127,15 @@ class RichTextParser {
|
||||
): RichTextViewerState {
|
||||
val urlSet = parseValidUrls(content)
|
||||
|
||||
val base64Images = parseBase64Images(content)
|
||||
|
||||
val imagesForPager =
|
||||
urlSet.mapNotNull { fullUrl -> parseMediaUrl(fullUrl, tags, content, callbackUri) }.associateBy { it.url }
|
||||
val imageList = imagesForPager.values.toList()
|
||||
|
||||
val emojiMap = Nip30CustomEmoji.createEmojiMap(tags)
|
||||
|
||||
val segments = findTextSegments(content, imagesForPager.keys, urlSet, emojiMap, tags)
|
||||
val segments = findTextSegments(content, imagesForPager.keys, urlSet, emojiMap, tags, base64Images)
|
||||
|
||||
return RichTextViewerState(
|
||||
urlSet.toImmutableSet(),
|
||||
@@ -126,6 +143,7 @@ class RichTextParser {
|
||||
imageList.toImmutableList(),
|
||||
emojiMap.toImmutableMap(),
|
||||
segments,
|
||||
base64Images.toImmutableSet(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -135,6 +153,7 @@ class RichTextParser {
|
||||
urls: Set<String>,
|
||||
emojis: Map<String, String>,
|
||||
tags: ImmutableListOfLists<String>,
|
||||
base64Images: Set<String>,
|
||||
): ImmutableList<ParagraphState> {
|
||||
val lines = content.split('\n')
|
||||
val paragraphSegments = ArrayList<ParagraphState>(lines.size)
|
||||
@@ -146,7 +165,7 @@ class RichTextParser {
|
||||
val wordList = paragraph.trimEnd().split(' ')
|
||||
val segments = ArrayList<Segment>(wordList.size)
|
||||
wordList.forEach { word ->
|
||||
val wordSegment = wordIdentifier(word, images, urls, emojis, tags)
|
||||
val wordSegment = wordIdentifier(word, images, urls, emojis, tags, base64Images)
|
||||
if (wordSegment !is RegularTextSegment) {
|
||||
isDirty = true
|
||||
}
|
||||
@@ -200,9 +219,12 @@ class RichTextParser {
|
||||
urls: Set<String>,
|
||||
emojis: Map<String, String>,
|
||||
tags: ImmutableListOfLists<String>,
|
||||
base64Images: Set<String>,
|
||||
): Segment {
|
||||
if (word.isEmpty()) return RegularTextSegment(word)
|
||||
|
||||
if (base64Images.contains(word)) return Base64Segment(word)
|
||||
|
||||
if (images.contains(word)) return ImageSegment(word)
|
||||
|
||||
if (urls.contains(word)) return LinkSegment(word)
|
||||
|
||||
@@ -32,6 +32,7 @@ data class RichTextViewerState(
|
||||
val imageList: ImmutableList<MediaUrlContent>,
|
||||
val customEmoji: ImmutableMap<String, String>,
|
||||
val paragraphs: ImmutableList<ParagraphState>,
|
||||
val base64Images: ImmutableSet<String>,
|
||||
)
|
||||
|
||||
@Immutable
|
||||
@@ -67,6 +68,9 @@ class PhoneSegment(segment: String) : Segment(segment)
|
||||
@Immutable
|
||||
class BechSegment(segment: String) : Segment(segment)
|
||||
|
||||
@Immutable
|
||||
class Base64Segment(segment: String) : Segment(segment)
|
||||
|
||||
@Immutable
|
||||
open class HashIndexSegment(segment: String, val hex: String, val extras: String?) :
|
||||
Segment(segment)
|
||||
|
||||
Reference in New Issue
Block a user