Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Ref #1499: Add link preview to long press context menu. #1625

Merged
merged 4 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BraveShared/BraveStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extension Strings {
public static let SaveButtonTitle = NSLocalizedString("SaveButtonTitle", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Save", comment: "Label for the button used to save data")
public static let Share = NSLocalizedString("CommonShare", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Share", comment: "Text to select sharing something (example: image, video, URL)")
public static let Download = NSLocalizedString("CommonDownload", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Download", comment: "Text to choose for downloading a file (example: saving an image to phone)")
public static let ShowLinkPreviewsActionTitle = NSLocalizedString("ShowLinkPreviewsActionTitle", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Show Link Previews", comment: "Context menu item for showing link previews")
public static let HideLinkPreviewsActionTitle = NSLocalizedString("HideLinkPreviewsActionTitle", tableName: "BraveShared", bundle: Bundle.braveShared, value: "Hide Link Previews", comment: "Context menu item for hiding link previews")
}

// MARK:- UIAlertControllerExtensions.swift
Expand Down
4 changes: 4 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
0A0D3D5221A565C300BEE65B /* SafeBrowsingHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A0D3D5121A565C300BEE65B /* SafeBrowsingHandler.swift */; };
0A0D3D6121A596BE00BEE65B /* MalwareList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A0D3D6021A596BE00BEE65B /* MalwareList.swift */; };
0A19362F234D44DB002E2B81 /* AppearanceAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A19362E234D44DB002E2B81 /* AppearanceAttributes.swift */; };
0A19365423508756002E2B81 /* LinkPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A19365323508756002E2B81 /* LinkPreviewViewController.swift */; };
0A1E842D21909CA70042F782 /* crypto.js in Resources */ = {isa = PBXBuildFile; fileRef = 0A1E842C21909CA70042F782 /* crypto.js */; };
0A1E842F21909CBC0042F782 /* bundle.js in Resources */ = {isa = PBXBuildFile; fileRef = 0A1E842E21909CBB0042F782 /* bundle.js */; };
0A1E843121909CC40042F782 /* fetch.js in Resources */ = {isa = PBXBuildFile; fileRef = 0A1E843021909CC30042F782 /* fetch.js */; };
Expand Down Expand Up @@ -1178,6 +1179,7 @@
0A0D3D5121A565C300BEE65B /* SafeBrowsingHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafeBrowsingHandler.swift; sourceTree = "<group>"; };
0A0D3D6021A596BE00BEE65B /* MalwareList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MalwareList.swift; sourceTree = "<group>"; };
0A19362E234D44DB002E2B81 /* AppearanceAttributes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppearanceAttributes.swift; sourceTree = "<group>"; };
0A19365323508756002E2B81 /* LinkPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkPreviewViewController.swift; sourceTree = "<group>"; };
0A1E841B219095410042F782 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
0A1E842C21909CA70042F782 /* crypto.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = crypto.js; path = "node_modules/brave-sync/node_modules/brave-crypto/browser/crypto.js"; sourceTree = SOURCE_ROOT; };
0A1E842E21909CBB0042F782 /* bundle.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = bundle.js; path = "node_modules/brave-sync/bundles/bundle.js"; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -4100,6 +4102,7 @@
279C756A219DDE3B001CD1CB /* FingerprintingProtection.swift */,
5E34780F22D7A1D200B0D5F8 /* ResourceDownloadManager.swift */,
5E4845C122DE3DF800372022 /* WindowRenderHelperScript.swift */,
0A19365323508756002E2B81 /* LinkPreviewViewController.swift */,
);
indentWidth = 4;
path = Browser;
Expand Down Expand Up @@ -5919,6 +5922,7 @@
DDA24A431FD84D630098F159 /* DefaultSearchPrefs.swift in Sources */,
4422D4B121BFFB7600BF1855 /* bloom.cc in Sources */,
4422D4FD21BFFB7600BF1855 /* dumpfile.cc in Sources */,
0A19365423508756002E2B81 /* LinkPreviewViewController.swift in Sources */,
E65075611E37F77D006961AC /* MenuHelper.swift in Sources */,
E63ED7D81BFCD9990097D08E /* LoginTableViewCell.swift in Sources */,
C615FACF2129FBD000A8168C /* ImageCacheProtocol.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Client/Application/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ extension Preferences {

/// The progress the user has made with onboarding
static let basicOnboardingProgress = Option<Int>(key: "general.basic-onboarding-progress", default: OnboardingProgress.none.rawValue)
/// Whether or not link preview upon long press action should be shown.
static let enableLinkPreview = Option<Bool>(key: "general.night-mode", default: true)
}
final class Search {
/// Whether or not to show suggestions while the user types
Expand Down
25 changes: 23 additions & 2 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2505,17 +2505,38 @@ extension BrowserViewController: WKUIDelegate {

actions.append(shareAction)
}

let linkPreview = Preferences.General.enableLinkPreview.value

let linkPreviewTitle = linkPreview ?
Strings.HideLinkPreviewsActionTitle : Strings.ShowLinkPreviewsActionTitle
let linkPreviewAction = UIAction(title: linkPreviewTitle, image: UIImage(systemName: "eye.fill")) { _ in
Preferences.General.enableLinkPreview.value.toggle()
}

actions.append(linkPreviewAction)
}

return UIMenu(title: url.absoluteString, children: actions)
return UIMenu(title: url.absoluteString.truncate(length: 100), children: actions)
}

let linkPreview: UIContextMenuContentPreviewProvider = {
return LinkPreviewViewController(url: url)
}

let config = UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: actionProvider)
let linkPreviewProvider = Preferences.General.enableLinkPreview.value ? linkPreview : nil
let config = UIContextMenuConfiguration(identifier: nil, previewProvider: linkPreviewProvider,
actionProvider: actionProvider)

completionHandler(config)
}

@available(iOS 13.0, *)
func webView(_ webView: WKWebView, contextMenuForElement elementInfo: WKContextMenuElementInfo, willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
guard let url = elementInfo.linkURL else { return }
webView.load(URLRequest(url: url))
}

fileprivate func addTab(url: URL, inPrivateMode: Bool, currentTab: Tab) {
let tab = self.tabManager.addTab(URLRequest(url: url), afterTab: currentTab, isPrivate: inPrivateMode)
if inPrivateMode && !PrivateBrowsingManager.shared.isPrivateBrowsing {
Expand Down
36 changes: 36 additions & 0 deletions Client/Frontend/Browser/LinkPreviewViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

import UIKit
import WebKit
import Data

class LinkPreviewViewController: UIViewController {

let url: URL

init(url: URL) {
self.url = url
super.init(nibName: nil, bundle: nil)
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) { fatalError() }

override func viewDidLoad() {
let wk = WKWebView(frame: view.frame)

let domain = Domain.getOrCreate(forUrl: url, persistent: !PrivateBrowsingManager.shared.isPrivateBrowsing)

BlocklistName.blocklists(forDomain: domain).on.forEach {
ContentBlockerHelper.ruleStore.lookUpContentRuleList(forIdentifier: $0.filename) { rule, _ in
guard let rule = rule else { return }
wk.configuration.userContentController.add(rule)
}
}

view.addSubview(wk)
wk.load(URLRequest(url: url))
}
}