From 517774c1becc100efc3c85ccfebee794ecb2983b Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Tue, 25 Jan 2022 16:15:19 +0300 Subject: [PATCH] Feature/search callback (#2505) * Add callback for search function * Use asc_findText instead of findText * remove findText Co-authored-by: Sergey Luzyanin --- cell/api.js | 17 ++++++++++------- common/Native/Wrappers/api.js | 4 +++- pdf/src/viewer.js | 17 ++++++++++------- slide/api.js | 31 +++++++++++++------------------ word/api.js | 24 ++++++++++++++++-------- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/cell/api.js b/cell/api.js index 8521ac19f9..773205b58f 100644 --- a/cell/api.js +++ b/cell/api.js @@ -3003,20 +3003,23 @@ var editor; spreadsheet_api.prototype.asc_searchEnabled = function(bIsEnabled) { }; - spreadsheet_api.prototype.asc_findText = function(options) { + spreadsheet_api.prototype.asc_findText = function(options, callback) { + var result = null; if (window["NATIVE_EDITOR_ENJINE"]) { if (this.wb.findCellText(options)) { var ws = this.wb.getWorksheet(); var activeCell = this.wbModel.getActiveWs().selectionRange.activeCell; - return [ws.getCellLeftRelative(activeCell.col, 0), ws.getCellTopRelative(activeCell.row, 0)]; + result = [ws.getCellLeftRelative(activeCell.col, 0), ws.getCellTopRelative(activeCell.row, 0)]; } - - return null; + } else { + var d = this.wb.findCellText(options); + this.controller.scroll(d); + result = !!d; } - var d = this.wb.findCellText(options); - this.controller.scroll(d); - return !!d; + if (callback) + callback(result); + return result; }; spreadsheet_api.prototype.asc_replaceText = function(options) { diff --git a/common/Native/Wrappers/api.js b/common/Native/Wrappers/api.js index d6435f13c3..59d1af2f96 100755 --- a/common/Native/Wrappers/api.js +++ b/common/Native/Wrappers/api.js @@ -5182,7 +5182,7 @@ Asc['asc_docs_api'].prototype.SetDocumentModified = function(bValue) }; // find ------------------------------------------------------------------------------------------------- -Asc['asc_docs_api'].prototype.asc_findText = function(text, isNext, isMatchCase) +Asc['asc_docs_api'].prototype.asc_findText = function(text, isNext, isMatchCase, callback) { var SearchEngine = editor.WordControl.m_oLogicDocument.Search( text, { MatchCase : isMatchCase } ); @@ -5191,6 +5191,8 @@ Asc['asc_docs_api'].prototype.asc_findText = function(text, isNext, isMatchCase) if ( null != Id ) this.WordControl.m_oLogicDocument.SelectSearchElement( Id ); + if (callback) + callback(SearchEngine.Count); return SearchEngine.Count; }; diff --git a/pdf/src/viewer.js b/pdf/src/viewer.js index ab7283ab6f..6a7f418f4a 100644 --- a/pdf/src/viewer.js +++ b/pdf/src/viewer.js @@ -1650,25 +1650,28 @@ return this.file.copy(_text_format); }; - this.findText = function(text, isMachingCase, isNext) + this.findText = function(text, isMachingCase, isNext, callback) { if (this.isFullTextMessage) return bRetValue; if (!this.isFullText) { - this.fullTextMessageCallbackArgs = [text, isMachingCase, isNext]; + this.fullTextMessageCallbackArgs = [text, isMachingCase, isNext, callback]; this.fullTextMessageCallback = function() { this.file.findText(this.fullTextMessageCallbackArgs[0], this.fullTextMessageCallbackArgs[1], this.fullTextMessageCallbackArgs[2]); this.onUpdateOverlay(); + + if (this.fullTextMessageCallbackArgs[3]) + this.fullTextMessageCallbackArgs[3](this.SearchResults.Count); }; this.showTextMessage(); + return true; // async } - else - { - this.file.findText(text, isMachingCase, isNext); - this.onUpdateOverlay(); - } + + this.file.findText(text, isMachingCase, isNext); + this.onUpdateOverlay(); + return false; }; this.ToSearchResult = function() diff --git a/slide/api.js b/slide/api.js index 5b76d3600b..6784a3a242 100644 --- a/slide/api.js +++ b/slide/api.js @@ -2408,21 +2408,6 @@ background-repeat: no-repeat;\ this.sendEvent("asc_onSearchEnd"); }; - asc_docs_api.prototype.findText = function(text, isNext, isMatchCase) - { - - var SearchEngine = editor.WordControl.m_oLogicDocument.Search(text, {MatchCase : isMatchCase}); - - var Id = this.WordControl.m_oLogicDocument.GetSearchElementId(isNext); - - if (null != Id) - this.WordControl.m_oLogicDocument.SelectSearchElement(Id); - - return SearchEngine.Count; - - //return this.WordControl.m_oLogicDocument.findText(text, scanForward); - }; - asc_docs_api.prototype.asc_replaceText = function(text, replaceWith, isReplaceAll, isMatchCase) { if (null == this.WordControl.m_oLogicDocument) @@ -2463,10 +2448,21 @@ background-repeat: no-repeat;\ // пустой метод }; - asc_docs_api.prototype.asc_findText = function(text, isNext, isMatchCase) + asc_docs_api.prototype.asc_findText = function(text, isNext, isMatchCase, callback) { - return this.WordControl.m_oLogicDocument.findText(text, isNext === true); + var SearchEngine = editor.WordControl.m_oLogicDocument.Search(text, {MatchCase : isMatchCase}); + + var Id = this.WordControl.m_oLogicDocument.GetSearchElementId(isNext); + + if (null != Id) + this.WordControl.m_oLogicDocument.SelectSearchElement(Id); + + var result = SearchEngine.Count; + if (callback) + callback(result); + return result; }; + // returns: CSearchResult asc_docs_api.prototype.sync_SearchFoundCallback = function(obj) { @@ -8115,7 +8111,6 @@ background-repeat: no-repeat;\ asc_docs_api.prototype['goToNextSearchResult'] = asc_docs_api.prototype.goToNextSearchResult; asc_docs_api.prototype['gotoSearchResultText'] = asc_docs_api.prototype.gotoSearchResultText; asc_docs_api.prototype['stopSearchText'] = asc_docs_api.prototype.stopSearchText; - asc_docs_api.prototype['findText'] = asc_docs_api.prototype.findText; asc_docs_api.prototype['asc_searchEnabled'] = asc_docs_api.prototype.asc_searchEnabled; asc_docs_api.prototype['asc_findText'] = asc_docs_api.prototype.asc_findText; asc_docs_api.prototype['asc_replaceText'] = asc_docs_api.prototype.asc_replaceText; diff --git a/word/api.js b/word/api.js index 60d5e6ecf7..d165c48ba0 100644 --- a/word/api.js +++ b/word/api.js @@ -3123,22 +3123,30 @@ background-repeat: no-repeat;\ } }; - asc_docs_api.prototype.asc_findText = function(text, isNext, isMatchCase) + asc_docs_api.prototype.asc_findText = function(text, isNext, isMatchCase, callback) { + var result = 0; + var isAsync = false; if (null != this.WordControl.m_oDrawingDocument.m_oDocumentRenderer) { - this.WordControl.m_oDrawingDocument.m_oDocumentRenderer.findText(text, isMatchCase, isNext); - return this.WordControl.m_oDrawingDocument.m_oDocumentRenderer.SearchResults.Count; + isAsync = (true === this.WordControl.m_oDrawingDocument.m_oDocumentRenderer.findText(text, isMatchCase, isNext, callback)) ? true : false; + result = this.WordControl.m_oDrawingDocument.m_oDocumentRenderer.SearchResults.Count; } + else + { + var SearchEngine = editor.WordControl.m_oLogicDocument.Search(text, {MatchCase: isMatchCase}); - var SearchEngine = editor.WordControl.m_oLogicDocument.Search(text, {MatchCase : isMatchCase}); + var Id = this.WordControl.m_oLogicDocument.GetSearchElementId(isNext); - var Id = this.WordControl.m_oLogicDocument.GetSearchElementId(isNext); + if (null != Id) + this.WordControl.m_oLogicDocument.SelectSearchElement(Id); - if (null != Id) - this.WordControl.m_oLogicDocument.SelectSearchElement(Id); + result = SearchEngine.Count; + } - return SearchEngine.Count; + if (!isAsync && callback) + callback(result); + return result; }; asc_docs_api.prototype.asc_replaceText = function(text, replaceWith, isReplaceAll, isMatchCase)