Query parser refactor (#683)

* Rename files to better match class name

* Don't pass setContent to QueryParser; use a generic event instead
This commit is contained in:
toasted-nutbread 2020-07-24 16:03:11 -04:00 committed by GitHub
parent 99f5655e53
commit 3754c92041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 12 deletions

View File

@ -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'};
}

View File

@ -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();

View File

@ -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>