mirror of https://github.com/lukechilds/damus.git
William Casarin
3 years ago
11 changed files with 209 additions and 6 deletions
@ -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() |
||||
|
} |
||||
|
} |
||||
|
*/ |
@ -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() |
||||
|
} |
||||
|
} |
||||
|
*/ |
@ -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() |
||||
|
} |
||||
|
} |
||||
|
*/ |
@ -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() |
||||
|
} |
||||
|
} |
||||
|
*/ |
Loading…
Reference in new issue