Skip to content

Commit 0ebcd7b

Browse files
author
Joseph Atkins-Turkish
committed
Improved global shortcut handler and made fuzzyprompt use it too.
1 parent 1a3e6cd commit 0ebcd7b

File tree

5 files changed

+72
-59
lines changed

5 files changed

+72
-59
lines changed

ide/static/ide/js/cloudpebble.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -184,46 +184,3 @@ CloudPebble.Utils = {
184184
return interpolate(ngettext("%s second", "%s seconds", n), [n]);
185185
}
186186
};
187-
188-
CloudPebble.GlobalShortcuts = (function() {
189-
var make_shortcut_checker = function (command) {
190-
if (!(command.indexOf('-') > -1)) {
191-
command = _.findKey(CodeMirror.keyMap.default, _.partial(_.isEqual, command));
192-
}
193-
var split = command.split('-');
194-
var modifier = ({
195-
'ctrl': 'ctrlKey',
196-
'cmd': 'metaKey'
197-
})[split[0].toLowerCase()];
198-
return function (e) {
199-
return (e[modifier] && String.fromCharCode(e.keyCode) == split[1]);
200-
}
201-
};
202-
203-
204-
var global_shortcuts = {};
205-
206-
$(document).keydown(function (e) {
207-
if (!e.isDefaultPrevented()) {
208-
_.each(global_shortcuts, function (shortcut) {
209-
if (shortcut.checker(e)) {
210-
shortcut.func(e);
211-
e.preventDefault();
212-
}
213-
});
214-
}
215-
});
216-
217-
return {
218-
SetShortcutHandlers: function (shortcuts) {
219-
var new_shortcuts = _.mapObject(shortcuts, function (func, key) {
220-
return {
221-
checker: make_shortcut_checker(key),
222-
func: func
223-
};
224-
225-
});
226-
_.extend(global_shortcuts, new_shortcuts);
227-
}
228-
}
229-
})();

