Skip to content

Instantly share code, notes, and snippets.

@rksm
Created February 17, 2015 08:32
Show Gist options
  • Save rksm/cb1317196a1b58b9162f to your computer and use it in GitHub Desktop.
Save rksm/cb1317196a1b58b9162f to your computer and use it in GitHub Desktop.
bind keys at runtime fix
(function loadUserKeyBindings() {
// user key bindings
try {
var cust = JSON.parse(lively.LocalStorage.get("user-key-bindings"));
ace.ext.keys.addKeyCustomizationLayer("user-key-bindings", cust || {});
var h = ace.require("ace/keyboard/keybinding").KeyBinding.prototype["ace.ext.keys.customized"].detect(function(ea) {
return ea.layerName === "user-key-bindings"; })
var proto = ace.ext.keys.KeyHandlerForCustomizations.prototype
proto.handleKeyboard = proto.handleKeyboard.getOriginal().wrap(function (proceed, data, hashId, keyString, keyCode) {
var cmd = proceed(data, hashId, keyString, keyCode);
if (!cmd || !cmd.command || !cmd.command.startsWith("global:")) return cmd;
var name = cmd.command.replace('global:', "");
var globalCommand = lively.ide.commands.byName[name];
if (!globalCommand) return cmd;
if (!data.editor.commands[name]) {
data.editor.commands.addCommand({
name: name, exec: function(ed, args) { lively.ide.commands.exec(name, args); },
})
}
return lively.lang.obj.merge(cmd, {command: name});
});
} catch (e) {
console.error("Error setting ace user keys:\n" + e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment