2019-10-29 21:49:36 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2019-2020 Yomichan Authors
|
2019-10-29 21:49:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-10-29 21:49:36 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-11 02:30:36 +00:00
|
|
|
/* global
|
|
|
|
* QueryParserGenerator
|
|
|
|
* TextScanner
|
2020-05-24 17:30:40 +00:00
|
|
|
* api
|
2020-03-11 02:30:36 +00:00
|
|
|
*/
|
2019-10-29 21:49:36 +00:00
|
|
|
|
2020-07-24 20:03:11 +00:00
|
|
|
class QueryParser extends EventDispatcher {
|
2020-08-09 17:27:21 +00:00
|
|
|
constructor({getOptionsContext, setSpinnerVisible, documentUtil}) {
|
2020-07-24 20:03:11 +00:00
|
|
|
super();
|
2020-05-07 23:41:27 +00:00
|
|
|
this._getOptionsContext = getOptionsContext;
|
|
|
|
this._setSpinnerVisible = setSpinnerVisible;
|
2020-08-09 17:19:42 +00:00
|
|
|
this._selectedParser = null;
|
2020-08-09 17:27:21 +00:00
|
|
|
this._documentUtil = documentUtil;
|
2020-05-07 23:41:27 +00:00
|
|
|
this._parseResults = [];
|
|
|
|
this._queryParser = document.querySelector('#query-parser-content');
|
|
|
|
this._queryParserSelect = document.querySelector('#query-parser-select-container');
|
|
|
|
this._queryParserGenerator = new QueryParserGenerator();
|
2020-05-08 23:05:50 +00:00
|
|
|
this._textScanner = new TextScanner({
|
|
|
|
node: this._queryParser,
|
|
|
|
ignoreElements: () => [],
|
|
|
|
ignorePoint: null,
|
2020-09-06 01:43:19 +00:00
|
|
|
getOptionsContext,
|
|
|
|
documentUtil,
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: false,
|
|
|
|
searchOnClick: true
|
2020-05-08 23:05:50 +00:00
|
|
|
});
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:09:23 +00:00
|
|
|
async prepare() {
|
2020-05-07 23:41:27 +00:00
|
|
|
await this._queryParserGenerator.prepare();
|
2020-06-21 20:14:05 +00:00
|
|
|
this._textScanner.prepare();
|
2020-09-06 01:43:19 +00:00
|
|
|
this._textScanner.on('searched', this._onSearched.bind(this));
|
2020-05-07 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 18:40:44 +00:00
|
|
|
setOptions({selectedParser, termSpacing, scanning}) {
|
2020-08-09 17:19:42 +00:00
|
|
|
if (selectedParser === null || typeof selectedParser === 'string') {
|
|
|
|
this._selectedParser = selectedParser;
|
|
|
|
}
|
|
|
|
if (typeof termSpacing === 'boolean') {
|
|
|
|
this._queryParser.dataset.termSpacing = `${termSpacing}`;
|
|
|
|
}
|
|
|
|
if (scanning !== null && typeof scanning === 'object') {
|
|
|
|
this._textScanner.setOptions(scanning);
|
|
|
|
}
|
2020-05-07 23:41:27 +00:00
|
|
|
this._textScanner.setEnabled(true);
|
2020-02-10 20:09:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
async setText(text) {
|
|
|
|
this._setSpinnerVisible(true);
|
|
|
|
|
|
|
|
this._setPreview(text);
|
|
|
|
|
2020-05-24 17:30:40 +00:00
|
|
|
this._parseResults = await api.textParse(text, this._getOptionsContext());
|
2020-05-07 23:41:27 +00:00
|
|
|
this._refreshSelectedParser();
|
|
|
|
|
|
|
|
this._renderParserSelect();
|
|
|
|
this._renderParseResult();
|
|
|
|
|
|
|
|
this._setSpinnerVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2020-09-08 23:23:32 +00:00
|
|
|
_onSearched({type, definitions, sentence, input, textSource, optionsContext, error}) {
|
2020-09-06 01:43:19 +00:00
|
|
|
if (error !== null) {
|
|
|
|
yomichan.logError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type === null) { return; }
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2020-07-24 20:03:11 +00:00
|
|
|
this.trigger('searched', {
|
2020-08-22 18:40:44 +00:00
|
|
|
type,
|
2020-07-24 20:03:11 +00:00
|
|
|
definitions,
|
2020-03-07 12:20:08 +00:00
|
|
|
sentence,
|
2020-09-08 23:23:32 +00:00
|
|
|
input,
|
2020-07-24 20:03:11 +00:00
|
|
|
textSource,
|
|
|
|
optionsContext
|
|
|
|
});
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_onParserChange(e) {
|
2020-05-06 23:32:28 +00:00
|
|
|
const value = e.target.value;
|
2020-08-09 17:19:42 +00:00
|
|
|
this._setSelectedParser(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_refreshSelectedParser() {
|
|
|
|
if (this._parseResults.length > 0 && !this._getParseResult()) {
|
|
|
|
const value = this._parseResults[0].id;
|
|
|
|
this._setSelectedParser(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setSelectedParser(value) {
|
|
|
|
const optionsContext = this._getOptionsContext();
|
2020-05-24 17:30:40 +00:00
|
|
|
api.modifySettings([{
|
2020-05-06 23:32:28 +00:00
|
|
|
action: 'set',
|
|
|
|
path: 'parsing.selectedParser',
|
|
|
|
value,
|
|
|
|
scope: 'profile',
|
2020-08-09 17:19:42 +00:00
|
|
|
optionsContext
|
2020-05-06 23:32:28 +00:00
|
|
|
}], 'search');
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_getParseResult() {
|
2020-08-09 17:19:42 +00:00
|
|
|
const selectedParser = this._selectedParser;
|
2020-05-07 23:41:27 +00:00
|
|
|
return this._parseResults.find((r) => r.id === selectedParser);
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_setPreview(text) {
|
2019-11-03 13:19:42 +00:00
|
|
|
const previewTerms = [];
|
2019-12-08 20:13:22 +00:00
|
|
|
for (let i = 0, ii = text.length; i < ii; i += 2) {
|
|
|
|
const tempText = text.substring(i, i + 2);
|
2020-04-13 19:55:33 +00:00
|
|
|
previewTerms.push([{text: tempText, reading: ''}]);
|
2019-11-03 13:19:42 +00:00
|
|
|
}
|
2020-05-07 23:41:27 +00:00
|
|
|
this._queryParser.textContent = '';
|
|
|
|
this._queryParser.appendChild(this._queryParserGenerator.createParseResult(previewTerms, true));
|
2019-11-03 13:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_renderParserSelect() {
|
|
|
|
this._queryParserSelect.textContent = '';
|
|
|
|
if (this._parseResults.length > 1) {
|
2020-08-09 17:19:42 +00:00
|
|
|
const selectedParser = this._selectedParser;
|
2020-05-07 23:41:27 +00:00
|
|
|
const select = this._queryParserGenerator.createParserSelect(this._parseResults, selectedParser);
|
|
|
|
select.addEventListener('change', this._onParserChange.bind(this));
|
|
|
|
this._queryParserSelect.appendChild(select);
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_renderParseResult() {
|
|
|
|
const parseResult = this._getParseResult();
|
|
|
|
this._queryParser.textContent = '';
|
2020-02-06 02:00:02 +00:00
|
|
|
if (!parseResult) { return; }
|
2020-05-07 23:41:27 +00:00
|
|
|
this._queryParser.appendChild(this._queryParserGenerator.createParseResult(parseResult.content));
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|