Menus automatically learn about keyboard shortcuts.

This commit is contained in:
Thomas Wilburn 2013-09-06 11:24:20 -07:00
parent 83807d4ee2
commit 6ccd0aa737
2 changed files with 60 additions and 7 deletions

View file

@ -12,10 +12,16 @@
margin: 4px 8px;
}
.shortcut {
color: #AAA;
margin-left: 1em;
}
//nested menus (default)
li {
display: block;
display: flex;
padding: 4px 8px;
justify-content: space-between;
&:hover, &.active {
background: #EEE;

View file

@ -1,5 +1,5 @@
define([
"settings!menus",
"settings!menus,keys",
"editor",
"dialog",
"command",
@ -7,6 +7,38 @@ define([
], function(Settings, editor, dialog, command) {
var cfg = Settings.get("menus");
var keys = Settings.get("keys");
var commands = editor.commands.commands;
var findKeyCombo = function(command) {
//check key config
for (var key in keys) {
var action = keys[key];
var verb = action.ace || action.command || action;
if (verb == command) {
var char = key.split("-").pop();
if (/[A-Z]$/.test(char)) {
char = "Shift-" + char.toUpperCase();
} else {
char = char[0].toUpperCase() + char.substr(1);
}
var prefix = "";
if (key.indexOf("^") > -1) {
prefix += "Ctrl-";
}
if (key.indexOf("M-") > -1) {
prefix += "Alt-";
}
return prefix + char;
}
}
for (var cmd in editor.commands.commands) {
if (cmd == command && editor.commands.commands[cmd].bindKey.win) {
return editor.commands.commands[cmd].bindKey.win.split("|").shift();
}
}
return false;
}
var menubar = document.find(".toolbar");
@ -26,8 +58,14 @@ define([
}
var li = document.createElement("li");
li.innerHTML = entry.label;
if (entry.command) li.setAttribute("command", entry.command);
if (entry.argument) li.setAttribute("argument", entry.argument);
if (entry.command) {
li.setAttribute("command", entry.command);
var shortcut = findKeyCombo(entry.command == "ace:command" ? entry.argument : entry.command);
if (shortcut) {
li.innerHTML += "<span class=shortcut>" + shortcut + "</span>";
}
if (entry.argument) li.setAttribute("argument", entry.argument);
}
if (entry.sub) {
if (depth) {
li.className = "parent";
@ -43,10 +81,19 @@ define([
}
return fragment;
}
var create = function() {
var menuElements = walker(cfg, 0);
menubar.innerHTML = "";
menubar.appendChild(menuElements);
}
var menuElements = walker(cfg, 0);
menubar.innerHTML = "";
menubar.appendChild(menuElements);
command.on("init:startup", create);
command.on("init:restart", function() {
cfg = Settings.get("menus");
keys = Settings.get("keys");
create();
})
menubar.addEventListener("click", function(e) {
menubar.focus();