Allow highlighting to be disabled on SelectableText

Changed the interface of SelectableText to allow highlighting to be
disabled in places where it is not applicable (For example, on the
AboutView).

This prevents the need for adding dummy events in places where
highlighting is not applicable, preventing the user from making bad
highlights.

Testing
-------

PASS

Device: iPhone 13 mini
iOS: 17.5
Damus: This version
Steps:
1. Go to a user profile and select some text in their bio. The "highlight" option should not be present.
2. Go to a note and select some text. The "highlight" option should be available
This commit is contained in:
Daniel D’Aquino
2024-06-21 14:18:36 -07:00
parent e7ed9dfe86
commit 4ccfe81558
2 changed files with 15 additions and 11 deletions

View File

@@ -10,7 +10,7 @@ import SwiftUI
struct SelectableText: View { struct SelectableText: View {
let damus_state: DamusState let damus_state: DamusState
let event: NostrEvent let event: NostrEvent?
let attributedString: AttributedString let attributedString: AttributedString
let textAlignment: NSTextAlignment let textAlignment: NSTextAlignment
@State private var showHighlightPost = false @State private var showHighlightPost = false
@@ -20,7 +20,7 @@ struct SelectableText: View {
let size: EventViewKind let size: EventViewKind
init(damus_state: DamusState, event: NostrEvent, attributedString: AttributedString, textAlignment: NSTextAlignment? = nil, size: EventViewKind) { init(damus_state: DamusState, event: NostrEvent?, attributedString: AttributedString, textAlignment: NSTextAlignment? = nil, size: EventViewKind) {
self.damus_state = damus_state self.damus_state = damus_state
self.event = event self.event = event
self.attributedString = attributedString self.attributedString = attributedString
@@ -36,6 +36,7 @@ struct SelectableText: View {
font: eventviewsize_to_uifont(size), font: eventviewsize_to_uifont(size),
fixedWidth: selectedTextWidth, fixedWidth: selectedTextWidth,
textAlignment: self.textAlignment, textAlignment: self.textAlignment,
enableHighlighting: self.enableHighlighting(),
showHighlightPost: $showHighlightPost, showHighlightPost: $showHighlightPost,
selectedText: $selectedText, selectedText: $selectedText,
height: $selectedTextHeight height: $selectedTextHeight
@@ -53,12 +54,18 @@ struct SelectableText: View {
} }
} }
.sheet(isPresented: $showHighlightPost) { .sheet(isPresented: $showHighlightPost) {
HighlightPostView(damus_state: damus_state, event: event, selectedText: $selectedText) if let event {
.presentationDragIndicator(.visible) HighlightPostView(damus_state: damus_state, event: event, selectedText: $selectedText)
.presentationDetents([.height(selectedTextHeight + 150), .medium, .large]) .presentationDragIndicator(.visible)
.presentationDetents([.height(selectedTextHeight + 150), .medium, .large])
}
} }
.frame(height: selectedTextHeight) .frame(height: selectedTextHeight)
} }
func enableHighlighting() -> Bool {
self.event != nil
}
} }
fileprivate class TextView: UITextView { fileprivate class TextView: UITextView {
@@ -97,6 +104,7 @@ fileprivate class TextView: UITextView {
let font: UIFont let font: UIFont
let fixedWidth: CGFloat let fixedWidth: CGFloat
let textAlignment: NSTextAlignment let textAlignment: NSTextAlignment
let enableHighlighting: Bool
@Binding var showHighlightPost: Bool @Binding var showHighlightPost: Bool
@Binding var selectedText: String @Binding var selectedText: String
@Binding var height: CGFloat @Binding var height: CGFloat
@@ -115,7 +123,7 @@ fileprivate class TextView: UITextView {
let menuController = UIMenuController.shared let menuController = UIMenuController.shared
let highlightItem = UIMenuItem(title: "Highlight", action: #selector(view.highlightText(_:))) let highlightItem = UIMenuItem(title: "Highlight", action: #selector(view.highlightText(_:)))
menuController.menuItems = [highlightItem] menuController.menuItems = self.enableHighlighting ? [highlightItem] : []
return view return view
} }

View File

@@ -26,11 +26,7 @@ struct AboutView: View {
Group { Group {
if let about_string { if let about_string {
let truncated_about = show_full_about ? about_string : about_string.truncateOrNil(maxLength: max_about_length) let truncated_about = show_full_about ? about_string : about_string.truncateOrNil(maxLength: max_about_length)
SelectableText(damus_state: state, event: NostrEvent( SelectableText(damus_state: state, event: nil, attributedString: truncated_about ?? about_string, textAlignment: self.text_alignment, size: .subheadline)
content: "",
keypair: jack_keypair,
createdAt: UInt32(Date().timeIntervalSince1970 - 100)
)!, attributedString: truncated_about ?? about_string, textAlignment: self.text_alignment, size: .subheadline)
if truncated_about != nil { if truncated_about != nil {
if show_full_about { if show_full_about {