From 3754c920410e90fc6b98aadc9f0dbe60dfa6a14d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 24 Jul 2020 16:03:11 -0400 Subject: [PATCH] Query parser refactor (#683) * Rename files to better match class name * Don't pass setContent to QueryParser; use a generic event instead --- ...generator.js => query-parser-generator.js} | 0 ...search-query-parser.js => query-parser.js} | 21 +++++++++++-------- ext/bg/js/search.js | 16 +++++++++++++- ext/bg/search.html | 4 ++-- 4 files changed, 29 insertions(+), 12 deletions(-) rename ext/bg/js/{search-query-parser-generator.js => query-parser-generator.js} (100%) rename ext/bg/js/{search-query-parser.js => query-parser.js} (93%) diff --git a/ext/bg/js/search-query-parser-generator.js b/ext/bg/js/query-parser-generator.js similarity index 100% rename from ext/bg/js/search-query-parser-generator.js rename to ext/bg/js/query-parser-generator.js diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/query-parser.js similarity index 93% rename from ext/bg/js/search-query-parser.js rename to 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 @@ - - + +