Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
keyboard-shortcuts: Simplify `adjust_mac_shortcuts`.
Lauryn Menard04fd1c8773e3
diff --git a/frontend_tests/node_tests/common.js b/frontend_tests/node_tests/common.jsindex ab992e5d58e..28310c05cd3 100644--- a/frontend_tests/node_tests/common.js+++ b/frontend_tests/node_tests/common.js@@ -89,9 +89,7 @@ run_test("adjust_mac_shortcuts non-mac", ({override}) => {common.adjust_mac_shortcuts("selector-that-does-not-exist");});-// Test non-default value of adjust_mac_shortcuts boolean parameter:-// `kbd_elem = false`.-run_test("adjust_mac_shortcuts mac non-defaults", ({override}) => {+run_test("adjust_mac_shortcuts mac", ({override}) => {const keys_to_test_mac = new Map([["Backspace", "Delete"],["Enter", "Return"],@@ -101,64 +99,8 @@ run_test("adjust_mac_shortcuts mac non-defaults", ({override}) => {["PgDn", "↓"],["Ctrl", "⌘"],["Alt", "⌘"],- ["X + Shift", "X + Shift"],- ["⌘ + Return", "⌘ + Return"],- ["Enter or Backspace", "Enter or Backspace"],["#stream_name", "#stream_name"],["Ctrl+K", "Ctrl+K"],- ]);-- const fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);- const inserted_fn_key = "<code>Fn</code> + ";-- override(navigator, "platform", "MacIntel");-- const kbd_element = false;-- const test_items = [];- let key_no = 1;-- for (const [old_key, mac_key] of keys_to_test_mac) {- const test_item = {};- const $stub = $.create("hotkey_" + key_no);- $stub.text(old_key);- assert.equal($stub.hasClass("arrow-key"), false);- if (fn_shortcuts.has(old_key)) {- $stub.before = ($elem) => {- assert.equal($elem, inserted_fn_key);- };- }- test_item.$stub = $stub;- test_item.mac_key = mac_key;- test_item.adds_arrow_key = fn_shortcuts.has(old_key) && kbd_element;- test_items.push(test_item);- key_no += 1;- }-- const children = test_items.map((test_item) => ({to_$: () => test_item.$stub}));-- $.create(".markdown_content", {children});-- common.adjust_mac_shortcuts(".markdown_content", kbd_element);-- for (const test_item of test_items) {- assert.equal(test_item.$stub.text(), test_item.mac_key);- assert.equal(test_item.$stub.hasClass("arrow-key"), test_item.adds_arrow_key);- }-});--// Test default value of adjust_mac_shortcuts boolean parameter:-// `kbd_elem = true`.-run_test("adjust_mac_shortcuts mac defaults", ({override}) => {- const keys_to_test_mac = new Map([- ["Backspace", "Delete"],- ["Enter", "Return"],- ["Home", "←"],- ["End", "→"],- ["PgUp", "↑"],- ["PgDn", "↓"],- ["Ctrl", "⌘"],- ["Alt", "⌘"],["[", "["],["X", "X"],]);@@ -168,8 +110,6 @@ run_test("adjust_mac_shortcuts mac defaults", ({override}) => {override(navigator, "platform", "MacIntel");- const kbd_element = true;-const test_items = [];let key_no = 1;@@ -185,16 +125,16 @@ run_test("adjust_mac_shortcuts mac defaults", ({override}) => {}test_item.$stub = $stub;test_item.mac_key = mac_key;- test_item.adds_arrow_key = fn_shortcuts.has(old_key) && kbd_element;+ test_item.adds_arrow_key = fn_shortcuts.has(old_key);test_items.push(test_item);key_no += 1;}const children = test_items.map((test_item) => ({to_$: () => test_item.$stub}));- $.create(".hotkeys_table kbd", {children});+ $.create(".markdown kbd", {children});- common.adjust_mac_shortcuts(".hotkeys_table kbd");+ common.adjust_mac_shortcuts(".markdown kbd");for (const test_item of test_items) {assert.equal(test_item.$stub.text(), test_item.mac_key);diff --git a/static/js/common.ts b/static/js/common.tsindex a3b4b34db81..a9c4b54e9b3 100644--- a/static/js/common.ts+++ b/static/js/common.ts@@ -48,7 +48,7 @@ export function has_mac_keyboard(): boolean {return /mac/i.test(navigator.platform);}-export function adjust_mac_shortcuts(key_elem_class: string, kbd_elem = true): void {+export function adjust_mac_shortcuts(kbd_elem_class: string): void {if (!has_mac_keyboard()) {return;}@@ -66,25 +66,12 @@ export function adjust_mac_shortcuts(key_elem_class: string, kbd_elem = true): vconst fn_shortcuts = new Set(["Home", "End", "PgUp", "PgDn"]);- $(key_elem_class).each(function () {+ $(kbd_elem_class).each(function () {let key_text = $(this).text();- // There may be matches to `key_elem_class` that are not- // keyboard shortcuts. Since keyboard shortcuts should be an- // exact match to the text on a physical key of a keyboard,- // none of which have spaces, we check and return early for- // any matched element's text that contains whitespace.- if (/\s/.test(key_text)) {- return;- }-if (fn_shortcuts.has(key_text)) {- if (kbd_elem) {- $(this).before("<kbd>Fn</kbd> + ");- $(this).addClass("arrow-key");- } else {- $(this).before("<code>Fn</code> + ");- }+ $(this).before("<kbd>Fn</kbd> + ");+ $(this).addClass("arrow-key");}const replace_key = keys_map.get(key_text);