Skip to content

Commit 7d71d44

Browse files
committed
Improve stability of navigation in command palette
1 parent c65b7d1 commit 7d71d44

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

client/views/commands/palette.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,33 @@ templateFile, commandTemplateFile) {
134134

135135
// Do search
136136
doSearch: function(query) {
137-
var that = this;
137+
var that = this, toRemove = [];
138138
if (this.commands.collection.query == query) return;
139139

140140
if (this.commands.collection.query && query
141141
&& query.indexOf(this.commands.collection.query) == 0) {
142142
// Continue current search
143143
this.commands.collection.query = query;
144-
this.commands.filter(function(command) {
145-
return command.textScore(query) > 0;
144+
this.commands.collection.each(function(model) {
145+
if (model.textScore(query) == 0) {
146+
toRemove.push(model);
147+
}
146148
});
147-
this.selectItem(this.selected || 0);
149+
this.commands.collection.remove(toRemove);
150+
this.commands.collection.sort();
151+
this.selectItem(this.getSelectedItem());
148152
} else {
149153
// Different search
150154
this.commands.collection.query = query;
151155
this.commands.collection.reset([]);
152-
this.commands.clearFilter();
153156
this.options.searchHandler(query)
154157
.then(function() {},
155158
function(err) {},
156159
function(result) {
157160
that.commands.collection.add(_.filter(result.results, function(command) {
158161
return !command.hasFlag("disabled");
159162
}));
160-
that.selectItem(that.selected || 0);
163+
that.selectItem(that.getSelectedItem());
161164
});
162165
}
163166
},
@@ -172,24 +175,26 @@ templateFile, commandTemplateFile) {
172175
keyup: function(e) {
173176
var key = e.which || e.keyCode;
174177
var q = $(e.currentTarget).val();
178+
var selected = this.getSelectedItem();
179+
var pSelected = selected;
175180

176181
if (key == 27) {
177182
/* ESC */
178183
e.preventDefault();
179184
return;
180185
} else if (key == 38) {
181186
/* UP */
182-
this.selected = this.selected - 1;
187+
selected = selected - 1;
183188
} else if (key == 40) {
184189
/* DOWN */
185-
this.selected = this.selected + 1;
190+
selected = selected + 1;
186191
} else if (key == 13) {
187192
/* ENTER */
188193
e.preventDefault();
189-
this.openItem(this.getSelectedItem());
194+
this.openItem(selected);
190195
}
191196
this.doSearch(q);
192-
this.selectItem(this.selected);
197+
if (selected != pSelected) this.selectItem(selected);
193198
},
194199

195200
selectItem: function(i) {
@@ -203,7 +208,6 @@ templateFile, commandTemplateFile) {
203208
i = 0;
204209
this.commands.collection.each(function(model) {
205210
var y, h, item = this.commands.items[model.id];
206-
if (item.$el.hasClass("hr-list-fiter-on")) return;
207211
item.$el.toggleClass("selected", i == this.selected);
208212

209213
if (i == this.selected) {
@@ -225,7 +229,10 @@ templateFile, commandTemplateFile) {
225229
var _ret = 0;
226230
this.commands.collection.each(function(model, i) {
227231
var item = this.commands.items[model.id];
228-
if (item.$el.hasClass("selected")) _ret = i;
232+
if (item.$el.hasClass("selected")) {
233+
_ret = i;
234+
return false;
235+
}
229236
}, this);
230237
return _ret;
231238
},

0 commit comments

Comments
 (0)