static/js/common: Extract function adjust_mac_shortcuts.

This commit is contained in:
Yashashvi Dave
2019-06-07 14:33:13 +05:30
committed by Tim Abbott
parent 251ed94bfc
commit fed64cc59d
5 changed files with 96 additions and 53 deletions

View File

@@ -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());
});
});

View File

@@ -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;
}());

View File

@@ -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;
};

View File

@@ -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, '<code>' + key + '</code>');
});
$(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");

View File

@@ -12,6 +12,7 @@
<tbody>
<tr>
<td>*italic*</td>
<td class="rendered_markdown"><i>italic</i></td>
</tr>