diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-11-23 20:31:48 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-23 20:31:48 -0500 |
commit | 068b1eef71ed1167e7e39effa00cda7deb9251f2 (patch) | |
tree | a1a593e6cff0009da2829d2803b570a19fa1ac46 /ext/mixed/js/text-scanner.js | |
parent | 12e5cec99c64af164ddb56fd8262d98a23205083 (diff) |
Text scanner improvements (#1056)
* Only ignore nodes on non-web pages
* Fix issue where options might not be assigned on nested frontends
* Refactor default TextScanner options
* Add option to enable search only on click
* Simplify restore state assignment
* Update options context passing
* Fix empty title
* Use TextScanner to scan content inside of Display
* Rename ignoreNodes to excludeSelector(s)
* Fix options update incorrectly triggering a re-search
* Fix copy throwing an error on the search page
* Replace _onSearchQueryUpdated with _search
* Use include selector instead of exclude selector
Diffstat (limited to 'ext/mixed/js/text-scanner.js')
-rw-r--r-- | ext/mixed/js/text-scanner.js | 74 |
1 files changed, 55 insertions, 19 deletions
diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index 66d37c93..f0903370 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -21,19 +21,31 @@ */ class TextScanner extends EventDispatcher { - constructor({node, ignoreElements, ignorePoint, documentUtil, getOptionsContext, searchTerms=false, searchKanji=false, searchOnClick=false}) { + constructor({ + node, + documentUtil, + getOptionsContext, + ignoreElements=null, + ignorePoint=null, + searchTerms=false, + searchKanji=false, + searchOnClick=false, + searchOnClickOnly=false + }) { super(); this._node = node; - this._ignoreElements = ignoreElements; - this._ignorePoint = ignorePoint; this._documentUtil = documentUtil; this._getOptionsContext = getOptionsContext; + this._ignoreElements = ignoreElements; + this._ignorePoint = ignorePoint; this._searchTerms = searchTerms; this._searchKanji = searchKanji; this._searchOnClick = searchOnClick; + this._searchOnClickOnly = searchOnClickOnly; this._isPrepared = false; - this._ignoreNodes = null; + this._includeSelector = null; + this._excludeSelector = null; this._inputInfoCurrent = null; this._scanTimerPromise = null; @@ -76,12 +88,20 @@ class TextScanner extends EventDispatcher { this._canClearSelection = value; } - get ignoreNodes() { - return this._ignoreNodes; + get includeSelector() { + return this._includeSelector; + } + + set includeSelector(value) { + this._includeSelector = value; } - set ignoreNodes(value) { - this._ignoreNodes = value; + get excludeSelector() { + return this._excludeSelector; + } + + set excludeSelector(value) { + this._excludeSelector = value; } prepare() { @@ -178,15 +198,8 @@ class TextScanner extends EventDispatcher { clonedTextSource.setEndOffset(length, layoutAwareScan); - if (this._ignoreNodes !== null) { - length = clonedTextSource.text().length; - while ( - length > 0 && - DocumentUtil.anyNodeMatchesSelector(clonedTextSource.getNodesInRange(), this._ignoreNodes) - ) { - --length; - clonedTextSource.setEndOffset(length, layoutAwareScan); - } + if (this._excludeSelector !== null) { + this._constrainTextSource(clonedTextSource, this._includeSelector, this._excludeSelector, layoutAwareScan); } return clonedTextSource.text(); @@ -287,7 +300,7 @@ class TextScanner extends EventDispatcher { } _onMouseOver(e) { - if (this._ignoreElements().includes(e.target)) { + if (this._ignoreElements !== null && this._ignoreElements().includes(e.target)) { this._scanTimerClear(); } } @@ -613,7 +626,9 @@ class TextScanner extends EventDispatcher { _hookEvents() { let eventListenerInfos; - if (this._arePointerEventsSupported()) { + if (this._searchOnClickOnly) { + eventListenerInfos = this._getMouseClickOnlyEventListeners(); + } else if (this._arePointerEventsSupported()) { eventListenerInfos = this._getPointerEventListeners(); } else { eventListenerInfos = this._getMouseEventListeners(); @@ -652,6 +667,11 @@ class TextScanner extends EventDispatcher { ]; } + _getMouseClickOnlyEventListeners() { + return [ + [this._node, 'click', this._onClick.bind(this)] + ]; + } _getTouchEventListeners() { return [ [this._node, 'auxclick', this._onAuxClick.bind(this)], @@ -873,4 +893,20 @@ class TextScanner extends EventDispatcher { const cachedPointerType = this._pointerIdTypeMap.get(e.pointerId); return (typeof cachedPointerType !== 'undefined' ? cachedPointerType : e.pointerType); } + + _constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan) { + let length = textSource.text().length; + while (length > 0) { + const nodes = textSource.getNodesInRange(); + if ( + (includeSelector !== null && !DocumentUtil.everyNodeMatchesSelector(nodes, includeSelector)) || + (excludeSelector !== null && DocumentUtil.anyNodeMatchesSelector(nodes, excludeSelector)) + ) { + --length; + textSource.setEndOffset(length, layoutAwareScan); + } else { + break; + } + } + } } |