diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-08-22 14:40:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-22 14:40:44 -0400 |
commit | 9fa0f2a56a79159227f1f42455157fe9c84132d3 (patch) | |
tree | d9a69e94a42939da1d184a3eec8f5ad03ef450ea /ext/mixed/js | |
parent | bdb4c21a784a3b5c342bf456a8ed754d6d5e63f2 (diff) |
Move findTerms and findKanji functions into TextScanner (#747)
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/display.js | 8 | ||||
-rw-r--r-- | ext/mixed/js/text-scanner.js | 46 |
2 files changed, 49 insertions, 5 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 1cc08bc7..3f8a43ab 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -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 } }); } diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index d0256b1e..5a64c14a 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -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) { |