From 3754c920410e90fc6b98aadc9f0dbe60dfa6a14d Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 24 Jul 2020 16:03:11 -0400 Subject: Query parser refactor (#683) * Rename files to better match class name * Don't pass setContent to QueryParser; use a generic event instead --- ext/bg/js/query-parser-generator.js | 90 ++++++++++++++++ ext/bg/js/query-parser.js | 161 +++++++++++++++++++++++++++++ ext/bg/js/search-query-parser-generator.js | 90 ---------------- ext/bg/js/search-query-parser.js | 158 ---------------------------- ext/bg/js/search.js | 16 ++- ext/bg/search.html | 4 +- 6 files changed, 268 insertions(+), 251 deletions(-) create mode 100644 ext/bg/js/query-parser-generator.js create mode 100644 ext/bg/js/query-parser.js delete mode 100644 ext/bg/js/search-query-parser-generator.js delete mode 100644 ext/bg/js/search-query-parser.js (limited to 'ext/bg') diff --git a/ext/bg/js/query-parser-generator.js b/ext/bg/js/query-parser-generator.js new file mode 100644 index 00000000..6989e157 --- /dev/null +++ b/ext/bg/js/query-parser-generator.js @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2020 Yomichan Authors + * + * 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 + * along with this program. If not, see . + */ + +/* global + * TemplateHandler + * api + */ + +class QueryParserGenerator { + constructor() { + this._templateHandler = null; + } + + async prepare() { + const html = await api.getQueryParserTemplatesHtml(); + this._templateHandler = new TemplateHandler(html); + } + + createParseResult(terms, preview=false) { + const fragment = document.createDocumentFragment(); + for (const term of terms) { + const termContainer = this._templateHandler.instantiate(preview ? 'term-preview' : 'term'); + for (const segment of term) { + if (!segment.text.trim()) { continue; } + if (!segment.reading.trim()) { + termContainer.appendChild(this.createSegmentText(segment.text)); + } else { + termContainer.appendChild(this.createSegment(segment)); + } + } + fragment.appendChild(termContainer); + } + return fragment; + } + + createSegment(segment) { + const segmentContainer = this._templateHandler.instantiate('segment'); + const segmentTextContainer = segmentContainer.querySelector('.query-parser-segment-text'); + const segmentReadingContainer = segmentContainer.querySelector('.query-parser-segment-reading'); + segmentTextContainer.appendChild(this.createSegmentText(segment.text)); + segmentReadingContainer.textContent = segment.reading; + return segmentContainer; + } + + createSegmentText(text) { + const fragment = document.createDocumentFragment(); + for (const chr of text) { + const charContainer = this._templateHandler.instantiate('char'); + charContainer.textContent = chr; + fragment.appendChild(charContainer); + } + return fragment; + } + + createParserSelect(parseResults, selectedParser) { + const selectContainer = this._templateHandler.instantiate('select'); + for (const parseResult of parseResults) { + const optionContainer = this._templateHandler.instantiate('select-option'); + optionContainer.value = parseResult.id; + switch (parseResult.source) { + case 'scanning-parser': + optionContainer.textContent = 'Scanning parser'; + break; + case 'mecab': + optionContainer.textContent = `MeCab: ${parseResult.dictionary}`; + break; + default: + optionContainer.textContent = 'Unrecognized dictionary'; + break; + } + optionContainer.defaultSelected = selectedParser === parseResult.id; + selectContainer.appendChild(optionContainer); + } + return selectContainer; + } +} diff --git a/ext/bg/js/query-parser.js b/ext/bg/js/query-parser.js new file mode 100644 index 00000000..88c40c93 --- /dev/null +++ b/ext/bg/js/query-parser.js @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2019-2020 Yomichan Authors + * + * 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 + * along with this program. If not, see . + */ + +/* global + * QueryParserGenerator + * TextScanner + * api + * docSentenceExtract + */ + +class QueryParser extends EventDispatcher { + constructor({getOptionsContext, setSpinnerVisible}) { + super(); + this._options = null; + this._getOptionsContext = getOptionsContext; + this._setSpinnerVisible = setSpinnerVisible; + this._parseResults = []; + this._queryParser = document.querySelector('#query-parser-content'); + this._queryParserSelect = document.querySelector('#query-parser-select-container'); + this._queryParserGenerator = new QueryParserGenerator(); + this._textScanner = new TextScanner({ + node: this._queryParser, + ignoreElements: () => [], + ignorePoint: null, + search: this._search.bind(this) + }); + } + + async prepare() { + await this._queryParserGenerator.prepare(); + this._textScanner.prepare(); + this._queryParser.addEventListener('click', this._onClick.bind(this)); + } + + 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 api.textParse(text, this._getOptionsContext()); + this._refreshSelectedParser(); + + this._renderParserSelect(); + this._renderParseResult(); + + this._setSpinnerVisible(false); + } + + // Private + + _onClick(e) { + this._textScanner.searchAt(e.clientX, e.clientY, 'click'); + } + + async _search(textSource, cause) { + if (textSource === null) { return null; } + + const {length: scanLength, layoutAwareScan} = this._options.scanning; + const searchText = this._textScanner.getTextSourceContent(textSource, scanLength, layoutAwareScan); + if (searchText.length === 0) { return null; } + + const optionsContext = this._getOptionsContext(); + const {definitions, length} = await api.termsFind(searchText, {}, optionsContext); + if (definitions.length === 0) { return null; } + + const sentenceExtent = this._options.anki.sentenceExt; + const sentence = docSentenceExtract(textSource, sentenceExtent, layoutAwareScan); + + textSource.setEndOffset(length, layoutAwareScan); + + this.trigger('searched', { + type: 'terms', + definitions, + sentence, + cause, + textSource, + optionsContext + }); + + return {definitions, type: 'terms'}; + } + + _onParserChange(e) { + const value = e.target.value; + api.modifySettings([{ + action: 'set', + path: 'parsing.selectedParser', + value, + scope: 'profile', + optionsContext: this._getOptionsContext() + }], 'search'); + } + + _refreshSelectedParser() { + if (this._parseResults.length > 0) { + if (!this._getParseResult()) { + const value = this._parseResults[0].id; + api.modifySettings([{ + action: 'set', + path: 'parsing.selectedParser', + value, + scope: 'profile', + optionsContext: this._getOptionsContext() + }], 'search'); + } + } + } + + _getParseResult() { + const {selectedParser} = this._options.parsing; + return this._parseResults.find((r) => r.id === selectedParser); + } + + _setPreview(text) { + const previewTerms = []; + for (let i = 0, ii = text.length; i < ii; i += 2) { + const tempText = text.substring(i, i + 2); + previewTerms.push([{text: tempText, reading: ''}]); + } + this._queryParser.textContent = ''; + this._queryParser.appendChild(this._queryParserGenerator.createParseResult(previewTerms, true)); + } + + _renderParserSelect() { + this._queryParserSelect.textContent = ''; + if (this._parseResults.length > 1) { + const {selectedParser} = this._options.parsing; + const select = this._queryParserGenerator.createParserSelect(this._parseResults, selectedParser); + select.addEventListener('change', this._onParserChange.bind(this)); + this._queryParserSelect.appendChild(select); + } + } + + _renderParseResult() { + const parseResult = this._getParseResult(); + this._queryParser.textContent = ''; + if (!parseResult) { return; } + this._queryParser.appendChild(this._queryParserGenerator.createParseResult(parseResult.content)); + } +} diff --git a/ext/bg/js/search-query-parser-generator.js b/ext/bg/js/search-query-parser-generator.js deleted file mode 100644 index 6989e157..00000000 --- a/ext/bg/js/search-query-parser-generator.js +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2020 Yomichan Authors - * - * 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 - * along with this program. If not, see . - */ - -/* global - * TemplateHandler - * api - */ - -class QueryParserGenerator { - constructor() { - this._templateHandler = null; - } - - async prepare() { - const html = await api.getQueryParserTemplatesHtml(); - this._templateHandler = new TemplateHandler(html); - } - - createParseResult(terms, preview=false) { - const fragment = document.createDocumentFragment(); - for (const term of terms) { - const termContainer = this._templateHandler.instantiate(preview ? 'term-preview' : 'term'); - for (const segment of term) { - if (!segment.text.trim()) { continue; } - if (!segment.reading.trim()) { - termContainer.appendChild(this.createSegmentText(segment.text)); - } else { - termContainer.appendChild(this.createSegment(segment)); - } - } - fragment.appendChild(termContainer); - } - return fragment; - } - - createSegment(segment) { - const segmentContainer = this._templateHandler.instantiate('segment'); - const segmentTextContainer = segmentContainer.querySelector('.query-parser-segment-text'); - const segmentReadingContainer = segmentContainer.querySelector('.query-parser-segment-reading'); - segmentTextContainer.appendChild(this.createSegmentText(segment.text)); - segmentReadingContainer.textContent = segment.reading; - return segmentContainer; - } - - createSegmentText(text) { - const fragment = document.createDocumentFragment(); - for (const chr of text) { - const charContainer = this._templateHandler.instantiate('char'); - charContainer.textContent = chr; - fragment.appendChild(charContainer); - } - return fragment; - } - - createParserSelect(parseResults, selectedParser) { - const selectContainer = this._templateHandler.instantiate('select'); - for (const parseResult of parseResults) { - const optionContainer = this._templateHandler.instantiate('select-option'); - optionContainer.value = parseResult.id; - switch (parseResult.source) { - case 'scanning-parser': - optionContainer.textContent = 'Scanning parser'; - break; - case 'mecab': - optionContainer.textContent = `MeCab: ${parseResult.dictionary}`; - break; - default: - optionContainer.textContent = 'Unrecognized dictionary'; - break; - } - optionContainer.defaultSelected = selectedParser === parseResult.id; - selectContainer.appendChild(optionContainer); - } - return selectContainer; - } -} diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js deleted file mode 100644 index 86524b66..00000000 --- a/ext/bg/js/search-query-parser.js +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2019-2020 Yomichan Authors - * - * 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 - * along with this program. If not, see . - */ - -/* global - * QueryParserGenerator - * TextScanner - * api - * docSentenceExtract - */ - -class QueryParser { - constructor({getOptionsContext, setContent, setSpinnerVisible}) { - this._options = null; - this._getOptionsContext = getOptionsContext; - this._setContent = setContent; - this._setSpinnerVisible = setSpinnerVisible; - this._parseResults = []; - this._queryParser = document.querySelector('#query-parser-content'); - this._queryParserSelect = document.querySelector('#query-parser-select-container'); - this._queryParserGenerator = new QueryParserGenerator(); - this._textScanner = new TextScanner({ - node: this._queryParser, - ignoreElements: () => [], - ignorePoint: null, - search: this._search.bind(this) - }); - } - - async prepare() { - await this._queryParserGenerator.prepare(); - this._textScanner.prepare(); - this._queryParser.addEventListener('click', this._onClick.bind(this)); - } - - 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 api.textParse(text, this._getOptionsContext()); - this._refreshSelectedParser(); - - this._renderParserSelect(); - this._renderParseResult(); - - this._setSpinnerVisible(false); - } - - // Private - - _onClick(e) { - this._textScanner.searchAt(e.clientX, e.clientY, 'click'); - } - - async _search(textSource, cause) { - if (textSource === null) { return null; } - - const {length: scanLength, layoutAwareScan} = this._options.scanning; - const searchText = this._textScanner.getTextSourceContent(textSource, scanLength, layoutAwareScan); - if (searchText.length === 0) { return null; } - - const {definitions, length} = await api.termsFind(searchText, {}, this._getOptionsContext()); - if (definitions.length === 0) { return null; } - - const sentenceExtent = this._options.anki.sentenceExt; - const sentence = docSentenceExtract(textSource, sentenceExtent, layoutAwareScan); - - textSource.setEndOffset(length, layoutAwareScan); - - this._setContent('terms', {definitions, context: { - focus: false, - disableHistory: cause === 'mouse', - sentence, - url: window.location.href - }}); - - return {definitions, type: 'terms'}; - } - - _onParserChange(e) { - const value = e.target.value; - api.modifySettings([{ - action: 'set', - path: 'parsing.selectedParser', - value, - scope: 'profile', - optionsContext: this._getOptionsContext() - }], 'search'); - } - - _refreshSelectedParser() { - if (this._parseResults.length > 0) { - if (!this._getParseResult()) { - const value = this._parseResults[0].id; - api.modifySettings([{ - action: 'set', - path: 'parsing.selectedParser', - value, - scope: 'profile', - optionsContext: this._getOptionsContext() - }], 'search'); - } - } - } - - _getParseResult() { - const {selectedParser} = this._options.parsing; - return this._parseResults.find((r) => r.id === selectedParser); - } - - _setPreview(text) { - const previewTerms = []; - for (let i = 0, ii = text.length; i < ii; i += 2) { - const tempText = text.substring(i, i + 2); - previewTerms.push([{text: tempText, reading: ''}]); - } - this._queryParser.textContent = ''; - this._queryParser.appendChild(this._queryParserGenerator.createParseResult(previewTerms, true)); - } - - _renderParserSelect() { - this._queryParserSelect.textContent = ''; - if (this._parseResults.length > 1) { - const {selectedParser} = this._options.parsing; - const select = this._queryParserGenerator.createParserSelect(this._parseResults, selectedParser); - select.addEventListener('change', this._onParserChange.bind(this)); - this._queryParserSelect.appendChild(select); - } - } - - _renderParseResult() { - const parseResult = this._getParseResult(); - this._queryParser.textContent = ''; - if (!parseResult) { return; } - this._queryParser.appendChild(this._queryParserGenerator.createParseResult(parseResult.content)); - } -} diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 5be71555..b3e3ebca 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -40,7 +40,6 @@ class DisplaySearch extends Display { }); this._queryParser = new QueryParser({ getOptionsContext: this.getOptionsContext.bind(this), - setContent: this.setContent.bind(this), setSpinnerVisible: this.setSpinnerVisible.bind(this) }); this._onKeyDownIgnoreKeys = new Map([ @@ -67,6 +66,9 @@ class DisplaySearch extends Display { await this.updateOptions(); yomichan.on('optionsUpdated', () => this.updateOptions()); await this._queryParser.prepare(); + + this._queryParser.on('searched', this._onQueryParserSearch.bind(this)); + const options = this.getOptions(); const {queryParams: {query='', mode=''}} = parseUrl(window.location.href); @@ -169,6 +171,18 @@ class DisplaySearch extends Display { // Private + _onQueryParserSearch({type, definitions, sentence, cause}) { + this.setContent(type, { + definitions, + context: { + focus: false, + disableHistory: cause === 'mouse', + sentence, + url: window.location.href + } + }); + } + _onSearchInput() { this._updateSearchButton(); diff --git a/ext/bg/search.html b/ext/bg/search.html index cfcf1f96..8f7c1d4c 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -92,8 +92,8 @@ - - + + -- cgit v1.2.3