2019-10-29 21:49:36 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2019-2022 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
|
|
|
|
*/
|
2019-10-29 21:49:36 +00:00
|
|
|
|
2020-07-24 20:03:11 +00:00
|
|
|
class QueryParser extends EventDispatcher {
|
2022-09-24 20:05:19 +00:00
|
|
|
constructor({getSearchContext, japaneseUtil}) {
|
2020-07-24 20:03:11 +00:00
|
|
|
super();
|
2021-01-12 04:13:35 +00:00
|
|
|
this._getSearchContext = getSearchContext;
|
2021-07-09 20:05:57 +00:00
|
|
|
this._japaneseUtil = japaneseUtil;
|
2020-11-19 01:15:30 +00:00
|
|
|
this._text = '';
|
|
|
|
this._setTextToken = null;
|
|
|
|
this._selectedParser = null;
|
2021-07-09 20:05:57 +00:00
|
|
|
this._readingMode = 'none';
|
|
|
|
this._scanLength = 1;
|
|
|
|
this._useInternalParser = true;
|
|
|
|
this._useMecabParser = false;
|
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
|
|
|
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();
|
2021-05-20 00:09:37 +00:00
|
|
|
this._textScanner.on('clear', this._onTextScannerClear.bind(this));
|
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
|
|
|
}
|
|
|
|
|
2021-07-09 20:05:57 +00:00
|
|
|
setOptions({selectedParser, termSpacing, readingMode, useInternalParser, useMecabParser, scanning}) {
|
2021-02-08 22:53:17 +00:00
|
|
|
let selectedParserChanged = false;
|
2020-08-09 17:19:42 +00:00
|
|
|
if (selectedParser === null || typeof selectedParser === 'string') {
|
2021-02-08 22:53:17 +00:00
|
|
|
selectedParserChanged = (this._selectedParser !== selectedParser);
|
2020-08-09 17:19:42 +00:00
|
|
|
this._selectedParser = selectedParser;
|
|
|
|
}
|
|
|
|
if (typeof termSpacing === 'boolean') {
|
|
|
|
this._queryParser.dataset.termSpacing = `${termSpacing}`;
|
|
|
|
}
|
2021-07-09 20:05:57 +00:00
|
|
|
if (typeof readingMode === 'string') {
|
|
|
|
this._readingMode = readingMode;
|
|
|
|
}
|
|
|
|
if (typeof useInternalParser === 'boolean') {
|
|
|
|
this._useInternalParser = useInternalParser;
|
|
|
|
}
|
|
|
|
if (typeof useMecabParser === 'boolean') {
|
|
|
|
this._useMecabParser = useMecabParser;
|
|
|
|
}
|
2020-08-09 17:19:42 +00:00
|
|
|
if (scanning !== null && typeof scanning === 'object') {
|
2021-07-09 20:05:57 +00:00
|
|
|
const {scanLength} = scanning;
|
|
|
|
if (typeof scanLength === 'number') {
|
|
|
|
this._scanLength = scanLength;
|
|
|
|
}
|
2020-08-09 17:19:42 +00:00
|
|
|
this._textScanner.setOptions(scanning);
|
|
|
|
}
|
2020-05-07 23:41:27 +00:00
|
|
|
this._textScanner.setEnabled(true);
|
2021-02-08 22:53:17 +00:00
|
|
|
if (selectedParserChanged && this._parseResults.length > 0) {
|
|
|
|
this._renderParseResult();
|
|
|
|
}
|
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;
|
2021-07-09 20:05:57 +00:00
|
|
|
this._parseResults = await yomichan.api.parseText(text, this._getOptionsContext(), this._scanLength, this._useInternalParser, this._useMecabParser);
|
2020-11-19 01:15:30 +00:00
|
|
|
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-05-20 00:09:37 +00:00
|
|
|
_onTextScannerClear() {
|
|
|
|
this._textScanner.clearSelection();
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:43:05 +00:00
|
|
|
_onSearched(e) {
|
|
|
|
const {error} = e;
|
2020-09-06 01:43:19 +00:00
|
|
|
if (error !== null) {
|
2021-02-14 22:52:01 +00:00
|
|
|
log.error(error);
|
2020-09-06 01:43:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-17 16:43:05 +00:00
|
|
|
if (e.type === null) { return; }
|
2019-10-30 01:58:24 +00:00
|
|
|
|
2021-09-27 23:07:28 +00:00
|
|
|
e.sentenceOffset = this._getSentenceOffset(e.textSource);
|
|
|
|
|
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();
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.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 = '';
|
2021-02-28 21:38:01 +00:00
|
|
|
this._queryParser.dataset.parsed = 'false';
|
|
|
|
this._queryParser.appendChild(this._createParseResult(terms));
|
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 = '';
|
2021-02-28 21:38:01 +00:00
|
|
|
this._queryParser.dataset.parsed = 'true';
|
2020-02-06 02:00:02 +00:00
|
|
|
if (!parseResult) { return; }
|
2021-03-03 23:06:12 +00:00
|
|
|
this._queryParser.appendChild(this._createParseResult(parseResult.content));
|
2020-11-08 17:50:16 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-09 20:05:57 +00:00
|
|
|
_createParseResult(data) {
|
2021-09-27 23:07:28 +00:00
|
|
|
let offset = 0;
|
2020-11-08 17:50:16 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
2021-07-09 20:05:57 +00:00
|
|
|
for (const term of data) {
|
2020-11-08 17:50:16 +00:00
|
|
|
const termNode = document.createElement('span');
|
|
|
|
termNode.className = 'query-parser-term';
|
2021-09-27 23:07:28 +00:00
|
|
|
termNode.dataset.offset = `${offset}`;
|
2021-07-09 20:05:57 +00:00
|
|
|
for (const {text, reading} of term) {
|
|
|
|
if (reading.length === 0) {
|
|
|
|
termNode.appendChild(document.createTextNode(text));
|
2020-11-08 17:50:16 +00:00
|
|
|
} else {
|
2021-07-09 21:31:16 +00:00
|
|
|
const reading2 = this._convertReading(text, reading);
|
2021-09-27 23:07:28 +00:00
|
|
|
termNode.appendChild(this._createSegment(text, reading2, offset));
|
2020-11-08 17:50:16 +00:00
|
|
|
}
|
2021-09-27 23:07:28 +00:00
|
|
|
offset += text.length;
|
2020-11-08 17:50:16 +00:00
|
|
|
}
|
|
|
|
fragment.appendChild(termNode);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:07:28 +00:00
|
|
|
_createSegment(text, reading, offset) {
|
2020-11-08 17:50:16 +00:00
|
|
|
const segmentNode = document.createElement('ruby');
|
|
|
|
segmentNode.className = 'query-parser-segment';
|
|
|
|
|
|
|
|
const textNode = document.createElement('span');
|
|
|
|
textNode.className = 'query-parser-segment-text';
|
2021-09-27 23:07:28 +00:00
|
|
|
textNode.dataset.offset = `${offset}`;
|
2020-11-08 17:50:16 +00:00
|
|
|
|
|
|
|
const readingNode = document.createElement('rt');
|
|
|
|
readingNode.className = 'query-parser-segment-reading';
|
|
|
|
|
|
|
|
segmentNode.appendChild(textNode);
|
|
|
|
segmentNode.appendChild(readingNode);
|
|
|
|
|
2021-07-09 20:05:57 +00:00
|
|
|
textNode.textContent = text;
|
|
|
|
readingNode.textContent = reading;
|
2020-11-08 17:50:16 +00:00
|
|
|
|
|
|
|
return segmentNode;
|
|
|
|
}
|
2021-07-09 21:31:16 +00:00
|
|
|
|
|
|
|
_convertReading(term, reading) {
|
|
|
|
switch (this._readingMode) {
|
|
|
|
case 'hiragana':
|
|
|
|
return this._japaneseUtil.convertKatakanaToHiragana(reading);
|
|
|
|
case 'katakana':
|
|
|
|
return this._japaneseUtil.convertHiraganaToKatakana(reading);
|
|
|
|
case 'romaji':
|
|
|
|
if (this._japaneseUtil.convertToRomajiSupported()) {
|
|
|
|
if (reading.length > 0) {
|
|
|
|
return this._japaneseUtil.convertToRomaji(reading);
|
|
|
|
} else if (this._japaneseUtil.isStringEntirelyKana(term)) {
|
|
|
|
return this._japaneseUtil.convertToRomaji(term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reading;
|
|
|
|
case 'none':
|
|
|
|
return '';
|
|
|
|
default:
|
|
|
|
return reading;
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 23:07:28 +00:00
|
|
|
|
|
|
|
_getSentenceOffset(textSource) {
|
|
|
|
if (textSource.type === 'range') {
|
|
|
|
const {range} = textSource;
|
|
|
|
const node = this._getParentElement(range.startContainer);
|
|
|
|
if (node !== null) {
|
|
|
|
const {offset} = node.dataset;
|
|
|
|
if (typeof offset === 'string') {
|
|
|
|
const value = Number.parseInt(offset, 10);
|
|
|
|
if (Number.isFinite(value)) {
|
|
|
|
return Math.max(0, value) + range.startOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getParentElement(node) {
|
|
|
|
const {ELEMENT_NODE} = Node;
|
|
|
|
while (true) {
|
|
|
|
node = node.parentNode;
|
|
|
|
if (node === null) { return null; }
|
|
|
|
if (node.nodeType === ELEMENT_NODE) { return node; }
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 21:49:36 +00:00
|
|
|
}
|