Skip to content

Commit

Permalink
Support for the deprecated WebView control.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbarex committed Apr 4, 2022
1 parent 4e7a002 commit 305b24a
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 32 deletions.
4 changes: 2 additions & 2 deletions QLExtension/Base.lproj/PreviewViewController.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17701"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
46 changes: 22 additions & 24 deletions QLExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class MyWebView: WebView {
}

class PreviewViewController: NSViewController, QLPreviewingController {
var webView: MyWKWebView!
var webView: MyWKWebView?
var legacyWebView: MyWebView?

var handler: ((Error?) -> Void)? = nil

Expand Down Expand Up @@ -85,8 +86,7 @@ class PreviewViewController: NSViewController, QLPreviewingController {
previewRect = self.view.bounds.insetBy(dx: 2, dy: 2)
}

/*
if #available(macOS 11, *) {
if settings.useLegacyPreview {
// On Big Sur there are some bugs with the current WKWebView:
// - WKWebView crash on launch because ignore the com.apple.security.network.client entitlement (workaround setting the com.apple.security.temporary-exception.mach-lookup.global-name exception for com.apple.nsurlsessiond
// - WKWebView cannot scroll when QL preview window is in fullscreen.
Expand All @@ -96,38 +96,38 @@ class PreviewViewController: NSViewController, QLPreviewingController {
webView.preferences.isJavaScriptEnabled = false
webView.preferences.allowsAirPlayForMediaPlayback = false
webView.preferences.arePlugInsEnabled = false
webView.preferences.loadsImagesAutomatically = true

self.view.addSubview(webView)

webView.mainFrame.loadHTMLString(html, baseURL: nil)
self.legacyWebView = webView
webView.frameLoadDelegate = self
webView.drawsBackground = false // Best solution is use the same color of the body
} else {
*/
// Create a configuration for the preferences
let configuration = WKWebViewConfiguration()
configuration.preferences.javaScriptEnabled = settings.unsafeHTMLOption && settings.inlineImageExtension
configuration.allowsAirPlayForMediaPlayback = false

self.webView = MyWKWebView(frame: previewRect, configuration: configuration)
self.webView.autoresizingMask = [.height, .width]
self.webView!.autoresizingMask = [.height, .width]

self.webView.wantsLayer = true
self.webView!.wantsLayer = true
if #available(macOS 11, *) {
self.webView.layer?.borderWidth = 0
self.webView!.layer?.borderWidth = 0
} else {
// Draw a border around the web view
self.webView.layer?.borderColor = NSColor.gridColor.cgColor
self.webView.layer?.borderWidth = 1
self.webView!.layer?.borderColor = NSColor.gridColor.cgColor
self.webView!.layer?.borderWidth = 1
}

self.webView.navigationDelegate = self
// webView.uiDelegate = self
self.webView!.navigationDelegate = self
// webView!.uiDelegate = self

self.view.addSubview(self.webView)
self.view.addSubview(self.webView!)

/*
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.webView!.translatesAutoresizingMaskIntoConstraints = false
var padding: CGFloat = 2
if #available(macOS 11, *) {
padding = 0
Expand All @@ -139,7 +139,7 @@ class PreviewViewController: NSViewController, QLPreviewingController {
NSLayoutConstraint(item: self.webView!, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: padding).isActive = true
*/

/* } */
}
}

internal func getBundleContents(forResource: String, ofType: String) -> String?
Expand Down Expand Up @@ -177,14 +177,12 @@ class PreviewViewController: NSViewController, QLPreviewingController {
self.handler = handler

let html = try renderMD(url: url)
/*
if #available(macOS 11, *) {
self.webView.mainFrame.loadHTMLString(html, baseURL: nil)
if Settings.shared.useLegacyPreview, let webView = self.legacyWebView {
webView.mainFrame.loadHTMLString("LEGACY VIEW " + html, baseURL: nil)
} else {
*/
self.webView.isHidden = true // hide the webview until complete rendering
self.webView.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
/* } */
self.webView?.isHidden = true // hide the webview until complete rendering
self.webView?.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
}
} catch {
handler(error)
}
Expand Down Expand Up @@ -268,15 +266,15 @@ extension PreviewViewController: WKNavigationDelegate {
// Show the Quick Look preview only after the complete rendering (preventing a flickering glitch).
// Wait to show the webview to prevent a resize glitch.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webView.isHidden = false
self.webView?.isHidden = false
}
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
if let handler = self.handler {
handler(error)
self.handler = nil
self.webView.isHidden = false
self.webView?.isHidden = false
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmark-gfm/MIMEtype/MIMEType.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef enum MIME_MAGICK_CHECK {
*/
char *get_mime(const char *path, MIME_MAGICK_CHECK check_magic);

char *get_mime_from_buffer(const char *ext, const char *buffer, MIME_MAGICK_CHECK check_magic);

#ifdef __cplusplus
}
#endif
Expand Down
42 changes: 42 additions & 0 deletions cmark-gfm/MIMEtype/MIMEtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ static const char *mime_search(const char **mimes,size_t len,const char *buf){
return NULL;
}

static const char *mime_from_magic_buffer(const char *buf) {
const char *ret;
ret = mime_search(mimes0,LEN(mimes0), buf);
if (ret && strlen(ret) > 0) {
return ret;
}
ret = mime_search(mimes4, LEN(mimes4), buf+4);
//TODO mimes8 and mimesX + adjust BUFLEN to accomodate
return ret;
}

static const char *mime_from_magic(FILE *fd) {
#define BUFLEN 256 //must be larger than max offset + max magic length
char buf[BUFLEN];
Expand Down Expand Up @@ -148,3 +159,34 @@ char *get_mime(const char *path, MIME_MAGICK_CHECK check_magic) {

return mime;
}

char *get_mime_from_buffer(const char *ext, const char *buffer, MIME_MAGICK_CHECK check_magic) {
const char *ret = mime_from_ext(ext);

if (check_magic != MAGIC_NOT_CHECKED) {
if (ret == NULL || check_magic == MAGIC_CONFRONT) {
const char *ret2 = mime_from_magic_buffer(buffer);
if (ret && ret2 && memcmp(ret, ret2, 1) != 0) {
// different mayor type.
ret = NULL;
} else if (ret2) {
ret = ret2;
}
}
}
if (!ret) {
return NULL;
}

const char *major_type = major_types[*ret];
int size1 = (int)strlen(major_type);
int size2 = (int)strlen(ret);

char *mime = calloc(size1 + size2 + 1, sizeof(char));

strcpy(mime, major_type);
strcat(mime, "/");
strcat(mime, &ret[1]);

return mime;
}
Loading

0 comments on commit 305b24a

Please sign in to comment.