Refactor text scanner options context generation (#1225)

* Refactor how options context is created and updated

* Udpate TextScanner/QueryParser to use getSearchContext instead of getOptionsContext
This commit is contained in:
toasted-nutbread 2021-01-11 23:13:35 -05:00 committed by GitHub
parent 5e87a490f7
commit 983e2c7936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 14 deletions

View File

@ -21,9 +21,9 @@
*/ */
class QueryParser extends EventDispatcher { class QueryParser extends EventDispatcher {
constructor({getOptionsContext, documentUtil}) { constructor({getSearchContext, documentUtil}) {
super(); super();
this._getOptionsContext = getOptionsContext; this._getSearchContext = getSearchContext;
this._documentUtil = documentUtil; this._documentUtil = documentUtil;
this._text = ''; this._text = '';
this._setTextToken = null; this._setTextToken = null;
@ -34,7 +34,7 @@ class QueryParser extends EventDispatcher {
this._queryParserModeSelect = document.querySelector('#query-parser-mode-select'); this._queryParserModeSelect = document.querySelector('#query-parser-mode-select');
this._textScanner = new TextScanner({ this._textScanner = new TextScanner({
node: this._queryParser, node: this._queryParser,
getOptionsContext, getSearchContext,
documentUtil, documentUtil,
searchTerms: true, searchTerms: true,
searchKanji: false, searchKanji: false,
@ -82,7 +82,7 @@ class QueryParser extends EventDispatcher {
// Private // Private
_onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, error}) { _onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, detail, error}) {
if (error !== null) { if (error !== null) {
yomichan.logError(error); yomichan.logError(error);
return; return;
@ -95,7 +95,8 @@ class QueryParser extends EventDispatcher {
sentence, sentence,
inputInfo, inputInfo,
textSource, textSource,
optionsContext optionsContext,
detail
}); });
} }
@ -104,6 +105,10 @@ class QueryParser extends EventDispatcher {
this._setSelectedParser(value); this._setSelectedParser(value);
} }
_getOptionsContext() {
return this._getSearchContext().optionsContext;
}
_refreshSelectedParser() { _refreshSelectedParser() {
if (this._parseResults.length > 0 && !this._getParseResult()) { if (this._parseResults.length > 0 && !this._getParseResult()) {
const value = this._parseResults[0].id; const value = this._parseResults[0].id;

View File

@ -54,7 +54,7 @@ class Frontend {
node: window, node: window,
ignoreElements: this._ignoreElements.bind(this), ignoreElements: this._ignoreElements.bind(this),
ignorePoint: this._ignorePoint.bind(this), ignorePoint: this._ignorePoint.bind(this),
getOptionsContext: this._getOptionsContext.bind(this), getSearchContext: this._getSearchContext.bind(this),
documentUtil: this._documentUtil, documentUtil: this._documentUtil,
searchTerms: true, searchTerms: true,
searchKanji: true searchKanji: true
@ -615,6 +615,10 @@ class Frontend {
} }
async _getOptionsContext() { async _getOptionsContext() {
return (await this._getSearchContext()).optionsContext;
}
async _getSearchContext() {
if (this._optionsContextOverride !== null) { if (this._optionsContextOverride !== null) {
return this._optionsContextOverride; return this._optionsContextOverride;
} }
@ -629,6 +633,6 @@ class Frontend {
} }
const depth = this._depth; const depth = this._depth;
return {depth, url}; return {optionsContext: {depth, url}};
} }
} }

View File

@ -82,7 +82,7 @@ class Display extends EventDispatcher {
this._queryParserVisibleOverride = null; this._queryParserVisibleOverride = null;
this._queryParserContainer = document.querySelector('#query-parser-container'); this._queryParserContainer = document.querySelector('#query-parser-container');
this._queryParser = new QueryParser({ this._queryParser = new QueryParser({
getOptionsContext: this.getOptionsContext.bind(this), getSearchContext: this._getSearchContext.bind(this),
documentUtil: this._documentUtil documentUtil: this._documentUtil
}); });
this._mode = null; this._mode = null;
@ -1764,7 +1764,7 @@ class Display extends EventDispatcher {
if (this._definitionTextScanner === null) { if (this._definitionTextScanner === null) {
this._definitionTextScanner = new TextScanner({ this._definitionTextScanner = new TextScanner({
node: window, node: window,
getOptionsContext: this.getOptionsContext.bind(this), getSearchContext: this._getSearchContext.bind(this),
documentUtil: this._documentUtil, documentUtil: this._documentUtil,
searchTerms: true, searchTerms: true,
searchKanji: false, searchKanji: false,
@ -1861,6 +1861,10 @@ class Display extends EventDispatcher {
} }
} }
_getSearchContext() {
return {optionsContext: this.getOptionsContext()};
}
_startFrameResize(e) { _startFrameResize(e) {
if (this._frameResizeToken !== null) { return; } if (this._frameResizeToken !== null) { return; }

View File

@ -24,7 +24,7 @@ class TextScanner extends EventDispatcher {
constructor({ constructor({
node, node,
documentUtil, documentUtil,
getOptionsContext, getSearchContext,
ignoreElements=null, ignoreElements=null,
ignorePoint=null, ignorePoint=null,
searchTerms=false, searchTerms=false,
@ -35,7 +35,7 @@ class TextScanner extends EventDispatcher {
super(); super();
this._node = node; this._node = node;
this._documentUtil = documentUtil; this._documentUtil = documentUtil;
this._getOptionsContext = getOptionsContext; this._getSearchContext = getSearchContext;
this._ignoreElements = ignoreElements; this._ignoreElements = ignoreElements;
this._ignorePoint = ignorePoint; this._ignorePoint = ignorePoint;
this._searchTerms = searchTerms; this._searchTerms = searchTerms;
@ -290,8 +290,8 @@ class TextScanner extends EventDispatcher {
// Private // Private
async _getOptionsContextForInput(inputInfo) { _createOptionsContextForInput(baseOptionsContext, inputInfo) {
const optionsContext = clone(await this._getOptionsContext()); const optionsContext = clone(baseOptionsContext);
const {modifiers, modifierKeys} = inputInfo; const {modifiers, modifierKeys} = inputInfo;
optionsContext.modifiers = [...modifiers]; optionsContext.modifiers = [...modifiers];
optionsContext.modifierKeys = [...modifierKeys]; optionsContext.modifierKeys = [...modifierKeys];
@ -305,13 +305,16 @@ class TextScanner extends EventDispatcher {
let error = null; let error = null;
let searched = false; let searched = false;
let optionsContext = null; let optionsContext = null;
let detail = null;
try { try {
if (this._textSourceCurrent !== null && this._textSourceCurrent.hasSameStart(textSource)) { if (this._textSourceCurrent !== null && this._textSourceCurrent.hasSameStart(textSource)) {
return; return;
} }
optionsContext = await this._getOptionsContextForInput(inputInfo); ({optionsContext, detail} = await this._getSearchContext());
optionsContext = this._createOptionsContextForInput(optionsContext, inputInfo);
searched = true; searched = true;
const result = await this._findDefinitions(textSource, searchTerms, searchKanji, optionsContext); const result = await this._findDefinitions(textSource, searchTerms, searchKanji, optionsContext);
@ -334,6 +337,7 @@ class TextScanner extends EventDispatcher {
inputInfo, inputInfo,
textSource, textSource,
optionsContext, optionsContext,
detail,
error error
}); });
} }