Skip to content

Commit

Permalink
Improved global shortcut handler and made fuzzyprompt use it too.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Atkins-Turkish committed Jun 22, 2016
1 parent 1a3e6cd commit 0ebcd7b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 59 deletions.
43 changes: 0 additions & 43 deletions ide/static/ide/js/cloudpebble.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,46 +184,3 @@ CloudPebble.Utils = {
return interpolate(ngettext("%s second", "%s seconds", n), [n]);
}
};

CloudPebble.GlobalShortcuts = (function() {
var make_shortcut_checker = function (command) {
if (!(command.indexOf('-') > -1)) {
command = _.findKey(CodeMirror.keyMap.default, _.partial(_.isEqual, command));
}
var split = command.split('-');
var modifier = ({
'ctrl': 'ctrlKey',
'cmd': 'metaKey'
})[split[0].toLowerCase()];
return function (e) {
return (e[modifier] && String.fromCharCode(e.keyCode) == split[1]);
}
};


var global_shortcuts = {};

$(document).keydown(function (e) {
if (!e.isDefaultPrevented()) {
_.each(global_shortcuts, function (shortcut) {
if (shortcut.checker(e)) {
shortcut.func(e);
e.preventDefault();
}
});
}
});

return {
SetShortcutHandlers: function (shortcuts) {
var new_shortcuts = _.mapObject(shortcuts, function (func, key) {
return {
checker: make_shortcut_checker(key),
func: func
};

});
_.extend(global_shortcuts, new_shortcuts);
}
}
})();
13 changes: 6 additions & 7 deletions ide/static/ide/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ CloudPebble.Editor = (function() {
settings.extraKeys = {'Ctrl-Space': 'autocomplete'};
}
if(!is_js && USER_SETTINGS.autocomplete !== 0) {
settings.extraKeys['Tab'] = function() {
settings.extraKeys['Tab'] = function selectNextArgument() {
var marks = code_mirror.getAllMarks();
var cursor = code_mirror.getCursor();
var closest = null;
Expand Down Expand Up @@ -130,7 +130,7 @@ CloudPebble.Editor = (function() {
if(USER_SETTINGS.use_spaces) {
var spaces = Array(settings.indentUnit + 1).join(' ');
var oldTab = settings.extraKeys['Tab'];
settings.extraKeys['Tab'] = function(cm) {
settings.extraKeys['Tab'] = function indentMoreOrSelectNextArgument(cm) {
// If we already overrode tab, check that one.
if(oldTab) {
if(oldTab(cm) !== CodeMirror.Pass) {
Expand Down Expand Up @@ -180,10 +180,10 @@ CloudPebble.Editor = (function() {
return CodeMirror.Pass;
};
}
settings.extraKeys['Cmd-/'] = function(cm) {
settings.extraKeys['Cmd-/'] = function toggleComment(cm) {
CodeMirror.commands.toggleComment(cm);
};
settings.extraKeys['Ctrl-/'] = function(cm) {
settings.extraKeys['Ctrl-/'] = function toggleComment(cm) {
CodeMirror.commands.toggleComment(cm);
};
if(is_js) {
Expand Down Expand Up @@ -227,10 +227,9 @@ CloudPebble.Editor = (function() {

var help_shortcut = /Mac/.test(navigator.platform) ? 'Shift-Cmd-Ctrl-/' : 'Shift-Ctrl-Alt-/';

settings.extraKeys[help_shortcut] = function(cm) {
settings.extraKeys[help_shortcut] = function lookupFunction(cm) {
var pos = cm.cursorCoords();
var token = code_mirror.getTokenAt(cm.getCursor());

create_popover(cm, token.string, pos.left, pos.top);
};

Expand Down Expand Up @@ -733,7 +732,7 @@ CloudPebble.Editor = (function() {
cm.showHint({hint: CloudPebble.Editor.Autocomplete.complete, completeSingle: false});
};
CodeMirror.commands.save = function(cm) {
cm.cloudpebble_save().catch(alert);;
cm.cloudpebble_save().catch(alert);
};
CodeMirror.commands.saveAll = function(cm) {
save_all().catch(alert);
Expand Down
21 changes: 12 additions & 9 deletions ide/static/ide/js/fuzzyprompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ CloudPebble.FuzzyPrompt = (function() {
var modifier = /Mac/.test(navigator.platform) ? 'metaKey' : 'ctrlKey';

// Register ctrl-p and ctrl-shift-p
$(document).keydown(function(e) {
if ((e[modifier]) && e.keyCode == 80) {
e.preventDefault();
if (e.shiftKey) {
input.attr('placeholder', gettext("Enter Command"));
show_prompt('commands');
}
else if (!e.shiftKey) {
CloudPebble.GlobalShortcuts.SetShortcutHandlers({
'PlatformCmd-P': {
func: function () {
input.attr('placeholder', gettext("Search Files"));
show_prompt('files');
}
},
name: gettext("Find File")
},
'Shift-PlatformCmd-P': {
func: function () {
input.attr('placeholder', gettext("Enter Command"));
show_prompt('commands');
},
name: gettext("Find Action")
}
});

Expand Down
53 changes: 53 additions & 0 deletions ide/static/ide/js/global_shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CloudPebble.GlobalShortcuts = (function () {
var global_shortcuts = {};

$(document).keydown(function (e) {
if (!e.isDefaultPrevented()) {
var shortcut = global_shortcuts[CodeMirror.keyName(e)];
if (shortcut) {
e.preventDefault();
shortcut.func(e);
}
}
});

function shortcut_for_command(command) {
// If the command is a name like "save", get they key-combo from CodeMirror
if (!(command.indexOf('-') > -1)) {
command = _.findKey(CodeMirror.keyMap.default, _.partial(_.isEqual, command));
}

// If any of the shortcut items are "platformcmd", convert them to 'Ctrl' or 'Cmd' depending on the platform.
function key_for_platform(name) {
if (name.toLowerCase() == "platformcmd") {
return /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
} else return name;
}

return command.split('-').map(key_for_platform).join('-');
}

return {
/** Add or replace global shortcuts
*
* @param {Object} shortcuts The keys of this object must be strings which represent keyboard shortcuts.
* They can Codemirror-compatible shortcut descriptors e.g. "Shift-Cmd-V", or they can reference CodeMirror
* commands such as "Save".
* The values should be objects which have a descriptive "name" property, and also either have a "func" property
* or be functions themselves. For example, a named function fully satisfies the requirements, as does an object
* such as {name: "My Function", func: my_anonymous_function}
*/
SetShortcutHandlers: function (shortcuts) {
_.each(shortcuts, function (descriptor, key) {
var shortcut = shortcut_for_command(key);
global_shortcuts[shortcut] = {
name: descriptor.name ? descriptor.name : key,
func: _.isFunction(descriptor) ? descriptor : descriptor.func
};
});
},
GetShortcuts: function() {
return global_shortcuts;
}
}
})();
1 change: 1 addition & 0 deletions ide/templates/ide/project.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ <h3>{% trans 'Compass and Accelerometer' %}</h3>
<script src="/ide/jsi18n/"></script>
<script src="{% static 'ide/js/csrf.js' %}" type="text/javascript"></script>
<script src="{% static 'ide/js/cloudpebble.js' %}" type="text/javascript"></script>
<script src="{% static 'ide/js/global_shortcuts.js' %}" type="text/javascript"></script>
<script src="{% static 'ide/js/sidebar.js' %}" type="text/javascript"></script>
<script src="{% static 'ide/js/radix.js' %}" type="text/javascript"></script>
<script src="{% static 'ide/js/ycm.js' %}" type="text/javascript"></script>
Expand Down

0 comments on commit 0ebcd7b

Please sign in to comment.