-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbe1af8
commit 1492ee6
Showing
6 changed files
with
66 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,50 @@ | ||
import Cocoa | ||
import Quartz | ||
import WebKit | ||
|
||
class PreviewViewController: NSViewController, QLPreviewingController, WKNavigationDelegate { | ||
var webView: WKWebView! | ||
|
||
class PreviewViewController: NSViewController, QLPreviewingController { | ||
override func loadView() { | ||
view = NSView() | ||
// Do not call `super.loadView()` (related to storybooks) | ||
|
||
// Create NSView (overrides inherited storybook logic) | ||
view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 100)) | ||
} | ||
|
||
/* | ||
* Implement this method and set QLSupportsSearchableItems to YES in the Info.plist of the | ||
* extension if you support CoreSpotlight. | ||
* | ||
* func preparePreviewOfSearchableItem( | ||
* identifier: String, | ||
* queryString: String?, | ||
* completionHandler handler: @escaping (Error?) -> Void | ||
* ) { | ||
* // Perform any setup necessary in order to prepare the view. | ||
* | ||
* // Call the completion handler so Quick Look knows that the preview is fully loaded. | ||
* // Quick Look will display a loading spinner while the completion handler is not called. | ||
* handler(nil) | ||
* } | ||
*/ | ||
// swiftlint:disable:next overridden_super_call | ||
override func viewDidLoad() { | ||
// Do not call `super.viewDidLoad()` (related to storybooks) | ||
|
||
func preparePreviewOfFile(at _: URL, completionHandler handler: @escaping (Error?) -> Void) { | ||
// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of | ||
// the extension. | ||
// Create web view for rendering file preview | ||
webView = WKWebView(frame: view.bounds) | ||
webView.autoresizingMask = [.height, .width] | ||
view.addSubview(webView) | ||
} | ||
|
||
// Perform any setup necessary in order to prepare the view. | ||
/// Spotlight preview handler | ||
func preparePreviewOfSearchableItem( | ||
identifier _: String, | ||
queryString _: String?, | ||
completionHandler handler: @escaping (Error?) -> Void | ||
) { | ||
previewFile(completionHandler: handler) | ||
} | ||
|
||
/// File preview handler (e.g. preview in the Finder or when pressing space on a file) | ||
func preparePreviewOfFile(at _: URL, completionHandler handler: @escaping (Error?) -> Void) { | ||
previewFile(completionHandler: handler) | ||
} | ||
|
||
// Call the completion handler so Quick Look knows that the preview is fully loaded. | ||
// Quick Look will display a loading spinner while the completion handler is not called. | ||
private func previewFile(completionHandler handler: @escaping (Error?) -> Void) { | ||
if let url = Bundle.main.url(forResource: "content", withExtension: "html") { | ||
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent()) | ||
} else { | ||
// TODO: Display error message if HTML cannot be loaded | ||
print("Error loading preview HTML") | ||
} | ||
|
||
// Stop displaying Quick Look loading spinner | ||
handler(nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
</head> | ||
|
||
<body> | ||
<p>Hello world!</p> | ||
</body> | ||
</html> |