Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
12 changes: 0 additions & 12 deletions html/modal-exec.html

This file was deleted.

5 changes: 3 additions & 2 deletions html/panel.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<div id="brackets-nodejs-terminal" class="bottom-panel">
<div class="toolbar simple-toolbar-layout" style="position: relative">
<div style="position: absolute;top: -2px;left: 0;height: 10px;width: 100%; cursor: n-resize" class="resize"></div>
<div style="position: absolute;top: -2px;left: 0;height: 10px;width: 100%; cursor: n-resize" class="resize"></div>
<div class="title">
Terminal:&nbsp;
<code class="cmd"></code>
<input class="cmd-value" type="text" />
<span class="action-execute" style="font-size: 200%;cursor: pointer;" title="Execute">&#187;</span>
</div>
<div class="close" style="font-size: 200%;">
<span class="action-rerun" title="Rerun">&#8635;</span>
Expand Down
202 changes: 100 additions & 102 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ define(function (require, exports, module) {
* The ConnectionManager helps to build and run request to execute a file on the serverside
*/
var ConnectionManager = {

last: {
command: null,
cwd: null
},


last: [],

//
/**
* Creates a new EventSource
*
Expand All @@ -79,28 +77,33 @@ define(function (require, exports, module) {
"new": function (command, useCurrentCwd, cwd) {

if (source && source.close) source.close();

// Current document
var doc = DocumentManager.getCurrentDocument();
if(!doc.file.isFile) return;
if (!doc.file.isFile) return;

// Build url
var url = "http://" + config.host + ":" + config.port + "/?command=" + encodeURIComponent(command);
var dir = null;
if(useCurrentCwd) {
if (useCurrentCwd) {
dir = doc.file.parentPath;
} else if(cwd) {
} else if (cwd) {
dir = cwd;
}
if(dir !== null) {

if (dir !== null) {
url += "&cwd=" + encodeURIComponent(dir);
}

// Store the last command and cwd
this.last.command = command;
this.last.cwd = dir;

var lastCommand = {
command: command,
cwd: dir
};
var lastCommandLength = this.last.unshift(lastCommand);
if (lastCommandLength > 10)
this.last.pop();

// Server should be running
source = new EventSource(url);

Expand All @@ -111,57 +114,64 @@ define(function (require, exports, module) {
source.close();
Panel.write("Program exited.");
}, false);

Panel.show(command);
Panel.clear();
},

newNpm: function (command) {

var npmBin = get("npm");
if(!npmBin) {
if (!npmBin) {
npmBin = "npm";
} else {
// Add quotation because windows paths can contain spaces
npmBin = '"' + npmBin + '"';
}

this.new(npmBin + " " + command, true);

},

newNode: function () {

var nodeBin = get("node");
if(!nodeBin) {
if (!nodeBin) {
nodeBin = "node";
} else {
// Add quotation because windows paths can contain spaces
nodeBin = '"' + nodeBin + '"';
}

// Current document
var doc = DocumentManager.getCurrentDocument();
if(!doc.file.isFile) return;
if (!doc.file.isFile) return;

this.new(nodeBin + ' "' + doc.file.fullPath + '"', true);

},

rerun: function () {

var last = this.last;
if(last.command === null) return;

this.new(last.command, false, last.cwd);

if (last[0].command === null) return;

this.new(last[0].command, false, last[0].cwd);

},

execute: function () {
var cmd = Panel.get(".cmd-value").value;
ConnectionManager.new(cmd, true, null);
},

/**
* Close the current connection if server is started
*/
exit: function () {
source.close();
if (source)
source.close();
Panel.hide();
Copy link
Owner

Choose a reason for hiding this comment

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

The Panel shouldn't be closed by exit. It depends whether the user closes the panel or terminates the process, see https://github.com/Acconut/brackets-nodejs/blob/master/main.js#L248-L254.

}
};

Expand All @@ -185,7 +195,7 @@ define(function (require, exports, module) {
*/
show: function (command) {
this.panel.style.display = "block";
this.commandTitle.textContent = command;
//this.commandTitle.textContent = command;
EditorManager.resizeEditor();
},
hide: function () {
Expand Down Expand Up @@ -226,12 +236,46 @@ define(function (require, exports, module) {
Panel.height = Panel.height + (Panel.y - e.pageY);

},
y: 0

keyup: function (e) {
if (e.keyCode === 38) {
if (ConnectionManager.last.length === 0) {
return;
}
if (Panel.currentLastIndex >= ConnectionManager.last.length) {
Panel.currentLastIndex = ConnectionManager.last.length - 1;
}
if (Panel.currentLastIndex <= 0) {
Panel.currentLastIndex = 0;
}


var cmd = ConnectionManager.last[Panel.currentLastIndex].command;
Panel.get(".cmd-value").value = cmd;
Panel.currentLastIndex++;
} else if (e.keyCode === 40) {
if (ConnectionManager.last.length === 0) {
return;
}
if (Panel.currentLastIndex >= ConnectionManager.last.length) {
Panel.currentLastIndex = ConnectionManager.last.length - 1;
}
if (Panel.currentLastIndex <= 0) {
Panel.currentLastIndex = 0;
}
var cmd = ConnectionManager.last[Panel.currentLastIndex].command;
Panel.get(".cmd-value").value = cmd;
Panel.currentLastIndex--;
} else if (e.keyCode === 13) {
ConnectionManager.execute();
}
},
y: 0,
currentLastIndex: 0
};

// Still resizing
Panel.panel = document.getElementById(Panel.id);
Panel.commandTitle = Panel.get(".cmd");
Panel.pre = Panel.get(".table-container pre");
Panel.get(".resize").addEventListener("mousedown", function (e) {

Expand All @@ -241,6 +285,9 @@ define(function (require, exports, module) {
document.addEventListener("mouseup", Panel.mouseup);

});
Panel.get(".cmd-value").addEventListener("keyup", function (e) {
return Panel.keyup;
});

/**
* Terminal buttons
Expand All @@ -255,6 +302,9 @@ define(function (require, exports, module) {
document.querySelector("#" + Panel.id + " .action-rerun").addEventListener("click", function () {
ConnectionManager.rerun();
});
document.querySelector("#" + Panel.id + " .action-execute").addEventListener("click", function () {
ConnectionManager.execute();
});

var Dialog = {
/**
Expand Down Expand Up @@ -363,82 +413,26 @@ define(function (require, exports, module) {
save = document.querySelector("." + NODE_INSTALL_DIALOG_ID + " .save");

name.focus();

}
},

/**
* The exec modal is used to execute a command
* HTML: html/modal-install.html
*/
exec: {

/**
* HTML put inside the dialog
*/
html: require("text!html/modal-exec.html"),

/**
* Opens up the modal
*/
show: function () {

Dialogs.showModalDialog(
NODE_EXEC_DIALOG_ID,
"Execute command",
this.html, [{
className: Dialogs.DIALOG_BTN_CLASS_PRIMARY,
id: Dialogs.DIALOG_BTN_OK,
text: "Run"
}, {
className: Dialogs.DIALOG_BTN_CLASS_NORMAL,
id: Dialogs.DIALOG_BTN_CANCEL,
text: "Cancel"
}]
).done(function (id) {

if (id !== "ok") return;

// Command musn't be empty
if (command.value == "") {
Dialogs.showModalDialog(Dialogs.DIALOG_ID_ERROR, "Error", "Please enter a command");
return;
}

// Should it be executed in the current working directory
var useCwd = !!cwd.checked;

ConnectionManager.new(command.value, useCwd);

});

// It's important to get the elements after the modal is rendered but before the done event
var command = document.querySelector("." + NODE_EXEC_DIALOG_ID + " .command"),
cwd = document.querySelector("." + NODE_EXEC_DIALOG_ID + " .cwd");

command.focus();

}
}
},
};

/**
* Menu
*/
var RUN_CMD_ID = "brackets-nodejs.run",
EXEC_CMD_ID = "brackets-nodejs.exec",
RUN_NPM_START_CMD_ID = "brackets-nodejs.run_npm_start",
RUN_NPM_STOP_CMD_ID = "brackets-nodejs.run_npm_stop",
RUN_NPM_TEST_CMD_ID = "brackets-nodejs.run_npm_test",
RUN_NPM_INSTALL_CMD_ID = "brackets-nodejs.run_npm_install",
INSTALL_CMD_ID = "brackets-nodejs.install",
CONFIG_CMD_ID = "brackets-nodejs.config";
CONFIG_CMD_ID = "brackets-nodejs.config",
SHOW_TERMINAL = "brackets-nodejs.showterminal";

CommandManager.register("Run", RUN_CMD_ID, function () {
ConnectionManager.newNode();
});
CommandManager.register("Execute command", EXEC_CMD_ID, function() {
Dialog.exec.show();
});
CommandManager.register("Run as npm start", RUN_NPM_START_CMD_ID, function () {
ConnectionManager.newNpm("start");
});
Expand All @@ -458,9 +452,13 @@ define(function (require, exports, module) {
Dialog.settings.show();

});
CommandManager.register("Show Terminal", SHOW_TERMINAL, function () {
Panel.show();
Panel.clear();
});

NodeMenu.addMenuItem(RUN_CMD_ID, "Alt-N");
NodeMenu.addMenuItem(EXEC_CMD_ID);
NodeMenu.addMenuItem(SHOW_TERMINAL, "Alt-T");
NodeMenu.addMenuDivider();
NodeMenu.addMenuItem(RUN_NPM_START_CMD_ID);
NodeMenu.addMenuItem(RUN_NPM_STOP_CMD_ID);
Expand All @@ -471,4 +469,4 @@ define(function (require, exports, module) {
NodeMenu.addMenuDivider();
NodeMenu.addMenuItem(CONFIG_CMD_ID);

});
});