Remove search query parser inheritance (#509)
* Remove getMouseEventListeners override * Use this._options instead of this.options * Make QueryParser use composition instead of inheritance for TextScanner * Remove repeat call to document.querySelector('#query-parser-content')
This commit is contained in:
parent
38d6f58fac
commit
4eeb879fb8
@ -24,10 +24,9 @@
|
|||||||
* docSentenceExtract
|
* docSentenceExtract
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class QueryParser extends TextScanner {
|
class QueryParser {
|
||||||
constructor({getOptionsContext, setContent, setSpinnerVisible}) {
|
constructor({getOptionsContext, setContent, setSpinnerVisible}) {
|
||||||
super(document.querySelector('#query-parser-content'), () => [], []);
|
this._options = null;
|
||||||
|
|
||||||
this.getOptionsContext = getOptionsContext;
|
this.getOptionsContext = getOptionsContext;
|
||||||
this.setContent = setContent;
|
this.setContent = setContent;
|
||||||
this.setSpinnerVisible = setSpinnerVisible;
|
this.setSpinnerVisible = setSpinnerVisible;
|
||||||
@ -38,26 +37,34 @@ class QueryParser extends TextScanner {
|
|||||||
this.queryParserSelect = document.querySelector('#query-parser-select-container');
|
this.queryParserSelect = document.querySelector('#query-parser-select-container');
|
||||||
|
|
||||||
this.queryParserGenerator = new QueryParserGenerator();
|
this.queryParserGenerator = new QueryParserGenerator();
|
||||||
|
|
||||||
|
this._textScanner = new TextScanner(
|
||||||
|
this.queryParser,
|
||||||
|
() => [],
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
this._textScanner.onSearchSource = this.onSearchSource.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
await this.queryParserGenerator.prepare();
|
await this.queryParserGenerator.prepare();
|
||||||
|
this.queryParser.addEventListener('click', this.onClick2.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onClick2(e) {
|
onClick2(e) {
|
||||||
this.searchAt(e.clientX, e.clientY, 'click');
|
this._textScanner.searchAt(e.clientX, e.clientY, 'click');
|
||||||
}
|
}
|
||||||
|
|
||||||
async onSearchSource(textSource, cause) {
|
async onSearchSource(textSource, cause) {
|
||||||
if (textSource === null) { return null; }
|
if (textSource === null) { return null; }
|
||||||
|
|
||||||
const searchText = this.getTextSourceContent(textSource, this.options.scanning.length);
|
const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
|
||||||
if (searchText.length === 0) { return; }
|
if (searchText.length === 0) { return; }
|
||||||
|
|
||||||
const {definitions, length} = await apiTermsFind(searchText, {}, this.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.options.anki.sentenceExt);
|
const sentence = docSentenceExtract(textSource, this._options.anki.sentenceExt);
|
||||||
|
|
||||||
textSource.setEndOffset(length);
|
textSource.setEndOffset(length);
|
||||||
|
|
||||||
@ -82,16 +89,10 @@ class QueryParser extends TextScanner {
|
|||||||
}], 'search');
|
}], 'search');
|
||||||
}
|
}
|
||||||
|
|
||||||
getMouseEventListeners() {
|
|
||||||
return [
|
|
||||||
...super.getMouseEventListeners(),
|
|
||||||
[this.node, 'click', this.onClick2.bind(this)]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
setOptions(options) {
|
setOptions(options) {
|
||||||
super.setOptions(options);
|
this._options = options;
|
||||||
super.setEnabled(true);
|
this._textScanner.setOptions(options);
|
||||||
|
this._textScanner.setEnabled(true);
|
||||||
this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
|
this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +112,7 @@ class QueryParser extends TextScanner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getParseResult() {
|
getParseResult() {
|
||||||
const {selectedParser} = this.options.parsing;
|
const {selectedParser} = this._options.parsing;
|
||||||
return this.parseResults.find((r) => r.id === selectedParser);
|
return this.parseResults.find((r) => r.id === selectedParser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +143,7 @@ class QueryParser extends TextScanner {
|
|||||||
renderParserSelect() {
|
renderParserSelect() {
|
||||||
this.queryParserSelect.textContent = '';
|
this.queryParserSelect.textContent = '';
|
||||||
if (this.parseResults.length > 1) {
|
if (this.parseResults.length > 1) {
|
||||||
const {selectedParser} = this.options.parsing;
|
const {selectedParser} = this._options.parsing;
|
||||||
const select = this.queryParserGenerator.createParserSelect(this.parseResults, selectedParser);
|
const select = this.queryParserGenerator.createParserSelect(this.parseResults, selectedParser);
|
||||||
select.addEventListener('change', this.onParserChange.bind(this));
|
select.addEventListener('change', this.onParserChange.bind(this));
|
||||||
this.queryParserSelect.appendChild(select);
|
this.queryParserSelect.appendChild(select);
|
||||||
|
Loading…
Reference in New Issue
Block a user