From 224661f905d80b5f48f361154696f377e2b234bf Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 9 Jul 2018 15:21:45 -0400 Subject: [PATCH] fix https://github.com/uBlockOrigin/uBlock-issues/issues/118 --- src/js/codemirror/search.js | 53 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/js/codemirror/search.js b/src/js/codemirror/search.js index 47fa13452..078d0c138 100644 --- a/src/js/codemirror/search.js +++ b/src/js/codemirror/search.js @@ -79,31 +79,27 @@ ); } - function searchWidgetTimerHandler(cm) { - var state = getSearchState(cm); - state.queryTimer = null; - findCommit(cm); - } - function searchWidgetInputHandler(cm) { - var state = getSearchState(cm); - if ( queryTextFromSearchWidget(cm) !== state.queryText ) { - if ( state.queryTimer !== null ) { - clearTimeout(state.queryTimer); - } - state.queryTimer = setTimeout( - searchWidgetTimerHandler.bind(null, cm), - 350 - ); + let state = getSearchState(cm); + if ( queryTextFromSearchWidget(cm) === state.queryText ) { return; } + if ( state.queryTimer !== null ) { + clearTimeout(state.queryTimer); } + state.queryTimer = setTimeout( + () => { + state.queryTimer = null; + findCommit(cm, 0); + }, + 350 + ); } function searchWidgetClickHandler(cm, ev) { var tcl = ev.target.classList; if ( tcl.contains('cm-search-widget-up') ) { - findNext(cm, true); + findNext(cm, -1); } else if ( tcl.contains('cm-search-widget-down') ) { - findNext(cm, false); + findNext(cm, 1); } if ( ev.target.localName !== 'input' ) { ev.preventDefault(); @@ -156,7 +152,7 @@ return true; } if ( queryText.length !== 0 ) { - findNext(cm, command === 'findPrev'); + findNext(cm, command === 'findPrev' ? -1 : 1); } return true; } @@ -223,22 +219,23 @@ } } - function findNext(cm, rev, callback) { + function findNext(cm, dir, callback) { cm.operation(function() { var state = getSearchState(cm); if ( !state.query ) { return; } var cursor = getSearchCursor( cm, 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( cm, 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.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20); @@ -270,7 +267,7 @@ }); } - function findCommit(cm) { + function findCommit(cm, dir) { var state = getSearchState(cm); if ( state.queryTimer !== null ) { clearTimeout(state.queryTimer); @@ -284,7 +281,7 @@ } else { cm.operation(function() { startSearch(cm, state); - findNext(cm, false); + findNext(cm, dir); }); } } @@ -300,17 +297,17 @@ cm.setCursor(word.anchor); } queryTextToSearchWidget(cm, queryText); - findCommit(cm); + findCommit(cm, 1); } function findNextCommand(cm) { var state = getSearchState(cm); - if ( state.query ) { return findNext(cm, false); } + if ( state.query ) { return findNext(cm, 1); } } function findPrevCommand(cm) { var state = getSearchState(cm); - if ( state.query ) { return findNext(cm, true); } + if ( state.query ) { return findNext(cm, -1); } } CodeMirror.commands.find = findCommand;