Add highlighter extension

This commit adds a highlighting extension for web pages. This works on
Safari, and can be used by selecting a text on a page and hitting the
share button at the bottom of the Safari UI

To make this possible, some refactoring was necessary:
1. Several sources were included in the extension bundle to provide access to DamusState, PostView, and the postbox
2. UIApplication.shared was replaced with `this_app`, which routes to UIApplication.shared on the main app bundle,
   and routes to a bogus UIApplication() in the extension. This is needed because UIApplication.shared cannot be used on an extension.
3. Some items were moved to different files to facilitate the transition.

The extension itself uses PostView, and implements views for several edge cases, and tries to handle the note publishing process gracefully.

Changelog-Added: Add highlighter for web pages
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2024-08-17 15:38:27 -07:00
parent 40d3d273f0
commit 55090bc102
21 changed files with 1642 additions and 19 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1540"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D703D7162C66E47100A400EA"
BuildableName = "HighlighterActionExtension.appex"
BlueprintName = "HighlighterActionExtension"
ReferencedContainer = "container:damus.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4CE6DEE227F7A08100C66700"
BuildableName = "damus.app"
BlueprintName = "damus"
ReferencedContainer = "container:damus.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = ""
selectedLauncherIdentifier = "Xcode.IDEFoundation.Launcher.PosixSpawn"
launchStyle = "0"
askForAppToLaunch = "Yes"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES"
launchAutomaticallySubstyle = "2">
<RemoteRunnable
runnableDebuggingMode = "1"
BundleIdentifier = "com.apple.mobilesafari"
RemotePath = "/Library/Developer/CoreSimulator/Volumes/iOS_21F79/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.5.simruntime/Contents/Resources/RuntimeRoot/Applications/MobileSafari.app">
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4CE6DEE227F7A08100C66700"
BuildableName = "damus.app"
BlueprintName = "damus"
ReferencedContainer = "container:damus.xcodeproj">
</BuildableReference>
</MacroExpansion>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
askForAppToLaunch = "Yes"
launchAutomaticallySubstyle = "2">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4CE6DEE227F7A08100C66700"
BuildableName = "damus.app"
BlueprintName = "damus"
ReferencedContainer = "container:damus.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@@ -94,8 +94,8 @@ enum OpenWalletError: Error {
} }
func open_with_wallet(wallet: Wallet.Model, invoice: String) throws { func open_with_wallet(wallet: Wallet.Model, invoice: String) throws {
if let url = URL(string: "\(wallet.link)\(invoice)"), UIApplication.shared.canOpenURL(url) { if let url = URL(string: "\(wallet.link)\(invoice)"), this_app.canOpenURL(url) {
UIApplication.shared.open(url) this_app.open(url)
} else { } else {
guard let store_link = wallet.appStoreLink else { guard let store_link = wallet.appStoreLink else {
throw OpenWalletError.no_wallet_to_open throw OpenWalletError.no_wallet_to_open
@@ -105,11 +105,11 @@ func open_with_wallet(wallet: Wallet.Model, invoice: String) throws {
throw OpenWalletError.store_link_invalid throw OpenWalletError.store_link_invalid
} }
guard UIApplication.shared.canOpenURL(url) else { guard this_app.canOpenURL(url) else {
throw OpenWalletError.system_cannot_open_store_link throw OpenWalletError.system_cannot_open_store_link
} }
UIApplication.shared.open(url) this_app.open(url)
} }
} }
@@ -122,8 +122,3 @@ struct InvoiceView_Previews: PreviewProvider {
.frame(width: 300, height: 200) .frame(width: 300, height: 200)
} }
} }
func present_sheet(_ sheet: Sheets) {
notify(.present_sheet(sheet))
}

View File

