@@ -54,12 +54,14 @@ class PreviewViewController: NSViewController, QLPreviewingController {
54
54
}
55
55
56
56
override func viewDidDisappear( ) {
57
+ // This code will not be called on macOS 12 Monterey with QLIsDataBasedPreview set.
58
+
57
59
self . launcherService = nil
58
- // Releases the script handler which retain a strong reference to self which prevents the WebKit process from being released.
59
- self . webView. configuration. userContentController. removeScriptMessageHandler ( forName: " imageExtensionHandler " )
60
60
}
61
61
62
62
override func loadView( ) {
63
+ // This code will not be called on macOS 12 Monterey with QLIsDataBasedPreview set.
64
+
63
65
super. loadView ( )
64
66
// Do any additional setup after loading the view.
65
67
@@ -109,9 +111,6 @@ class PreviewViewController: NSViewController, QLPreviewingController {
109
111
configuration. preferences. javaScriptEnabled = settings. unsafeHTMLOption && settings. inlineImageExtension
110
112
configuration. allowsAirPlayForMediaPlayback = false
111
113
112
- // Handler to replace raw <image> src with the embedded data.
113
- configuration. userContentController. add ( self , name: " imageExtensionHandler " )
114
-
115
114
self . webView = MyWKWebView ( frame: previewRect, configuration: configuration)
116
115
self . webView. autoresizingMask = [ . height, . width]
117
116
@@ -167,6 +166,7 @@ class PreviewViewController: NSViewController, QLPreviewingController {
167
166
*/
168
167
169
168
func preparePreviewOfFile( at url: URL , completionHandler handler: @escaping ( Error ? ) -> Void ) {
169
+ // This code will not be called on macOS 12 Monterey with QLIsDataBasedPreview set.
170
170
171
171
// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of the extension.
172
172
@@ -175,6 +175,44 @@ class PreviewViewController: NSViewController, QLPreviewingController {
175
175
// Call the completion handler so Quick Look knows that the preview is fully loaded.
176
176
// Quick Look will display a loading spinner while the completion handler is not called.
177
177
178
+ do {
179
+ self . handler = handler
180
+
181
+ let html = try renderMD ( url: url)
182
+ /*
183
+ if #available(macOS 11, *) {
184
+ self.webView.mainFrame.loadHTMLString(html, baseURL: nil)
185
+ } else {
186
+ */
187
+ self . webView. isHidden = true // hide the webview until complete rendering
188
+ self . webView. loadHTMLString ( html, baseURL: url. deletingLastPathComponent ( ) )
189
+ /* } */
190
+ } catch {
191
+ handler ( error)
192
+ }
193
+ }
194
+
195
+ @available ( macOSApplicationExtension 12 . 0 , * )
196
+ func providePreview( for request: QLFilePreviewRequest , completionHandler handler: @escaping ( QLPreviewReply ? , Error ? ) -> Void ) {
197
+ // This code will be called on macOS 12 Monterey with QLIsDataBasedPreview set.
198
+
199
+ // print("providePreview for \(request.fileURL)")
200
+
201
+ do {
202
+ let html = try renderMD ( url: request. fileURL)
203
+ let replay = QLPreviewReply ( dataOfContentType: . html, contentSize: . zero) { _ in
204
+ return html. data ( using: . utf8) !
205
+ }
206
+
207
+ // replay.title = request.fileURL.lastPathComponent
208
+ replay. stringEncoding = . utf8
209
+ handler ( replay, nil )
210
+ } catch {
211
+ handler ( nil , error)
212
+ }
213
+ }
214
+
215
+ func renderMD( url: URL ) throws -> String {
178
216
os_log (
179
217
" Generating preview for file %{public}s " ,
180
218
log: self . log,
@@ -184,8 +222,6 @@ class PreviewViewController: NSViewController, QLPreviewingController {
184
222
185
223
let type = UserDefaults . standard. string ( forKey: " AppleInterfaceStyle " ) ?? " Light "
186
224
187
- self . handler = handler
188
-
189
225
let settings = Settings . shared
190
226
191
227
let markdown_url : URL
@@ -199,27 +235,11 @@ class PreviewViewController: NSViewController, QLPreviewingController {
199
235
markdown_url = url
200
236
}
201
237
202
- do {
203
- let text = try settings. render ( file: markdown_url, forAppearance: type == " Light " ? . light : . dark, baseDir: markdown_url. deletingLastPathComponent ( ) . path, log: self . log)
238
+ let text = try settings. render ( file: markdown_url, forAppearance: type == " Light " ? . light : . dark, baseDir: markdown_url. deletingLastPathComponent ( ) . path, log: self . log)
239
+
240
+ let html = settings. getCompleteHTML ( title: url. lastPathComponent, body: text, footer: " " , basedir: url. deletingLastPathComponent ( ) )
204
241
205
- let extrajs : String
206
- if settings. unsafeHTMLOption && settings. inlineImageExtension {
207
- extrajs = " <script type= \" text/javascript \" > " + ( settings. getBundleContents ( forResource: " inlineimages " , ofType: " js " ) ?? " " ) + " </script> \n " ;
208
- } else {
209
- extrajs = " "
210
- }
211
- let html = settings. getCompleteHTML ( title: url. lastPathComponent, body: text, footer: extrajs)
212
- /*
213
- if #available(macOS 11, *) {
214
- self.webView.mainFrame.loadHTMLString(html, baseURL: nil)
215
- } else {
216
- */
217
- self . webView. isHidden = true // hide the webview until complete rendering
218
- self . webView. loadHTMLString ( html, baseURL: url. deletingLastPathComponent ( ) )
219
- /* } */
220
- } catch {
221
- handler ( error)
222
- }
242
+ return html
223
243
}
224
244
}
225
245
@@ -239,69 +259,13 @@ extension PreviewViewController: WebFrameLoadDelegate {
239
259
}
240
260
}
241
261
242
- extension PreviewViewController : WKScriptMessageHandler {
243
- func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) {
244
- guard message. name == " imageExtensionHandler " , Settings . shared. unsafeHTMLOption && Settings . shared. inlineImageExtension else {
245
- return
246
- }
247
- guard let dict = message. body as? [ String : AnyObject ] , let src = dict [ " src " ] as? String , let id = dict [ " id " ] as? String else {
248
- return
249
- }
250
-
251
- guard let data = get_base64_image (
252
- src. cString ( using: . utf8) ,
253
- { ( path: UnsafePointer < Int8 > ? , context: UnsafeMutableRawPointer ? ) -> UnsafeMutablePointer < Int8 > ? in
254
- let magic_file = Settings . shared. getResourceBundle ( ) . path ( forResource: " magic " , ofType: " mgc " ) ? . cString ( using: . utf8)
255
-
256
- let r = magic_get_mime_by_file ( path, magic_file)
257
- return r
258
- } ,
259
- nil
260
- ) else {
261
- return
262
- }
263
- defer {
264
- data. deallocate ( )
265
- }
266
- let response : [ String : String ] = [
267
- " src " : src,
268
- " id " : id,
269
- " data " : String ( cString: data)
270
- ]
271
- let encoder = JSONEncoder ( )
272
- guard let j = try ? encoder. encode ( response) , let js = String ( data: j, encoding: . utf8) else {
273
- return
274
- }
275
-
276
- message. webView? . evaluateJavaScript ( " replaceImageSrc( \( js) ) " ) { ( r, error) in
277
- if let result = r as? Bool , !result {
278
- os_log (
279
- " Unable to replace <img> src %{public}s with the inline data. " ,
280
- log: self . log,
281
- type: . error,
282
- src
283
- )
284
- }
285
- if let error = error {
286
- os_log (
287
- " Unable to replace <img> src %{public}s with the inline data: %{public}s. " ,
288
- log: self . log,
289
- type: . error,
290
- src, error. localizedDescription
291
- )
292
- }
293
- }
294
- }
295
- }
296
-
297
262
extension PreviewViewController : WKNavigationDelegate {
298
263
func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
299
264
if let handler = self . handler {
300
- // Show the Quick Look preview only after the complete rendering (preventing a flickering glitch).
301
-
302
265
handler ( nil )
303
266
self . handler = nil
304
267
}
268
+ // Show the Quick Look preview only after the complete rendering (preventing a flickering glitch).
305
269
// Wait to show the webview to prevent a resize glitch.
306
270
DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
307
271
self . webView. isHidden = false
0 commit comments