Merge pull request #1911 from ONLYOFFICE/fix/fix-search

Fix/fix search
This commit is contained in:
maxkadushkin 2022-08-23 19:08:11 +03:00 committed by GitHub
commit b3f1a3d677
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 66 deletions

View file

@ -285,8 +285,7 @@ define([
}); });
} }
Common.NotificationCenter.on('window:resize', function() { Common.NotificationCenter.on('window:resize', function() {
me.$resultsContainer.outerHeight($('#search-box').outerHeight() - $('#search-header').outerHeight() - $('#search-adv-settings').outerHeight()); me.updateResultsContainerHeight();
me.$resultsContainer.scroller.update({alwaysVisibleY: true});
}); });
} }
@ -299,8 +298,7 @@ define([
Common.UI.BaseView.prototype.show.call(this,arguments); Common.UI.BaseView.prototype.show.call(this,arguments);
this.fireEvent('show', this ); this.fireEvent('show', this );
this.$resultsContainer.outerHeight($('#search-box').outerHeight() - $('#search-header').outerHeight() - $('#search-adv-settings').outerHeight()); this.updateResultsContainerHeight();
this.$resultsContainer.scroller.update({alwaysVisibleY: true});
}, },
hide: function () { hide: function () {
@ -328,6 +326,13 @@ define([
ChangeSettings: function(props) { ChangeSettings: function(props) {
}, },
updateResultsContainerHeight: function () {
if (this.$resultsContainer) {
this.$resultsContainer.outerHeight($('#search-box').outerHeight() - $('#search-header').outerHeight() - $('#search-adv-settings').outerHeight());
this.$resultsContainer.scroller.update({alwaysVisibleY: true});
}
},
updateResultsNumber: function (current, count) { updateResultsNumber: function (current, count) {
var text; var text;
if (count > 300) { if (count > 300) {
@ -335,9 +340,19 @@ define([
} else { } else {
text = current === 'no-results' ? this.textNoSearchResults : text = current === 'no-results' ? this.textNoSearchResults :
(current === 'stop' ? this.textSearchHasStopped : (current === 'stop' ? this.textSearchHasStopped :
(!count ? this.textNoMatches : Common.Utils.String.format(this.textSearchResults, current + 1, count))); (current === 'content-changed' ? (this.textContentChanged + ' ' + Common.Utils.String.format(this.textSearchAgain, '<a class="search-again">','</a>')) :
(!count ? this.textNoMatches : Common.Utils.String.format(this.textSearchResults, current + 1, count))));
} }
this.$reaultsNumber.text(text); if (current === 'content-changed') {
var me = this;
this.$reaultsNumber.html(text);
this.$reaultsNumber.find('.search-again').on('click', function () {
me.fireEvent('search:next', [me.inputText.getValue(), true]);
});
} else {
this.$reaultsNumber.text(text);
}
this.updateResultsContainerHeight();
!window.SSE && this.disableReplaceButtons(!count); !window.SSE && this.disableReplaceButtons(!count);
}, },
@ -367,8 +382,7 @@ define([
this.$searchOptionsBlock[this.extendedOptions ? 'removeClass' : 'addClass']('no-expand'); this.$searchOptionsBlock[this.extendedOptions ? 'removeClass' : 'addClass']('no-expand');
Common.localStorage.setBool('sse-search-options-extended', this.extendedOptions); Common.localStorage.setBool('sse-search-options-extended', this.extendedOptions);
this.$resultsContainer.outerHeight($('#search-box').outerHeight() - $('#search-header').outerHeight() - $('#search-adv-settings').outerHeight()); this.updateResultsContainerHeight();
this.$resultsContainer.scroller.update({alwaysVisibleY: true});
}, },
setFindText: function (val) { setFindText: function (val) {
@ -422,7 +436,9 @@ define([
textCell: 'Cell', textCell: 'Cell',
textValue: 'Value', textValue: 'Value',
textFormula: 'Formula', textFormula: 'Formula',
textSearchHasStopped: 'Search has stopped' textSearchHasStopped: 'Search has stopped',
textContentChanged: 'Document content has changed.',
textSearchAgain: '{0}Search again{1} to make sure the results are current.'
}, Common.Views.SearchPanel || {})); }, Common.Views.SearchPanel || {}));
}); });

