Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

emoji_picker: Replace deprecated `e.which` with `e.key`.

Prakhar Pratyush46b6436b19a3
diff --git a/web/src/common.ts b/web/src/common.ts
index 2ad5cfc2463..ca1bd70982c 100644
--- a/web/src/common.ts
+++ b/web/src/common.ts
@@ -98,6 +98,13 @@ export function adjust_shift_hotkey(hotkeys: string[]): boolean {
return false;
}
+export function is_printable_ascii(key: string): boolean {
+ // ASCII printable characters (character code 32-126) -> " " to "~".
+ // It includes letters, digits, punctuation marks, and a few
+ // miscellaneous symbols.
+ return key.length === 1 && key >= " " && key <= "~";
+}
+
// See https://zulip.readthedocs.io/en/latest/development/authentication.html#password-form-implementation
// for design details on this feature.
function set_password_toggle_label(
diff --git a/web/src/emoji_picker.ts b/web/src/emoji_picker.ts
index 6174f03576c..3bcab3a1fa6 100644
--- a/web/src/emoji_picker.ts
+++ b/web/src/emoji_picker.ts
@@ -11,6 +11,7 @@ import render_emoji_popover_search_results from "../templates/popovers/emoji/emo
import render_emoji_showcase from "../templates/popovers/emoji/emoji_showcase.hbs";
import * as blueslip from "./blueslip.ts";
+import * as common from "./common.ts";
import * as compose_ui from "./compose_ui.ts";
import * as composebox_typeahead from "./composebox_typeahead.ts";
import * as emoji from "./emoji.ts";
@@ -573,12 +574,12 @@ export function navigate(event_name: string, e?: JQuery.KeyDownEvent): boolean {
function process_keypress(e: JQuery.KeyPressEvent | JQuery.KeyDownEvent): void {
const is_filter_focused = $("#emoji-popover-filter").is(":focus");
- const pressed_key = e.which;
+ const pressed_key = e.key;
if (
!is_filter_focused &&
- // ':' => 58, is a hotkey for toggling reactions popover.
- pressed_key !== 58 &&
- ((pressed_key >= 32 && pressed_key <= 126) || pressed_key === 8)
+ // ":" is a hotkey for toggling reactions popover.
+ pressed_key !== ":" &&
+ (common.is_printable_ascii(pressed_key) || pressed_key === "Backspace")
) {
// Handle only printable characters or Backspace.
e.preventDefault();
@@ -588,13 +589,11 @@ function process_keypress(e: JQuery.KeyPressEvent | JQuery.KeyDownEvent): void {
const old_query = $emoji_filter.val()!;
let new_query = "";
- if (pressed_key === 8) {
- // Handles Backspace.
+ if (pressed_key === "Backspace") {
new_query = old_query.slice(0, -1);
} else {
// Handles any printable character.
- const key_str = String.fromCodePoint(e.which);
- new_query = old_query + key_str;
+ new_query = old_query + pressed_key;
}
$emoji_filter.val(new_query);
@@ -653,7 +652,7 @@ function register_popover_events($popover: JQuery): void {
// key separately. Firefox fires `keypress` event for Backspace
// key but chrome doesn't so we need to trigger the logic for
// handling Backspace in `keydown` event which is fired by both.
- if (e.which === 8) {
+ if (e.key === "Backspace") {
process_keypress(e);
}
});
diff --git a/web/src/hotkey.js b/web/src/hotkey.js
index e0c4097abaa..a9712b679b5 100644
--- a/web/src/hotkey.js
+++ b/web/src/hotkey.js
@@ -217,13 +217,6 @@ const KEYDOWN_MAPPINGS = {
"Shift+Delete": {name: "delete", message_view_only: false},
};
-function is_printable_ascii(key) {
- // ASCII printable characters (character code 32-126) -> " " to "~".
- // It includes letters, digits, punctuation marks, and a few
- // miscellaneous symbols.
- return key.length === 1 && key >= " " && key <= "~";
-}
-
const KNOWN_NAMED_KEY_ATTRIBUTE_VALUES = new Set([
"Alt",
"ArrowDown",
@@ -327,7 +320,8 @@ export function get_keydown_hotkey(e) {
// the same physical key combination that a QWERTY user would
// use (e.g. 'ф' on Cyrillic maps to 'a' on QWERTY). In such cases,
// we derive the QWERTY equivalent using `e.code` and the `CODE_TO_QWERTY_CHAR` map.
- const use_event_key = is_printable_ascii(e.key) || KNOWN_NAMED_KEY_ATTRIBUTE_VALUES.has(e.key);
+ const use_event_key =
+ common.is_printable_ascii(e.key) || KNOWN_NAMED_KEY_ATTRIBUTE_VALUES.has(e.key);
let key = e.key;
if (!use_event_key) {
const code = `${e.shiftKey ? "Shift+" : ""}${e.code}`;