Skip to content

Commit 85248d3

Browse files
author
Joseph Atkins-Turkish
committed
Improved global shortcut handler and made fuzzyprompt use it too.
1 parent 76c0865 commit 85248d3

File tree

5 files changed

+62
-57
lines changed

5 files changed

+62
-57
lines changed

ide/static/ide/js/cloudpebble.js

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

ide/static/ide/js/editor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CloudPebble.Editor = (function() {
116116
settings.extraKeys = {'Ctrl-Space': 'autocomplete'};
117117
}
118118
if(!is_js && USER_SETTINGS.autocomplete !== 0) {
119-
settings.extraKeys['Tab'] = function() {
119+
settings.extraKeys['Tab'] = function selectNextArgument() {
120120
var marks = code_mirror.getAllMarks();
121121
var cursor = code_mirror.getCursor();
122122
var closest = null;
@@ -149,7 +149,7 @@ CloudPebble.Editor = (function() {
149149
if(USER_SETTINGS.use_spaces) {
150150
var spaces = Array(settings.indentUnit + 1).join(' ');
151151
var oldTab = settings.extraKeys['Tab'];
152-
settings.extraKeys['Tab'] = function(cm) {
152+
settings.extraKeys['Tab'] = function indentMoreOrSelectNextArgument(cm) {
153153
// If we already overrode tab, check that one.
154154
if(oldTab) {
155155
if(oldTab(cm) !== CodeMirror.Pass) {
@@ -199,10 +199,10 @@ CloudPebble.Editor = (function() {
199199
return CodeMirror.Pass;
200200
};
201201
}
202-
settings.extraKeys['Cmd-/'] = function(cm) {
202+
settings.extraKeys['Cmd-/'] = function toggleComment(cm) {
203203
CodeMirror.commands.toggleComment(cm);
204204
};
205-
settings.extraKeys['Ctrl-/'] = function(cm) {
205+
settings.extraKeys['Ctrl-/'] = function toggleComment(cm) {
206206
CodeMirror.commands.toggleComment(cm);
207207
};
208208
if(is_js) {
@@ -246,7 +246,7 @@ CloudPebble.Editor = (function() {
246246

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

249-
settings.extraKeys[help_shortcut] = function(cm) {
249+
settings.extraKeys[help_shortcut] = function lookupFunction(cm) {
250250
var pos = cm.cursorCoords();
251251
var token = code_mirror.getTokenAt(cm.getCursor());
252252

ide/static/ide/js/fuzzyprompt.js

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

3838
// Register ctrl-p and ctrl-shift-p
39-
$(document).keydown(function(e) {
40-
if ((e[modifier]) && e.keyCode == 80) {
41-
e.preventDefault();
42-
if (COMMANDS_ENABLED && e.shiftKey) {
43-
input.attr('placeholder', gettext("Enter Command"));
44-
show_prompt('commands');
45-
}
46-
if (!e.shiftKey) {
39+
CloudPebble.GlobalShortcuts.SetShortcutHandlers({
40+
'PlatformCmd-P': {
41+
func: function () {
4742
input.attr('placeholder', gettext("Search Files"));
4843
show_prompt('files');
49-
}
44+
},
45+
name: gettext("Find File")
46+
},
47+
'Shift-PlatformCmd-P': {
48+
func: function () {
49+
input.attr('placeholder', gettext("Enter Command"));
50+
show_prompt('commands');
51+
},
52+
name: gettext("Find Action")
5053
}
5154
});
5255

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
SetShortcutHandlers: function (shortcuts) {
32+
_.each(shortcuts, function (descriptor, key) {
33+
var shortcut = shortcut_for_command(key);
34+
global_shortcuts[shortcut] = {
35+
name: descriptor.name ? descriptor.name : key,
36+
func: _.isFunction(descriptor) ? descriptor : descriptor.func
37+
};
38+
});
39+
},
40+
GetShortcuts: function() {
41+
return global_shortcuts;
42+
}
43+
}
44+
})();

ide/templates/ide/project.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ <h3>{% trans 'Compass and Accelerometer' %}</h3>
498498
<script src="/ide/jsi18n/"></script>
499499
<script src="{% static 'ide/js/csrf.js' %}" type="text/javascript"></script>
500500
<script src="{% static 'ide/js/cloudpebble.js' %}" type="text/javascript"></script>
501+
<script src="{% static 'ide/js/global_shortcuts.js' %}" type="text/javascript"></script>
501502
<script src="{% static 'ide/js/sidebar.js' %}" type="text/javascript"></script>
502503
<script src="{% static 'ide/js/radix.js' %}" type="text/javascript"></script>
503504
<script src="{% static 'ide/js/ycm.js' %}" type="text/javascript"></script>

0 commit comments

Comments
 (0)