Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15088 from niteskum/OpenWithExternalEditor
Browse files Browse the repository at this point in the history
Feature to associate file types with external editors
  • Loading branch information
niteskum authored and Nitesh Kumar committed Mar 24, 2020
1 parent 8defae3 commit 8f985f9
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 4 deletions.
72 changes: 72 additions & 0 deletions src/extensions/default/OpenWithExternalApplication/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 AppInit = brackets.getModule("utils/AppInit"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Strings = brackets.getModule("strings"),
FileViewController = brackets.getModule("project/FileViewController"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
FileUtils = brackets.getModule("file/FileUtils");

/**
* @private
* @type {string} fullPath of the OpenWithExternalEditor Domain implementation
*/
var _domainPath = ExtensionUtils.getModulePath(module, "node/OpenWithExternalApplicationDomain");

/**
* @private
* @type {NodeDomain}
*/
var _nodeDomain = new NodeDomain("OpenWithExternalApplication", _domainPath);

var extensionToExtApplicationMap = {};

function _openWithExternalApplication(event, path) {
_nodeDomain.exec("open", {
path: path,
app: extensionToExtApplicationMap[FileUtils.getFileExtension(path).toLowerCase()]
});
}

PreferencesManager.definePreference("externalApplications", "object", {}, {
description: Strings.DESCRIPTION_EXTERNAL_APPLICATION_ASSOCIATE
});

PreferencesManager.on("change", "externalApplications", function () {
extensionToExtApplicationMap = PreferencesManager.get("externalApplications");
FileUtils.addExtensionToExternalAppList(Object.keys(extensionToExtApplicationMap));
});

FileViewController.on("openWithExternalApplication", _openWithExternalApplication);

AppInit.appReady(function () {
extensionToExtApplicationMap = PreferencesManager.get("externalApplications");
FileUtils.addExtensionToExternalAppList(Object.keys(extensionToExtApplicationMap));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2012 - 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.
*
*/

/*eslint-env node */
/*jslint node: true */
"use strict";

var open = require("open");

var _domainManager;

/**
* @private
*
* @param {Object} params Object to use
*/
function _openWithExternalApplication(params) {
var application = "default" === params.app ? "": params.app;
open(params.path, application);
}


/**
* Initializes the OpenWithExternalEditor domain with its commands.
* @param {DomainManager} domainManager The DomainManager for the server
*/
function init(domainManager) {
_domainManager = domainManager;

if (!domainManager.hasDomain("OpenWithExternalApplication")) {
domainManager.registerDomain("OpenWithExternalApplication", {major: 0, minor: 1});
}
_domainManager.registerCommand(
"OpenWithExternalApplication",
"open",
_openWithExternalApplication,
true,
"open document with External Application.",
[{
name: "params",
type: "object",
description: "Params Object having document and App Path."
}],
[]
);
}

exports.init = init;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "brackets-open-external_application",
"dependencies": {
"open": "0.0.5"
}
}
29 changes: 29 additions & 0 deletions src/file/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ define(function (require, exports, module) {
*/
var MAX_FILE_SIZE = MAX_FILE_SIZE_MB * 1024 * 1024;

/**
* @const {List} list of File Extensions which will be opened in external Application
*/
var extListToBeOpenedInExtApp = [];


/**
* Asynchronously reads a file as UTF-8 encoded text.
Expand Down Expand Up @@ -526,6 +531,28 @@ define(function (require, exports, module) {
return pathArray.join("/");
}

/**
* @param {string} ext extension string a file
* @return {string} returns true If file to be opened in External Application.
*
*/
function shouldOpenInExternalApplication(ext) {
return extListToBeOpenedInExtApp.includes(ext);
}

/**
* @param {string} ext File Extensions to be added in External App List
*
*/
function addExtensionToExternalAppList(ext) {

if(Array.isArray(ext)) {
extListToBeOpenedInExtApp = ext;
} else if (typeof ext === 'string'){
extListToBeOpenedInExtApp.push(ext);
}
}

// Asynchronously load DocumentCommandHandlers
// This avoids a temporary circular dependency created
// by relocating showFileOpenError() until deprecation is over
Expand Down Expand Up @@ -568,4 +595,6 @@ define(function (require, exports, module) {
exports.comparePaths = comparePaths;
exports.MAX_FILE_SIZE = MAX_FILE_SIZE;
exports.encodeFilePath = encodeFilePath;
exports.shouldOpenInExternalApplication = shouldOpenInExternalApplication;
exports.addExtensionToExternalAppList = addExtensionToExternalAppList;
});
2 changes: 1 addition & 1 deletion src/language/languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
"jsz", "lib", "mpeg", "mpg", "mp4", "msi", "node", "o", "obj", "odc",
"odb", "odf", "odg", "odp", "ods", "odt", "otf", "pak", "pdb", "pdf",
"pdi", "ppt", "pptx", "psd", "rar", "sdf", "so", "sqlite", "suo", "svgz",
"swf", "tar", "tif", "tiff", "ttf", "woff", "xls", "xlsx", "zip"
"swf", "tar", "tif", "tiff", "ttf", "woff", "xls", "xlsx", "zip", "xd"
],
"isBinary": true
},
Expand Down
5 changes: 4 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,5 +903,8 @@ define({
"REMOTE_DEBUGGING_ENABLED" : "Remote debugging enabled on localhost:",

// Remote debugging port argument is invalid
"REMOTE_DEBUGGING_PORT_INVALID" : "Cannot enable remote debugging on port {0}. Port numbers should be between {1} and {2}."
"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"
});
20 changes: 18 additions & 2 deletions src/project/FileTreeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ define(function (require, exports, module) {
LanguageManager = require("language/LanguageManager"),
FileTreeViewModel = require("project/FileTreeViewModel"),
ViewUtils = require("utils/ViewUtils"),
KeyEvent = require("utils/KeyEvent");
KeyEvent = require("utils/KeyEvent"),
PreferencesManager = require("preferences/PreferencesManager");

var DOM = Preact.DOM;

Expand Down Expand Up @@ -554,7 +555,16 @@ define(function (require, exports, module) {
});
}
} else {
this.props.actions.setSelected(this.myPath());
var language = LanguageManager.getLanguageForPath(this.myPath()),
doNotOpen = false;
if (language && language.isBinary() && "image" !== language.getId() &&
FileUtils.shouldOpenInExternalApplication(
FileUtils.getFileExtension(this.myPath()).toLowerCase()
)
) {
doNotOpen = true;
}
this.props.actions.setSelected(this.myPath(), doNotOpen);
}
e.stopPropagation();
e.preventDefault();
Expand All @@ -569,6 +579,12 @@ define(function (require, exports, module) {
if (this.state.clickTimer !== null) {
this.clearTimer();
}
if (FileUtils.shouldOpenInExternalApplication(
FileUtils.getFileExtension(this.myPath()).toLowerCase()
)) {
this.props.actions.openWithExternalApplication(this.myPath());
return;
}
this.props.actions.selectInWorkingSet(this.myPath());
}
},
Expand Down
8 changes: 8 additions & 0 deletions src/project/FileViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ define(function (require, exports, module) {
return result.promise();
}

/**
* Opens the specified document with its associated external editor,
*/
function openWithExternalApplication(fullPath) {
exports.trigger("openWithExternalApplication", fullPath);
}

/**
* Opens the specified document if it's not already open, adds it to the working set,
* and selects it in the WorkingSetView
Expand Down Expand Up @@ -275,4 +282,5 @@ define(function (require, exports, module) {
exports.setFileViewFocus = setFileViewFocus;
exports.WORKING_SET_VIEW = WORKING_SET_VIEW;
exports.PROJECT_MANAGER = PROJECT_MANAGER;
exports.openWithExternalApplication = openWithExternalApplication;
});
8 changes: 8 additions & 0 deletions src/project/ProjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ define(function (require, exports, module) {
this.model.selectInWorkingSet(path);
};

/**
* See `FileViewController.openWithExternalApplication`
*/
ActionCreator.prototype.openWithExternalApplication = function (path) {
FileViewController.openWithExternalApplication(path);
};


/**
* See `ProjectModel.setContext`
*/
Expand Down

0 comments on commit 8f985f9

Please sign in to comment.