Move findTerms and findKanji functions into TextScanner (#747)

This commit is contained in:
toasted-nutbread 2020-08-22 14:40:44 -04:00 committed by GitHub
parent bdb4c21a78
commit 9fa0f2a56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 60 deletions

View File

@ -27,9 +27,6 @@ class QueryParser extends EventDispatcher {
this._getOptionsContext = getOptionsContext;
this._setSpinnerVisible = setSpinnerVisible;
this._selectedParser = null;
this._scanLength = 1;
this._sentenceExtent = 1;
this._layoutAwareScan = false;
this._documentUtil = documentUtil;
this._parseResults = [];
this._queryParser = document.querySelector('#query-parser-content');
@ -50,19 +47,10 @@ class QueryParser extends EventDispatcher {
this._queryParser.addEventListener('click', this._onClick.bind(this));
}
setOptions({selectedParser, scanLength, sentenceExtent, layoutAwareScan, termSpacing, scanning}) {
setOptions({selectedParser, termSpacing, scanning}) {
if (selectedParser === null || typeof selectedParser === 'string') {
this._selectedParser = selectedParser;
}
if (typeof scanLength === 'number') {
this._scanLength = scanLength;
}
if (typeof sentenceExtent === 'number') {
this._sentenceExtent = sentenceExtent;
}
if (typeof layoutAwareScan === 'boolean') {
this._layoutAwareScan = layoutAwareScan;
}
if (typeof termSpacing === 'boolean') {
this._queryParser.dataset.termSpacing = `${termSpacing}`;
}
@ -95,22 +83,14 @@ class QueryParser extends EventDispatcher {
async _search(textSource, cause) {
if (textSource === null) { return null; }
const scanLength = this._scanLength;
const sentenceExtent = this._sentenceExtent;
const layoutAwareScan = this._layoutAwareScan;
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 results = await this._textScanner.findTerms(textSource, optionsContext);
if (results === null) { return null; }
const sentence = this._documentUtil.extractSentence(textSource, sentenceExtent, layoutAwareScan);
textSource.setEndOffset(length, layoutAwareScan);
const {definitions, sentence, type} = results;
this.trigger('searched', {
type: 'terms',
type,
definitions,
sentence,
cause,
@ -118,7 +98,7 @@ class QueryParser extends EventDispatcher {
optionsContext
});
return {definitions, type: 'terms'};
return {definitions, type};
}
_onParserChange(e) {

View File

@ -253,7 +253,10 @@ class Frontend {
modifier: scanningOptions.modifier,
useMiddleMouse: scanningOptions.middleMouse,
delay: scanningOptions.delay,
touchInputEnabled: scanningOptions.touchInputEnabled
touchInputEnabled: scanningOptions.touchInputEnabled,
scanLength: scanningOptions.length,
sentenceExtent: options.anki.sentenceExt,
layoutAwareScan: scanningOptions.layoutAwareScan
});
this._updateTextScannerEnabled();
@ -399,8 +402,8 @@ class Frontend {
if (textSource !== null) {
const optionsContext = await this.getOptionsContext();
results = (
await this._findTerms(textSource, optionsContext) ||
await this._findKanji(textSource, optionsContext)
await this._textScanner.findTerms(textSource, optionsContext) ||
await this._textScanner.findKanji(textSource, optionsContext)
);
if (results !== null) {
const focus = (cause === 'mouse');
@ -424,32 +427,6 @@ class Frontend {
return results;
}
async _findTerms(textSource, optionsContext) {
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, {}, optionsContext);
if (definitions.length === 0) { return null; }
textSource.setEndOffset(length, layoutAwareScan);
return {definitions, type: 'terms'};
}
async _findKanji(textSource, optionsContext) {
const layoutAwareScan = this._options.scanning.layoutAwareScan;
const searchText = this._textScanner.getTextSourceContent(textSource, 1, layoutAwareScan);
if (searchText.length === 0) { return null; }
const definitions = await api.kanjiFind(searchText, optionsContext);
if (definitions.length === 0) { return null; }
textSource.setEndOffset(1, layoutAwareScan);
return {definitions, type: 'kanji'};
}
async _showExtensionUnloaded(textSource) {
if (textSource === null) {
textSource = this._textScanner.getCurrentTextSource();

View File

@ -228,9 +228,6 @@ class Display extends EventDispatcher {
this._queryParser.setOptions({
selectedParser: options.parsing.selectedParser,
scanLength: scanning.length,
sentenceExtent: options.anki.sentenceExt,
layoutAwareScan: scanning.layoutAwareScan,
termSpacing: options.parsing.termSpacing,
scanning: {
deepContentScan: scanning.deepDomScan,
@ -238,7 +235,10 @@ class Display extends EventDispatcher {
modifier: scanning.modifier,
useMiddleMouse: scanning.middleMouse,
delay: scanning.delay,
touchInputEnabled: scanning.touchInputEnabled
touchInputEnabled: scanning.touchInputEnabled,
scanLength: scanning.length,
sentenceExtent: options.anki.sentenceExt,
layoutAwareScan: scanning.layoutAwareScan
}
});
}

View File

@ -17,6 +17,7 @@
/* global
* DocumentUtil
* api
*/
class TextScanner extends EventDispatcher {
@ -43,6 +44,9 @@ class TextScanner extends EventDispatcher {
this._useMiddleMouse = false;
this._delay = 0;
this._touchInputEnabled = false;
this._scanLength = 1;
this._sentenceExtent = 1;
this._layoutAwareScan = false;
this._enabled = false;
this._eventListeners = new EventListenerCollection();
@ -91,7 +95,7 @@ class TextScanner extends EventDispatcher {
}
}
setOptions({deepContentScan, selectText, modifier, useMiddleMouse, delay, touchInputEnabled}) {
setOptions({deepContentScan, selectText, modifier, useMiddleMouse, delay, touchInputEnabled, scanLength, sentenceExtent, layoutAwareScan}) {
if (typeof deepContentScan === 'boolean') {
this._deepContentScan = deepContentScan;
}
@ -110,6 +114,15 @@ class TextScanner extends EventDispatcher {
if (typeof touchInputEnabled === 'boolean') {
this._touchInputEnabled = false;
}
if (typeof scanLength === 'number') {
this._scanLength = scanLength;
}
if (typeof sentenceExtent === 'number') {
this._sentenceExtent = sentenceExtent;
}
if (typeof layoutAwareScan === 'boolean') {
this._layoutAwareScan = layoutAwareScan;
}
}
async searchAt(x, y, cause) {
@ -193,6 +206,37 @@ class TextScanner extends EventDispatcher {
}
}
async findTerms(textSource, optionsContext) {
const scanLength = this._scanLength;
const sentenceExtent = this._sentenceExtent;
const layoutAwareScan = this._layoutAwareScan;
const searchText = this.getTextSourceContent(textSource, scanLength, layoutAwareScan);
if (searchText.length === 0) { return null; }
const {definitions, length} = await api.termsFind(searchText, {}, optionsContext);
if (definitions.length === 0) { return null; }
textSource.setEndOffset(length, layoutAwareScan);
const sentence = this._documentUtil.extractSentence(textSource, sentenceExtent, layoutAwareScan);
return {definitions, sentence, type: 'terms'};
}
async findKanji(textSource, optionsContext) {
const sentenceExtent = this._sentenceExtent;
const layoutAwareScan = this._layoutAwareScan;
const searchText = this.getTextSourceContent(textSource, 1, layoutAwareScan);
if (searchText.length === 0) { return null; }
const definitions = await api.kanjiFind(searchText, optionsContext);
if (definitions.length === 0) { return null; }
textSource.setEndOffset(1, layoutAwareScan);
const sentence = this._documentUtil.extractSentence(textSource, sentenceExtent, layoutAwareScan);
return {definitions, sentence, type: 'kanji'};
}
// Private
_onMouseOver(e) {