From 7a38307d1f8b65b3199b71a577f719360cb361da Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 5 Apr 2014 10:24:46 +0800 Subject: [PATCH] Use dialog API to override file chooser dialog in web inspector. --- atom/renderer/lib/inspector.coffee | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/atom/renderer/lib/inspector.coffee b/atom/renderer/lib/inspector.coffee index b8aaebf3d4b..7a07d51dc06 100644 --- a/atom/renderer/lib/inspector.coffee +++ b/atom/renderer/lib/inspector.coffee @@ -1,5 +1,5 @@ -# Use menu API to show context menu. window.onload = -> + # Use menu API to show context menu. WebInspector.ContextMenu.prototype.show = -> menuObject = @_buildDescriptor() if menuObject.length @@ -7,6 +7,13 @@ window.onload = -> createMenu(menuObject, @_event) @_event.consume() + # Use dialog API to override file chooser dialog. + WebInspector.createFileSelectorElement = (callback) -> + fileSelectorElement = document.createElement 'span' + fileSelectorElement.style.display = 'none' + fileSelectorElement.click = showFileChooserDialog.bind this, callback + return fileSelectorElement + convertToMenuTemplate = (items) -> template = [] for item in items @@ -40,3 +47,15 @@ createMenu = (items, event) -> menu = Menu.buildFromTemplate convertToMenuTemplate(items) menu.popup remote.getCurrentWindow() event.consume true + +showFileChooserDialog = (callback) -> + remote = require 'remote' + dialog = remote.require 'dialog' + dialog.showOpenDialog remote.getCurrentWindow(), null, (files) -> + callback pathToHtml5FileObject(files[0]) if files? + +pathToHtml5FileObject = (path) -> + fs = require 'fs' + blob = new Blob([fs.readFileSync(path)]) + blob.name = path + blob