search: highlight terms in note search results
Changelog-Changed: Highlight note search results Signed-off-by: alltheseas
This commit is contained in:
committed by
William Casarin
parent
9eda7e5886
commit
d3a54458f5
@@ -11,6 +11,38 @@ struct NDBSearchView: View {
|
||||
|
||||
let damus_state: DamusState
|
||||
@Binding var results: [NostrEvent]
|
||||
let searchQuery: String
|
||||
|
||||
var highlightTerms: [String] {
|
||||
let trimmed = searchQuery.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty else { return [] }
|
||||
|
||||
let parts = trimmed.split(whereSeparator: { $0.isWhitespace })
|
||||
var terms: [String] = []
|
||||
|
||||
for part in parts {
|
||||
let term = String(part)
|
||||
let strippedHashtag = term.hasPrefix("#") ? String(term.dropFirst()) : nil
|
||||
|
||||
if let stripped = strippedHashtag, !stripped.isEmpty {
|
||||
terms.append(stripped)
|
||||
}
|
||||
|
||||
if !term.isEmpty {
|
||||
terms.append(term)
|
||||
}
|
||||
}
|
||||
|
||||
var deduped: [String] = []
|
||||
var seen = Set<String>()
|
||||
for term in terms.map({ $0.lowercased() }) {
|
||||
if seen.insert(term).inserted {
|
||||
deduped.append(term)
|
||||
}
|
||||
}
|
||||
|
||||
return deduped
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
@@ -24,9 +56,16 @@ struct NDBSearchView: View {
|
||||
.padding()
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
if !highlightTerms.isEmpty {
|
||||
Text("Search: \(searchQuery)")
|
||||
.font(.footnote)
|
||||
.foregroundColor(.secondary)
|
||||
.padding(.bottom, 4)
|
||||
}
|
||||
|
||||
LazyVStack {
|
||||
ForEach(results, id: \.self) { note in
|
||||
EventView(damus: damus_state, event: note, options: [.truncate_content])
|
||||
EventView(damus: damus_state, event: note, options: [.truncate_content], highlightTerms: highlightTerms)
|
||||
.onTapGesture {
|
||||
let event = note.get_inner_event(cache: damus_state.events) ?? note
|
||||
let thread = ThreadModel(event: event, damus_state: damus_state)
|
||||
|
||||
@@ -68,7 +68,7 @@ struct InnerSearchResults: View {
|
||||
}
|
||||
|
||||
func TextSearch(_ txt: String) -> some View {
|
||||
return NavigationLink(value: Route.NDBSearch(results: $results)) {
|
||||
return NavigationLink(value: Route.NDBSearch(results: $results, query: txt)) {
|
||||
HStack {
|
||||
Text("Search word: \(txt)", comment: "Navigation link to search for a word.")
|
||||
}
|
||||
@@ -296,4 +296,3 @@ func search_profiles(profiles: Profiles, contacts: Contacts, search: String) ->
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user