Skip to content

Commit

Permalink
🎨 Export high-definition images and enhance effects
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed May 23, 2024
1 parent c8091f7 commit 1b647c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 9 additions & 3 deletions Mac/Extensions/NSImage+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,16 @@ public extension NSImage {
///
/// - parameter url: The location url to which to write the png file.
func savePNGRepresentationToURL(url: URL) throws {
if let png = PNGRepresentation {
try png.write(to: url, options: .atomicWrite)
guard let tiffData = self.tiffRepresentation,
// Create an NSBitmapImageRep object using TIFF data
let bitmapImage = NSBitmapImageRep(data: tiffData),
// Converts an NSBitmapImageRep object to PNG data
let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
throw NSError(domain: "Error creating PNG representation", code: 0, userInfo: nil)
}
// Writes PNG data to the specified URL
try pngData.write(to: url, options: .atomicWrite)
}
}

func saveJPEGRepresentationToURL(url: URL) throws {
if let jpeg = JPEGRepresentation {
Expand Down
24 changes: 14 additions & 10 deletions Mac/View/MPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,24 @@ class MPreviewView: WKWebView, WKUIDelegate, WKNavigationDelegate {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
super.evaluateJavaScript("document.readyState", completionHandler: { complete, _ in
if complete != nil {
super.evaluateJavaScript("document.body.offsetHeight", completionHandler: { height, _ in
super.evaluateJavaScript("document.body.scrollHeight", completionHandler: { height, _ in
guard let contentHeight = height as? CGFloat else {
print("Content height could not be obtained"); return
}
super.evaluateJavaScript("document.body.offsetWidth", completionHandler: { [weak self] width, _ in
super.evaluateJavaScript("document.body.scrollWidth", completionHandler: { [weak self] width, _ in
if let contentWidth = width as? CGFloat {
let config = WKSnapshotConfiguration()
config.rect = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
config.afterScreenUpdates = false
config.afterScreenUpdates = true
// Improve resolution
config.snapshotWidth = NSNumber(value: Double(contentWidth) * 2.0)
self?.frame.size.height = contentHeight
self?.takeSnapshot(with: config, completionHandler: { image, error in
if let image = image {
if let desktopURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first {
let currentName = self?.note?.getExportTitle()
let destinationURL = desktopURL.appendingPathComponent(currentName! + ".jpeg")
try! image.saveJPEGRepresentationToURL(url: destinationURL)
let destinationURL = desktopURL.appendingPathComponent(currentName! + ".png")
try! image.savePNGRepresentationToURL(url: destinationURL)
}
vc.toastExport(status: true)
print("Got snapshot")
Expand Down Expand Up @@ -368,9 +370,9 @@ class MPreviewView: WKWebView, WKUIDelegate, WKNavigationDelegate {
// Regular expression matching<img> The tag does not contain the loading="lazy" attribute
let pattern = #"<img(?![^>]*\bloading\s*=\s*['"]?lazy['"]?)([^>]*)>"#
let regex = try! NSRegularExpression(pattern: pattern, options: [])

let modifiedHTML = regex.stringByReplacingMatches(in: html, options: [], range: NSRange(location: 0, length: html.utf16.count), withTemplate: "<img loading=\"lazy\"$1>")

return modifiedHTML
}

Expand All @@ -387,11 +389,13 @@ class MPreviewView: WKWebView, WKUIDelegate, WKNavigationDelegate {
pageHTMLString = try htmlFromTemplate(markdownString, css: css)
}

pageHTMLString = addLazyLoadToImages(in: pageHTMLString)

if !UserDefaultsManagement.isOnExport {
pageHTMLString = addLazyLoadToImages(in: pageHTMLString)
}

// print(">>>>>>")
// print(pageHTMLString)

let indexURL = createTemporaryBundle(pageHTMLString: pageHTMLString)

if let i = indexURL {
Expand Down

0 comments on commit 1b647c1

Please sign in to comment.