Merge pull request #469 from brismuth/selectingSearchText

Selecting search text on searchbar activate and adding search history
This commit is contained in:
Thomas Wilburn 2015-11-05 11:14:06 -08:00
commit 43a6eb362d
2 changed files with 52 additions and 6 deletions

View file

@ -43,6 +43,10 @@
background-color: @background; background-color: @background;
color: @foreground; color: @foreground;
font-size: 12px; font-size: 12px;
&::selection {
background: mix(@accent, #ccc, 15%);
}
} }
button { button {

View file

@ -17,8 +17,8 @@ define([
var self = this; var self = this;
this.element = document.find(".searchbar"); this.element = document.find(".searchbar");
this.input = this.element.find(".search-box"); this.input = this.element.find(".search-box");
this.maxMatches = Settings.get("user").maxSearchMatches || 50;
this.maxMatches = Settings.get("user").maxSearchMatches || 50;
command.on("init:restart", function() { command.on("init:restart", function() {
self.maxMatches = Settings.get("user").maxSearchMatches || 50; self.maxMatches = Settings.get("user").maxSearchMatches || 50;
}); });
@ -28,6 +28,12 @@ define([
running: false running: false
}; };
this.searchHistory = {
history: [],
currentIndex: 0,
temporaryQuery: ''
};
this.bindInput(); this.bindInput();
this.bindButtons(); this.bindButtons();
}; };
@ -35,6 +41,7 @@ define([
Searchbar.prototype = { Searchbar.prototype = {
bindInput: function() { bindInput: function() {
var input = this.input; var input = this.input;
var hist = this.searchHistory;
var self = this; var self = this;
input.on("keydown", function(e) { input.on("keydown", function(e) {
@ -51,6 +58,33 @@ define([
self.deactivate(); self.deactivate();
return; return;
} }
//up/down
if (e.keyCode == 38 || e.keyCode == 40) {
e.preventDefault();
e.stopImmediatePropagation();
if (e.keyCode == 38) { // up
// show previous search query
if (hist.currentIndex == hist.history.length) {
hist.temporaryQuery = input.value;
// skip previous search if we're already showing the same value
if (hist.temporaryQuery == hist.history[hist.currentIndex-1]) {
hist.currentIndex--;
}
}
if (hist.currentIndex - 1 >= 0) {
input.value = hist.history[--hist.currentIndex];
}
} else { // down
//show next search query
if (hist.currentIndex + 1 < hist.history.length) {
input.value = hist.history[++hist.currentIndex];
} else if (hist.currentIndex + 1 == hist.history.length) {
hist.currentIndex++;
input.value = hist.temporaryQuery;
}
}
return;
}
}); });
}, },
@ -64,7 +98,6 @@ define([
}, },
// todo add regex support // todo add regex support
// todo add search history
// we don't have to worry about the files blacklist because they are already removed from the project structure // we don't have to worry about the files blacklist because they are already removed from the project structure
search: function() { search: function() {
if (this.currentSearch.running) { if (this.currentSearch.running) {
@ -75,6 +108,14 @@ define([
var isCaseSensitive = this.element.find("#search-case-check").checked; var isCaseSensitive = this.element.find("#search-case-check").checked;
var displayQuery = this.input.value; var displayQuery = this.input.value;
// add query to search history
var hist = this.searchHistory;
if (displayQuery != hist.history[hist.history.length - 1]) { // deduplicate
hist.history.push(displayQuery);
}
hist.currentIndex = hist.history.length;
hist.temporaryQuery = '';
var resultsTab = this.createResultsTab(displayQuery); var resultsTab = this.createResultsTab(displayQuery);
this.currentSearch = { this.currentSearch = {
@ -139,7 +180,7 @@ define([
var options = this.currentSearch; var options = this.currentSearch;
chrome.fileSystem.getDisplayPath(nodeEntry, function(path) { chrome.fileSystem.getDisplayPath(nodeEntry, function(path) {
if (!options.running) return c(); if (!options.running) return c();
var file = new File(nodeEntry); var file = new File(nodeEntry);
var path = nodeEntry.fullPath; var path = nodeEntry.fullPath;
@ -233,13 +274,14 @@ define([
}, },
activate: function(mode) { activate: function(mode) {
var highlighted = editor.getSelectedText(); var selected = editor.getSelectedText();
if (highlighted) { if (selected) {
this.input.value = highlighted; this.input.value = selected;
} }
this.element.addClass("active"); this.element.addClass("active");
this.input.focus(); this.input.focus();
this.input.select();
}, },
deactivate: function(cancel) { deactivate: function(cancel) {