2019-10-29 21:49:36 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2019-2021 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
|
|
|
|
* 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 {
|
2021-01-12 04:13:35 +00:00
|
|
|
constructor({getSearchContext, documentUtil}) {
|
2020-07-24 20:03:11 +00:00
|
|
|
super();
|
2021-01-12 04:13:35 +00:00
|
|
|
this._getSearchContext = getSearchContext;
|
2020-08-09 17:27:21 +00:00
|
|
|
this._documentUtil = documentUtil;
|
2020-11-19 01:15:30 +00:00
|
|
|
this._text = '';
|
|
|
|
this._setTextToken = null;
|
|
|
|
this._selectedParser = null;
|
2020-05-07 23:41:27 +00:00
|
|
|
this._parseResults = [];
|
|
|
|
this._queryParser = document.querySelector('#query-parser-content');
|
2020-11-08 18:25:07 +00:00
|
|
|
this._queryParserModeContainer = document.querySelector('#query-parser-mode-container');
|
|
|
|
this._queryParserModeSelect = document.querySelector('#query-parser-mode-select');
|
2020-05-08 23:05:50 +00:00
|
|
|
this._textScanner = new TextScanner({
|
|
|
|
node: this._queryParser,
|
2021-01-12 04:13:35 +00:00
|
|
|
getSearchContext,
|
2020-09-06 01:43:19 +00:00
|
|
|
documentUtil,
|
|
|
|
searchTerms: true,
|
|
|
|
searchKanji: false,
|
|
|
|
searchOnClick: true
|
2020-05-08 23:05:50 +00:00
|
|
|
});
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 01:15:30 +00:00
|
|
|
get text() {
|
|
|
|
return this._text;
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:35:32 +00:00
|
|
|
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-11-08 18:25:07 +00:00
|
|
|
this._queryParserModeSelect.addEventListener('change', this._onParserChange.bind(this), false);
|
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) {
|
2020-11-19 01:15:30 +00:00
|
|
|
this._text = text;
|
|
|
|
this._setPreview(text);
|
2020-05-07 23:41:27 +00:00
|
|
|
|
2020-11-19 01:15:30 +00:00
|
|
|
const token = {};
|
|
|
|
this._setTextToken = token;
|
|
|
|
this._parseResults = await api.textParse(text, this._getOptionsContext());
|
|
|
|
if (this._setTextToken !== token) { return; }
|
2020-05-07 23:41:27 +00:00
|
|
|
|
2020-11-19 01:15:30 +00:00
|
|
|
this._refreshSelectedParser();
|
|
|
|
|
|
|
|
this._renderParserSelect();
|
|
|
|
this._renderParseResult();
|
2020-05-07 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_onSearched(e) {
|
|
|
|
const {error} = e;
|
2020-09-06 01:43:19 +00:00
|
|
|
if (error !== null) {
|
|
|
|
yomichan.logError(error);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-17 16:43:05 +00:00
|
|
|
if (e.type === null) { return; }
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
this.trigger('searched', e);
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_onParserChange(e) {
|
2020-11-08 18:25:07 +00:00
|
|
|
const value = e.currentTarget.value;
|
2020-08-09 17:19:42 +00:00
|
|
|
this._setSelectedParser(value);
|
|
|
|
}
|
|
|
|
|
2021-01-12 04:13:35 +00:00
|
|
|
_getOptionsContext() {
|
|
|
|
return this._getSearchContext().optionsContext;
|
|
|
|
}
|
|
|
|
|
2020-08-09 17:19:42 +00:00
|
|
|
_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) {
|
2020-11-08 17:35:32 +00:00
|
|
|
const terms = [[{text, reading: ''}]];
|
2020-05-07 23:41:27 +00:00
|
|
|
this._queryParser.textContent = '';
|
2020-11-08 17:50:16 +00:00
|
|
|
this._queryParser.appendChild(this._createParseResult(terms, true));
|
2019-11-03 13:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 23:41:27 +00:00
|
|
|
_renderParserSelect() {
|
2020-11-08 18:25:07 +00:00
|
|
|
const visible = (this._parseResults.length > 1);
|
|
|
|
if (visible) {
|
|
|
|
this._updateParserModeSelect(this._queryParserModeSelect, this._parseResults, this._selectedParser);
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
2020-11-08 18:25:07 +00:00
|
|
|
this._queryParserModeContainer.hidden = !visible;
|
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-11-08 17:50:16 +00:00
|
|
|
this._queryParser.appendChild(this._createParseResult(parseResult.content, false));
|
|
|
|
}
|
|
|
|
|
2020-11-08 18:25:07 +00:00
|
|
|
_updateParserModeSelect(select, parseResults, selectedParser) {
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
let selectedIndex = -1;
|
2020-11-08 17:50:16 +00:00
|
|
|
for (const parseResult of parseResults) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = parseResult.id;
|
|
|
|
switch (parseResult.source) {
|
|
|
|
case 'scanning-parser':
|
|
|
|
option.textContent = 'Scanning parser';
|
|
|
|
break;
|
|
|
|
case 'mecab':
|
|
|
|
option.textContent = `MeCab: ${parseResult.dictionary}`;
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-08 18:25:07 +00:00
|
|
|
option.textContent = `Unknown source: ${parseResult.source}`;
|
2020-11-08 17:50:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-08 18:25:07 +00:00
|
|
|
fragment.appendChild(option);
|
|
|
|
|
|
|
|
if (selectedParser === parseResult.id) {
|
|
|
|
selectedIndex = index;
|
|
|
|
}
|
|
|
|
++index;
|
2020-11-08 17:50:16 +00:00
|
|
|
}
|
2020-11-08 18:25:07 +00:00
|
|
|
|
|
|
|
select.textContent = '';
|
|
|
|
select.appendChild(fragment);
|
|
|
|
select.selectedIndex = selectedIndex;
|
2020-11-08 17:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_createParseResult(terms, preview) {
|
|
|
|
const type = preview ? 'preview' : 'normal';
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const term of terms) {
|
|
|
|
const termNode = document.createElement('span');
|
|
|
|
termNode.className = 'query-parser-term';
|
|
|
|
termNode.dataset.type = type;
|
|
|
|
for (const segment of term) {
|
|
|
|
if (segment.reading.trim().length === 0) {
|
|
|
|
this._addSegmentText(segment.text, termNode);
|
|
|
|
} else {
|
|
|
|
termNode.appendChild(this._createSegment(segment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment.appendChild(termNode);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createSegment(segment) {
|
|
|
|
const segmentNode = document.createElement('ruby');
|
|
|
|
segmentNode.className = 'query-parser-segment';
|
|
|
|
|
|
|
|
const textNode = document.createElement('span');
|
|
|
|
textNode.className = 'query-parser-segment-text';
|
|
|
|
|
|
|
|
const readingNode = document.createElement('rt');
|
|
|
|
readingNode.className = 'query-parser-segment-reading';
|
|
|
|
|
|
|
|
segmentNode.appendChild(textNode);
|
|
|
|
segmentNode.appendChild(readingNode);
|
|
|
|
|
|
|
|
this._addSegmentText(segment.text, textNode);
|
|
|
|
readingNode.textContent = segment.reading;
|
|
|
|
|
|
|
|
return segmentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addSegmentText(text, container) {
|
|
|
|
for (const character of text) {
|
|
|
|
const node = document.createElement('span');
|
|
|
|
node.className = 'query-parser-char';
|
|
|
|
node.textContent = character;
|
|
|
|
container.appendChild(node);
|
|
|
|
}
|
2019-11-12 21:57:21 +00:00
|
|
|
}
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|