From 125b14dc17884796f56461e34390d229368d06fa Mon Sep 17 00:00:00 2001 From: Peter Josling Date: Sun, 12 Apr 2015 22:01:47 +0100 Subject: [PATCH 1/3] Add image pasting functionality Safari doesn't implement DataTransfer.items, so there's no way to extract image data (i.e. a screenshot copied to the clipboard) in Javascript. You can get copied files, but not data. Capture the paste in Swift and then forward on the image data to the webview. Currently supports pasting image data (and forwards on text), but pasting an image file doesn't work. --- Goofy/AppDelegate.swift | 26 +++++++++++- Goofy/Base.lproj/MainMenu.xib | 77 ++--------------------------------- server/src/fb.js | 70 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 75 deletions(-) diff --git a/Goofy/AppDelegate.swift b/Goofy/AppDelegate.swift index e10ea39..f023d84 100644 --- a/Goofy/AppDelegate.swift +++ b/Goofy/AppDelegate.swift @@ -316,6 +316,28 @@ class AppDelegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKUIDe let previewItemTitle: String = "Image" } + + // Attempt to read image data from the pasteboard and forward on to Javascript. + @IBAction func handlePaste(sender: NSMenuItem) { + var pasteboard = NSPasteboard.generalPasteboard(); + var classArray : Array = [NSImage.self]; + var options = Dictionary(); + + var canReadData = pasteboard.canReadObjectForClasses(classArray, options: options); + + if (canReadData) { + var objectsToPaste = pasteboard.readObjectsForClasses(classArray, options: options) as! Array; + var image = objectsToPaste[0]; + image.lockFocus(); + var bitmapRep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)); + image.unlockFocus(); + var imageData = bitmapRep?.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: options); + var base64String = imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.EncodingEndLineWithLineFeed); + + webView.evaluateJavaScript("pasteImage('\(base64String!)')", completionHandler: nil); + } else { + // Forward any non-image pastes (text) to the webview as a standard paste event. + NSApp.sendAction("paste:", to:nil, from:self); + } + } } - - diff --git a/Goofy/Base.lproj/MainMenu.xib b/Goofy/Base.lproj/MainMenu.xib index 38dc698..d8d13c3 100644 --- a/Goofy/Base.lproj/MainMenu.xib +++ b/Goofy/Base.lproj/MainMenu.xib @@ -59,11 +59,7 @@ - - - - - + @@ -152,7 +148,7 @@ - + @@ -359,19 +355,11 @@ - - + - - - - - - - @@ -382,63 +370,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -482,7 +413,7 @@ - + diff --git a/server/src/fb.js b/server/src/fb.js index 975908b..5b450a7 100644 --- a/server/src/fb.js +++ b/server/src/fb.js @@ -5,6 +5,15 @@ var lastNotificationTime; for(g=0;g Date: Sat, 2 May 2015 13:15:16 +0100 Subject: [PATCH 2/3] Whitespace --- server/src/fb.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/server/src/fb.js b/server/src/fb.js index 5b450a7..41747ee 100644 --- a/server/src/fb.js +++ b/server/src/fb.js @@ -306,15 +306,15 @@ function getPhotoUploadComponent() { // Unwrap the children from a React class instance. function getChildren(instance) { - var children = null; - if (instance._renderedComponent) { - children = getChildren(instance._renderedComponent); - } else if (instance._renderedChildren) { - children = instance._renderedChildren; - } else { - children = []; - } - return children; + var children = null; + if (instance._renderedComponent) { + children = getChildren(instance._renderedComponent); + } else if (instance._renderedChildren) { + children = instance._renderedChildren; + } else { + children = []; + } + return children; } function __triggerKeyboardEvent(el, keyCode, meta) { From 59beeb35814da085b4f2e30eecedab760cc9c7ab Mon Sep 17 00:00:00 2001 From: Peter Josling Date: Sat, 2 May 2015 13:15:33 +0100 Subject: [PATCH 3/3] Update photo upload component react-id --- server/src/fb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/fb.js b/server/src/fb.js index 41747ee..777ecaa 100644 --- a/server/src/fb.js +++ b/server/src/fb.js @@ -290,7 +290,7 @@ function b64toBlob(b64Data, contentType, sliceSize) { // Find the instance of the photo upload class from the React component tree. function getPhotoUploadComponent() { - var id = '.0.1.$1.0.1.$0.1.0.$1.0.1.0'; + var id = '.0.1.$1.0.1.$0.1.0.1.1.0'; var idComponents = id.split('.'); var children = __REACT_DEVTOOLS_GLOBAL_HOOK__._reactRuntime.Mount._instancesByReactRootID; var component;