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

prevent event from being used to enter a newline into editor #7493

Merged
merged 3 commits into from
Jun 12, 2014
Merged
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
33 changes: 27 additions & 6 deletions src/widgets/Dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,27 @@ define(function (require, exports, module) {
function _handleTab(event, $dlg) {
var $inputs = $(":input:enabled, a", $dlg).filter(":visible");

function stopEvent() {
event.stopPropagation();
event.preventDefault();
}

if ($(event.target).closest($dlg).length) {
// If it's the first or last tabbable element, focus the last/first element
if ((!event.shiftKey && event.target === $inputs[$inputs.length - 1]) ||
(event.shiftKey && event.target === $inputs[0])) {
$inputs.filter(event.shiftKey ? ":last" : ":first").focus();
event.preventDefault();
stopEvent();

// If there is no element to focus, don't let it focus outside of the dialog
} else if (!$inputs.length) {
event.preventDefault();
stopEvent();
}

// If the focus left the dialog, focus the first element in the dialog
} else {
$inputs.first().focus();
event.preventDefault();
stopEvent();
}
}

Expand All @@ -141,21 +146,36 @@ define(function (require, exports, module) {
which = String.fromCharCode(e.which),
$focusedElement = this.find(".dialog-button:focus, a:focus");

function stopEvent() {
e.preventDefault();
e.stopPropagation();
}

// There might be a textfield in the dialog's UI; don't want to mistake normal typing for dialog dismissal
var inTextArea = (e.target.tagName === "TEXTAREA"),
inTypingField = inTextArea || ($(e.target).filter(":text, :password").length > 0);

if (e.which === KeyEvent.DOM_VK_TAB) {
// We don't want to stopEvent() in this case since we might want the default behavior.
// _handleTab takes care of stopping/preventing default as necessary.
_handleTab(e, this);
} else if (e.which === KeyEvent.DOM_VK_ESCAPE) {
buttonId = DIALOG_BTN_CANCEL;
} else if (e.which === KeyEvent.DOM_VK_RETURN && (!inTextArea || e.ctrlKey)) {
// Enter key in single-line text input always dismisses; in text area, only Ctrl+Enter dismisses
// Click primary
$primaryBtn.click();
stopEvent();
if (e.target.tagName === "BUTTON") {
this.find(e.target).click();
} else {
$primaryBtn.click();
}
} else if (e.which === KeyEvent.DOM_VK_SPACE) {
// Space bar on focused button or link
$focusedElement.click();
if ($focusedElement.length) {
// Space bar on focused button or link
stopEvent();
$focusedElement.click();
}
} else if (brackets.platform === "mac") {
// CMD+D Don't Save
if (e.metaKey && (which === "D")) {
Expand All @@ -176,6 +196,7 @@ define(function (require, exports, module) {
}

if (buttonId) {
stopEvent();
_processButton(this, buttonId, autoDismiss);
}

Expand Down