2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"command",
|
|
|
|
"settings!api"
|
|
|
|
], function(command, Settings) {
|
2015-02-21 16:23:33 +00:00
|
|
|
|
2013-11-25 01:53:55 +00:00
|
|
|
//handles sending custom messages based on Caret commands (builds, plugins, etc)
|
|
|
|
var targets = Settings.get("api");
|
2013-12-02 23:17:46 +00:00
|
|
|
command.on("init:restart", function() {
|
|
|
|
targets = Settings.get("api");
|
|
|
|
});
|
2015-02-21 16:23:33 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("api:execute", function(id, c) {
|
|
|
|
if (!id in targets) return c();
|
2013-11-25 01:53:55 +00:00
|
|
|
var config = targets[id];
|
2015-02-21 20:51:16 +00:00
|
|
|
var message = config.message;
|
2014-06-22 07:13:11 +00:00
|
|
|
var send = function() {
|
2015-02-21 20:51:16 +00:00
|
|
|
chrome.runtime.sendMessage(config.id, message, null, function() {
|
2014-06-22 07:13:11 +00:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.error(chrome.runtime.lastError);
|
|
|
|
}
|
|
|
|
if (c) c(chrome.runtime.lastError);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
if (config.sendEditorContext) {
|
|
|
|
//add context information to the message
|
2015-02-21 20:51:16 +00:00
|
|
|
message.context = {};
|
|
|
|
|
|
|
|
if (editor.session.file && editor.session.file.getPath) {
|
|
|
|
editor.session.file.getPath(function(err, path) {
|
|
|
|
message.context = {
|
|
|
|
path: path,
|
|
|
|
selection: editor.session.getTextRange()
|
|
|
|
};
|
|
|
|
send();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//no path for Caret config files or unsaved "untitled.txt"
|
|
|
|
message.context.path = "";
|
|
|
|
message.context.selection = editor.session.getTextRange();
|
2014-06-22 07:13:11 +00:00
|
|
|
send();
|
2015-02-21 20:51:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 07:13:11 +00:00
|
|
|
} else {
|
|
|
|
//send message as-is
|
|
|
|
send();
|
|
|
|
}
|
2013-11-25 01:53:55 +00:00
|
|
|
});
|
2015-02-21 16:23:33 +00:00
|
|
|
|
2013-11-25 01:53:55 +00:00
|
|
|
//External apps can send messages by matching Caret's command/argument config objects
|
|
|
|
chrome.runtime.onMessageExternal.addListener(function(message, sender, c) {
|
|
|
|
command.fire(message.command, message.argument, c);
|
|
|
|
});
|
|
|
|
});
|