From 954d84fab21b80fba3a8758f56df1085b793ef06 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 16 May 2022 12:05:45 -0700 Subject: [PATCH] add share option to web reader context menu --- .../OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift | 6 ++++++ .../Sources/App/Views/WebReader/WebReaderContainer.swift | 6 ++++++ .../Sources/App/Views/WebReader/WebReaderCoordinator.swift | 1 + apple/OmnivoreKit/Sources/Views/Article/WebView.swift | 4 ++++ 4 files changed, 17 insertions(+) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index c1001ca07..3c9e2cc75 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -17,6 +17,7 @@ import WebKit @Binding var decreaseFontActionID: UUID? @Binding var annotationSaveTransactionID: UUID? @Binding var showNavBarActionID: UUID? + @Binding var shareActionID: UUID? @Binding var annotation: String func makeCoordinator() -> WebReaderCoordinator { @@ -81,6 +82,11 @@ import WebKit context.coordinator.showNavBar() } + if shareActionID != context.coordinator.previousShareActionID { + context.coordinator.previousShareActionID = shareActionID + (webView as? WebView)?.shareOriginalItem() + } + // If the webview had been terminated `needsReload` will have been set to true if context.coordinator.needsReload { loadContent(webView: webView) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index 6126708f2..b17cf4abf 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -19,6 +19,7 @@ import WebKit @State var decreaseFontActionID: UUID? @State var annotationSaveTransactionID: UUID? @State var showNavBarActionID: UUID? + @State var shareActionID: UUID? @State var annotation = String() @EnvironmentObject var dataService: DataService @@ -104,6 +105,10 @@ import WebKit ) } ) + Button( + action: { shareActionID = UUID() }, + label: { Label("Share", systemImage: "square.and.arrow.up") } + ) Button( action: { showDeleteConfirmation = true }, label: { Label("Delete", systemImage: "trash") } @@ -156,6 +161,7 @@ import WebKit decreaseFontActionID: $decreaseFontActionID, annotationSaveTransactionID: $annotationSaveTransactionID, showNavBarActionID: $showNavBarActionID, + shareActionID: $shareActionID, annotation: $annotation ) .onTapGesture { diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift index 7ed3a19f6..e88339d84 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift @@ -18,6 +18,7 @@ final class WebReaderCoordinator: NSObject { var previousIncreaseFontActionID: UUID? var previousDecreaseFontActionID: UUID? var previousShowNavBarActionID: UUID? + var previousShareActionID: UUID? var updateNavBarVisibilityRatio: (Double) -> Void = { _ in } private var yOffsetAtStartOfDrag: Double? private var lastYOffset: Double = 0 diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebView.swift index 5a6277f00..b45018a91 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebView.swift @@ -34,6 +34,10 @@ public final class WebView: WKWebView { dispatchEvent("decreaseFontSize") } + public func shareOriginalItem() { + dispatchEvent("share") + } + func dispatchEvent(_ name: String) { let dispatch = "document.dispatchEvent(new Event('\(name)'));" evaluateJavaScript(dispatch) { obj, err in