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

Feature to associate file types with external editors #15088

Merged
merged 5 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open module does not seem to be latest.
Quoting https://www.npmjs.com/package/open

Note: The original open package was previously deprecated in favor of this package, and we got the name, so this package is now named open instead of opn. If you're upgrading from the original open package ([email protected] or lower), keep in mind that the API is different.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above 0.0.5 , open module version starts with 6.0.0. Open module starting 6.0.0 is not compatible with node version 6.11.0.

It gives below error when requiring module:
const wslToWindowsPath = async path => {
^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)

}
}
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