more mention progress

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-07 13:50:19 -07:00
parent 73652513d9
commit 0eb1372937
21 changed files with 611 additions and 144 deletions

View File

@@ -128,7 +128,7 @@ struct ChatView: View {
}
}
NoteContentView(event)
NoteContentView(event: event, profiles: profiles)
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)
}
NoteContentView(event)
NoteContentView(event: event, profiles: profiles)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)

View File

@@ -7,46 +7,69 @@
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
}
func render_note_content(ev: NostrEvent, profiles: Profiles) -> String {
return ev.blocks.reduce("") { str, block in
switch block {
case .mention(let m):
return str + mention_str(m, profiles: profiles)
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 {
struct NoteContentView: View {
let event: NostrEvent
let profiles: Profiles
@State var content: String = ""
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(event.content)
}
return Text(txt)
}
var body: some View {
MainContent()
.onAppear() {
self.content = render_note_content(ev: event, profiles: profiles)
}
.onReceive(handle_notify(.profile_update)) { notif in
let profile = notif.object as! ProfileUpdate
for block in event.blocks {
switch block {
case .mention(let m):
if m.type == .pubkey && m.ref.ref_id == profile.pubkey {
content = render_note_content(ev: event, profiles: profiles)
}
case .text:
return
}
}
}
}
}
func mention_str(_ m: Mention, profiles: Profiles) -> String {
switch m.type {
case .pubkey:
let pk = m.ref.ref_id
return "[@\(abbrev_pubkey(pk))](nostr:\(encode_pubkey(m.ref)))"
let profile = profiles.lookup(id: pk)
let disp = Profile.displayName(profile: profile, pubkey: pk)
return "[@\(disp)](nostr:\(encode_pubkey_uri(m.ref)))"
case .event:
let evid = m.ref.ref_id
return "[*\(abbrev_pubkey(evid))](nostr:\(encode_event_id(m.ref)))"
return "[&\(abbrev_pubkey(evid))](nostr:\(encode_event_id_uri(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 {

View File

@@ -12,26 +12,6 @@ enum NostrPostResult {
case cancel
}
struct NostrPost {
let content: String
let references: [ReferencedId]
public func to_event(privkey: String, pubkey: String) -> NostrEvent {
let new_ev = NostrEvent(content: content, pubkey: pubkey)
for id in references {
var tag = [id.key, id.ref_id]
if let relay_id = id.relay_id {
tag.append(relay_id)
}
new_ev.tags.append(tag)
}
new_ev.calculate_id()
new_ev.sign(privkey: privkey)
return new_ev
}
}
struct PostView: View {
@State var post: String = ""
@FocusState var focus: Bool

View File

@@ -31,6 +31,10 @@ struct ProfileView: View {
ProfileName(pubkey: pubkey, profile: data)
.font(.title)
//.border(Color.green)
Text("\(pubkey)")
.textSelection(.enabled)
.font(.footnote)
.foregroundColor(id_to_color(pubkey))
}
Text(data?.about ?? "")
//.border(Color.red)

View File

@@ -20,8 +20,8 @@ struct PubkeyView: View {
}
}
func abbrev_pubkey(_ pubkey: String) -> String {
return pubkey.prefix(4) + ":" + pubkey.suffix(4)
func abbrev_pubkey(_ pubkey: String, amount: Int = 8) -> String {
return pubkey.prefix(amount) + ":" + pubkey.suffix(amount)
}
/*