From 2f8408ffcc0be1321bcd105e7675a1210b8f7df8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 5 Sep 2020 21:43:19 -0400 Subject: Text scanner refactor (#771) * Create searchAt wrappers * Add optional support for searching on the click event * Update QueryParser to use TextScanner's searchOnClick functionality * Move/rename searchAt * Move pendingLookup checks * Add 'searched' event to TextScanner * Use common searched event for Frontend and QueryParser * Move functions, make private --- ext/bg/js/query-parser.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/query-parser.js b/ext/bg/js/query-parser.js index b0ef2d78..a2a04606 100644 --- a/ext/bg/js/query-parser.js +++ b/ext/bg/js/query-parser.js @@ -36,15 +36,18 @@ class QueryParser extends EventDispatcher { node: this._queryParser, ignoreElements: () => [], ignorePoint: null, - search: this._search.bind(this), - documentUtil + getOptionsContext, + documentUtil, + searchTerms: true, + searchKanji: false, + searchOnClick: true }); } async prepare() { await this._queryParserGenerator.prepare(); this._textScanner.prepare(); - this._queryParser.addEventListener('click', this._onClick.bind(this)); + this._textScanner.on('searched', this._onSearched.bind(this)); } setOptions({selectedParser, termSpacing, scanning}) { @@ -76,18 +79,12 @@ class QueryParser extends EventDispatcher { // Private - _onClick(e) { - this._textScanner.searchAt(e.clientX, e.clientY, 'click'); - } - - async _search(textSource, cause) { - if (textSource === null) { return null; } - - const optionsContext = this._getOptionsContext(); - const results = await this._textScanner.findTerms(textSource, optionsContext); - if (results === null) { return null; } - - const {definitions, sentence, type} = results; + _onSearched({type, definitions, sentence, cause, textSource, optionsContext, error}) { + if (error !== null) { + yomichan.logError(error); + return; + } + if (type === null) { return; } this.trigger('searched', { type, @@ -97,8 +94,6 @@ class QueryParser extends EventDispatcher { textSource, optionsContext }); - - return {definitions, type}; } _onParserChange(e) { -- cgit v1.2.3