2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as emoji from "../shared/js/emoji";
|
|
|
|
|
2021-02-28 01:13:42 +01:00
|
|
|
import * as activity from "./activity";
|
2021-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as common from "./common";
|
|
|
|
import * as compose from "./compose";
|
2021-02-28 21:32:22 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
|
|
|
import * as condense from "./condense";
|
|
|
|
import * as copy_and_paste from "./copy_and_paste";
|
2021-06-15 16:17:28 +02:00
|
|
|
import * as deprecated_feature_notice from "./deprecated_feature_notice";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as drafts from "./drafts";
|
2021-02-28 01:26:16 +01:00
|
|
|
import * as emoji_picker from "./emoji_picker";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as feedback_widget from "./feedback_widget";
|
|
|
|
import * as gear_menu from "./gear_menu";
|
2021-03-31 07:48:11 +02:00
|
|
|
import * as giphy from "./giphy";
|
2021-02-28 01:07:47 +01:00
|
|
|
import * as hashchange from "./hashchange";
|
2021-02-28 01:15:02 +01:00
|
|
|
import * as hotspots from "./hotspots";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as lightbox from "./lightbox";
|
|
|
|
import * as list_util from "./list_util";
|
|
|
|
import * as message_edit from "./message_edit";
|
2021-02-28 01:08:13 +01:00
|
|
|
import * as message_flags from "./message_flags";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 01:24:04 +01:00
|
|
|
import * as message_view_header from "./message_view_header";
|
2021-07-08 20:36:52 +02:00
|
|
|
import * as muted_topics_ui from "./muted_topics_ui";
|
2021-02-28 21:31:57 +01:00
|
|
|
import * as narrow from "./narrow";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as navigate from "./navigate";
|
|
|
|
import * as overlays from "./overlays";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as popovers from "./popovers";
|
2021-02-28 01:25:00 +01:00
|
|
|
import * as reactions from "./reactions";
|
2021-06-10 14:18:46 +02:00
|
|
|
import * as recent_topics_ui from "./recent_topics_ui";
|
|
|
|
import * as recent_topics_util from "./recent_topics_util";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as search from "./search";
|
2021-04-28 21:58:33 +02:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as stream_popover from "./stream_popover";
|
2021-07-09 15:51:31 +02:00
|
|
|
import * as stream_settings_ui from "./stream_settings_ui";
|
2021-02-28 01:06:02 +01:00
|
|
|
import * as topic_zoom from "./topic_zoom";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-02-10 16:47:06 +01:00
|
|
|
|
2013-06-11 04:06:16 +02:00
|
|
|
function do_narrow_action(action) {
|
2021-03-30 02:21:21 +02:00
|
|
|
action(message_lists.current.selected_id(), {trigger: "hotkey"});
|
2013-06-11 04:06:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:52:00 +02:00
|
|
|
// For message actions and user profile menu.
|
2020-10-07 10:45:28 +02:00
|
|
|
const menu_dropdown_hotkeys = new Set(["down_arrow", "up_arrow", "vim_up", "vim_down", "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
|
2020-08-11 02:09:14 +02:00
|
|
|
// views. It is set for hotkeys (like `Ctrl + S`) that only have an effect
|
2018-02-14 18:34:32 +01:00
|
|
|
// 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.).
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keydown_shift_mappings = {
|
2020-08-11 02:09:14 +02:00
|
|
|
// these can be triggered by Shift + key only
|
|
|
|
9: {name: "shift_tab", message_view_only: false}, // Tab
|
2020-07-16 23:29:01 +02:00
|
|
|
32: {name: "shift_spacebar", message_view_only: true}, // space bar
|
2020-07-15 01:29:15 +02:00
|
|
|
37: {name: "left_arrow", message_view_only: false}, // left arrow
|
|
|
|
39: {name: "right_arrow", message_view_only: false}, // right arrow
|
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keydown_unshift_mappings = {
|
2020-08-11 02:09:14 +02:00
|
|
|
// these can be triggered by key only (without Shift)
|
|
|
|
9: {name: "tab", message_view_only: false}, // Tab
|
|
|
|
27: {name: "escape", message_view_only: false}, // Esc
|
2020-07-15 01:29:15 +02:00
|
|
|
32: {name: "spacebar", message_view_only: true}, // space bar
|
2020-08-11 02:09:14 +02:00
|
|
|
33: {name: "page_up", message_view_only: true}, // PgUp
|
|
|
|
34: {name: "page_down", message_view_only: true}, // PgDn
|
|
|
|
35: {name: "end", message_view_only: true}, // End
|
|
|
|
36: {name: "home", message_view_only: true}, // Home
|
2020-07-15 01:29:15 +02:00
|
|
|
37: {name: "left_arrow", message_view_only: false}, // left arrow
|
|
|
|
39: {name: "right_arrow", message_view_only: false}, // right arrow
|
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keydown_ctrl_mappings = {
|
2020-07-15 01:29:15 +02:00
|
|
|
219: {name: "escape", message_view_only: false}, // '['
|
2018-05-24 17:29:53 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keydown_cmd_or_ctrl_mappings = {
|
2020-07-15 01:29:15 +02:00
|
|
|
67: {name: "copy_with_c", message_view_only: false}, // 'C'
|
|
|
|
75: {name: "search_with_k", message_view_only: false}, // 'K'
|
2020-08-11 02:09:14 +02:00
|
|
|
83: {name: "star_message", message_view_only: true}, // 'S'
|
2020-07-15 01:29:15 +02:00
|
|
|
190: {name: "narrow_to_compose_target", message_view_only: true}, // '.'
|
2017-03-19 19:04:14 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keydown_either_mappings = {
|
2020-08-11 02:09:14 +02:00
|
|
|
// these can be triggered by key or Shift + key
|
2016-06-14 00:08:02 +02:00
|
|
|
// Note that codes for letters are still case sensitive!
|
2017-03-13 21:41:28 +01:00
|
|
|
//
|
2020-08-11 02:09:14 +02:00
|
|
|
// We may want to revisit both of these. For Backspace, we don't
|
2017-03-13 21:41:28 +01:00
|
|
|
// have any specific mapping behavior; we are just trying to disable
|
|
|
|
// the normal browser features for certain OSes when we are in the
|
2020-08-11 02:09:14 +02:00
|
|
|
// 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
|
2017-03-13 21:41:28 +01:00
|
|
|
// in other apps, but that's also slightly dubious.
|
2020-08-11 02:09:14 +02:00
|
|
|
8: {name: "backspace", message_view_only: true}, // Backspace
|
|
|
|
13: {name: "enter", message_view_only: false}, // Enter
|
|
|
|
46: {name: "delete", message_view_only: false}, // Delete
|
2017-03-13 21:41:28 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const keypress_mappings = {
|
2020-07-15 01:29:15 +02:00
|
|
|
42: {name: "star_deprecated", message_view_only: true}, // '*'
|
|
|
|
43: {name: "thumbs_up_emoji", message_view_only: true}, // '+'
|
|
|
|
45: {name: "toggle_message_collapse", message_view_only: true}, // '-'
|
|
|
|
47: {name: "search", message_view_only: false}, // '/'
|
|
|
|
58: {name: "toggle_reactions_popover", message_view_only: true}, // ':'
|
|
|
|
62: {name: "compose_quote_reply", message_view_only: true}, // '>'
|
|
|
|
63: {name: "show_shortcuts", message_view_only: false}, // '?'
|
|
|
|
64: {name: "compose_reply_with_mention", message_view_only: true}, // '@'
|
|
|
|
65: {name: "stream_cycle_backward", message_view_only: true}, // 'A'
|
|
|
|
67: {name: "C_deprecated", message_view_only: true}, // 'C'
|
|
|
|
68: {name: "stream_cycle_forward", message_view_only: true}, // 'D'
|
|
|
|
71: {name: "G_end", message_view_only: true}, // 'G'
|
|
|
|
74: {name: "vim_page_down", message_view_only: true}, // 'J'
|
|
|
|
75: {name: "vim_page_up", message_view_only: true}, // 'K'
|
2021-01-03 12:28:09 +01:00
|
|
|
77: {name: "toggle_topic_mute", message_view_only: true}, // 'M'
|
2020-07-15 01:29:15 +02:00
|
|
|
80: {name: "narrow_private", message_view_only: true}, // 'P'
|
|
|
|
82: {name: "respond_to_author", message_view_only: true}, // 'R'
|
2021-03-26 20:38:59 +01:00
|
|
|
83: {name: "narrow_by_topic", message_view_only: true}, // 'S'
|
|
|
|
86: {name: "view_selected_stream", message_view_only: false}, // 'V'
|
2020-09-20 10:14:24 +02:00
|
|
|
97: {name: "all_messages", message_view_only: true}, // 'a'
|
2020-07-15 01:29:15 +02:00
|
|
|
99: {name: "compose", message_view_only: true}, // 'c'
|
|
|
|
100: {name: "open_drafts", message_view_only: true}, // 'd'
|
|
|
|
101: {name: "edit_message", message_view_only: true}, // 'e'
|
|
|
|
103: {name: "gear_menu", message_view_only: true}, // 'g'
|
2020-07-02 14:02:51 +02:00
|
|
|
104: {name: "vim_left", message_view_only: true}, // 'h'
|
2020-07-15 01:29:15 +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'
|
2020-07-02 14:02:51 +02:00
|
|
|
108: {name: "vim_right", message_view_only: true}, // 'l'
|
2020-07-15 01:29:15 +02:00
|
|
|
110: {name: "n_key", message_view_only: false}, // 'n'
|
|
|
|
112: {name: "p_key", message_view_only: false}, // 'p'
|
|
|
|
113: {name: "query_streams", message_view_only: true}, // 'q'
|
|
|
|
114: {name: "reply_message", message_view_only: true}, // 'r'
|
|
|
|
115: {name: "narrow_by_recipient", message_view_only: true}, // 's'
|
|
|
|
116: {name: "open_recent_topics", message_view_only: true}, // 't'
|
|
|
|
117: {name: "show_sender_info", message_view_only: true}, // 'u'
|
|
|
|
118: {name: "show_lightbox", message_view_only: true}, // 'v'
|
|
|
|
119: {name: "query_users", message_view_only: true}, // 'w'
|
|
|
|
120: {name: "compose_private_message", message_view_only: true}, // 'x'
|
2016-06-14 00:08:02 +02:00
|
|
|
};
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function get_keydown_hotkey(e) {
|
2018-03-29 22:29:10 +02:00
|
|
|
if (e.altKey) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-13 21:41:28 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let hotkey;
|
2018-08-14 00:05:36 +02:00
|
|
|
|
2018-06-21 01:30:19 +02:00
|
|
|
if (e.ctrlKey && !e.shiftKey) {
|
2018-05-24 17:29:53 +02:00
|
|
|
hotkey = keydown_ctrl_mappings[e.which];
|
|
|
|
if (hotkey) {
|
|
|
|
return hotkey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isCmdOrCtrl = common.has_mac_keyboard() ? e.metaKey : e.ctrlKey;
|
2018-06-20 18:43:01 +02:00
|
|
|
if (isCmdOrCtrl && !e.shiftKey) {
|
2018-03-29 22:29:10 +02:00
|
|
|
hotkey = keydown_cmd_or_ctrl_mappings[e.which];
|
2017-03-19 19:04:14 +01:00
|
|
|
if (hotkey) {
|
|
|
|
return hotkey;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-03-29 22:29:10 +02:00
|
|
|
} else if (e.metaKey || e.ctrlKey) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-19 19:04:14 +01:00
|
|
|
}
|
|
|
|
|
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];
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-13 21:41:28 +01:00
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function get_keypress_hotkey(e) {
|
2017-03-13 21:41:28 +01:00
|
|
|
if (e.metaKey || e.ctrlKey || e.altKey) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-13 21:41:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return keypress_mappings[e.which];
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2012-10-23 20:55:24 +02:00
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function processing_text() {
|
2020-01-28 22:15:13 +01:00
|
|
|
const $focused_elt = $(":focus");
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
$focused_elt.is("input") ||
|
2020-01-28 22:15:13 +01:00
|
|
|
$focused_elt.is("select") ||
|
|
|
|
$focused_elt.is("textarea") ||
|
2020-01-29 13:43:57 +01:00
|
|
|
$focused_elt.parents(".pill-container").length >= 1 ||
|
2020-07-15 00:34:28 +02:00
|
|
|
$focused_elt.attr("id") === "compose-send-button"
|
|
|
|
);
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-12 02:43:00 +01:00
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function in_content_editable_widget(e) {
|
2017-03-10 22:57:25 +01:00
|
|
|
return $(e.target).is(".editable-section");
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-10 22:57:25 +01:00
|
|
|
|
2017-03-12 16:22:55 +01:00
|
|
|
// Returns true if we handled it, false if the browser should.
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_escape_key(e) {
|
2021-03-16 15:15:33 +01:00
|
|
|
if (
|
2021-06-10 14:18:46 +02:00
|
|
|
recent_topics_util.is_in_focus() &&
|
2021-03-15 10:31:31 +01:00
|
|
|
// This will return false if `e.target` is not
|
|
|
|
// any of the recent topics elements by design.
|
2021-06-10 14:18:46 +02:00
|
|
|
recent_topics_ui.change_focused_element($(e.target), "escape")
|
2021-03-16 15:15:33 +01:00
|
|
|
) {
|
2021-03-15 10:31:31 +01:00
|
|
|
// Recent topics uses escape to switch focus from RT search / filters to topics table.
|
|
|
|
// If focus is already on the table it returns false.
|
2021-03-09 13:21:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 16:18:08 +01:00
|
|
|
if (feedback_widget.is_open()) {
|
|
|
|
feedback_widget.dismiss();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:39:42 +02:00
|
|
|
if (popovers.any_active()) {
|
|
|
|
popovers.hide_all();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
if (processing_text()) {
|
2017-03-12 16:22:55 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:48:11 +02:00
|
|
|
if (giphy.is_popped_from_edit_messsage()) {
|
|
|
|
giphy.focus_current_edit_message();
|
|
|
|
// Hide after setting focus so that `edit_message_id` is
|
|
|
|
// still set in giphy.
|
|
|
|
giphy.hide_giphy_popover();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:47 +02:00
|
|
|
if (compose_state.composing()) {
|
2021-03-31 07:48:11 +02:00
|
|
|
// Check if the giphy popover was open using compose box.
|
2021-03-19 13:21:18 +01:00
|
|
|
// Hide GIPHY popover if it's open.
|
2021-03-31 07:48:11 +02:00
|
|
|
if (!giphy.is_popped_from_edit_messsage() && giphy.hide_giphy_popover()) {
|
2021-03-19 13:21:18 +01:00
|
|
|
$("#compose-textarea").trigger("focus");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:55:33 +01:00
|
|
|
// Check for errors in compose box; close errors if they exist
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($("#compose-send-status").css("display") !== "none") {
|
2017-12-03 16:55:33 +01:00
|
|
|
$("#compose-send-status").hide();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// If the user hit the Esc key, cancel the current compose
|
2017-05-01 00:13:47 +02:00
|
|
|
compose_actions.cancel();
|
2017-03-21 07:51:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($("#searchbox").has(":focus")) {
|
2020-07-20 21:24:26 +02:00
|
|
|
$("input:focus,textarea:focus").trigger("blur");
|
2020-02-03 17:01:11 +01:00
|
|
|
if (page_params.search_pills_enabled) {
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#searchbox .pill").trigger("blur");
|
|
|
|
$("#searchbox #search_query").trigger("blur");
|
2020-02-03 17:01:11 +01:00
|
|
|
} else {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.exit_search();
|
2020-02-03 17:01:11 +01:00
|
|
|
}
|
2018-07-23 19:47:52 +02: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.
|
2020-07-20 21:24:26 +02:00
|
|
|
$("input:focus,textarea:focus").trigger("blur");
|
2017-03-12 16:22:55 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-11 13:04:57 +02:00
|
|
|
if (topic_zoom.is_zoomed_in()) {
|
|
|
|
topic_zoom.zoom_out();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-12 23:38:55 +02:00
|
|
|
hashchange.set_hash_to_default_view();
|
2017-03-12 16:22:55 +01:00
|
|
|
return true;
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-12 16:22:55 +01:00
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
function handle_popover_events(event_name) {
|
|
|
|
if (popovers.actions_popped()) {
|
|
|
|
popovers.actions_menu_handle_keyboard(event_name);
|
2017-03-18 20:30:57 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (popovers.message_info_popped()) {
|
|
|
|
popovers.user_info_popover_for_message_handle_keyboard(event_name);
|
|
|
|
return true;
|
2017-08-02 04:46:56 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (popovers.user_info_popped()) {
|
|
|
|
popovers.user_info_popover_handle_keyboard(event_name);
|
|
|
|
return true;
|
2017-04-19 07:49:54 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (popovers.user_sidebar_popped()) {
|
|
|
|
popovers.user_sidebar_popover_handle_keyboard(event_name);
|
|
|
|
return true;
|
2017-03-13 17:01:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (stream_popover.stream_popped()) {
|
|
|
|
stream_popover.stream_sidebar_menu_handle_keyboard(event_name);
|
2017-03-13 17:01:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (stream_popover.topic_popped()) {
|
|
|
|
stream_popover.topic_sidebar_menu_handle_keyboard(event_name);
|
2020-07-09 16:59:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (stream_popover.all_messages_popped()) {
|
|
|
|
stream_popover.all_messages_sidebar_menu_handle_keyboard(event_name);
|
2020-09-22 19:19:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (stream_popover.starred_messages_popped()) {
|
|
|
|
stream_popover.starred_messages_sidebar_menu_handle_keyboard(event_name);
|
2021-03-12 11:31:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-12 19:16:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-12 11:31:26 +01:00
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
// Returns true if we handled it, false if the browser should.
|
|
|
|
export function process_enter_key(e) {
|
|
|
|
if ($(".dropdown.open").length && $(e.target).attr("role") === "menuitem") {
|
|
|
|
// 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();
|
2020-07-08 22:17:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (hotspots.is_open()) {
|
|
|
|
$(e.target).find(".hotspot.overlay.show .hotspot-confirm").trigger("click");
|
|
|
|
return false;
|
2020-07-09 03:18:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
|
|
|
return emoji_picker.navigate("enter", e);
|
2020-07-10 19:39:26 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (handle_popover_events("enter")) {
|
2020-07-10 20:25:24 +02:00
|
|
|
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
|
2020-08-11 02:09:14 +02:00
|
|
|
// the Enter key for things like submitting forms.
|
2017-03-13 17:01:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.streams_open()) {
|
2017-05-16 00:08:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
if (processing_text()) {
|
2017-03-13 17:01:09 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:33:49 +02:00
|
|
|
// This handles when pressing Enter while looking at drafts.
|
2017-03-18 21:26:24 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:05:59 +02:00
|
|
|
if ($(e.target).attr("role") === "button") {
|
|
|
|
e.target.click();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-02 10:15:21 +02:00
|
|
|
// All custom logic for overlays/modals is above; if we're in a
|
|
|
|
// modal at this point, let the browser handle the event.
|
|
|
|
if (overlays.is_modal_open()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// If we're on a button or a link and have pressed Enter, let the
|
2017-03-13 17:01:09 +01:00
|
|
|
// 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
|
2020-08-11 02:09:14 +02:00
|
|
|
// the link on the sidebar! So you keep pressing Enter over and
|
2017-03-13 17:01:09 +01:00
|
|
|
// over again. Until you click somewhere or press r.
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($("a:focus,button:focus").length > 0) {
|
2017-03-13 17:01:09 +01:00
|
|
|
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
|
2020-08-11 02:09:14 +02:00
|
|
|
// view, so in that case "Enter" is the hotkey to respond to a message.
|
2017-04-21 09:33:05 +02:00
|
|
|
// Note that "r" has same effect, but that is handled in process_hotkey().
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_actions.respond_to_message({trigger: "hotkey enter"});
|
2017-03-13 17:01:09 +01:00
|
|
|
return true;
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-13 17:01:09 +01:00
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_tab_key() {
|
2017-03-14 23:01:08 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let message_edit_form;
|
2017-03-14 23:01:08 +01:00
|
|
|
|
2020-10-07 11:38:36 +02:00
|
|
|
const focused_message_edit_content = $(".message_edit_content:focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
if (focused_message_edit_content.length > 0) {
|
|
|
|
message_edit_form = focused_message_edit_content.closest(".message_edit_form");
|
2018-12-09 15:48:40 +01:00
|
|
|
// Open message edit forms either have a save button or a close button, but not both.
|
2020-07-20 21:24:26 +02:00
|
|
|
message_edit_form.find(".message_edit_save,.message_edit_close").trigger("focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-07 11:38:36 +02:00
|
|
|
const focused_message_edit_save = $(".message_edit_save:focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
if (focused_message_edit_save.length > 0) {
|
|
|
|
message_edit_form = focused_message_edit_save.closest(".message_edit_form");
|
2020-07-20 21:24:26 +02:00
|
|
|
message_edit_form.find(".message_edit_cancel").trigger("focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:28:56 +02:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return emoji_picker.navigate("tab");
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
return false;
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-14 23:01:08 +01:00
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_shift_tab_key() {
|
2017-03-14 23:01:08 +01:00
|
|
|
// 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.
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($("#compose-send-button").is(":focus")) {
|
2017-03-14 23:01:08 +01:00
|
|
|
// Shift-Tab: go back to content textarea and restore
|
|
|
|
// cursor position.
|
|
|
|
ui.restore_compose_cursor();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// Shift-Tabbing from the edit message cancel button takes you to save.
|
2020-10-07 11:38:36 +02:00
|
|
|
if ($(".message_edit_cancel:focus").length > 0) {
|
2020-07-20 21:24:26 +02:00
|
|
|
$(".message_edit_save").trigger("focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// Shift-Tabbing from the edit message save button takes you to the content.
|
2020-10-07 11:38:36 +02:00
|
|
|
const focused_message_edit_save = $(".message_edit_save:focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
if (focused_message_edit_save.length > 0) {
|
2020-07-15 00:34:28 +02:00
|
|
|
focused_message_edit_save
|
|
|
|
.closest(".message_edit_form")
|
|
|
|
.find(".message_edit_content")
|
2020-07-20 21:24:26 +02:00
|
|
|
.trigger("focus");
|
2017-03-14 23:01:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// Shift-Tabbing from emoji catalog/search results takes you back to search textbox.
|
2017-07-19 00:28:56 +02:00
|
|
|
if (emoji_picker.reactions_popped()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return emoji_picker.navigate("shift_tab");
|
2017-07-19 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
return false;
|
2021-02-28 01:06:02 +01:00
|
|
|
}
|
2017-03-14 23:01:08 +01:00
|
|
|
|
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.
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_hotkey(e, hotkey) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event_name = hotkey.name;
|
2017-03-03 17:28:16 +01:00
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// This block needs to be before the `Tab` handler.
|
2020-06-20 10:17:44 +02:00
|
|
|
switch (event_name) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "up_arrow":
|
|
|
|
case "down_arrow":
|
|
|
|
case "left_arrow":
|
|
|
|
case "right_arrow":
|
2020-07-02 14:02:51 +02:00
|
|
|
case "vim_up":
|
|
|
|
case "vim_down":
|
|
|
|
case "vim_left":
|
|
|
|
case "vim_right":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "tab":
|
|
|
|
case "shift_tab":
|
2020-09-23 09:43:59 +02:00
|
|
|
case "open_recent_topics":
|
2021-06-10 14:18:46 +02:00
|
|
|
if (recent_topics_util.is_in_focus()) {
|
|
|
|
return recent_topics_ui.change_focused_element($(e.target), event_name);
|
2020-07-15 02:14:03 +02:00
|
|
|
}
|
2020-06-20 10:17:44 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 23:01:08 +01:00
|
|
|
// We handle the most complex keys in their own functions.
|
|
|
|
switch (event_name) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "escape":
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_escape_key(e);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "enter":
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_enter_key(e);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "tab":
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_tab_key();
|
2020-07-15 02:14:03 +02:00
|
|
|
case "shift_tab":
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_shift_tab_key();
|
2018-05-07 01:38:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 09:28:15 +02:00
|
|
|
// This block needs to be before the open modals check, because
|
|
|
|
// the "user status" modal can show the emoji picker.
|
|
|
|
if (emoji_picker.reactions_popped()) {
|
|
|
|
return emoji_picker.navigate(event_name);
|
|
|
|
}
|
|
|
|
|
2021-07-08 08:37:44 +02:00
|
|
|
if (overlays.is_modal_open()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-07 01:38:14 +02:00
|
|
|
// TODO: break out specific handlers for up_arrow,
|
|
|
|
// down_arrow, and backspace
|
2017-04-04 20:48:08 +02:00
|
|
|
switch (event_name) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "up_arrow":
|
|
|
|
case "down_arrow":
|
2021-05-08 15:22:44 +02:00
|
|
|
case "vim_up":
|
|
|
|
case "vim_down":
|
2020-07-15 02:14:03 +02:00
|
|
|
case "backspace":
|
|
|
|
case "delete":
|
|
|
|
if (overlays.drafts_open()) {
|
|
|
|
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()) {
|
2021-02-28 01:06:02 +01:00
|
|
|
if (processing_text()) {
|
2017-05-24 20:15:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "narrow_by_topic" && overlays.streams_open()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.keyboard_sub();
|
2017-05-24 20:15:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "show_lightbox" && overlays.lightbox_open()) {
|
|
|
|
overlays.close_overlay("lightbox");
|
2017-09-14 01:32:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "open_drafts" && overlays.drafts_open()) {
|
|
|
|
overlays.close_overlay("drafts");
|
2017-10-20 19:21:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-24 20:15:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-03 21:13:55 +01:00
|
|
|
|
|
|
|
if (hotkey.message_view_only && gear_menu.is_open()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-24 20:15:51 +02:00
|
|
|
|
2020-09-22 19:19:40 +02:00
|
|
|
if (overlays.settings_open() && !popovers.user_info_popped()) {
|
2017-04-04 19:59:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "show_shortcuts") {
|
2017-09-07 03:01:17 +02:00
|
|
|
overlays.close_active();
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-12 20:58:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if ((event_name === "up_arrow" || event_name === "down_arrow") && overlays.streams_open()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
return stream_settings_ui.switch_rows(event_name);
|
2017-04-04 21:04:59 +02:00
|
|
|
}
|
2012-11-30 00:43:41 +01:00
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
if (event_name === "up_arrow" && list_util.inside_list(e)) {
|
|
|
|
list_util.go_up(e);
|
|
|
|
return true;
|
2017-03-03 17:41:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
if (event_name === "down_arrow" && list_util.inside_list(e)) {
|
|
|
|
list_util.go_down(e);
|
|
|
|
return true;
|
2016-09-28 01:19:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 19:16:51 +01:00
|
|
|
if (menu_dropdown_hotkeys.has(event_name) && handle_popover_events(event_name)) {
|
|
|
|
return true;
|
2013-07-11 23:06:58 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2020-12-22 11:26:39 +01:00
|
|
|
if (event_name === "backspace" && $("#compose-send-button").is(":focus")) {
|
|
|
|
// Ignore Backspace; don't navigate back a page.
|
|
|
|
return true;
|
2017-03-11 22:05:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "narrow_to_compose_target") {
|
2018-11-30 00:20:10 +01:00
|
|
|
narrow.to_compose_target();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-12 15:02:14 +01:00
|
|
|
// Process hotkeys specially when in an input, select, textarea, or send button
|
2021-02-28 01:06:02 +01:00
|
|
|
if (processing_text()) {
|
2020-08-11 02:09:14 +02:00
|
|
|
// Note that there is special handling for Enter/Esc too, but
|
2017-03-13 17:01:09 +01:00
|
|
|
// we handle this in other functions.
|
2014-01-14 17:57:34 +01:00
|
|
|
|
2020-07-15 01:29:15 +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;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
2021-04-01 07:33:40 +02:00
|
|
|
(event_name === "up_arrow" ||
|
|
|
|
event_name === "down_arrow" ||
|
|
|
|
event_name === "page_up" ||
|
|
|
|
event_name === "page_down" ||
|
|
|
|
event_name === "home" ||
|
|
|
|
event_name === "end") &&
|
2020-07-15 00:34:28 +02:00
|
|
|
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
|
2013-06-07 18:37:18 +02:00
|
|
|
} else {
|
2021-05-09 23:36:58 +02:00
|
|
|
switch (event_name) {
|
|
|
|
case "page_up":
|
|
|
|
$(":focus").caret(0).animate({scrollTop: 0}, "fast");
|
|
|
|
return true;
|
|
|
|
case "page_down": {
|
|
|
|
// so that it always goes to the end of the text box.
|
|
|
|
const height = $(":focus")[0].scrollHeight;
|
|
|
|
$(":focus")
|
|
|
|
.caret(Number.POSITIVE_INFINITY)
|
|
|
|
.animate({scrollTop: height}, "fast");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "search_with_k":
|
|
|
|
// Do nothing; this allows one to use Ctrl+K inside compose.
|
|
|
|
break;
|
|
|
|
case "star_message":
|
|
|
|
// Do nothing; this allows one to use Ctrl+S inside compose.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Let the browser handle the key normally.
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-07 18:37:18 +02:00
|
|
|
}
|
2012-10-09 19:50:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02: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()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.toggle_view(event_name);
|
2017-03-23 06:02:01 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02: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()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.toggle_view(event_name);
|
2017-03-23 06:02:01 +01:00
|
|
|
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()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (event_name === "view_selected_stream" && overlays.streams_open()) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.view_stream();
|
2017-10-21 19:21:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-28 21:58:33 +02:00
|
|
|
if (
|
|
|
|
event_name === "n_key" &&
|
|
|
|
overlays.streams_open() &&
|
|
|
|
settings_data.user_can_create_streams()
|
|
|
|
) {
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.open_create_stream();
|
2017-10-21 19:21:44 +02:00
|
|
|
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) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "narrow_private":
|
|
|
|
return do_narrow_action((target, opts) => {
|
|
|
|
narrow.by("is", "private", opts);
|
|
|
|
});
|
|
|
|
case "query_streams":
|
|
|
|
stream_list.initiate_search();
|
|
|
|
return true;
|
|
|
|
case "query_users":
|
|
|
|
activity.initiate_search();
|
|
|
|
return true;
|
|
|
|
case "search":
|
|
|
|
case "search_with_k":
|
|
|
|
search.initiate_search();
|
|
|
|
return true;
|
|
|
|
case "gear_menu":
|
|
|
|
gear_menu.open();
|
|
|
|
return true;
|
|
|
|
case "show_shortcuts": // Show keyboard shortcuts page
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.go_to_location("keyboard-shortcuts");
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
|
|
|
case "stream_cycle_backward":
|
|
|
|
narrow.stream_cycle_backward();
|
|
|
|
return true;
|
|
|
|
case "stream_cycle_forward":
|
|
|
|
narrow.stream_cycle_forward();
|
|
|
|
return true;
|
|
|
|
case "n_key":
|
|
|
|
narrow.narrow_to_next_topic();
|
|
|
|
return true;
|
|
|
|
case "p_key":
|
|
|
|
narrow.narrow_to_next_pm_string();
|
|
|
|
return true;
|
2020-07-05 12:19:09 +02:00
|
|
|
case "open_recent_topics":
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.go_to_location("#recent_topics");
|
2020-07-05 12:19:09 +02:00
|
|
|
return true;
|
2020-09-20 10:14:24 +02:00
|
|
|
case "all_messages":
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.go_to_location("#all_messages");
|
2020-09-20 10:14:24 +02:00
|
|
|
return true;
|
2020-07-05 12:19:09 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 02:58:18 +02:00
|
|
|
// Shortcuts that are useful with an empty message feed, like opening compose.
|
2020-07-05 12:19:09 +02:00
|
|
|
switch (event_name) {
|
2021-03-18 13:57:28 +01: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;
|
2020-07-05 12:19:09 +02:00
|
|
|
case "compose": // 'c': compose
|
|
|
|
compose_actions.start("stream", {trigger: "compose_hotkey"});
|
|
|
|
return true;
|
|
|
|
case "compose_private_message":
|
|
|
|
compose_actions.start("private", {trigger: "compose_hotkey"});
|
|
|
|
return true;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "open_drafts":
|
2021-05-18 03:48:34 +02:00
|
|
|
browser_history.go_to_location("drafts");
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
|
|
|
case "C_deprecated":
|
2021-06-15 16:17:28 +02:00
|
|
|
deprecated_feature_notice.maybe_show_deprecation_notice("C");
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
|
|
|
case "star_deprecated":
|
2021-06-15 16:17:28 +02:00
|
|
|
deprecated_feature_notice.maybe_show_deprecation_notice("*");
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
2012-09-21 22:35:32 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 13:57:28 +01:00
|
|
|
// We don't want hotkeys below this to work when recent topics is
|
|
|
|
// open. These involve hotkeys that can only be performed on a message.
|
2021-06-10 14:18:46 +02:00
|
|
|
if (recent_topics_util.is_visible()) {
|
2021-03-18 13:57:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current.empty()) {
|
2013-07-24 22:51:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-05 02:58:18 +02:00
|
|
|
// Shortcuts for navigation and other applications that require a
|
|
|
|
// nonempty message feed but do not depend on the selected message.
|
2013-07-24 22:51:13 +02:00
|
|
|
switch (event_name) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "down_arrow":
|
|
|
|
case "vim_down":
|
|
|
|
navigate.down(true); // with_centering
|
|
|
|
return true;
|
|
|
|
case "up_arrow":
|
|
|
|
case "vim_up":
|
|
|
|
navigate.up();
|
|
|
|
return true;
|
|
|
|
case "home":
|
|
|
|
navigate.to_home();
|
|
|
|
return true;
|
|
|
|
case "end":
|
|
|
|
case "G_end":
|
|
|
|
navigate.to_end();
|
|
|
|
return true;
|
|
|
|
case "page_up":
|
|
|
|
case "vim_page_up":
|
|
|
|
case "shift_spacebar":
|
|
|
|
navigate.page_up();
|
|
|
|
return true;
|
|
|
|
case "page_down":
|
|
|
|
case "vim_page_down":
|
|
|
|
case "spacebar":
|
|
|
|
navigate.page_down();
|
|
|
|
return true;
|
2021-04-05 02:58:18 +02:00
|
|
|
case "copy_with_c":
|
|
|
|
copy_and_paste.copy_handler();
|
|
|
|
return true;
|
2013-07-24 22:51:13 +02:00
|
|
|
}
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
const msg = message_lists.current.selected_message();
|
2013-08-08 00:03:28 +02:00
|
|
|
// Shortcuts that operate on a message
|
|
|
|
switch (event_name) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "message_actions":
|
|
|
|
return popovers.open_message_menu(msg);
|
|
|
|
case "star_message":
|
|
|
|
message_flags.toggle_starred_and_update_server(msg);
|
|
|
|
return true;
|
|
|
|
case "narrow_by_recipient":
|
|
|
|
return do_narrow_action(narrow.by_recipient);
|
|
|
|
case "narrow_by_topic":
|
|
|
|
return do_narrow_action(narrow.by_topic);
|
|
|
|
case "respond_to_author": // 'R': respond to author
|
|
|
|
compose_actions.respond_to_message({reply_type: "personal", trigger: "hotkey pm"});
|
|
|
|
return true;
|
|
|
|
case "compose_reply_with_mention": // '@': respond to message with mention to author
|
|
|
|
compose_actions.reply_with_mention({trigger: "hotkey"});
|
|
|
|
return true;
|
|
|
|
case "show_lightbox":
|
|
|
|
lightbox.show_from_selected_message();
|
|
|
|
return true;
|
|
|
|
case "show_sender_info":
|
|
|
|
popovers.show_sender_info();
|
|
|
|
return true;
|
|
|
|
case "toggle_reactions_popover": // ':': open reactions to message
|
|
|
|
reactions.open_reactions_popover();
|
|
|
|
return true;
|
2020-07-15 00:34:28 +02:00
|
|
|
case "thumbs_up_emoji": {
|
|
|
|
// '+': reacts with thumbs up emoji on selected message
|
|
|
|
// Use canonical name.
|
2020-07-15 02:14:03 +02:00
|
|
|
const thumbs_up_emoji_code = "1f44d";
|
|
|
|
const canonical_name = emoji.get_emoji_name(thumbs_up_emoji_code);
|
|
|
|
reactions.toggle_emoji_reaction(msg.id, canonical_name);
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-03 12:28:09 +01:00
|
|
|
case "toggle_topic_mute":
|
2021-07-08 20:36:52 +02:00
|
|
|
muted_topics_ui.toggle_topic_mute(msg);
|
2020-07-15 02:14:03 +02:00
|
|
|
return true;
|
|
|
|
case "toggle_message_collapse":
|
|
|
|
condense.toggle_collapse(msg);
|
|
|
|
return true;
|
|
|
|
case "compose_quote_reply": // > : respond to selected message with quote
|
|
|
|
compose_actions.quote_and_reply({trigger: "hotkey"});
|
|
|
|
return true;
|
|
|
|
case "edit_message": {
|
2021-03-30 02:21:21 +02:00
|
|
|
const row = message_lists.current.get_row(msg.id);
|
2020-07-15 02:14:03 +02:00
|
|
|
message_edit.start(row);
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-25 23:55:37 +02:00
|
|
|
}
|
2013-08-08 00:03:28 +02:00
|
|
|
|
2012-09-21 22:35:32 +02:00
|
|
|
return false;
|
2021-02-28 01:06:02 +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
|
2020-08-11 02:09:14 +02:00
|
|
|
we want to intercept PgUp/PgDn, Esc, etc, and process them
|
2012-10-03 17:04:43 +02:00
|
|
|
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. */
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_keydown(e) {
|
2018-08-04 08:22:44 +02:00
|
|
|
activity.set_new_user_input(true);
|
2021-02-28 01:06:02 +01:00
|
|
|
const hotkey = get_keydown_hotkey(e);
|
2017-03-13 21:41:28 +01:00
|
|
|
if (!hotkey) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_hotkey(e, hotkey);
|
|
|
|
}
|
2017-03-13 21:41:28 +01:00
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(document).on("keydown", (e) => {
|
2021-02-28 01:06:02 +01:00
|
|
|
if (process_keydown(e)) {
|
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
|
|
|
});
|
|
|
|
|
2021-02-28 01:06:02 +01:00
|
|
|
export function process_keypress(e) {
|
|
|
|
const hotkey = get_keypress_hotkey(e);
|
2017-03-13 21:41:28 +01:00
|
|
|
if (!hotkey) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-28 01:06:02 +01:00
|
|
|
return process_hotkey(e, hotkey);
|
|
|
|
}
|
2017-03-13 21:41:28 +01:00
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(document).on("keypress", (e) => {
|
2021-02-28 01:06:02 +01:00
|
|
|
if (process_keypress(e)) {
|
2017-03-13 21:41:28 +01:00
|
|
|
e.preventDefault();
|
2012-11-29 19:55:08 +01:00
|
|
|
}
|
2012-09-21 22:35:32 +02:00
|
|
|
});
|