View file

@ -159,6 +159,12 @@
width: calc(100% - 48px); width: calc(100% - 48px);
color: @text-secondary-ie; color: @text-secondary-ie;
color: @text-secondary; color: @text-secondary;
.search-again {
color: @text-secondary;
cursor: pointer;
text-decoration: underline;
}
} }
.search-nav-btns { .search-nav-btns {

View file

@ -93,7 +93,9 @@ define([
withinSheet: Asc.c_oAscSearchBy.Sheet, withinSheet: Asc.c_oAscSearchBy.Sheet,
searchByRows: true, searchByRows: true,
lookInFormulas: true, lookInFormulas: true,
isValidSelectedRange: true isValidSelectedRange: true,
lastSelectedItem: undefined,
isContentChanged: false
}; };
}, },
@ -111,8 +113,8 @@ define([
this.api.asc_registerCallback('asc_onEndTextAroundSearch', _.bind(this.onEndTextAroundSearch, this)); this.api.asc_registerCallback('asc_onEndTextAroundSearch', _.bind(this.onEndTextAroundSearch, this));
this.api.asc_registerCallback('asc_onGetTextAroundSearchPack', _.bind(this.onApiGetTextAroundSearch, this)); this.api.asc_registerCallback('asc_onGetTextAroundSearchPack', _.bind(this.onApiGetTextAroundSearch, this));
this.api.asc_registerCallback('asc_onRemoveTextAroundSearch', _.bind(this.onApiRemoveTextAroundSearch, this)); this.api.asc_registerCallback('asc_onRemoveTextAroundSearch', _.bind(this.onApiRemoveTextAroundSearch, this));
this.api.asc_registerCallback('asc_onSearchEnd', _.bind(this.onApiSearchEnd, this));
this.api.asc_registerCallback('asc_onActiveSheetChanged', _.bind(this.onActiveSheetChanged, this)); this.api.asc_registerCallback('asc_onActiveSheetChanged', _.bind(this.onActiveSheetChanged, this));
this.api.asc_registerCallback('asc_onModifiedDocument', _.bind(this.onApiModifiedDocument, this));
} }
return this; return this;
}, },
@ -166,12 +168,7 @@ define([
break; break;
} }
if (runSearch) { if (runSearch) {
this.hideResults(); this.onQuerySearch();
if (this.onQuerySearch()) {
this.searchTimer && clearInterval(this.searchTimer);
this.searchTimer = undefined;
this.api.asc_StartTextAroundSearch();
}
} }
}, },
@ -205,16 +202,7 @@ define([
var isReturnKey = type === 'keydown' && e.keyCode === Common.UI.Keys.RETURN; var isReturnKey = type === 'keydown' && e.keyCode === Common.UI.Keys.RETURN;
if (isReturnKey || type !== 'keydown') { if (isReturnKey || type !== 'keydown') {
this._state.searchText = text; this._state.searchText = text;
if (this.onQuerySearch(type) && (this.searchTimer || isReturnKey)) { this.onQuerySearch(type);
this.hideResults();
if (this.searchTimer) {
clearInterval(this.searchTimer);
this.searchTimer = undefined;
}
if (this.view.$el.is(':visible')) {
this.api.asc_StartTextAroundSearch();
}
}
} }
}, },
@ -227,25 +215,21 @@ define([
this.searchTimer = setInterval(function(){ this.searchTimer = setInterval(function(){
if ((new Date()) - me._lastInputChange < 400) return; if ((new Date()) - me._lastInputChange < 400) return;
me.hideResults();
me._state.searchText = me._state.newSearchText; me._state.searchText = me._state.newSearchText;
if (me.onQuerySearch()) { if (!me.onQuerySearch() && me._state.newSearchText === '') {
if (me.view.$el.is(':visible')) {
me.api.asc_StartTextAroundSearch();
}
} else if (me._state.newSearchText === '') {
me.view.updateResultsNumber('no-results'); me.view.updateResultsNumber('no-results');
me.view.disableNavButtons(); me.view.disableNavButtons();
Common.NotificationCenter.trigger('search:updateresults', undefined, 0); Common.NotificationCenter.trigger('search:updateresults', undefined, 0);
} }
clearInterval(me.searchTimer);
me.searchTimer = undefined;
}, 10); }, 10);
} }
} }
}, },
onQuerySearch: function (d, isNeedRecalc) { onQuerySearch: function (d, isNeedRecalc) {
this.searchTimer && clearInterval(this.searchTimer);
this.searchTimer = undefined;
var me = this; var me = this;
if (this._state.withinSheet === Asc.c_oAscSearchBy.Range && !this._state.isValidSelectedRange) { if (this._state.withinSheet === Asc.c_oAscSearchBy.Range && !this._state.isValidSelectedRange) {
Common.UI.warning({ Common.UI.warning({
@ -259,6 +243,8 @@ define([
return; return;
} }
this.hideResults();
var options = new Asc.asc_CFindOptions(); var options = new Asc.asc_CFindOptions();
options.asc_setFindWhat(this._state.searchText); options.asc_setFindWhat(this._state.searchText);
options.asc_setScanForward(d != 'back'); options.asc_setScanForward(d != 'back');
@ -271,19 +257,24 @@ define([
options.asc_setScanByRows(this._state.searchByRows); options.asc_setScanByRows(this._state.searchByRows);
options.asc_setLookIn(this._state.lookInFormulas ? Asc.c_oAscFindLookIn.Formulas : Asc.c_oAscFindLookIn.Value); options.asc_setLookIn(this._state.lookInFormulas ? Asc.c_oAscFindLookIn.Formulas : Asc.c_oAscFindLookIn.Value);
options.asc_setNeedRecalc(isNeedRecalc); options.asc_setNeedRecalc(isNeedRecalc);
this._state.isContentChanged && options.asc_setLastSearchElem(this._state.lastSelectedItem);
this._state.isContentChanged = false;
if (!this.api.asc_findText(options)) { if (!this.api.asc_findText(options)) {
this.resultItems = []; this.removeResultItems();
this.view.updateResultsNumber(undefined, 0);
this._state.currentResult = 0;
this._state.resultsNumber = 0;
this.view.disableNavButtons();
Common.NotificationCenter.trigger('search:updateresults', undefined, 0);
return false; return false;
} }
if (this.view.$el.is(':visible')) {
this.api.asc_StartTextAroundSearch();
}
return true; return true;
}, },
onQueryReplace: function(textSearch, textReplace) { onQueryReplace: function(textSearch, textReplace) {
if (this._state.isContentChanged) {
this.onQuerySearch();
return;
}
this.api.isReplaceAll = false; this.api.isReplaceAll = false;
var options = new Asc.asc_CFindOptions(); var options = new Asc.asc_CFindOptions();
options.asc_setFindWhat(textSearch); options.asc_setFindWhat(textSearch);
@ -302,6 +293,10 @@ define([
}, },
onQueryReplaceAll: function(textSearch, textReplace) { onQueryReplaceAll: function(textSearch, textReplace) {
if (this._state.isContentChanged) {
this.onQuerySearch();
return;
}
this.api.isReplaceAll = true; this.api.isReplaceAll = true;
var options = new Asc.asc_CFindOptions(); var options = new Asc.asc_CFindOptions();
options.asc_setFindWhat(textSearch); options.asc_setFindWhat(textSearch);
@ -334,20 +329,7 @@ define([
}); });
} }
} else { } else {
var options = new Asc.asc_CFindOptions(); this.onQuerySearch();
options.asc_setFindWhat(this._state.searchText);
options.asc_setScanForward(true);
options.asc_setIsMatchCase(this._state.matchCase);
options.asc_setIsWholeCell(this._state.matchWord);
options.asc_setScanOnOnlySheet(this._state.withinSheet);
if (this._state.withinSheet === Asc.c_oAscSearchBy.Range) {
options.asc_setSpecificRange(this._state.selectedRange);
}
options.asc_setScanByRows(this._state.searchByRows);
options.asc_setLookIn(this._state.lookInFormulas ? Asc.c_oAscFindLookIn.Formulas : Asc.c_oAscFindLookIn.Value);
if (!this.api.asc_findText(options)) {
this.removeResultItems();
}
} }
}, },
@ -384,10 +366,10 @@ define([
item.selected = false; item.selected = false;
}); });
if (this.resultItems[current]) { if (this.resultItems[current]) {
this._state.lastSelectedItem = this.resultItems[current].data;
this.resultItems[current].selected = true; this.resultItems[current].selected = true;
$('#search-results').find('.item').removeClass('selected'); $('#search-results').find('.item').removeClass('selected');
this.resultItems[current].$el.addClass('selected'); this.resultItems[current].$el.addClass('selected');
this.scrollToSelectedResult(current);
} }
} }
} }
@ -428,7 +410,8 @@ define([
if (this.view && this._state.isStartedAddingResults) { if (this.view && this._state.isStartedAddingResults) {
if (data.length > 300 || !data.length) return; if (data.length > 300 || !data.length) return;
var me = this, var me = this,
$innerResults = me.view.$resultsContainer.find('.search-items'); $innerResults = me.view.$resultsContainer.find('.search-items'),
selectedInd;
me.resultItems = []; me.resultItems = [];
data.forEach(function (item, ind) { data.forEach(function (item, ind) {
var isSelected = ind === me._state.currentResult; var isSelected = ind === me._state.currentResult;
@ -442,16 +425,24 @@ define([
var $item = $(tr).appendTo($innerResults); var $item = $(tr).appendTo($innerResults);
if (isSelected) { if (isSelected) {
$item.addClass('selected'); $item.addClass('selected');
me._state.lastSelectedItem = item;
selectedInd = ind;
} }
var resultItem = {id: item[0], $el: $item, el: tr, selected: isSelected, data: data}; var resultItem = {id: item[0], $el: $item, el: tr, selected: isSelected, data: item};
me.resultItems.push(resultItem); me.resultItems.push(resultItem);
$item.on('click', _.bind(function (el) { $item.on('click', _.bind(function (el) {
if (me._state.isContentChanged) {
me._state.lastSelectedItem = item;
me.onQuerySearch();
return;
}
var id = item[0]; var id = item[0];
me.api.asc_SelectSearchElement(id); me.api.asc_SelectSearchElement(id);
}, me)); }, me));
me.addTooltips($item, item); me.addTooltips($item, item);
}); });
this.view.$resultsContainer.show(); this.view.$resultsContainer.show();
this.scrollToSelectedResult(selectedInd);
} }
}, },
@ -551,18 +542,14 @@ define([
} }
}, },
onApiSearchEnd: function () { onApiModifiedDocument: function () {
this.removeResultItems('stop'); this._state.isContentChanged = true;
this.view.updateResultsNumber('content-changed');
}, },
onActiveSheetChanged: function (index) { onActiveSheetChanged: function (index) {
if (this._state.isHighlightedResults && this._state.withinSheet === Asc.c_oAscSearchBy.Sheet) { if (this._state.isHighlightedResults && this._state.withinSheet === Asc.c_oAscSearchBy.Sheet) {
this.hideResults(); this.onQuerySearch(undefined, true);
if (this.onQuerySearch(undefined, true)) {
this.searchTimer && clearInterval(this.searchTimer);
this.searchTimer = undefined;
this.api.asc_StartTextAroundSearch();
}
} }
}, },

View file

@ -423,6 +423,8 @@
"Common.Views.SearchPanel.textWorkbook": "Workbook", "Common.Views.SearchPanel.textWorkbook": "Workbook",
"Common.Views.SearchPanel.tipNextResult": "Next result", "Common.Views.SearchPanel.tipNextResult": "Next result",
"Common.Views.SearchPanel.tipPreviousResult": "Previous result", "Common.Views.SearchPanel.tipPreviousResult": "Previous result",
"Common.Views.SearchPanel.textContentChanged": "Document content has changed.",
"Common.Views.SearchPanel.textSearchAgain": "{0}Search again{1} to make sure the results are current.",
"Common.Views.SelectFileDlg.textLoading": "Loading", "Common.Views.SelectFileDlg.textLoading": "Loading",
"Common.Views.SelectFileDlg.textTitle": "Select Data Source", "Common.Views.SelectFileDlg.textTitle": "Select Data Source",
"Common.Views.SignDialog.textBold": "Bold", "Common.Views.SignDialog.textBold": "Bold",