Inline image loading

Changelog-Added: Added inline image loading
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-10-16 16:11:27 -07:00
parent 66eefa0ff2
commit a47645929e
16 changed files with 267 additions and 60 deletions

View File

@@ -8,9 +8,10 @@
import SwiftUI
func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?) -> String {
func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?) -> (String, [URL]) {
let blocks = ev.blocks(privkey)
return blocks.reduce("") { str, block in
var img_urls: [URL] = []
let txt = blocks.reduce("") { str, block in
switch block {
case .mention(let m):
return str + mention_str(m, profiles: profiles)
@@ -18,8 +19,20 @@ func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?) -
return str + txt
case .hashtag(let htag):
return str + hashtag_str(htag)
case .url(let url):
if is_image_url(url) {
img_urls.append(url)
}
return str + url.absoluteString
}
}
return (txt, img_urls)
}
func is_image_url(_ url: URL) -> Bool {
let str = url.lastPathComponent
return str.hasSuffix("png") || str.hasSuffix("jpg") || str.hasSuffix("jpeg")
}
struct NoteContentView: View {
@@ -27,23 +40,33 @@ struct NoteContentView: View {
let event: NostrEvent
let profiles: Profiles
let show_images: Bool
@State var content: String
@State var images: [URL] = []
func MainContent() -> some View {
let md_opts: AttributedString.MarkdownParsingOptions =
.init(interpretedSyntax: .inlineOnlyPreservingWhitespace)
guard let txt = try? AttributedString(markdown: content, options: md_opts) else {
return Text(content)
return VStack(alignment: .leading) {
if let txt = try? AttributedString(markdown: content, options: md_opts) {
Text(txt)
} else {
Text(content)
}
if show_images && images.count > 0 {
ImageCarousel(urls: images)
}
}
return Text(txt)
}
var body: some View {
MainContent()
.onAppear() {
self.content = render_note_content(ev: event, profiles: profiles, privkey: privkey)
let (txt, images) = render_note_content(ev: event, profiles: profiles, privkey: privkey)
self.content = txt
self.images = images
}
.onReceive(handle_notify(.profile_updated)) { notif in
let profile = notif.object as! ProfileUpdate
@@ -52,10 +75,13 @@ struct NoteContentView: View {
switch block {
case .mention(let m):
if m.type == .pubkey && m.ref.ref_id == profile.pubkey {
content = render_note_content(ev: event, profiles: profiles, privkey: privkey)
let (txt, images) = render_note_content(ev: event, profiles: profiles, privkey: privkey)
self.content = txt
self.images = images
}
case .text: return
case .hashtag: return
case .url: return
}
}
}
@@ -80,10 +106,10 @@ func mention_str(_ m: Mention, profiles: Profiles) -> String {
}
/*
struct NoteContentView_Previews: PreviewProvider {
static var previews: some View {
NoteContentView()
let state = test_damus_state()
let content = "hi there https://jb55.com/s/Oct12-150217.png 5739a762ef6124dd.jpg"
NoteContentView(privkey: "", event: NostrEvent(content: content, pubkey: "pk"), profiles: state.profiles, show_images: true, content: content)
}
}
*/