Text scanner fixes and refactoring (#788)

* Fix incorrect argument being passed

* Clear causeCurrent

* Change cause string to a more general input object
This commit is contained in:
toasted-nutbread 2020-09-08 19:23:32 -04:00 committed by GitHub
parent c1b16cebe7
commit 46af4c9da4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 18 deletions

View File

@ -79,7 +79,7 @@ class QueryParser extends EventDispatcher {
// Private // Private
_onSearched({type, definitions, sentence, cause, textSource, optionsContext, error}) { _onSearched({type, definitions, sentence, input, textSource, optionsContext, error}) {
if (error !== null) { if (error !== null) {
yomichan.logError(error); yomichan.logError(error);
return; return;
@ -90,7 +90,7 @@ class QueryParser extends EventDispatcher {
type, type,
definitions, definitions,
sentence, sentence,
cause, input,
textSource, textSource,
optionsContext optionsContext
}); });

View File

@ -249,7 +249,7 @@ class Frontend {
await this.updateOptions(); await this.updateOptions();
} }
_onSearched({textScanner, type, definitions, sentence, cause, textSource, optionsContext, error}) { _onSearched({textScanner, type, definitions, sentence, input: {cause}, textSource, optionsContext, error}) {
if (error !== null) { if (error !== null) {
if (yomichan.isExtensionUnloaded) { if (yomichan.isExtensionUnloaded) {
if (textSource !== null && this._options.scanning.modifier !== 'none') { if (textSource !== null && this._options.scanning.modifier !== 'none') {

View File

@ -501,7 +501,7 @@ class Display extends EventDispatcher {
} }
} }
_onQueryParserSearch({type, definitions, sentence, cause, textSource}) { _onQueryParserSearch({type, definitions, sentence, input: {cause}, textSource}) {
const query = textSource.text(); const query = textSource.text();
const details = { const details = {
focus: false, focus: false,

View File

@ -35,7 +35,7 @@ class TextScanner extends EventDispatcher {
this._isPrepared = false; this._isPrepared = false;
this._ignoreNodes = null; this._ignoreNodes = null;
this._causeCurrent = null; this._inputCurrent = null;
this._scanTimerPromise = null; this._scanTimerPromise = null;
this._textSourceCurrent = null; this._textSourceCurrent = null;
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
@ -152,6 +152,7 @@ class TextScanner extends EventDispatcher {
} }
this._textSourceCurrent = null; this._textSourceCurrent = null;
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
this._inputCurrent = null;
} }
this.trigger('clearSelection', {passive}); this.trigger('clearSelection', {passive});
} }
@ -171,20 +172,20 @@ class TextScanner extends EventDispatcher {
} }
async searchLast() { async searchLast() {
if (this._textSourceCurrent !== null && this._causeCurrent !== null) { if (this._textSourceCurrent !== null && this._inputCurrent !== null) {
await this._search(this._textSourceCurrent, this._causeCurrent); await this._search(this._textSourceCurrent, this._inputCurrent);
return true; return true;
} }
return false; return false;
} }
async search(textSource) { async search(textSource) {
return await this._search(textSource, 'script'); return await this._search(textSource, {cause: 'script'});
} }
// Private // Private
async _search(textSource, cause) { async _search(textSource, input) {
let definitions = null; let definitions = null;
let sentence = null; let sentence = null;
let type = null; let type = null;
@ -200,10 +201,10 @@ class TextScanner extends EventDispatcher {
optionsContext = await this._getOptionsContext(); optionsContext = await this._getOptionsContext();
searched = true; searched = true;
const result = await this._findDefinitions(textSource, cause); const result = await this._findDefinitions(textSource, optionsContext);
if (result !== null) { if (result !== null) {
({definitions, sentence, type} = result); ({definitions, sentence, type} = result);
this._causeCurrent = cause; this._inputCurrent = input;
this.setCurrentTextSource(textSource); this.setCurrentTextSource(textSource);
} }
} catch (e) { } catch (e) {
@ -217,7 +218,7 @@ class TextScanner extends EventDispatcher {
type, type,
definitions, definitions,
sentence, sentence,
cause, input,
textSource, textSource,
optionsContext, optionsContext,
error error
@ -271,7 +272,7 @@ class TextScanner extends EventDispatcher {
_onClick(e) { _onClick(e) {
if (this._searchOnClick) { if (this._searchOnClick) {
this._searchAt(e.clientX, e.clientY, 'click'); this._searchAt(e.clientX, e.clientY, {cause: 'click'});
} }
if (this._preventNextClick) { if (this._preventNextClick) {
@ -344,7 +345,7 @@ class TextScanner extends EventDispatcher {
return; return;
} }
this._searchAt(primaryTouch.clientX, primaryTouch.clientY, 'touchMove'); this._searchAt(primaryTouch.clientX, primaryTouch.clientY, {cause: 'touchMove'});
e.preventDefault(); // Disable scroll e.preventDefault(); // Disable scroll
} }
@ -467,7 +468,7 @@ class TextScanner extends EventDispatcher {
return {definitions, sentence, type: 'kanji'}; return {definitions, sentence, type: 'kanji'};
} }
async _searchAt(x, y, cause) { async _searchAt(x, y, input) {
if (this._pendingLookup) { return; } if (this._pendingLookup) { return; }
try { try {
@ -480,7 +481,7 @@ class TextScanner extends EventDispatcher {
const textSource = this._documentUtil.getRangeFromPoint(x, y, this._deepContentScan); const textSource = this._documentUtil.getRangeFromPoint(x, y, this._deepContentScan);
try { try {
await this._search(textSource, cause); await this._search(textSource, input);
} finally { } finally {
if (textSource !== null) { if (textSource !== null) {
textSource.cleanup(); textSource.cleanup();
@ -503,7 +504,7 @@ class TextScanner extends EventDispatcher {
} }
} }
await this._searchAt(x, y, 'mouse'); await this._searchAt(x, y, {cause: 'mouse'});
} }
async _searchAtFromTouchStart(x, y) { async _searchAtFromTouchStart(x, y) {
@ -511,7 +512,7 @@ class TextScanner extends EventDispatcher {
const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null; const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null;
await this._searchAt(x, y, 'touchStart'); await this._searchAt(x, y, {cause: 'touchStart'});
if ( if (
this._textSourceCurrent !== null && this._textSourceCurrent !== null &&