use dependency injection in QueryParser

Also fix an issue with settings update triggering a lookup on unprepared
QueryParser.
This commit is contained in:
siikamiika 2020-03-14 02:51:39 +02:00
parent 962c2a381f
commit 46c6ad98f3
2 changed files with 39 additions and 15 deletions

View File

@ -27,9 +27,12 @@
*/ */
class QueryParser extends TextScanner { class QueryParser extends TextScanner {
constructor(search) { constructor({getOptionsContext, setContent, setSpinnerVisible}) {
super(document.querySelector('#query-parser-content'), [], []); super(document.querySelector('#query-parser-content'), [], []);
this.search = search;
this.getOptionsContext = getOptionsContext;
this.setContent = setContent;
this.setSpinnerVisible = setSpinnerVisible;
this.parseResults = []; this.parseResults = [];
@ -55,18 +58,18 @@ class QueryParser extends TextScanner {
async onSearchSource(textSource, cause) { async onSearchSource(textSource, cause) {
if (textSource === null) { return null; } if (textSource === null) { return null; }
this.setTextSourceScanLength(textSource, this.search.options.scanning.length); this.setTextSourceScanLength(textSource, this.options.scanning.length);
const searchText = textSource.text(); const searchText = textSource.text();
if (searchText.length === 0) { return; } if (searchText.length === 0) { return; }
const {definitions, length} = await apiTermsFind(searchText, {}, this.search.getOptionsContext()); const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext());
if (definitions.length === 0) { return null; } if (definitions.length === 0) { return null; }
const sentence = docSentenceExtract(textSource, this.search.options.anki.sentenceExt); const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
textSource.setEndOffset(length); textSource.setEndOffset(length);
this.search.setContent('terms', {definitions, context: { this.setContent('terms', {definitions, context: {
focus: false, focus: false,
disableHistory: cause === 'mouse', disableHistory: cause === 'mouse',
sentence, sentence,
@ -78,7 +81,7 @@ class QueryParser extends TextScanner {
onParserChange(e) { onParserChange(e) {
const selectedParser = e.target.value; const selectedParser = e.target.value;
apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); apiOptionsSet({parsing: {selectedParser}}, this.getOptionsContext());
} }
getMouseEventListeners() { getMouseEventListeners() {
@ -107,11 +110,23 @@ class QueryParser extends TextScanner {
this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`; this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
} }
getOptionsContext() {
throw new Error('Override me');
}
setContent(_type, _details) {
throw new Error('Override me');
}
setSpinnerVisible(_visible) {
throw new Error('Override me');
}
refreshSelectedParser() { refreshSelectedParser() {
if (this.parseResults.length > 0) { if (this.parseResults.length > 0) {
if (!this.getParseResult()) { if (!this.getParseResult()) {
const selectedParser = this.parseResults[0].id; const selectedParser = this.parseResults[0].id;
apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); apiOptionsSet({parsing: {selectedParser}}, this.getOptionsContext());
} }
} }
} }
@ -122,7 +137,7 @@ class QueryParser extends TextScanner {
} }
async setText(text) { async setText(text) {
this.search.setSpinnerVisible(true); this.setSpinnerVisible(true);
this.setPreview(text); this.setPreview(text);
@ -132,20 +147,20 @@ class QueryParser extends TextScanner {
this.renderParserSelect(); this.renderParserSelect();
this.renderParseResult(); this.renderParseResult();
this.search.setSpinnerVisible(false); this.setSpinnerVisible(false);
} }
async parseText(text) { async parseText(text) {
const results = []; const results = [];
if (this.search.options.parsing.enableScanningParser) { if (this.options.parsing.enableScanningParser) {
results.push({ results.push({
name: 'Scanning parser', name: 'Scanning parser',
id: 'scan', id: 'scan',
parsedText: await apiTextParse(text, this.search.getOptionsContext()) parsedText: await apiTextParse(text, this.getOptionsContext())
}); });
} }
if (this.search.options.parsing.enableMecabParser) { if (this.options.parsing.enableMecabParser) {
const mecabResults = await apiTextParseMecab(text, this.search.getOptionsContext()); const mecabResults = await apiTextParseMecab(text, this.getOptionsContext());
for (const [mecabDictName, mecabDictResults] of mecabResults) { for (const [mecabDictName, mecabDictResults] of mecabResults) {
results.push({ results.push({
name: `MeCab: ${mecabDictName}`, name: `MeCab: ${mecabDictName}`,

View File

@ -29,12 +29,18 @@ class DisplaySearch extends Display {
constructor() { constructor() {
super(document.querySelector('#spinner'), document.querySelector('#content')); super(document.querySelector('#spinner'), document.querySelector('#content'));
this._isPrepared = false;
this.optionsContext = { this.optionsContext = {
depth: 0, depth: 0,
url: window.location.href url: window.location.href
}; };
this.queryParser = new QueryParser(this); this.queryParser = new QueryParser({
getOptionsContext: this.getOptionsContext.bind(this),
setContent: this.setContent.bind(this),
setSpinnerVisible: this.setSpinnerVisible.bind(this)
});
this.search = document.querySelector('#search'); this.search = document.querySelector('#search');
this.query = document.querySelector('#query'); this.query = document.querySelector('#query');
@ -112,6 +118,8 @@ class DisplaySearch extends Display {
this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this)); this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this));
this.updateSearchButton(); this.updateSearchButton();
this._isPrepared = true;
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
} }
@ -278,6 +286,7 @@ class DisplaySearch extends Display {
async updateOptions() { async updateOptions() {
await super.updateOptions(); await super.updateOptions();
this.queryParser.setOptions(this.options); this.queryParser.setOptions(this.options);
if (!this._isPrepared) { return; }
const query = this.query.value; const query = this.query.value;
if (query) { if (query) {
this.setQuery(query); this.setQuery(query);