From aa569cd7d2e60498e320ca3560e3bbd1eee0fc5f Mon Sep 17 00:00:00 2001 From: Nitesh Kumar <38075523+niteskum@users.noreply.github.com> Date: Wed, 18 Mar 2020 16:47:47 +0530 Subject: [PATCH] Merge pull request #15092 from niteskum/graphicsFileExternalApplication Detect Graphics file in project and associate it with OS default application --- .../GraphicsFile.js | 209 ++++++++++++++++++ .../OpenWithExternalApplication/main.js | 5 +- .../node/OpenWithExternalApplicationDomain.js | 63 +++++- .../node/package.json | 3 +- src/nls/root/strings.js | 8 +- 5 files changed, 284 insertions(+), 4 deletions(-) create mode 100644 src/extensions/default/OpenWithExternalApplication/GraphicsFile.js diff --git a/src/extensions/default/OpenWithExternalApplication/GraphicsFile.js b/src/extensions/default/OpenWithExternalApplication/GraphicsFile.js new file mode 100644 index 00000000000..d8ac00ce077 --- /dev/null +++ b/src/extensions/default/OpenWithExternalApplication/GraphicsFile.js @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +define(function (require, exports, module) { + "use strict"; + + + var PreferencesManager = brackets.getModule("preferences/PreferencesManager"), + Strings = brackets.getModule("strings"), + StringsUtils = brackets.getModule("utils/StringUtils"), + ProjectManager = brackets.getModule("project/ProjectManager"), + Dialogs = brackets.getModule("widgets/Dialogs"), + DefaultDialogs = brackets.getModule("widgets/DefaultDialogs"), + HealthLogger = brackets.getModule("utils/HealthLogger"); + + + var _requestID = 0, + _initialized = false; + + var _graphicsFileTypes = ["jpg", "jpeg", "png", "svg", "xd", "psd", "ai"]; + + var _nodeDomain; + + function init(nodeDomain) { + + if (_initialized) { + return; + } + _initialized = true; + + _nodeDomain = nodeDomain; + + _nodeDomain.on('checkFileTypesInFolderResponse', function (event, response) { + if (response.id !== _requestID) { + return; + } + _graphicsFilePresentInProject(response.present); + }); + + ProjectManager.on("projectOpen", function () { + _checkForGraphicsFileInPrjct(); + }); + + _checkForGraphicsFileInPrjct(); + + } + + + function _checkForGraphicsFileInPrjct() { + + if (PreferencesManager.getViewState("AssociateGraphicsFileDialogShown")) { + return; + } + + _nodeDomain.exec("checkFileTypesInFolder", { + extensions: _graphicsFileTypes.join(), + folder: ProjectManager.getProjectRoot().fullPath, + reqId: ++_requestID + }); + + } + + function _graphicsFilePresentInProject(isPresent) { + + if (!isPresent) { + return; + } + + Dialogs.showModalDialog( + DefaultDialogs.DIALOG_ID_INFO, + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE, + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_MSG, + [ + { className: Dialogs.DIALOG_BTN_CLASS_NORMAL, id: Dialogs.DIALOG_BTN_CANCEL, + text: Strings.CANCEL + }, + { className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK, + text: Strings.OK + } + ] + ).done(function (id) { + + if (id !== Dialogs.DIALOG_BTN_OK) { + HealthLogger.sendAnalyticsData( + "externalEditorsCancelled", + "usage", + "externalEditors", + "Cancelled", + "" + ); + return; + } + HealthLogger.sendAnalyticsData( + "LinkExternalEditors", + "usage", + "externalEditors", + "LinkExternalEditors", + "" + ); + + brackets.app.getSystemDefaultApp(_graphicsFileTypes.join(), function (err, out) { + + if (err) { + return; + } + var associateApp = out.split(','), + fileTypeToAppMap = {}, + AppToFileTypeMap = {}; + + associateApp.forEach(function (item) { + + var filetype = item.split('##')[0], + app = item.split('##')[1]; + + if (!filetype) { + return; + } + + if (filetype === "xd") { + if (app.toLowerCase() !== "adobe xd" && app.toLowerCase() !== "adobe.cc.xd") { + return; + } + + app = "Adobe XD"; + } + fileTypeToAppMap[filetype] = app; + + if (brackets.platform === "win" && app.toLowerCase().endsWith('.exe')) { + app = app.substring(app.lastIndexOf('\\') + 1, app.length - 4); + } + if (AppToFileTypeMap[app]) { + AppToFileTypeMap[app].push(filetype); + } else { + AppToFileTypeMap[app] = [filetype]; + } + }); + + var prefs = PreferencesManager.get('externalApplications'); + + for (var key in fileTypeToAppMap) { + if (fileTypeToAppMap.hasOwnProperty(key)) { + if(key && !prefs[key]) { + prefs[key] = fileTypeToAppMap[key]; + if(brackets.platform === "win" && !fileTypeToAppMap[key].toLowerCase().endsWith('.exe')) { + prefs[key] = "default"; + } + HealthLogger.sendAnalyticsData( + "AddExternalEditorForFileType_" + key.toUpperCase(), + "usage", + "externalEditors", + "AddExternalEditorForFileType_" + key.toUpperCase(), + "" + ); + } + } + } + + PreferencesManager.set('externalApplications', prefs); + + var str = ""; + for(var app in AppToFileTypeMap) { + str += AppToFileTypeMap[app].join() + "->" + app + "
"; + } + + if(!str) { + return; + } + + str+="
"; + + Dialogs.showModalDialog( + DefaultDialogs.DIALOG_ID_INFO, + Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE, + StringsUtils.format(Strings.ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_CNF_MSG, str), + [ + { className: Dialogs.DIALOG_BTN_CLASS_PRIMARY, id: Dialogs.DIALOG_BTN_OK, + text: Strings.OK + } + ] + ); + }); + }); + PreferencesManager.setViewState("AssociateGraphicsFileDialogShown", true); + + } + + exports.init = init; + +}); diff --git a/src/extensions/default/OpenWithExternalApplication/main.js b/src/extensions/default/OpenWithExternalApplication/main.js index 535a6333ee7..5587da39775 100644 --- a/src/extensions/default/OpenWithExternalApplication/main.js +++ b/src/extensions/default/OpenWithExternalApplication/main.js @@ -31,7 +31,8 @@ define(function (require, exports, module) { FileViewController = brackets.getModule("project/FileViewController"), ExtensionUtils = brackets.getModule("utils/ExtensionUtils"), NodeDomain = brackets.getModule("utils/NodeDomain"), - FileUtils = brackets.getModule("file/FileUtils"); + FileUtils = brackets.getModule("file/FileUtils"), + GraphicsFile = require("GraphicsFile"); /** * @private @@ -66,6 +67,8 @@ define(function (require, exports, module) { FileViewController.on("openWithExternalApplication", _openWithExternalApplication); AppInit.appReady(function () { + + GraphicsFile.init(_nodeDomain); extensionToExtApplicationMap = PreferencesManager.get("externalApplications"); FileUtils.addExtensionToExternalAppList(Object.keys(extensionToExtApplicationMap)); }); diff --git a/src/extensions/default/OpenWithExternalApplication/node/OpenWithExternalApplicationDomain.js b/src/extensions/default/OpenWithExternalApplication/node/OpenWithExternalApplicationDomain.js index 8a864689ef6..e5a7e414585 100644 --- a/src/extensions/default/OpenWithExternalApplication/node/OpenWithExternalApplicationDomain.js +++ b/src/extensions/default/OpenWithExternalApplication/node/OpenWithExternalApplicationDomain.js @@ -25,7 +25,9 @@ /*jslint node: true */ "use strict"; -var open = require("open"); +var open = require("open"), + Glob = require("glob").Glob, + path = require('path'); var _domainManager; @@ -39,6 +41,38 @@ function _openWithExternalApplication(params) { open(params.path, application); } +/** + * @private + * + * @param {Object} params Object to use + */ +function _checkFileTypesInFolder(params) { + + var extList = params.extensions, + dirPath = path.normalize(params.folder), + + pattern = dirPath + "/**/*.+(" + extList.replace(/,/g, "|") + ")"; + + var globMgr = new Glob(pattern, function (err, matches) { + + var respObj = { + id: params.reqId, + present: matches.length > 0 ? true : false + }; + _domainManager.emitEvent('OpenWithExternalApplication', 'checkFileTypesInFolderResponse', [respObj]); + }); + + globMgr.on("match", function() { + globMgr.abort(); + var respObj = { + id: params.reqId, + present: true + }; + _domainManager.emitEvent('OpenWithExternalApplication', 'checkFileTypesInFolderResponse', [respObj]); + }); + +} + /** * Initializes the OpenWithExternalEditor domain with its commands. @@ -50,6 +84,7 @@ function init(domainManager) { if (!domainManager.hasDomain("OpenWithExternalApplication")) { domainManager.registerDomain("OpenWithExternalApplication", {major: 0, minor: 1}); } + _domainManager.registerCommand( "OpenWithExternalApplication", "open", @@ -63,6 +98,32 @@ function init(domainManager) { }], [] ); + + _domainManager.registerCommand( + "OpenWithExternalApplication", + "checkFileTypesInFolder", + _checkFileTypesInFolder, + true, + "looks for File Types in a folder.", + [{ + name: "params", + type: "object", + description: "Params Object having File Extensions and Folder Path." + }], + [] + ); + + _domainManager.registerEvent( + "OpenWithExternalApplication", + "checkFileTypesInFolderResponse", + [ + { + name: "msgObj", + type: "object", + description: "json object containing message info to pass to brackets" + } + ] + ); } exports.init = init; diff --git a/src/extensions/default/OpenWithExternalApplication/node/package.json b/src/extensions/default/OpenWithExternalApplication/node/package.json index 65bb049736e..0e646495964 100644 --- a/src/extensions/default/OpenWithExternalApplication/node/package.json +++ b/src/extensions/default/OpenWithExternalApplication/node/package.json @@ -1,6 +1,7 @@ { "name": "brackets-open-external_application", "dependencies": { - "open": "0.0.5" + "open": "0.0.5", + "glob": "7.1.1" } } diff --git a/src/nls/root/strings.js b/src/nls/root/strings.js index e01b2300aeb..5dbd01360ec 100644 --- a/src/nls/root/strings.js +++ b/src/nls/root/strings.js @@ -906,5 +906,11 @@ define({ "REMOTE_DEBUGGING_PORT_INVALID" : "Cannot enable remote debugging on port {0}. Port numbers should be between {1} and {2}.", //Associate File Type to External App - "DESCRIPTION_EXTERNAL_APPLICATION_ASSOCIATE" : "Add File type association to external App here" + "DESCRIPTION_EXTERNAL_APPLICATION_ASSOCIATE" : "Associate File type to external App settings. e.g { \"\": \"\" } app_name is OS dependant, for example \"google chrome\" on macOS and \"chrome\" on Windows. app_name can also be given as \"default\" for OS default application.", + + "ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_TITLE" : "Open Graphic Files in External Editors.", + "ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_MSG" : "Your current folder has graphic file types which are not supported by Brackets.
You can now associate specific file types with external editors. Once associated, you can open graphic files like .xd, .psd, .jpg, .png, .ai, .svg in their default applications by double clicking on the files in File Tree.

Please click on ‘Ok’ button to associate the graphic file types with their respective default applications.", + "ASSOCIATE_GRAPHICS_FILE_TO_DEFAULT_APP_CNF_MSG" : "Following file types have been successfully associated with default applications.
{0} You can further add new file type associations or customize in brackets.json by going to “Debug->Open Preferences File” menu." + + });