@@ -57,6 +57,10 @@ enum Sheets: Identifiable {
} }
} }
func present_sheet(_ sheet: Sheets) {
notify(.present_sheet(sheet))
}
struct ContentView: View { struct ContentView: View {
let keypair: Keypair let keypair: Keypair
let appDelegate: AppDelegate? let appDelegate: AppDelegate?
@@ -877,7 +881,7 @@ func update_filters_with_since(last_of_kind: [UInt32: NostrEvent], filters: [Nos
func setup_notifications() { func setup_notifications() {
UIApplication.shared.registerForRemoteNotifications() this_app.registerForRemoteNotifications()
let center = UNUserNotificationCenter.current() let center = UNUserNotificationCenter.current()
center.getNotificationSettings { settings in center.getNotificationSettings { settings in

View File

@@ -151,7 +151,7 @@ public class CameraService: NSObject, Identifiable {
DispatchQueue.main.async { DispatchQueue.main.async {
self.alertError = AlertError(title: "Camera Access", message: "Damus needs camera and microphone access. Enable in settings.", primaryButtonTitle: "Go to settings", secondaryButtonTitle: nil, primaryAction: { self.alertError = AlertError(title: "Camera Access", message: "Damus needs camera and microphone access. Enable in settings.", primaryButtonTitle: "Go to settings", secondaryButtonTitle: nil, primaryAction: {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, this_app.open(URL(string: UIApplication.openSettingsURLString)!,
options: [:], completionHandler: nil) options: [:], completionHandler: nil)
}, secondaryAction: nil) }, secondaryAction: nil)

View File

@@ -0,0 +1,11 @@
//
// DamusAliases.swift
// damus
//
// Created by Daniel DAquino on 2024-08-12.
//
import Foundation
import UIKit
let this_app: UIApplication = UIApplication.shared

View File

@@ -30,7 +30,7 @@ public struct DismissKeyboardOnTap: ViewModifier {
} }
public func end_editing() { public func end_editing() {
UIApplication.shared.connectedScenes this_app.connectedScenes
.filter {$0.activationState == .foregroundActive} .filter {$0.activationState == .foregroundActive}
.map {$0 as? UIWindowScene} .map {$0 as? UIWindowScene}
.compactMap({$0}) .compactMap({$0})

View File

@@ -11,8 +11,7 @@ import UIKit
class Theme { class Theme {
static var safeAreaInsets: UIEdgeInsets? { static var safeAreaInsets: UIEdgeInsets? {
return UIApplication return this_app
.shared
.connectedScenes .connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] } .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }?.safeAreaInsets .first { $0.isKeyWindow }?.safeAreaInsets

View File

@@ -116,7 +116,7 @@ struct AddRelayView: View {
} }
new_relay = "" new_relay = ""
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) this_app.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
dismiss() dismiss()
}) { }) {

View File

@@ -87,7 +87,7 @@ struct AddMuteItemView: View {
new_text = "" new_text = ""
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) this_app.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
dismiss() dismiss()
}) { }) {

View File

@@ -96,7 +96,7 @@ struct DamusAppNotificationView: View {
@MainActor @MainActor
func open_url(url: URL) { func open_url(url: URL) {
UIApplication.shared.open(url) this_app.open(url)
} }
var body: some View { var body: some View {

View File

@@ -356,7 +356,7 @@ struct ZapSheetViewIfPossible: View {
extension View { extension View {
func hideKeyboard() { func hideKeyboard() {
let resign = #selector(UIResponder.resignFirstResponder) let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil) this_app.sendAction(resign, to: nil, from: nil, for: nil)
} }
} }

View File

@@ -0,0 +1,250 @@
//
// ActionViewController.swift
// highlighter action extension
//
// Created by Daniel DAquino on 2024-08-09.
//
import UIKit
import MobileCoreServices
import UniformTypeIdentifiers
import SwiftUI
struct ShareExtensionView: View {
@State var highlighter_state: HighlighterState = .loading
let extensionContext: NSExtensionContext
@State var state: DamusState? = nil
@State var signedEvent: String? = nil
@State private var selectedText = ""
@State private var selectedTextHeight: CGFloat = .zero
@State private var selectedTextWidth: CGFloat = .zero
var body: some View {
VStack(spacing: 15) {
if let state {
switch self.highlighter_state {
case .loading:
ProgressView()
case .no_highlight_text:
Group {
Text("No text selected", comment: "Title indicating that a highlight cannot be posted because no text was selected.")
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding()
Text("You cannot post a highlight because you have selected no text on the page! Please close this, select some text, and try again.", comment: "Label explaining a highlight cannot be made because there was no selected text, and some instructions on how to resolve the issue")
.multilineTextAlignment(.center)
Button(action: {
self.done()
}, label: {
Text("Close", comment: "Button label giving the user the option to close the sheet from which they were trying to post a highlight")
})
.foregroundStyle(.secondary)
}
case .not_logged_in:
Group {
Text("Not logged in", comment: "Title indicating that a highlight cannot be posted because the user is not logged in.")
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding()
Text("You cannot post a highlight because you are not logged in with a private key! Please close this, login with a private key (or nsec), and try again.", comment: "Label explaining a highlight cannot be made because the user is not logged in")
.multilineTextAlignment(.center)
Button(action: {
self.done()
}, label: {
Text("Close", comment: "Button label giving the user the option to close the sheet from which they were trying to post a highlight")
})
.foregroundStyle(.secondary)
}
case .loaded(let highlighted_text, let source_url):
PostView(
action: .highlighting(HighlightContentDraft(selected_text: highlighted_text, source: .external_url(source_url))),
damus_state: state
)
case .failed(let error):
Group {
Text("Error", comment: "Title indicating that an error has occurred.")
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding()
Text("An unexpected error occurred. Please contact Damus support via [Nostr](damus:npub18m76awca3y37hkvuneavuw6pjj4525fw90necxmadrvjg0sdy6qsngq955) or [email](support@damus.io) with the error message below.", comment: "Label explaining there was an error, and suggesting next steps")
.multilineTextAlignment(.center)
Text("Error: \(error)")
Button(action: {
self.done()
}, label: {
Text("Close", comment: "Button label giving the user the option to close the sheet from which they were trying to post a highlight")
})
.foregroundStyle(.secondary)
}
case .posted(event: let event):
Group {
Image(systemName: "checkmark.circle.fill")
.resizable()
.frame(width: 60, height: 60)
Text("Posted", comment: "Title indicating that the user has posted a highlight successfully")
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding(.bottom)
Link(destination: URL(string: "damus:\(event.id.bech32)")!, label: {
Text("Go to the app", comment: "Button label giving the user the option to go to the app after posting a highlight")
})
.buttonStyle(GradientButtonStyle())
Button(action: {
self.done()
}, label: {
Text("Close", comment: "Button label giving the user the option to close the sheet from which they posted a highlight")
})
.foregroundStyle(.secondary)
}
case .cancelled:
Group {
Text("Cancelled", comment: "Title indicating that the user has cancelled.")
.font(.largeTitle)
.padding()
Button(action: {
self.done()
}, label: {
Text("Close", comment: "Button label giving the user the option to close the sheet from which they were trying to post a highlight")
})
.foregroundStyle(.secondary)
}
case .posting:
Group {
ProgressView()
.frame(width: 20, height: 20)
Text("Posting", comment: "Title indicating that the highlight post is being published to the network")
.font(.largeTitle)
.multilineTextAlignment(.center)
.padding(.bottom)
Text("Your highlight is being broadcasted to the network. Please wait.", comment: "Label explaining there their highlight publishing action is in progress")
.multilineTextAlignment(.center)
.padding()
}
}
}
}
.onAppear(perform: {
self.loadSharedUrl()
guard let keypair = get_saved_keypair() else { return }
guard keypair.privkey != nil else {
self.highlighter_state = .not_logged_in
return
}
self.state = DamusState(keypair: keypair)
})
.onChange(of: self.highlighter_state) {
if case .cancelled = highlighter_state {
self.done()
}
}
.onReceive(handle_notify(.post)) { post_notification in
switch post_notification {
case .post(let post):
self.post(post)
case .cancel:
self.highlighter_state = .cancelled
}
}
}
func loadSharedUrl() {
guard
let extensionItem = extensionContext.inputItems.first as? NSExtensionItem,
let itemProvider = extensionItem.attachments?.first else {
self.highlighter_state = .failed(error: "Can't get itemProvider")
return
}
let propertyList = UTType.propertyList.identifier
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
if error != nil {
self.highlighter_state = .failed(error: "Error loading plist item: \(error?.localizedDescription ?? "Unknown")")
return
}
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary,
let urlString = results["URL"] as? String,
let selection = results["selectedText"] as? String,
let url = URL(string: urlString) {
guard selection != "" else {
self.highlighter_state = .no_highlight_text
return
}
self.highlighter_state = .loaded(highlighted_text: selection, source_url: url)
}
else {
self.highlighter_state = .failed(error: "Cannot load results")
}
}
})
}
else {
self.highlighter_state = .failed(error: "No plist detected")
}
}
func post(_ post: NostrPost) {
self.highlighter_state = .posting
guard let state else {
self.highlighter_state = .failed(error: "Damus state not initialized")
return
}
guard let full_keypair = state.keypair.to_full() else {
self.highlighter_state = .not_logged_in
return
}
guard let posted_event = post.to_event(keypair: full_keypair) else {
self.highlighter_state = .failed(error: "Cannot convert post data into a nostr event")
return
}
state.postbox.send(posted_event, on_flush: .once({ flushed_event in
if flushed_event.event.id == posted_event.id {
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { // Offset labor perception bias
self.highlighter_state = .posted(event: flushed_event.event)
})
}
else {
self.highlighter_state = .failed(error: "Flushed event is not the event we just tried to post.")
}
}))
}
func done() {
self.extensionContext.completeRequest(returningItems: [], completionHandler: nil)
}
enum HighlighterState: Equatable {
case loading
case no_highlight_text
case not_logged_in
case loaded(highlighted_text: String, source_url: URL)
case posting
case posted(event: NostrEvent)
case cancelled
case failed(error: String)
}
}
class ActionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.tintColor = UIColor(DamusColors.purple)
DispatchQueue.main.async {
let contentView = UIHostingController(rootView: ShareExtensionView(extensionContext: self.extensionContext!))
self.addChild(contentView)
self.view.addSubview(contentView.view)
// set up constraints
contentView.view.translatesAutoresizingMaskIntoConstraints = false
contentView.view.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
contentView.view.bottomAnchor.constraint (equalTo: self.view.bottomAnchor).isActive = true
contentView.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
contentView.view.rightAnchor.constraint (equalTo: self.view.rightAnchor).isActive = true
}
}
}

View File

@@ -0,0 +1,11 @@
//
// HighlighterExtensionAliases.swift
// highlighter action extension
//
// Created by Daniel DAquino on 2024-08-12.
//
import Foundation
import UIKit
let this_app: UIApplication = UIApplication()

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>getSelection</string>
<key>NSExtensionServiceAllowsFinderPreviewItem</key>
<true/>
<key>NSExtensionServiceAllowsTouchBarItem</key>
<true/>
<key>NSExtensionServiceFinderPreviewIconName</key>
<string>NSActionTemplate</string>
<key>NSExtensionServiceTouchBarBezelColorName</key>
<string>TouchBarBezel</string>
<key>NSExtensionServiceTouchBarIconName</key>
<string>NSActionTemplate</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.ui-services</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ActionViewController</string>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,14 @@
{
"images" : [
{
"filename" : "highlighter.png",
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,14 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "mac",
"color" : {
"reference" : "systemPurpleColor"
}
}
]
}

View File

@@ -0,0 +1,12 @@
var Share = function() {};
Share.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "selectedText": document.getSelection().toString()});
},
finalize: function(arguments) {
// alert shared!
}
};
var ExtensionPreprocessingJS = new Share

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.damus</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)com.jb55.damus2</string>
</array>
</dict>
</plist>