From 4ccfe81558427a8f5255b48ab55b614c11f6fe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Fri, 21 Jun 2024 14:18:36 -0700 Subject: [PATCH] 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 --- damus/Components/SelectableText.swift | 20 ++++++++++++++------ damus/Views/Profile/AboutView.swift | 6 +----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/damus/Components/SelectableText.swift b/damus/Components/SelectableText.swift index 05500eb9..db62aa96 100644 --- a/damus/Components/SelectableText.swift +++ b/damus/Components/SelectableText.swift @@ -10,7 +10,7 @@ import SwiftUI struct SelectableText: View { let damus_state: DamusState - let event: NostrEvent + let event: NostrEvent? let attributedString: AttributedString let textAlignment: NSTextAlignment @State private var showHighlightPost = false @@ -20,7 +20,7 @@ struct SelectableText: View { 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.event = event self.attributedString = attributedString @@ -36,6 +36,7 @@ struct SelectableText: View { font: eventviewsize_to_uifont(size), fixedWidth: selectedTextWidth, textAlignment: self.textAlignment, + enableHighlighting: self.enableHighlighting(), showHighlightPost: $showHighlightPost, selectedText: $selectedText, height: $selectedTextHeight @@ -53,12 +54,18 @@ struct SelectableText: View { } } .sheet(isPresented: $showHighlightPost) { - HighlightPostView(damus_state: damus_state, event: event, selectedText: $selectedText) - .presentationDragIndicator(.visible) - .presentationDetents([.height(selectedTextHeight + 150), .medium, .large]) + if let event { + HighlightPostView(damus_state: damus_state, event: event, selectedText: $selectedText) + .presentationDragIndicator(.visible) + .presentationDetents([.height(selectedTextHeight + 150), .medium, .large]) + } } .frame(height: selectedTextHeight) } + + func enableHighlighting() -> Bool { + self.event != nil + } } fileprivate class TextView: UITextView { @@ -97,6 +104,7 @@ fileprivate class TextView: UITextView { let font: UIFont let fixedWidth: CGFloat let textAlignment: NSTextAlignment + let enableHighlighting: Bool @Binding var showHighlightPost: Bool @Binding var selectedText: String @Binding var height: CGFloat @@ -115,7 +123,7 @@ fileprivate class TextView: UITextView { let menuController = UIMenuController.shared let highlightItem = UIMenuItem(title: "Highlight", action: #selector(view.highlightText(_:))) - menuController.menuItems = [highlightItem] + menuController.menuItems = self.enableHighlighting ? [highlightItem] : [] return view } diff --git a/damus/Views/Profile/AboutView.swift b/damus/Views/Profile/AboutView.swift index 892d23f0..d479c5bf 100644 --- a/damus/Views/Profile/AboutView.swift +++ b/damus/Views/Profile/AboutView.swift @@ -26,11 +26,7 @@ struct AboutView: View { Group { if let about_string { let truncated_about = show_full_about ? about_string : about_string.truncateOrNil(maxLength: max_about_length) - SelectableText(damus_state: state, event: NostrEvent( - content: "", - keypair: jack_keypair, - createdAt: UInt32(Date().timeIntervalSince1970 - 100) - )!, attributedString: truncated_about ?? about_string, textAlignment: self.text_alignment, size: .subheadline) + SelectableText(damus_state: state, event: nil, attributedString: truncated_about ?? about_string, textAlignment: self.text_alignment, size: .subheadline) if truncated_about != nil { if show_full_about {