diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-07-24 16:03:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-24 16:03:11 -0400 |
commit | 3754c920410e90fc6b98aadc9f0dbe60dfa6a14d (patch) | |
tree | 80bf3a530f4c7cb0254fc8d06dc4c42e94d3039c | |
parent | 99f5655e534421aca110bcf9bcdbf899b001f0dc (diff) |
Query parser refactor (#683)
* Rename files to better match class name
* Don't pass setContent to QueryParser; use a generic event instead
-rw-r--r-- | ext/bg/js/query-parser-generator.js (renamed from ext/bg/js/search-query-parser-generator.js) | 0 | ||||
-rw-r--r-- | ext/bg/js/query-parser.js (renamed from ext/bg/js/search-query-parser.js) | 21 | ||||
-rw-r--r-- | ext/bg/js/search.js | 16 | ||||
-rw-r--r-- | ext/bg/search.html | 4 |
4 files changed, 29 insertions, 12 deletions
diff --git a/ext/bg/js/search-query-parser-generator.js b/ext/bg/js/query-parser-generator.js index 6989e157..6989e157 100644 --- a/ext/bg/js/search-query-parser-generator.js +++ b/ext/bg/js/query-parser-generator.js diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/query-parser.js index 86524b66..88c40c93 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/query-parser.js @@ -22,11 +22,11 @@ * docSentenceExtract */ -class QueryParser { - constructor({getOptionsContext, setContent, setSpinnerVisible}) { +class QueryParser extends EventDispatcher { + constructor({getOptionsContext, setSpinnerVisible}) { + super(); this._options = null; this._getOptionsContext = getOptionsContext; - this._setContent = setContent; this._setSpinnerVisible = setSpinnerVisible; this._parseResults = []; this._queryParser = document.querySelector('#query-parser-content'); @@ -80,7 +80,8 @@ class QueryParser { const searchText = this._textScanner.getTextSourceContent(textSource, scanLength, layoutAwareScan); if (searchText.length === 0) { return null; } - const {definitions, length} = await api.termsFind(searchText, {}, this._getOptionsContext()); + const optionsContext = this._getOptionsContext(); + const {definitions, length} = await api.termsFind(searchText, {}, optionsContext); if (definitions.length === 0) { return null; } const sentenceExtent = this._options.anki.sentenceExt; @@ -88,12 +89,14 @@ class QueryParser { textSource.setEndOffset(length, layoutAwareScan); - this._setContent('terms', {definitions, context: { - focus: false, - disableHistory: cause === 'mouse', + this.trigger('searched', { + type: 'terms', + definitions, sentence, - url: window.location.href - }}); + cause, + textSource, + optionsContext + }); return {definitions, type: 'terms'}; } diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 5be71555..b3e3ebca 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -40,7 +40,6 @@ class DisplaySearch extends Display { }); this._queryParser = new QueryParser({ getOptionsContext: this.getOptionsContext.bind(this), - setContent: this.setContent.bind(this), setSpinnerVisible: this.setSpinnerVisible.bind(this) }); this._onKeyDownIgnoreKeys = new Map([ @@ -67,6 +66,9 @@ class DisplaySearch extends Display { await this.updateOptions(); yomichan.on('optionsUpdated', () => this.updateOptions()); await this._queryParser.prepare(); + + this._queryParser.on('searched', this._onQueryParserSearch.bind(this)); + const options = this.getOptions(); const {queryParams: {query='', mode=''}} = parseUrl(window.location.href); @@ -169,6 +171,18 @@ class DisplaySearch extends Display { // Private + _onQueryParserSearch({type, definitions, sentence, cause}) { + this.setContent(type, { + definitions, + context: { + focus: false, + disableHistory: cause === 'mouse', + sentence, + url: window.location.href + } + }); + } + _onSearchInput() { this._updateSearchButton(); diff --git a/ext/bg/search.html b/ext/bg/search.html index cfcf1f96..8f7c1d4c 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -92,8 +92,8 @@ <script src="/mixed/js/text-scanner.js"></script> <script src="/mixed/js/template-handler.js"></script> - <script src="/bg/js/search-query-parser-generator.js"></script> - <script src="/bg/js/search-query-parser.js"></script> + <script src="/bg/js/query-parser-generator.js"></script> + <script src="/bg/js/query-parser.js"></script> <script src="/bg/js/clipboard-monitor.js"></script> <script src="/bg/js/search.js"></script> |