ide/static/ide/js/editor.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ CloudPebble.Editor = (function() {
9797
settings.extraKeys = {'Ctrl-Space': 'autocomplete'};
9898
}
9999
if(!is_js && USER_SETTINGS.autocomplete !== 0) {
100-
settings.extraKeys['Tab'] = function() {
100+
settings.extraKeys['Tab'] = function selectNextArgument() {
101101
var marks = code_mirror.getAllMarks();
102102
var cursor = code_mirror.getCursor();
103103
var closest = null;
@@ -130,7 +130,7 @@ CloudPebble.Editor = (function() {
130130
if(USER_SETTINGS.use_spaces) {
131131
var spaces = Array(settings.indentUnit + 1).join(' ');
132132
var oldTab = settings.extraKeys['Tab'];
133-
settings.extraKeys['Tab'] = function(cm) {
133+
settings.extraKeys['Tab'] = function indentMoreOrSelectNextArgument(cm) {
134134
// If we already overrode tab, check that one.
135135
if(oldTab) {
136136
if(oldTab(cm) !== CodeMirror.Pass) {
@@ -180,10 +180,10 @@ CloudPebble.Editor = (function() {
180180
return CodeMirror.Pass;
181181
};
182182
}
183-
settings.extraKeys['Cmd-/'] = function(cm) {
183+
settings.extraKeys['Cmd-/'] = function toggleComment(cm) {
184184
CodeMirror.commands.toggleComment(cm);
185185
};
186-
settings.extraKeys['Ctrl-/'] = function(cm) {
186+
settings.extraKeys['Ctrl-/'] = function toggleComment(cm) {
187187
CodeMirror.commands.toggleComment(cm);
188188
};
189189
if(is_js) {
@@ -227,10 +227,9 @@ CloudPebble.Editor = (function() {
227227

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

230-
settings.extraKeys[help_shortcut] = function(cm) {
230+
settings.extraKeys[help_shortcut] = function lookupFunction(cm) {
231231
var pos = cm.cursorCoords();
232232
var token = code_mirror.getTokenAt(cm.getCursor());
233-
234233
create_popover(cm, token.string, pos.left, pos.top);
235234
};
236235

@@ -733,7 +732,7 @@ CloudPebble.Editor = (function() {
733732
cm.showHint({hint: CloudPebble.Editor.Autocomplete.complete, completeSingle: false});
734733
};
735734
CodeMirror.commands.save = function(cm) {
736-
cm.cloudpebble_save().catch(alert);;
735+
cm.cloudpebble_save().catch(alert);
737736
};
738737
CodeMirror.commands.saveAll = function(cm) {
739738
save_all().catch(alert);

ide/static/ide/js/fuzzyprompt.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ CloudPebble.FuzzyPrompt = (function() {
4949
var modifier = /Mac/.test(navigator.platform) ? 'metaKey' : 'ctrlKey';
5050

5151
// Register ctrl-p and ctrl-shift-p
52-
$(document).keydown(function(e) {
53-
if ((e[modifier]) && e.keyCode == 80) {
54-
e.preventDefault();
55-
if (e.shiftKey) {
56-
input.attr('placeholder', gettext("Enter Command"));
57-
show_prompt('commands');
58-
}
59-
else if (!e.shiftKey) {
52+
CloudPebble.GlobalShortcuts.SetShortcutHandlers({
53+
'PlatformCmd-P': {
54+
func: function () {
6055
input.attr('placeholder', gettext("Search Files"));
6156
show_prompt('files');
62-
}
57+
},
58+
name: gettext("Find File")
59+
},
60+
'Shift-PlatformCmd-P': {
61+
func: function () {
62+
input.attr('placeholder', gettext("Enter Command"));
63+
show_prompt('commands');
64+
},
65+
name: gettext("Find Action")
6366
}
6467
});
6568

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
CloudPebble.GlobalShortcuts = (function () {
2+
var global_shortcuts = {};
3+
4+
$(document).keydown(function (e) {
5+
if (!e.isDefaultPrevented()) {
6+
var shortcut = global_shortcuts[CodeMirror.keyName(e)];
7+
if (shortcut) {
8+
e.preventDefault();
9+
shortcut.func(e);
10+
}
11+
}
12+
});
13+
14+
function shortcut_for_command(command) {
15+
// If the command is a name like "save", get they key-combo from CodeMirror
16+
if (!(command.indexOf('-') > -1)) {
17+
command = _.findKey(CodeMirror.keyMap.default, _.partial(_.isEqual, command));
18+
}
19+
20+
// If any of the shortcut items are "platformcmd", convert them to 'Ctrl' or 'Cmd' depending on the platform.
21+
function key_for_platform(name) {
22+
if (name.toLowerCase() == "platformcmd") {
23+
return /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
24+
} else return name;
25+
}
26+
27+
return command.split('-').map(key_for_platform).join('-');
28+
}
29+
30+
return {
31+
/** Add or replace global shortcuts
32+
*
33+
* @param {Object} shortcuts The keys of this object must be strings which represent keyboard shortcuts.
34+
* They can Codemirror-compatible shortcut descriptors e.g. "Shift-Cmd-V", or they can reference CodeMirror
35+
* commands such as "Save".
36+
* The values should be objects which have a descriptive "name" property, and also either have a "func" property
37+
* or be functions themselves. For example, a named function fully satisfies the requirements, as does an object
38+
* such as {name: "My Function", func: my_anonymous_function}
39+
*/
40+
SetShortcutHandlers: function (shortcuts) {
41+
_.each(shortcuts, function (descriptor, key) {
42+
var shortcut = shortcut_for_command(key);
43+
global_shortcuts[shortcut] = {
44+
name: descriptor.name ? descriptor.name : key,
45+
func: _.isFunction(descriptor) ? descriptor : descriptor.func
46+
};
47+
});
48+
},
49+
GetShortcuts: function() {
50+
return global_shortcuts;
51+
}
52+
}
53+
})();

ide/templates/ide/project.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ <h3>{% trans 'Compass and Accelerometer' %}</h3>
503503
<script src="/ide/jsi18n/"></script>
504504
<script src="{% static 'ide/js/csrf.js' %}" type="text/javascript"></script>
505505
<script src="{% static 'ide/js/cloudpebble.js' %}" type="text/javascript"></script>
506+
<script src="{% static 'ide/js/global_shortcuts.js' %}" type="text/javascript"></script>
506507
<script src="{% static 'ide/js/sidebar.js' %}" type="text/javascript"></script>
507508
<script src="{% static 'ide/js/radix.js' %}" type="text/javascript"></script>
508509
<script src="{% static 'ide/js/ycm.js' %}" type="text/javascript"></script>

0 commit comments

Comments
 (0)