Query parser refactor (#514)
* Make functions private * Make fields private * Organize
This commit is contained in:
parent
edb86d9ec3
commit
33bd9682ea
@ -27,48 +27,67 @@
|
|||||||
class QueryParser {
|
class QueryParser {
|
||||||
constructor({getOptionsContext, setContent, setSpinnerVisible}) {
|
constructor({getOptionsContext, setContent, setSpinnerVisible}) {
|
||||||
this._options = null;
|
this._options = null;
|
||||||
this.getOptionsContext = getOptionsContext;
|
this._getOptionsContext = getOptionsContext;
|
||||||
this.setContent = setContent;
|
this._setContent = setContent;
|
||||||
this.setSpinnerVisible = setSpinnerVisible;
|
this._setSpinnerVisible = setSpinnerVisible;
|
||||||
|
this._parseResults = [];
|
||||||
this.parseResults = [];
|
this._queryParser = document.querySelector('#query-parser-content');
|
||||||
|
this._queryParserSelect = document.querySelector('#query-parser-select-container');
|
||||||
this.queryParser = document.querySelector('#query-parser-content');
|
this._queryParserGenerator = new QueryParserGenerator();
|
||||||
this.queryParserSelect = document.querySelector('#query-parser-select-container');
|
|
||||||
|
|
||||||
this.queryParserGenerator = new QueryParserGenerator();
|
|
||||||
|
|
||||||
this._textScanner = new TextScanner(
|
this._textScanner = new TextScanner(
|
||||||
this.queryParser,
|
this._queryParser,
|
||||||
() => [],
|
() => [],
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
this._textScanner.onSearchSource = this.onSearchSource.bind(this);
|
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));
|
this._queryParser.addEventListener('click', this._onClick.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
onClick2(e) {
|
setOptions(options) {
|
||||||
|
this._options = options;
|
||||||
|
this._textScanner.setOptions(options);
|
||||||
|
this._textScanner.setEnabled(true);
|
||||||
|
this._queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async setText(text) {
|
||||||
|
this._setSpinnerVisible(true);
|
||||||
|
|
||||||
|
this._setPreview(text);
|
||||||
|
|
||||||
|
this._parseResults = await apiTextParse(text, this._getOptionsContext());
|
||||||
|
this._refreshSelectedParser();
|
||||||
|
|
||||||
|
this._renderParserSelect();
|
||||||
|
this._renderParseResult();
|
||||||
|
|
||||||
|
this._setSpinnerVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private
|
||||||
|
|
||||||
|
_onClick(e) {
|
||||||
this._textScanner.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._textScanner.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);
|
||||||
|
|
||||||
this.setContent('terms', {definitions, context: {
|
this._setContent('terms', {definitions, context: {
|
||||||
focus: false,
|
focus: false,
|
||||||
disableHistory: cause === 'mouse',
|
disableHistory: cause === 'mouse',
|
||||||
sentence,
|
sentence,
|
||||||
@ -78,82 +97,61 @@ class QueryParser {
|
|||||||
return {definitions, type: 'terms'};
|
return {definitions, type: 'terms'};
|
||||||
}
|
}
|
||||||
|
|
||||||
onParserChange(e) {
|
_onParserChange(e) {
|
||||||
const value = e.target.value;
|
const value = e.target.value;
|
||||||
apiModifySettings([{
|
apiModifySettings([{
|
||||||
action: 'set',
|
action: 'set',
|
||||||
path: 'parsing.selectedParser',
|
path: 'parsing.selectedParser',
|
||||||
value,
|
value,
|
||||||
scope: 'profile',
|
scope: 'profile',
|
||||||
optionsContext: this.getOptionsContext()
|
optionsContext: this._getOptionsContext()
|
||||||
}], 'search');
|
}], 'search');
|
||||||
}
|
}
|
||||||
|
|
||||||
setOptions(options) {
|
_refreshSelectedParser() {
|
||||||
this._options = options;
|
if (this._parseResults.length > 0) {
|
||||||
this._textScanner.setOptions(options);
|
if (!this._getParseResult()) {
|
||||||
this._textScanner.setEnabled(true);
|
const value = this._parseResults[0].id;
|
||||||
this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
refreshSelectedParser() {
|
|
||||||
if (this.parseResults.length > 0) {
|
|
||||||
if (!this.getParseResult()) {
|
|
||||||
const value = this.parseResults[0].id;
|
|
||||||
apiModifySettings([{
|
apiModifySettings([{
|
||||||
action: 'set',
|
action: 'set',
|
||||||
path: 'parsing.selectedParser',
|
path: 'parsing.selectedParser',
|
||||||
value,
|
value,
|
||||||
scope: 'profile',
|
scope: 'profile',
|
||||||
optionsContext: this.getOptionsContext()
|
optionsContext: this._getOptionsContext()
|
||||||
}], 'search');
|
}], 'search');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
async setText(text) {
|
_setPreview(text) {
|
||||||
this.setSpinnerVisible(true);
|
|
||||||
|
|
||||||
this.setPreview(text);
|
|
||||||
|
|
||||||
this.parseResults = await apiTextParse(text, this.getOptionsContext());
|
|
||||||
this.refreshSelectedParser();
|
|
||||||
|
|
||||||
this.renderParserSelect();
|
|
||||||
this.renderParseResult();
|
|
||||||
|
|
||||||
this.setSpinnerVisible(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
setPreview(text) {
|
|
||||||
const previewTerms = [];
|
const previewTerms = [];
|
||||||
for (let i = 0, ii = text.length; i < ii; i += 2) {
|
for (let i = 0, ii = text.length; i < ii; i += 2) {
|
||||||
const tempText = text.substring(i, i + 2);
|
const tempText = text.substring(i, i + 2);
|
||||||
previewTerms.push([{text: tempText, reading: ''}]);
|
previewTerms.push([{text: tempText, reading: ''}]);
|
||||||
}
|
}
|
||||||
this.queryParser.textContent = '';
|
this._queryParser.textContent = '';
|
||||||
this.queryParser.appendChild(this.queryParserGenerator.createParseResult(previewTerms, true));
|
this._queryParser.appendChild(this._queryParserGenerator.createParseResult(previewTerms, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderParseResult() {
|
_renderParseResult() {
|
||||||
const parseResult = this.getParseResult();
|
const parseResult = this._getParseResult();
|
||||||
this.queryParser.textContent = '';
|
this._queryParser.textContent = '';
|
||||||
if (!parseResult) { return; }
|
if (!parseResult) { return; }
|
||||||
this.queryParser.appendChild(this.queryParserGenerator.createParseResult(parseResult.content));
|
this._queryParser.appendChild(this._queryParserGenerator.createParseResult(parseResult.content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user