diff --git a/frontend_tests/node_tests/common.js b/frontend_tests/node_tests/common.js index aac0409bb6..60082d984e 100644 --- a/frontend_tests/node_tests/common.js +++ b/frontend_tests/node_tests/common.js @@ -17,6 +17,15 @@ $(input).val = (arg) => { zrequire('common'); +function get_key_stub_html(key_text, expected_key, obj_name) { + const key_stub = $.create(obj_name); + key_stub.text(key_text); + key_stub.expected_key = function () { + return expected_key; + }; + return key_stub; +} + run_test('basics', () => { common.autofocus('#home'); assert($('#home').is_focused()); @@ -48,3 +57,60 @@ run_test('copy_data_attribute_value', () => { }; common.copy_data_attribute_value(elem, 'admin-emails'); }); + +run_test('adjust_mac_shortcuts', () => { + const keys_to_test_mac = new Map([ + ['Backspace', 'Delete'], + ['Enter', 'Return'], + ['Home', 'Fn + ←'], + ['End', 'Fn + →'], + ['PgUp', 'Fn + ↑'], + ['PgDn', 'Fn + ↓'], + ['X + Shift', 'X + Shift'], + ['⌘ + Return', '⌘ + Return'], + ['Enter or Backspace', "Return or Delete"], + ]); + const keys_to_test_non_mac = new Map([ + ['Backspace', 'Backspace'], + ['Enter', 'Enter'], + ['Home', 'Home'], + ['End', 'End'], + ['PgUp', 'PgUp'], + ['PgDn', 'PgDn'], + ['X + Shift', 'X + Shift'], + ['⌘ + Return', '⌘ + Return'], + ]); + + var key_no; + var keys_elem_list = []; + + common.has_mac_keyboard = function () { return false; }; + key_no = 1; + keys_to_test_non_mac.forEach(function (value, key) { + keys_elem_list.push(get_key_stub_html(key, value, "hotkey_non_mac_" + key_no)); + key_no += 1; + }); + + common.adjust_mac_shortcuts(".markdown_content"); + keys_elem_list.forEach(function (key_elem) { + assert(key_elem.text(), key_elem.expected_key()); + }); + + keys_elem_list = []; + key_no = 1; + common.has_mac_keyboard = function () { return true; }; + keys_to_test_mac.forEach(function (value, key) { + keys_elem_list.push(get_key_stub_html(key, value, "hotkey_" + key_no)); + key_no += 1; + }); + + $(".markdown_content").each = (f) => { + for (var key_id = 0; key_id < keys_elem_list.length; key_id += 1) { + f.call(keys_elem_list[key_id]); + } + }; + common.adjust_mac_shortcuts(".markdown_content"); + keys_elem_list.forEach(function (key_elem) { + assert.equal(key_elem.text(), key_elem.expected_key()); + }); +}); diff --git a/static/js/common.js b/static/js/common.js index 87ecb78ce3..dd4b0d4a75 100644 --- a/static/js/common.js +++ b/static/js/common.js @@ -104,6 +104,33 @@ exports.has_mac_keyboard = function () { return /Mac/i.test(navigator.platform); }; +exports.adjust_mac_shortcuts = function (key_elem_class) { + if (!exports.has_mac_keyboard()) { + return; + } + + var keys_map = new Map([ + ['Backspace', 'Delete'], + ['Enter', 'Return'], + ['Home', 'Fn + ←'], + ['End', 'Fn + →'], + ['PgUp', 'Fn + ↑'], + ['PgDn', 'Fn + ↓'], + ]); + + $(key_elem_class).each(function () { + var key_text = $(this).text(); + var keys = key_text.match(/[^\s\+]+/g); + + _.each(keys, function (key) { + if (keys_map.get(key)) { + key_text = key_text.replace(key, keys_map.get(key)); + } + }); + $(this).text(key_text); + }); +}; + return exports; }()); diff --git a/static/js/info_overlay.js b/static/js/info_overlay.js index 3a9bb8b5a0..a27e569a0c 100644 --- a/static/js/info_overlay.js +++ b/static/js/info_overlay.js @@ -2,25 +2,6 @@ var info_overlay = (function () { var exports = {}; -function adjust_mac_shortcuts() { - var keys_map = [ - ['Backspace', 'Delete'], - ['Enter', 'Return'], - ['Home', 'Fn + Left'], - ['End', 'Fn + Right'], - ['PgUp', 'Fn + Up'], - ['PgDn', 'Fn + Down'], - ]; - - $(".hotkeys_table").each(function () { - var html = $(this).html(); - keys_map.forEach(function (pair) { - html = html.replace(new RegExp(pair[0]), pair[1]); - }); - $(this).html(html); - }); -} - // Make it explicit that our toggler is undefined until // set_up_toggler is called. exports.toggler = undefined; @@ -63,9 +44,7 @@ exports.set_up_toggler = function () { $(".informational-overlays .overlay-tabs").append(elem); - if (common.has_mac_keyboard()) { - adjust_mac_shortcuts(); - } + common.adjust_mac_shortcuts(".hotkeys_table .hotkey kbd"); exports.toggler = toggler; }; diff --git a/static/js/portico/help.js b/static/js/portico/help.js index ec9adba2f9..c64559c99c 100644 --- a/static/js/portico/help.js +++ b/static/js/portico/help.js @@ -38,34 +38,6 @@ function highlight_current_article() { article.addClass('highlighted'); } -function adjust_mac_shortcuts() { - var keys_map = new Map([ - ['Backspace', 'Delete'], - ['Enter', 'Return'], - ['Home', 'Fn + ←'], - ['End', 'Fn + →'], - ['PgUp', 'Fn + ↑'], - ['PgDn', 'Fn + ↓'], - ]); - - $(".markdown .content code").each(function () { - var text = $(this).text(); - - if (!keys_map.has(text)) { - return; - } - - var key_string = keys_map.get(text); - var keys = key_string.match(/[^\s\+]+/g); - - _.each(keys, function (key) { - key_string = key_string.replace(key, '' + key + ''); - }); - - $(this).replaceWith(key_string); - }); -} - function render_code_sections() { $(".code-section").each(function () { activate_correct_tab($(this)); @@ -74,9 +46,7 @@ function render_code_sections() { highlight_current_article(); - if (common.has_mac_keyboard()) { - adjust_mac_shortcuts(); - } + common.adjust_mac_shortcuts(".markdown .content code"); $("table").each(function () { $(this).addClass("table table-striped"); diff --git a/templates/zerver/app/markdown_help.html b/templates/zerver/app/markdown_help.html index 0027475c22..707a0f8eee 100644 --- a/templates/zerver/app/markdown_help.html +++ b/templates/zerver/app/markdown_help.html @@ -12,6 +12,7 @@ + *italic* italic