1
0
mirror of git://jb55.com/damus synced 2024-09-30 00:40:45 +00:00

highlight: fixes and improvements

This patch allows highlights to be included in posts as well as removes context
when creating a highlight. Highlights now route as the root and selecting the
highlight in root routes to the highlighted event.

Signed-off-by: ericholguin <ericholguin@apache.org>
This commit is contained in:
ericholguin 2024-07-02 19:32:44 -06:00
parent b349de22b7
commit 1b8e3fe184
7 changed files with 29 additions and 32 deletions

View File

@ -16,7 +16,7 @@ enum FilterState : Int {
func filter(ev: NostrEvent) -> Bool { func filter(ev: NostrEvent) -> Bool {
switch self { switch self {
case .posts: case .posts:
return ev.known_kind == .boost || !ev.is_reply() return ev.known_kind == .boost || ev.known_kind == .highlight || !ev.is_reply()
case .posts_and_replies: case .posts_and_replies:
return true return true
} }

View File

@ -11,13 +11,15 @@ import Foundation
class ThreadModel: ObservableObject { class ThreadModel: ObservableObject {
@Published var event: NostrEvent @Published var event: NostrEvent
let original_event: NostrEvent let original_event: NostrEvent
let highlight: String?
var event_map: Set<NostrEvent> var event_map: Set<NostrEvent>
init(event: NostrEvent, damus_state: DamusState) { init(event: NostrEvent, damus_state: DamusState, highlight: String? = nil) {
self.damus_state = damus_state self.damus_state = damus_state
self.event_map = Set() self.event_map = Set()
self.event = event self.event = event
self.original_event = event self.original_event = event
self.highlight = highlight
add_event(event, keypair: damus_state.keypair) add_event(event, keypair: damus_state.keypair)
} }

View File

@ -15,18 +15,12 @@ struct ReplyPart: View {
var body: some View { var body: some View {
Group { Group {
if let reply_ref = event.thread_reply()?.reply { if event.known_kind == .highlight {
let highlighted_note = event.highlighted_note_id().flatMap { events.lookup($0) }
HighlightDescription(event: event, highlighted_event: highlighted_note, ndb: ndb)
} else if let reply_ref = event.thread_reply()?.reply {
let replying_to = events.lookup(reply_ref.note_id) let replying_to = events.lookup(reply_ref.note_id)
if event.known_kind != .highlight { ReplyDescription(event: event, replying_to: replying_to, ndb: ndb)
ReplyDescription(event: event, replying_to: replying_to, ndb: ndb)
} else if event.known_kind == .highlight {
HighlightDescription(event: event, highlighted_event: replying_to, ndb: ndb)
}
else {
EmptyView()
}
} else {
EmptyView()
} }
} }
} }

View File

@ -37,6 +37,12 @@ struct EventBody: View {
} }
} else if event.known_kind == .highlight { } else if event.known_kind == .highlight {
HighlightBodyView(state: damus_state, ev: event, options: options) HighlightBodyView(state: damus_state, ev: event, options: options)
.onTapGesture {
if let highlighted_note = event.highlighted_note_id().flatMap({ damus_state.events.lookup($0) }) {
let thread = ThreadModel(event: highlighted_note, damus_state: damus_state, highlight: event.content)
damus_state.nav.push(route: Route.Thread(thread: thread))
}
}
} else { } else {
note_content note_content
} }

View File

@ -29,8 +29,7 @@ struct HighlightPostView: View {
Spacer() Spacer()
Button(NSLocalizedString("Post", comment: "Button to post a highlight.")) { Button(NSLocalizedString("Post", comment: "Button to post a highlight.")) {
var tags: [[String]] = [ ["e", "\(self.event.id)"] ] let tags: [[String]] = [ ["e", "\(self.event.id)"] ]
tags.append(["context", self.event.content])
let kind = NostrKind.highlight.rawValue let kind = NostrKind.highlight.rawValue
guard let ev = NostrEvent(content: selectedText, keypair: damus_state.keypair, kind: kind, tags: tags) else { guard let ev = NostrEvent(content: selectedText, keypair: damus_state.keypair, kind: kind, tags: tags) else {
@ -53,7 +52,7 @@ struct HighlightPostView: View {
HStack { HStack {
var attributedString: AttributedString { var attributedString: AttributedString {
var attributedString = AttributedString(self.event.content) var attributedString = AttributedString(selectedText)
if let range = attributedString.range(of: selectedText) { if let range = attributedString.range(of: selectedText) {
attributedString[range].backgroundColor = DamusColors.highlight attributedString[range].backgroundColor = DamusColors.highlight

View File

@ -40,19 +40,8 @@ struct SelectedEventView: View {
.minimumScaleFactor(0.75) .minimumScaleFactor(0.75)
.lineLimit(1) .lineLimit(1)
if let reply_ref = event.thread_reply()?.reply { ReplyPart(events: damus.events, event: event, keypair: damus.keypair, ndb: damus.ndb)
let replying_to = damus.events.lookup(reply_ref.note_id) .padding(.horizontal)
if event.known_kind == .highlight {
HighlightDescription(event: event, highlighted_event: replying_to, ndb: damus.ndb)
.padding(.horizontal)
} else {
ReplyDescription(event: event, replying_to: replying_to, ndb: damus.ndb)
.padding(.horizontal)
}
} else if event.known_kind == .highlight {
HighlightDescription(event: event, highlighted_event: nil, ndb: damus.ndb)
.padding(.horizontal)
}
ProxyView(event: event) ProxyView(event: event)
.padding(.top, 5) .padding(.top, 5)

View File

@ -341,7 +341,14 @@ extension NdbNote {
} }
func thread_reply() -> ThreadReply? { func thread_reply() -> ThreadReply? {
ThreadReply(tags: self.tags) if self.known_kind != .highlight {
return ThreadReply(tags: self.tags)
}
return nil
}
func highlighted_note_id() -> NoteId? {
return ThreadReply(tags: self.tags)?.reply.note_id
} }
func get_content(_ keypair: Keypair) -> String { func get_content(_ keypair: Keypair) -> String {