Raymond Hill 2018-07-09 15:21:45 -04:00
parent 79d5af20fc
commit 224661f905
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 25 additions and 28 deletions

View File

@ -79,31 +79,27 @@
); );
} }
function searchWidgetTimerHandler(cm) {
var state = getSearchState(cm);
state.queryTimer = null;
findCommit(cm);
}
function searchWidgetInputHandler(cm) { function searchWidgetInputHandler(cm) {
var state = getSearchState(cm); let state = getSearchState(cm);
if ( queryTextFromSearchWidget(cm) !== state.queryText ) { if ( queryTextFromSearchWidget(cm) === state.queryText ) { return; }
if ( state.queryTimer !== null ) { if ( state.queryTimer !== null ) {
clearTimeout(state.queryTimer); clearTimeout(state.queryTimer);
} }
state.queryTimer = setTimeout( state.queryTimer = setTimeout(
searchWidgetTimerHandler.bind(null, cm), () => {
state.queryTimer = null;
findCommit(cm, 0);
},
350 350
); );
} }
}
function searchWidgetClickHandler(cm, ev) { function searchWidgetClickHandler(cm, ev) {
var tcl = ev.target.classList; var tcl = ev.target.classList;
if ( tcl.contains('cm-search-widget-up') ) { if ( tcl.contains('cm-search-widget-up') ) {
findNext(cm, true); findNext(cm, -1);
} else if ( tcl.contains('cm-search-widget-down') ) { } else if ( tcl.contains('cm-search-widget-down') ) {
findNext(cm, false); findNext(cm, 1);
} }
if ( ev.target.localName !== 'input' ) { if ( ev.target.localName !== 'input' ) {
ev.preventDefault(); ev.preventDefault();
@ -156,7 +152,7 @@
return true; return true;
} }
if ( queryText.length !== 0 ) { if ( queryText.length !== 0 ) {
findNext(cm, command === 'findPrev'); findNext(cm, command === 'findPrev' ? -1 : 1);
} }
return true; return true;
} }
@ -223,22 +219,23 @@
} }
} }
function findNext(cm, rev, callback) { function findNext(cm, dir, callback) {
cm.operation(function() { cm.operation(function() {
var state = getSearchState(cm); var state = getSearchState(cm);
if ( !state.query ) { return; } if ( !state.query ) { return; }
var cursor = getSearchCursor( var cursor = getSearchCursor(
cm, cm,
state.query, state.query,
rev ? cm.getCursor('from') : cm.getCursor('to') dir <= 0 ? cm.getCursor('from') : cm.getCursor('to')
); );
if (!cursor.find(rev)) { let previous = dir < 0;
if (!cursor.find(previous)) {
cursor = getSearchCursor( cursor = getSearchCursor(
cm, cm,
state.query, state.query,
rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0) previous ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0)
); );
if (!cursor.find(rev)) return; if (!cursor.find(previous)) return;
} }
cm.setSelection(cursor.from(), cursor.to()); cm.setSelection(cursor.from(), cursor.to());
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20); cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
@ -270,7 +267,7 @@
}); });
} }
function findCommit(cm) { function findCommit(cm, dir) {
var state = getSearchState(cm); var state = getSearchState(cm);
if ( state.queryTimer !== null ) { if ( state.queryTimer !== null ) {
clearTimeout(state.queryTimer); clearTimeout(state.queryTimer);
@ -284,7 +281,7 @@
} else { } else {
cm.operation(function() { cm.operation(function() {
startSearch(cm, state); startSearch(cm, state);
findNext(cm, false); findNext(cm, dir);
}); });
} }
} }
@ -300,17 +297,17 @@
cm.setCursor(word.anchor); cm.setCursor(word.anchor);
} }
queryTextToSearchWidget(cm, queryText); queryTextToSearchWidget(cm, queryText);
findCommit(cm); findCommit(cm, 1);
} }
function findNextCommand(cm) { function findNextCommand(cm) {
var state = getSearchState(cm); var state = getSearchState(cm);
if ( state.query ) { return findNext(cm, false); } if ( state.query ) { return findNext(cm, 1); }
} }
function findPrevCommand(cm) { function findPrevCommand(cm) {
var state = getSearchState(cm); var state = getSearchState(cm);
if ( state.query ) { return findNext(cm, true); } if ( state.query ) { return findNext(cm, -1); }
} }
CodeMirror.commands.find = findCommand; CodeMirror.commands.find = findCommand;