initial mention parsing

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-04 21:33:08 -07:00
parent 4704431c74
commit 73652513d9
11 changed files with 209 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
//
// BlocksView.swift
// damus
//
// Created by William Casarin on 2022-05-04.
//
import SwiftUI
/*
struct BlocksView_Previews: PreviewProvider {
static var previews: some View {
BlocksView()
}
}
*/

View File

@@ -128,8 +128,7 @@ struct ChatView: View {
}
}
Text(event.content)
.textSelection(.enabled)
NoteContentView(event)
if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey {
EventActionBar(event: event,

View File

@@ -74,7 +74,7 @@ struct EventView: View {
.frame(maxWidth: .infinity, alignment: .leading)
}
Text(event.content)
NoteContentView(event)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)

View File

@@ -0,0 +1,33 @@
//
// MentionView.swift
// damus
//
// Created by William Casarin on 2022-05-04.
//
import SwiftUI
struct MentionView: View {
let mention: Mention
@EnvironmentObject var profiles: Profiles
var body: some View {
switch mention.type {
case .pubkey:
PubkeyView(pubkey: mention.ref.ref_id, relay: mention.ref.relay_id)
.environmentObject(profiles)
case .event:
Text("< e >")
//EventBlockView(pubkey: mention.ref.ref_id, relay: mention.ref.relay_id)
}
}
}
/*
struct MentionView_Previews: PreviewProvider {
static var previews: some View {
MentionView()
}
}
*/

View File

@@ -0,0 +1,57 @@
//
// NoteContentView.swift
// damus
//
// Created by William Casarin on 2022-05-04.
//
import SwiftUI
func NoteContentView(_ ev: NostrEvent) -> some View {
let txt = parse_mentions(content: ev.content, tags: ev.tags)
.reduce("") { str, block in
switch block {
case .mention(let m):
return str + mention_str(m)
case .text(let txt):
return str + txt
}
}
let md_opts: AttributedString.MarkdownParsingOptions =
.init(interpretedSyntax: .inlineOnlyPreservingWhitespace)
guard let txt = try? AttributedString(markdown: txt, options: md_opts) else {
return Text(ev.content)
}
return Text(txt)
}
func mention_str(_ m: Mention) -> String {
switch m.type {
case .pubkey:
let pk = m.ref.ref_id
return "[@\(abbrev_pubkey(pk))](nostr:\(encode_pubkey(m.ref)))"
case .event:
let evid = m.ref.ref_id
return "[*\(abbrev_pubkey(evid))](nostr:\(encode_event_id(m.ref)))"
}
}
// TODO: bech32 and relay hints
func encode_event_id(_ ref: ReferencedId) -> String {
return "e_" + ref.ref_id
}
func encode_pubkey(_ ref: ReferencedId) -> String {
return "p_" + ref.ref_id
}
/*
struct NoteContentView_Previews: PreviewProvider {
static var previews: some View {
NoteContentView()
}
}
*/

View File

@@ -24,7 +24,7 @@ struct ProfileView: View {
var TopSection: some View {
HStack(alignment: .top) {
let data = profiles.lookup(id: profile.pubkey)
ProfilePicView(picture: data?.picture, size: 64, highlight: .custom(Color.black, 4), image_cache: damus.image_cache)
ProfilePicView(picture: data?.picture, size: PFP_SIZE!, highlight: .custom(Color.black, 4), image_cache: damus.image_cache)
//.border(Color.blue)
VStack(alignment: .leading) {
if let pubkey = profile.pubkey {
@@ -44,11 +44,14 @@ struct ProfileView: View {
var body: some View {
VStack(alignment: .leading) {
TopSection
/*
Picker("", selection: $selected_tab) {
Text("Posts").tag(ProfileTab.posts)
Text("Following").tag(ProfileTab.following)
}
.pickerStyle(SegmentedPickerStyle())
*/
Divider()
@@ -64,7 +67,9 @@ struct ProfileView: View {
.frame(maxHeight: .infinity, alignment: .topLeading)
}
//.border(Color.white)
.padding([.leading, .trailing], 6)
.frame(maxWidth: .infinity, alignment: .topLeading)
.navigationBarTitle("Profile")
.onAppear() {
profile.subscribe()

View File

@@ -0,0 +1,33 @@
//
// PubkeyView.swift
// damus
//
// Created by William Casarin on 2022-05-04.
//
import SwiftUI
struct PubkeyView: View {
let pubkey: String
let relay: String?
var body: some View {
let color: Color = id_to_color(pubkey)
ZStack {
Text("\(abbrev_pubkey(pubkey))")
.foregroundColor(color)
}
}
}
func abbrev_pubkey(_ pubkey: String) -> String {
return pubkey.prefix(4) + ":" + pubkey.suffix(4)
}
/*
struct PubkeyView_Previews: PreviewProvider {
static var previews: some View {
PubkeyView()
}
}
*/

View File

@@ -36,6 +36,7 @@ struct ThreadView: View {
}
*/
}
.padding([.leading, .trailing], 6)
.onReceive(NotificationCenter.default.publisher(for: .switched_timeline)) { n in
dismiss()
}