etiquette/frontends/etiquette_flask/static/js/hotkeys.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-07-23 02:55:29 +00:00
var hotkeys = {};
2018-07-23 02:55:29 +00:00
hotkeys.HOTKEYS = {};
hotkeys.hotkey_identifier =
function hotkey_identifier(key, ctrlKey, shiftKey, altKey)
{
// Return the string that will represent this hotkey in the dictionary.
return key.toLowerCase() + "." + (ctrlKey & 1) + "." + (shiftKey & 1) + "." + (altKey & 1);
}
2018-07-23 02:55:29 +00:00
hotkeys.hotkey_human =
function hotkey_human(key, ctrlKey, shiftKey, altKey)
{
// Return the string that will be displayed to the user to represent this hotkey.
var mods = [];
if (ctrlKey) { mods.push("Ctrl"); }
if (shiftKey) { mods.push("Shift"); }
if (altKey) { mods.push("Alt"); }
mods = mods.join("+");
if (mods) { mods = mods + "+"; }
return mods + key.toUpperCase();
}
2018-07-23 02:55:29 +00:00
hotkeys.register_hotkey =
function register_hotkey(hotkey, action, description)
{
if (! Array.isArray(hotkey))
{
hotkey = hotkey.split(/\s+/g);
}
var key = hotkey.pop();
modifiers = hotkey.map(word => word.toLocaleLowerCase());
var ctrlKey = modifiers.includes("control") || modifiers.includes("ctrl");
var shiftKey = modifiers.includes("shift");
var altKey = modifiers.includes("alt");
var identifier = hotkeys.hotkey_identifier(key, ctrlKey, shiftKey, altKey);
var human = hotkeys.hotkey_human(key, ctrlKey, shiftKey, altKey);
2018-07-23 02:55:29 +00:00
hotkeys.HOTKEYS[identifier] = {"action": action, "human": human, "description": description}
}
2018-07-23 02:55:29 +00:00
hotkeys.should_prevent_hotkey =
function should_prevent_hotkey(event)
{
if (event.target.tagName == "INPUT" && event.target.type == "checkbox")
{
return false;
}
else
{
2018-07-23 02:12:08 +00:00
return common.INPUT_TYPES.has(event.target.tagName);
}
}
hotkeys.show_all_hotkeys =
function show_all_hotkeys()
{
// Display an Alert with a list of all the hotkeys.
var lines = [];
2018-07-23 02:55:29 +00:00
for (var identifier in hotkeys.HOTKEYS)
{
2018-07-23 02:55:29 +00:00
var line = hotkeys.HOTKEYS[identifier]["human"] + " : " + hotkeys.HOTKEYS[identifier]["description"];
lines.push(line);
}
lines = lines.join("\n");
alert(lines);
}
hotkeys.hotkeys_listener =
function hotkeys_listener(event)
{
if (hotkeys.should_prevent_hotkey(event)) { return; }
identifier = hotkeys.hotkey_identifier(event.key, event.ctrlKey, event.shiftKey, event.altKey);
console.log(identifier);
if (identifier in hotkeys.HOTKEYS)
{
hotkeys.HOTKEYS[identifier]["action"]();
event.preventDefault();
}
}
window.addEventListener("keydown", hotkeys.hotkeys_listener);
hotkeys.register_hotkey("/", hotkeys.show_all_hotkeys, "Show hotkeys.");