2012-10-18 19:58:10 +02:00
|
|
|
var hotkeys = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
function do_narrow_action(action) {
|
|
|
|
action(current_msg_list.selected_id(), {trigger: 'hotkey'});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-11 23:06:58 +02:00
|
|
|
var actions_dropdown_hotkeys = [
|
|
|
|
'down_arrow',
|
|
|
|
'up_arrow',
|
|
|
|
'vim_up',
|
|
|
|
'vim_down',
|
2017-01-12 00:17:43 +01:00
|
|
|
'enter',
|
2013-07-11 23:06:58 +02:00
|
|
|
];
|
|
|
|
|
2016-06-14 00:08:02 +02:00
|
|
|
// Note that multiple keys can map to the same event_name, which
|
|
|
|
// we'll do in cases where they have the exact same semantics.
|
|
|
|
// DON'T FORGET: update keyboard_shortcuts.html
|
|
|
|
|
2018-02-14 18:34:32 +01:00
|
|
|
// The `message_view_only` property is a convenient and performant way
|
|
|
|
// to express a common case of which hotkeys do something in which
|
|
|
|
// views. It is set for hotkeys (like `*`) that only have an effect
|
|
|
|
// in the main message view with a selected message.
|
|
|
|
// `message_view_only` hotkeys, as a group, are not processed if any
|
|
|
|
// overlays are open (e.g. settings, streams, etc.).
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
var keydown_shift_mappings = {
|
2016-06-14 00:08:02 +02:00
|
|
|
// these can be triggered by shift + key only
|
|
|
|
9: {name: 'shift_tab', message_view_only: false}, // tab
|
2017-03-09 00:56:18 +01:00
|
|
|
32: {name: 'shift_spacebar', message_view_only: true}, // space bar
|
2016-06-14 00:08:02 +02:00
|
|
|
};
|
2017-03-13 21:41:28 +01:00
|
|
|
|
|
|
|
var keydown_unshift_mappings = {
|
2016-06-14 00:08:02 +02:00
|
|
|
// these can be triggered by key only (without shift)
|
|
|
|
9: {name: 'tab', message_view_only: false}, // tab
|
2017-03-13 21:41:28 +01:00
|
|
|
27: {name: 'escape', message_view_only: false}, // escape
|
2017-03-09 00:56:18 +01:00
|
|
|
32: {name: 'spacebar', message_view_only: true}, // space bar
|
2016-06-14 00:08:02 +02:00
|
|
|
33: {name: 'page_up', message_view_only: true}, // page up
|
|
|
|
34: {name: 'page_down', message_view_only: true}, // page down
|
|
|
|
35: {name: 'end', message_view_only: true}, // end
|
|
|
|
36: {name: 'home', message_view_only: true}, // home
|
2017-03-19 01:51:20 +01:00
|
|
|
37: {name: 'left_arrow', message_view_only: false}, // left arrow
|
|
|
|
39: {name: 'right_arrow', message_view_only: false}, // right arrow
|
2017-04-04 21:04:59 +02:00
|
|
|
38: {name: 'up_arrow', message_view_only: false}, // up arrow
|
|
|
|
40: {name: 'down_arrow', message_view_only: false}, // down arrow
|
2016-06-14 00:08:02 +02:00
|
|
|
};
|
2017-03-13 21:41:28 +01:00
|
|
|
|
2017-03-19 19:04:14 +01:00
|
|
|
var keydown_ctrl_mappings = {
|
2017-09-19 01:33:17 +02:00
|
|
|
219: {name: 'escape', message_view_only: false}, // '['
|
2017-03-19 19:04:14 +01:00
|
|
|
};
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
var keydown_either_mappings = {
|
2016-06-14 00:08:02 +02:00
|
|
|
// these can be triggered by key or shift + key
|
|
|
|
// Note that codes for letters are still case sensitive!
|
2017-03-13 21:41:28 +01:00
|
|
|
//
|
|
|
|
// We may want to revisit both of these. For backspace, we don't
|
|
|
|
// have any specific mapping behavior; we are just trying to disable
|
|
|
|
// the normal browser features for certain OSes when we are in the
|
|
|
|
// compose box, and the little bit of backspace-related code here is
|
|
|
|
// dubious, but may apply to shift-backspace.
|
|
|
|
// For enter, there is some possibly that shift-enter is intended to
|
|
|
|
// have special behavior for folks that are used to shift-enter behavior
|
|
|
|
// in other apps, but that's also slightly dubious.
|
2016-06-14 00:08:02 +02:00
|
|
|
8: {name: 'backspace', message_view_only: true}, // backspace
|
|
|
|
13: {name: 'enter', message_view_only: false}, // enter
|
2018-03-07 21:27:55 +01:00
|
|
|
46: {name: 'delete', message_view_only: false}, // delete
|
2017-03-13 21:41:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var keypress_mappings = {
|
2017-03-19 00:30:32 +01:00
|
|
|
42: {name: 'star_message', message_view_only: true}, // '*'
|
2017-03-20 17:46:26 +01:00
|
|
|
43: {name: 'thumbs_up_emoji', message_view_only: true}, // '+'
|
2017-06-16 06:46:46 +02:00
|
|
|
45: {name: 'toggle_message_collapse', message_view_only: true}, // '-'
|
2016-06-14 00:08:02 +02:00
|
|
|
47: {name: 'search', message_view_only: false}, // '/'
|
2017-10-05 08:02:46 +02:00
|
|
|
58: {name: 'toggle_reactions_popover', message_view_only: true}, // ':'
|
2018-01-31 04:23:02 +01:00
|
|
|
62: {name: 'compose_quote_reply', message_view_only: true}, // '>'
|
2016-06-14 00:08:02 +02:00
|
|
|
63: {name: 'show_shortcuts', message_view_only: false}, // '?'
|
2016-12-29 04:50:17 +01:00
|
|
|
64: {name: 'compose_reply_with_mention', message_view_only: true}, // '@'
|
2016-06-14 00:08:02 +02:00
|
|
|
65: {name: 'stream_cycle_backward', message_view_only: true}, // 'A'
|
2018-02-08 15:17:26 +01:00
|
|
|
67: {name: 'C_deprecated', message_view_only: true}, // 'C'
|
2016-06-14 00:08:02 +02:00
|
|
|
68: {name: 'stream_cycle_forward', message_view_only: true}, // 'D'
|
2017-03-23 08:11:00 +01:00
|
|
|
71: {name: 'G_end', message_view_only: true}, // 'G'
|
2017-03-09 01:02:28 +01:00
|
|
|
74: {name: 'vim_page_down', message_view_only: true}, // 'J'
|
|
|
|
75: {name: 'vim_page_up', message_view_only: true}, // 'K'
|
2017-03-23 04:25:01 +01:00
|
|
|
77: {name: 'toggle_mute', message_view_only: true}, // 'M'
|
2017-03-19 19:41:02 +01:00
|
|
|
80: {name: 'narrow_private', message_view_only: true}, // 'P'
|
2016-06-14 00:08:02 +02:00
|
|
|
82: {name: 'respond_to_author', message_view_only: true}, // 'R'
|
|
|
|
83: {name: 'narrow_by_subject', message_view_only: true}, //'S'
|
2017-03-23 06:41:31 +01:00
|
|
|
86: {name: 'view_selected_stream', message_view_only: false}, //'V'
|
2016-06-14 00:08:02 +02:00
|
|
|
99: {name: 'compose', message_view_only: true}, // 'c'
|
2017-10-20 19:21:08 +02:00
|
|
|
100: {name: 'open_drafts', message_view_only: true}, // 'd'
|
2017-03-18 20:30:20 +01:00
|
|
|
103: {name: 'gear_menu', message_view_only: true}, // 'g'
|
2016-06-14 00:08:02 +02:00
|
|
|
105: {name: 'message_actions', message_view_only: true}, // 'i'
|
|
|
|
106: {name: 'vim_down', message_view_only: true}, // 'j'
|
|
|
|
107: {name: 'vim_up', message_view_only: true}, // 'k'
|
2017-03-23 13:23:49 +01:00
|
|
|
110: {name: 'n_key', message_view_only: false}, // 'n'
|
2018-02-16 15:56:25 +01:00
|
|
|
112: {name: 'p_key', message_view_only: false}, // 'p'
|
2017-09-25 06:20:55 +02:00
|
|
|
113: {name: 'query_streams', message_view_only: false}, // 'q'
|
2016-06-14 00:08:02 +02:00
|
|
|
114: {name: 'reply_message', message_view_only: true}, // 'r'
|
|
|
|
115: {name: 'narrow_by_recipient', message_view_only: true}, // 's'
|
2017-05-23 20:01:24 +02:00
|
|
|
117: {name: 'show_sender_info', message_view_only: true}, // 'u'
|
2017-03-18 21:01:16 +01:00
|
|
|
118: {name: 'show_lightbox', message_view_only: true}, // 'v'
|
2017-09-25 06:20:55 +02:00
|
|
|
119: {name: 'query_users', message_view_only: false}, // 'w'
|
2018-02-08 15:17:26 +01:00
|
|
|
120: {name: 'compose_private_message', message_view_only: true}, // 'x'
|
2016-06-14 00:08:02 +02:00
|
|
|
};
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
exports.get_keydown_hotkey = function (e) {
|
2017-03-19 19:04:14 +01:00
|
|
|
if (e.metaKey || e.altKey) {
|
2017-03-13 21:41:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var hotkey;
|
2017-03-19 19:04:14 +01:00
|
|
|
if (e.ctrlKey) {
|
|
|
|
hotkey = keydown_ctrl_mappings[e.which];
|
|
|
|
if (hotkey) {
|
|
|
|
return hotkey;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
if (e.shiftKey) {
|
|
|
|
hotkey = keydown_shift_mappings[e.which];
|
|
|
|
if (hotkey) {
|
|
|
|
return hotkey;
|
|
|
|
}
|
2013-06-11 04:06:16 +02:00
|
|
|
}
|
2012-09-21 23:51:31 +02:00
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
if (!e.shiftKey) {
|
|
|
|
hotkey = keydown_unshift_mappings[e.which];
|
|
|
|
if (hotkey) {
|
|
|
|
return hotkey;
|
|
|
|
}
|
2012-12-12 20:34:47 +01:00
|
|
|
}
|
2013-06-11 04:06:16 +02:00
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
return keydown_either_mappings[e.which];
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_keypress_hotkey = function (e) {
|
|
|
|
if (e.metaKey || e.ctrlKey || e.altKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keypress_mappings[e.which];
|
|
|
|
};
|
2012-10-23 20:55:24 +02:00
|
|
|
|
2017-10-26 00:34:20 +02:00
|
|
|
exports.processing_text = (function () {
|
|
|
|
var selector = [
|
|
|
|
'input:focus',
|
|
|
|
'select:focus',
|
|
|
|
'textarea:focus',
|
|
|
|
'#compose-send-button:focus',
|
|
|
|
'.editable-section:focus',
|
2017-10-26 00:39:14 +02:00
|
|
|
'.pill-container div:focus',
|
2017-10-26 00:34:20 +02:00
|
|
|
].join(",");
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
return $(selector).length > 0;
|
|
|
|
};
|
|
|
|
}());
|
2017-03-12 02:43:00 +01:00
|
|
|
|
2017-03-10 22:57:25 +01:00
|
|
|
exports.is_editing_stream_name = function (e) {
|
|
|
|
return $(e.target).is(".editable-section");
|
|
|
|
};
|
|
|
|
|
2017-03-12 16:22:55 +01:00
|
|
|
// Returns true if we handled it, false if the browser should.
|
|
|
|
exports.process_escape_key = function (e) {
|
|
|
|
var row;
|
|
|
|
|
|
|
|
if (exports.is_editing_stream_name(e)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:05:30 +02:00
|
|
|
if (overlays.is_modal_open()) {
|
|
|
|
overlays.close_active_modal();
|
2017-08-28 18:45:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.is_active()) {
|
|
|
|
overlays.close_active();
|
2017-03-12 16:22:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-22 04:28:52 +02:00
|
|
|
if (gear_menu.is_open()) {
|
|
|
|
gear_menu.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-22 04:40:44 +02:00
|
|
|
if (message_edit.is_editing(current_msg_list.selected_id())) {
|
2017-10-20 00:57:40 +02:00
|
|
|
// Using this definition of "row" instead of "current_msg_list.selected_row()"
|
|
|
|
// because it returns a more complete object.
|
|
|
|
// Necessary for refocusing on message list in Firefox.
|
|
|
|
row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
|
|
|
row.find('.message_edit_content').blur();
|
|
|
|
message_edit.end(row);
|
2017-05-22 04:40:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:22:55 +01:00
|
|
|
if (exports.processing_text()) {
|
|
|
|
if ($(".message_edit_content").filter(":focus").length > 0) {
|
|
|
|
row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
2017-03-12 20:00:22 +01:00
|
|
|
row.find('.message_edit_content').blur();
|
2017-03-12 16:22:55 +01:00
|
|
|
message_edit.end(row);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($(".message_edit_topic").filter(":focus").length > 0) {
|
|
|
|
row = $(".message_edit_topic").filter(":focus").closest(".message_row");
|
2017-03-12 20:00:22 +01:00
|
|
|
row.find('.message_edit_topic').blur();
|
2017-03-12 16:22:55 +01:00
|
|
|
message_edit.end(row);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activity.searching()) {
|
|
|
|
activity.escape_search();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_list.searching()) {
|
|
|
|
stream_list.escape_search();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:47 +02:00
|
|
|
// Emoji picker goes before compose so compose emoji picker is closed properly.
|
|
|
|
if (emoji_picker.reactions_popped()) {
|
|
|
|
emoji_picker.hide_emoji_popover();
|
2017-03-12 16:22:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:47 +02:00
|
|
|
if (compose_state.composing()) {
|
2017-12-03 16:55:33 +01:00
|
|
|
// Check for errors in compose box; close errors if they exist
|
|
|
|
if ($("#compose-send-status").css('display') !== 'none') {
|
|
|
|
$("#compose-send-status").hide();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:47 +02:00
|
|
|
// If the user hit the escape key, cancel the current compose
|
|
|
|
compose_actions.cancel();
|
2017-03-21 07:51:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:22:55 +01:00
|
|
|
// We pressed Esc and something was focused, and the composebox
|
|
|
|
// wasn't open. In that case, we should blur the input.
|
|
|
|
// (this is almost certainly the searchbar)
|
|
|
|
$("input:focus,textarea:focus").blur();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (popovers.any_active()) {
|
|
|
|
popovers.hide_all();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 18:48:43 +01:00
|
|
|
if (compose_state.composing()) {
|
2017-03-18 17:55:11 +01:00
|
|
|
compose_actions.cancel();
|
2017-03-12 16:22:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
search.clear_search();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-03-13 17:01:09 +01:00
|
|
|
// Returns true if we handled it, false if the browser should.
|
|
|
|
exports.process_enter_key = function (e) {
|
2017-08-28 01:30:49 +02:00
|
|
|
if ($(".dropdown.open").length && $(e.target).attr("role") === "menuitem") {
|
2017-03-18 20:30:57 +01:00
|
|
|
// on #gear-menu li a[tabindex] elements, force a click and prevent default.
|
|
|
|
// this is because these links do not have an href and so don't force a
|
|
|
|
// default action.
|
|
|
|
e.target.click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:46:56 +02:00
|
|
|
if (hotspots.is_open()) {
|
|
|
|
$(e.target).find('.hotspot.overlay.show .hotspot-confirm').click();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-27 07:27:25 +02:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
2017-05-10 19:46:20 +02:00
|
|
|
if (emoji_picker.is_composition(e.target)) {
|
|
|
|
e.target.click();
|
|
|
|
} else {
|
2017-05-30 00:50:45 +02:00
|
|
|
emoji_picker.toggle_selected_emoji();
|
2017-05-10 19:46:20 +02:00
|
|
|
}
|
2017-04-19 07:49:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-13 17:01:09 +01:00
|
|
|
if (exports.is_editing_stream_name(e)) {
|
|
|
|
$(e.target).parent().find(".checkmark").click();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (popovers.actions_popped()) {
|
|
|
|
popovers.actions_menu_handle_keyboard('enter');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.settings_open()) {
|
2017-03-13 17:01:09 +01:00
|
|
|
// On the settings page just let the browser handle
|
|
|
|
// the enter key for things like submitting forms.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.streams_open()) {
|
2017-05-16 00:08:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-13 17:01:09 +01:00
|
|
|
if (exports.processing_text()) {
|
|
|
|
if (activity.searching()) {
|
|
|
|
activity.blur_search();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_list.searching()) {
|
|
|
|
// This is sort of funny behavior, but I think
|
|
|
|
// the intention is that we want it super easy
|
|
|
|
// to close stream search.
|
|
|
|
stream_list.clear_and_hide_search();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
// This handles when pressing enter while looking at drafts.
|
|
|
|
// It restores draft that is focused.
|
2017-10-03 00:41:43 +02:00
|
|
|
if (overlays.drafts_open()) {
|
2017-04-14 08:57:41 +02:00
|
|
|
drafts.drafts_handle_events(e, "enter");
|
2017-03-18 21:26:24 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-13 17:01:09 +01:00
|
|
|
// If we're on a button or a link and have pressed enter, let the
|
|
|
|
// browser handle the keypress
|
|
|
|
//
|
|
|
|
// This is subtle and here's why: Suppose you have the focus on a
|
|
|
|
// stream name in your left sidebar. j and k will still move your
|
|
|
|
// cursor up and down, but Enter won't reply -- it'll just trigger
|
|
|
|
// the link on the sidebar! So you keep pressing enter over and
|
|
|
|
// over again. Until you click somewhere or press r.
|
|
|
|
if ($('a:focus,button:focus').length > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($("#preview_message_area").is(":visible")) {
|
|
|
|
compose.enter_with_preview_open();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, then we're presumably in the message
|
2017-04-21 09:33:05 +02:00
|
|
|
// view, so in that case "enter" is the hotkey to respond to a message.
|
|
|
|
// Note that "r" has same effect, but that is handled in process_hotkey().
|
2017-04-14 19:27:12 +02:00
|
|
|
compose_actions.respond_to_message({trigger: 'hotkey enter'});
|
2017-03-13 17:01:09 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
exports.process_tab_key = function () {
|
|
|
|
// Returns true if we handled it, false if the browser should.
|
|
|
|
// TODO: See if browsers like Safari can now handle tabbing correctly
|
|
|
|
// without our intervention.
|
|
|
|
|
|
|
|
var message_edit_form;
|
|
|
|
|
|
|
|
var focused_message_edit_content = $(".message_edit_content").filter(":focus");
|
|
|
|
if (focused_message_edit_content.length > 0) {
|
|
|
|
message_edit_form = focused_message_edit_content.closest(".message_edit_form");
|
|
|
|
message_edit_form.find(".message_edit_save").focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var focused_message_edit_save = $(".message_edit_save").filter(":focus");
|
|
|
|
if (focused_message_edit_save.length > 0) {
|
|
|
|
message_edit_form = focused_message_edit_save.closest(".message_edit_form");
|
|
|
|
message_edit_form.find(".message_edit_cancel").focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
|
|
|
return emoji_picker.navigate('tab');
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.process_shift_tab_key = function () {
|
|
|
|
// Returns true if we handled it, false if the browser should.
|
|
|
|
// TODO: See if browsers like Safari can now handle tabbing correctly
|
|
|
|
// without our intervention.
|
|
|
|
|
|
|
|
if ($('#compose-send-button').is(':focus')) {
|
|
|
|
// Shift-Tab: go back to content textarea and restore
|
|
|
|
// cursor position.
|
|
|
|
ui.restore_compose_cursor();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift-tabbing from the edit message cancel button takes you to save.
|
|
|
|
if ($(".message_edit_cancel").filter(":focus").length > 0) {
|
|
|
|
$(".message_edit_save").focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift-tabbing from the edit message save button takes you to the content.
|
|
|
|
var focused_message_edit_save = $(".message_edit_save").filter(":focus");
|
|
|
|
if (focused_message_edit_save.length > 0) {
|
|
|
|
focused_message_edit_save.closest(".message_edit_form")
|
|
|
|
.find(".message_edit_content").focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
// Shift-tabbing from emoji catalog/search results takes you back to search textbox.
|
|
|
|
if (emoji_picker.reactions_popped()) {
|
|
|
|
return emoji_picker.navigate('shift_tab');
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-11-29 19:55:08 +01:00
|
|
|
// Process a keydown or keypress event.
|
|
|
|
//
|
|
|
|
// Returns true if we handled it, false if the browser should.
|
2017-03-13 21:41:28 +01:00
|
|
|
exports.process_hotkey = function (e, hotkey) {
|
2016-06-14 00:08:02 +02:00
|
|
|
var event_name = hotkey.name;
|
2017-03-03 17:28:16 +01:00
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
// We handle the most complex keys in their own functions.
|
|
|
|
switch (event_name) {
|
|
|
|
case 'escape':
|
|
|
|
return exports.process_escape_key(e);
|
|
|
|
case 'enter':
|
|
|
|
return exports.process_enter_key(e);
|
|
|
|
case 'tab':
|
|
|
|
return exports.process_tab_key();
|
|
|
|
case 'shift_tab':
|
|
|
|
return exports.process_shift_tab_key();
|
2017-03-13 17:01:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-04 20:48:08 +02:00
|
|
|
switch (event_name) {
|
|
|
|
// TODO: break out specific handlers for up_arrow,
|
|
|
|
// down_arrow, and backspace
|
|
|
|
case 'up_arrow':
|
|
|
|
case 'down_arrow':
|
|
|
|
case 'backspace':
|
2018-03-07 21:27:55 +01:00
|
|
|
case 'delete':
|
2017-10-03 00:41:43 +02:00
|
|
|
if (overlays.drafts_open()) {
|
2017-04-04 20:48:08 +02:00
|
|
|
drafts.drafts_handle_events(e, event_name);
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (hotkey.message_view_only && overlays.is_active()) {
|
2017-05-24 20:15:51 +02:00
|
|
|
if (exports.processing_text()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-27 15:40:54 +02:00
|
|
|
if (event_name === 'narrow_by_subject' && overlays.streams_open()) {
|
2017-05-24 20:15:51 +02:00
|
|
|
subs.keyboard_sub();
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-03 00:41:13 +02:00
|
|
|
if (event_name === 'show_lightbox' && overlays.lightbox_open()) {
|
|
|
|
overlays.close_overlay('lightbox');
|
2017-09-14 01:32:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-20 19:21:08 +02:00
|
|
|
if (event_name === 'open_drafts' && overlays.drafts_open()) {
|
|
|
|
overlays.close_overlay('drafts');
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-24 20:15:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.settings_open()) {
|
2017-05-11 22:52:14 +02:00
|
|
|
if (exports.processing_text()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-04 19:59:04 +02:00
|
|
|
switch (event_name) {
|
|
|
|
case 'up_arrow':
|
|
|
|
settings.handle_up_arrow(e);
|
|
|
|
return true;
|
|
|
|
case 'down_arrow':
|
|
|
|
settings.handle_down_arrow(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-27 07:27:25 +02:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
2017-07-19 00:28:56 +02:00
|
|
|
return emoji_picker.navigate(event_name);
|
2017-04-19 07:37:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 04:46:56 +02:00
|
|
|
if (hotspots.is_open()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.info_overlay_open()) {
|
2017-09-07 03:01:17 +02:00
|
|
|
if (event_name === 'show_shortcuts') {
|
|
|
|
overlays.close_active();
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-12 20:58:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if ((event_name === 'up_arrow' || event_name === 'down_arrow') && overlays.streams_open()) {
|
2017-04-05 10:49:19 +02:00
|
|
|
return subs.switch_rows(event_name);
|
2017-04-04 21:04:59 +02:00
|
|
|
}
|
2012-11-30 00:43:41 +01:00
|
|
|
|
2017-03-10 22:57:25 +01:00
|
|
|
if (exports.is_editing_stream_name(e)) {
|
2017-03-13 17:01:09 +01:00
|
|
|
// We handle the enter key in process_enter_key().
|
|
|
|
// We ignore all other keys.
|
2016-12-17 03:44:15 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:41:41 +01:00
|
|
|
if (event_name === "up_arrow") {
|
2017-04-04 18:14:27 +02:00
|
|
|
if (list_util.inside_list(e)) {
|
|
|
|
list_util.go_up(e);
|
2016-09-28 01:19:20 +02:00
|
|
|
return true;
|
2017-03-03 17:41:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event_name === "down_arrow") {
|
2017-04-04 18:14:27 +02:00
|
|
|
if (list_util.inside_list(e)) {
|
|
|
|
list_util.go_down(e);
|
2016-09-28 01:19:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 00:44:03 +01:00
|
|
|
if ((actions_dropdown_hotkeys.indexOf(event_name) !== -1) && popovers.actions_popped()) {
|
2013-07-11 23:06:58 +02:00
|
|
|
popovers.actions_menu_handle_keyboard(event_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:05:11 +01:00
|
|
|
// The next two sections date back to 00445c84 and are Mac/Chrome-specific,
|
|
|
|
// and they should possibly be eliminated in favor of keeping standard
|
|
|
|
// browser behavior.
|
|
|
|
if (event_name === 'backspace') {
|
|
|
|
if ($('#compose-send-button').is(':focus')) {
|
2013-02-18 08:16:57 +01:00
|
|
|
// Ignore backspace; don't navigate back a page.
|
|
|
|
return true;
|
2017-03-11 22:05:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 15:02:14 +01:00
|
|
|
// Process hotkeys specially when in an input, select, textarea, or send button
|
2017-03-12 02:43:00 +01:00
|
|
|
if (exports.processing_text()) {
|
2017-03-13 17:01:09 +01:00
|
|
|
// Note that there is special handling for enter/escape too, but
|
|
|
|
// we handle this in other functions.
|
2014-01-14 17:57:34 +01:00
|
|
|
|
2017-09-18 21:54:06 +02:00
|
|
|
if (event_name === 'left_arrow' && compose_state.focus_in_empty_compose()) {
|
2016-11-23 05:06:34 +01:00
|
|
|
message_edit.edit_last_sent_message();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-18 21:54:06 +02:00
|
|
|
if ((event_name === 'up_arrow' || event_name === 'down_arrow') && compose_state.focus_in_empty_compose()) {
|
2017-03-18 17:55:11 +01:00
|
|
|
compose_actions.cancel();
|
2016-11-23 05:06:34 +01:00
|
|
|
// don't return, as we still want it to be picked up by the code below
|
2017-03-08 23:05:32 +01:00
|
|
|
} else if (event_name === "page_up") {
|
2017-12-08 22:04:54 +01:00
|
|
|
$("#compose-textarea").caret(0).animate({ scrollTop: 0 }, "fast");
|
2017-03-08 23:05:32 +01:00
|
|
|
return true;
|
|
|
|
} else if (event_name === "page_down") {
|
|
|
|
// so that it always goes to the end of the compose box.
|
2017-12-08 22:04:54 +01:00
|
|
|
var height = $("#compose-textarea")[0].scrollHeight;
|
|
|
|
$("#compose-textarea").caret(Infinity).animate({ scrollTop: height }, "fast");
|
2017-03-08 23:05:32 +01:00
|
|
|
return true;
|
2013-06-07 18:37:18 +02:00
|
|
|
} else {
|
|
|
|
// Let the browser handle the key normally.
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-09 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
if (event_name === 'left_arrow') {
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.lightbox_open()) {
|
2017-03-19 01:51:20 +01:00
|
|
|
lightbox.prev();
|
|
|
|
return true;
|
2017-05-27 15:40:54 +02:00
|
|
|
} else if (overlays.streams_open()) {
|
2017-03-23 06:02:01 +01:00
|
|
|
subs.toggle_view(event_name);
|
|
|
|
return true;
|
2017-03-19 01:51:20 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
message_edit.edit_last_sent_message();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:51:20 +01:00
|
|
|
if (event_name === 'right_arrow') {
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.lightbox_open()) {
|
2017-03-19 01:51:20 +01:00
|
|
|
lightbox.next();
|
|
|
|
return true;
|
2017-05-27 15:40:54 +02:00
|
|
|
} else if (overlays.streams_open()) {
|
2017-03-23 06:02:01 +01:00
|
|
|
subs.toggle_view(event_name);
|
|
|
|
return true;
|
2017-03-19 01:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:21:44 +02:00
|
|
|
// Prevent navigation in the background when the overlays are active.
|
|
|
|
if (overlays.is_active()) {
|
|
|
|
if (event_name === 'view_selected_stream' && overlays.streams_open()) {
|
|
|
|
subs.view_stream();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (event_name === 'n_key' && overlays.streams_open() && page_params.can_create_streams) {
|
|
|
|
subs.new_stream_clicked();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:03:28 +02:00
|
|
|
// Shortcuts that don't require a message
|
2013-06-19 17:10:42 +02:00
|
|
|
switch (event_name) {
|
2017-03-03 18:04:47 +01:00
|
|
|
case 'compose': // 'c': compose
|
2017-03-18 17:41:47 +01:00
|
|
|
compose_actions.start('stream', {trigger: "compose_hotkey"});
|
2017-03-03 18:04:47 +01:00
|
|
|
return true;
|
|
|
|
case 'compose_private_message':
|
2017-03-18 17:41:47 +01:00
|
|
|
compose_actions.start('private', {trigger: "compose_hotkey"});
|
2017-03-03 18:04:47 +01:00
|
|
|
return true;
|
|
|
|
case 'narrow_private':
|
|
|
|
return do_narrow_action(function (target, opts) {
|
2017-06-06 00:20:40 +02:00
|
|
|
opts = _.defaults({}, opts, {select_first_unread: true});
|
2017-03-03 18:04:47 +01:00
|
|
|
narrow.by('is', 'private', opts);
|
|
|
|
});
|
|
|
|
case 'query_streams':
|
|
|
|
stream_list.initiate_search();
|
2013-08-08 00:03:28 +02:00
|
|
|
return true;
|
2014-02-14 22:49:26 +01:00
|
|
|
case 'query_users':
|
|
|
|
activity.initiate_search();
|
|
|
|
return true;
|
2013-08-08 00:03:28 +02:00
|
|
|
case 'search':
|
|
|
|
search.initiate_search();
|
|
|
|
return true;
|
2017-03-18 20:30:20 +01:00
|
|
|
case 'gear_menu':
|
|
|
|
gear_menu.open();
|
|
|
|
return true;
|
2013-08-08 00:03:28 +02:00
|
|
|
case 'show_shortcuts': // Show keyboard shortcuts page
|
2018-03-30 14:09:11 +02:00
|
|
|
info_overlay.maybe_show_keyboard_shortcuts();
|
2013-08-08 00:03:28 +02:00
|
|
|
return true;
|
2014-02-14 19:32:40 +01:00
|
|
|
case 'stream_cycle_backward':
|
2017-08-16 19:06:07 +02:00
|
|
|
narrow.stream_cycle_backward();
|
2014-02-14 19:32:40 +01:00
|
|
|
return true;
|
|
|
|
case 'stream_cycle_forward':
|
2017-08-16 19:06:07 +02:00
|
|
|
narrow.stream_cycle_forward();
|
2014-02-14 19:32:40 +01:00
|
|
|
return true;
|
2017-03-23 13:23:49 +01:00
|
|
|
case 'n_key':
|
2017-09-16 17:30:07 +02:00
|
|
|
narrow.narrow_to_next_topic();
|
2017-03-23 07:54:10 +01:00
|
|
|
return true;
|
2018-02-16 15:56:25 +01:00
|
|
|
case 'p_key':
|
|
|
|
narrow.narrow_to_next_pm_string();
|
|
|
|
return true;
|
2017-03-18 21:26:24 +01:00
|
|
|
case 'open_drafts':
|
2017-10-20 19:21:08 +02:00
|
|
|
drafts.launch();
|
2017-03-18 21:26:24 +01:00
|
|
|
return true;
|
2017-04-21 09:33:05 +02:00
|
|
|
case 'reply_message': // 'r': respond to message
|
|
|
|
// Note that you can "enter" to respond to messages as well,
|
|
|
|
// but that is handled in process_enter_key().
|
|
|
|
compose_actions.respond_to_message({trigger: 'hotkey'});
|
|
|
|
return true;
|
2018-02-08 15:17:26 +01:00
|
|
|
case 'C_deprecated':
|
|
|
|
ui.maybe_show_deprecation_notice('C');
|
|
|
|
return true;
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:51:13 +02:00
|
|
|
if (current_msg_list.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:03:28 +02:00
|
|
|
// Navigation shortcuts
|
2013-07-24 22:51:13 +02:00
|
|
|
switch (event_name) {
|
|
|
|
case 'down_arrow':
|
|
|
|
case 'vim_down':
|
2013-08-05 21:52:38 +02:00
|
|
|
navigate.down(true); // with_centering
|
2013-07-24 22:51:13 +02:00
|
|
|
return true;
|
|
|
|
case 'up_arrow':
|
|
|
|
case 'vim_up':
|
|
|
|
navigate.up();
|
|
|
|
return true;
|
|
|
|
case 'home':
|
|
|
|
navigate.to_home();
|
|
|
|
return true;
|
2013-07-24 23:45:19 +02:00
|
|
|
case 'end':
|
2017-03-23 08:11:00 +01:00
|
|
|
case 'G_end':
|
2013-07-24 23:45:19 +02:00
|
|
|
navigate.to_end();
|
|
|
|
return true;
|
|
|
|
case 'page_up':
|
2017-03-09 01:02:28 +01:00
|
|
|
case 'vim_page_up':
|
2017-03-09 00:56:18 +01:00
|
|
|
case 'shift_spacebar':
|
2017-03-14 19:32:10 +01:00
|
|
|
navigate.page_up();
|
|
|
|
return true;
|
2013-07-24 23:45:19 +02:00
|
|
|
case 'page_down':
|
2017-03-09 01:02:28 +01:00
|
|
|
case 'vim_page_down':
|
2017-03-09 00:56:18 +01:00
|
|
|
case 'spacebar':
|
2017-03-14 19:32:10 +01:00
|
|
|
navigate.page_down();
|
|
|
|
return true;
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 04:25:01 +01:00
|
|
|
var msg = current_msg_list.selected_message();
|
2013-08-08 00:03:28 +02:00
|
|
|
// Shortcuts that operate on a message
|
|
|
|
switch (event_name) {
|
|
|
|
case 'message_actions':
|
2017-12-16 15:42:26 +01:00
|
|
|
return popovers.open_message_menu(msg);
|
2017-03-19 00:30:32 +01:00
|
|
|
case 'star_message':
|
2017-03-23 04:25:01 +01:00
|
|
|
return message_flags.toggle_starred(msg);
|
2013-08-08 00:03:28 +02:00
|
|
|
case 'narrow_by_recipient':
|
|
|
|
return do_narrow_action(narrow.by_recipient);
|
|
|
|
case 'narrow_by_subject':
|
|
|
|
return do_narrow_action(narrow.by_subject);
|
|
|
|
case 'respond_to_author': // 'R': respond to author
|
2017-04-14 19:27:12 +02:00
|
|
|
compose_actions.respond_to_message({reply_type: "personal", trigger: 'hotkey pm'});
|
2013-08-08 00:03:28 +02:00
|
|
|
return true;
|
2016-12-29 04:50:17 +01:00
|
|
|
case 'compose_reply_with_mention': // '@': respond to message with mention to author
|
2017-04-14 19:27:12 +02:00
|
|
|
compose_actions.reply_with_mention({trigger: 'hotkey'});
|
2016-12-29 04:50:17 +01:00
|
|
|
return true;
|
2017-03-18 21:01:16 +01:00
|
|
|
case 'show_lightbox':
|
|
|
|
lightbox.show_from_selected_message();
|
|
|
|
return true;
|
2017-05-23 20:01:24 +02:00
|
|
|
case 'show_sender_info':
|
|
|
|
popovers.show_sender_info();
|
|
|
|
return true;
|
2017-10-05 08:02:46 +02:00
|
|
|
case 'toggle_reactions_popover': // ':': open reactions to message
|
2017-09-18 22:06:39 +02:00
|
|
|
reactions.open_reactions_popover();
|
2017-03-19 05:19:36 +01:00
|
|
|
return true;
|
2017-03-19 03:08:09 +01:00
|
|
|
case 'thumbs_up_emoji': // '+': reacts with thumbs up emoji on selected message
|
2018-03-21 21:24:34 +01:00
|
|
|
reactions.toggle_emoji_reaction(msg.id, "thumbs_up");
|
2017-03-23 04:25:01 +01:00
|
|
|
return true;
|
|
|
|
case 'toggle_mute':
|
|
|
|
muting_ui.toggle_mute(msg);
|
2017-03-19 03:08:09 +01:00
|
|
|
return true;
|
2017-06-16 06:46:46 +02:00
|
|
|
case 'toggle_message_collapse':
|
|
|
|
condense.toggle_collapse(msg);
|
|
|
|
return true;
|
2018-01-31 04:23:02 +01:00
|
|
|
case 'compose_quote_reply': // > : respond to selected message with quote
|
|
|
|
compose_actions.quote_and_reply({trigger: 'hotkey'});
|
|
|
|
return true;
|
2013-08-08 00:03:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
return false;
|
2017-03-10 22:29:07 +01:00
|
|
|
};
|
2012-09-21 22:35:32 +02:00
|
|
|
|
2012-10-03 17:04:43 +02:00
|
|
|
/* We register both a keydown and a keypress function because
|
|
|
|
we want to intercept pgup/pgdn, escape, etc, and process them
|
|
|
|
as they happen on the keyboard. However, if we processed
|
|
|
|
letters/numbers in keydown, we wouldn't know what the case of
|
|
|
|
the letters were.
|
|
|
|
|
|
|
|
We want case-sensitive hotkeys (such as in the case of r vs R)
|
|
|
|
so we bail in .keydown if the event is a letter or number and
|
|
|
|
instead just let keypress go for it. */
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
exports.process_keydown = function (e) {
|
|
|
|
activity.new_user_input = true;
|
|
|
|
var hotkey = exports.get_keydown_hotkey(e);
|
|
|
|
if (!hotkey) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return exports.process_hotkey(e, hotkey);
|
|
|
|
};
|
|
|
|
|
2012-11-11 22:48:51 +01:00
|
|
|
$(document).keydown(function (e) {
|
2017-03-13 21:41:28 +01:00
|
|
|
if (exports.process_keydown(e)) {
|
2017-03-14 02:28:33 +01:00
|
|
|
// TODO: We should really move this resize code
|
|
|
|
// so it only executes as part of navigation actions.
|
|
|
|
resize.resize_bottom_whitespace();
|
2017-03-13 21:41:28 +01:00
|
|
|
e.preventDefault();
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
2012-10-03 17:04:43 +02:00
|
|
|
});
|
|
|
|
|
2017-03-13 21:41:28 +01:00
|
|
|
exports.process_keypress = function (e) {
|
|
|
|
var hotkey = exports.get_keypress_hotkey(e);
|
|
|
|
if (!hotkey) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return exports.process_hotkey(e, hotkey);
|
|
|
|
};
|
|
|
|
|
2012-11-11 22:48:51 +01:00
|
|
|
$(document).keypress(function (e) {
|
2017-03-13 21:41:28 +01:00
|
|
|
if (exports.process_keypress(e)) {
|
|
|
|
e.preventDefault();
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
});
|
2012-10-18 19:58:10 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = hotkeys;
|
|
|
|
}
|