diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-17 11:43:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-17 11:43:05 -0500 |
commit | be590004fee5bbbbaefa8a11ddecdb4fca6f7da7 (patch) | |
tree | 40ff739237817035f3836667eba1068233ad0762 | |
parent | 5d9d96996e1b80ecca94023e20476e5e2f85bbff (diff) |
Text scanner improvements (#1261)
* Add support for allowing TextScanner.search to force focus
* Simplify query parser searched event forwarding
* Defer fallback creation
* Simplify event listeners
* Change type to pointerType
* Change cause to eventType
* Change empty to passive; make .search function passive
* Remove unused input index
-rw-r--r-- | ext/bg/js/query-parser.js | 17 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 10 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 4 | ||||
-rw-r--r-- | ext/mixed/js/text-scanner.js | 43 |
4 files changed, 38 insertions, 36 deletions
diff --git a/ext/bg/js/query-parser.js b/ext/bg/js/query-parser.js index fced2bda..ea215582 100644 --- a/ext/bg/js/query-parser.js +++ b/ext/bg/js/query-parser.js @@ -82,22 +82,15 @@ class QueryParser extends EventDispatcher { // Private - _onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, detail, error}) { + _onSearched(e) { + const {error} = e; if (error !== null) { yomichan.logError(error); return; } - if (type === null) { return; } - - this.trigger('searched', { - type, - definitions, - sentence, - inputInfo, - textSource, - optionsContext, - detail - }); + if (e.type === null) { return; } + + this.trigger('searched', e); } _onParserChange(e) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 8b2df1b0..d789c4f0 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -251,12 +251,12 @@ class Frontend { } } - _onSearched({type, definitions, sentence, inputInfo: {cause, empty}, textSource, optionsContext, detail: {documentTitle}, error}) { + _onSearched({type, definitions, sentence, inputInfo: {eventType, passive, detail}, textSource, optionsContext, detail: {documentTitle}, error}) { const scanningOptions = this._options.scanning; if (error !== null) { if (yomichan.isExtensionUnloaded) { - if (textSource !== null && !empty) { + if (textSource !== null && !passive) { this._showExtensionUnloaded(textSource); } } else { @@ -264,7 +264,11 @@ class Frontend { } } if (type !== null) { this._stopClearSelectionDelayed(); - const focus = (cause === 'mouseMove'); + let focus = (eventType === 'mouseMove'); + if (isObject(detail)) { + const focus2 = detail.focus; + if (typeof focus2 === 'boolean') { focus = focus2; } + } this._showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext); } else { if (scanningOptions.autoHideResults) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index eca4dda5..b8baccf9 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -587,11 +587,11 @@ class Display extends EventDispatcher { } } - _onQueryParserSearch({type, definitions, sentence, inputInfo: {cause}, textSource, optionsContext}) { + _onQueryParserSearch({type, definitions, sentence, inputInfo: {eventType}, textSource, optionsContext}) { const query = textSource.text(); const historyState = this._history.state; const history = ( - cause === 'click' || + eventType === 'click' || !isObject(historyState) || historyState.cause !== 'queryParser' ); diff --git a/ext/mixed/js/text-scanner.js b/ext/mixed/js/text-scanner.js index 411b1b20..7672b69d 100644 --- a/ext/mixed/js/text-scanner.js +++ b/ext/mixed/js/text-scanner.js @@ -283,8 +283,8 @@ class TextScanner extends EventDispatcher { return false; } - async search(textSource) { - const inputInfo = this._createInputInfo(-1, false, null, 'script', 'script', [], []); + async search(textSource, inputDetail) { + const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail); return await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo); } @@ -391,7 +391,7 @@ class TextScanner extends EventDispatcher { if (this._searchOnClick) { const modifiers = DocumentUtil.getActiveModifiersAndButtons(e); const modifierKeys = DocumentUtil.getActiveModifiers(e); - const inputInfo = this._createInputInfo(-1, false, null, 'mouse', 'click', modifiers, modifierKeys); + const inputInfo = this._createInputInfo(null, 'mouse', 'click', false, modifiers, modifierKeys); this._searchAt(e.clientX, e.clientY, inputInfo); } @@ -682,8 +682,8 @@ class TextScanner extends EventDispatcher { } } - for (const [node, type, listener, options] of eventListenerInfos) { - this._eventListeners.addEventListener(node, type, listener, options); + for (const args of eventListenerInfos) { + this._eventListeners.addEventListener(...args); } } @@ -840,7 +840,7 @@ class TextScanner extends EventDispatcher { async _searchAtFromMouseMove(x, y, inputInfo) { if (this._pendingLookup) { return; } - if (inputInfo.empty) { + if (inputInfo.passive) { if (!await this._scanTimerWait()) { // Aborted return; @@ -871,10 +871,10 @@ class TextScanner extends EventDispatcher { } } - async _searchAtFromPen(e, x, y, cause, prevent) { + async _searchAtFromPen(e, x, y, eventType, prevent) { if (this._pendingLookup) { return; } - const inputInfo = this._getMatchingInputGroupFromEvent('pen', cause, e); + const inputInfo = this._getMatchingInputGroupFromEvent('pen', eventType, e); if (inputInfo === null) { return; } const {input: {options}} = inputInfo; @@ -900,32 +900,37 @@ class TextScanner extends EventDispatcher { } } - _getMatchingInputGroupFromEvent(type, cause, event) { + _getMatchingInputGroupFromEvent(pointerType, eventType, event) { const modifiers = DocumentUtil.getActiveModifiersAndButtons(event); const modifierKeys = DocumentUtil.getActiveModifiers(event); - return this._getMatchingInputGroup(type, cause, modifiers, modifierKeys); + return this._getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys); } - _getMatchingInputGroup(type, cause, modifiers, modifierKeys) { - let fallback = null; + _getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys) { + let fallbackIndex = -1; const modifiersSet = new Set(modifiers); for (let i = 0, ii = this._inputs.length; i < ii; ++i) { const input = this._inputs[i]; const {include, exclude, types} = input; - if (!types.has(type)) { continue; } + if (!types.has(pointerType)) { continue; } if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) { if (include.length > 0) { - return this._createInputInfo(i, false, input, type, cause, modifiers, modifierKeys); - } else if (fallback === null) { - fallback = this._createInputInfo(i, true, input, type, cause, modifiers, modifierKeys); + return this._createInputInfo(input, pointerType, eventType, false, modifiers, modifierKeys); + } else if (fallbackIndex < 0) { + fallbackIndex = i; } } } - return fallback; + + return ( + fallbackIndex >= 0 ? + this._createInputInfo(this._inputs[fallbackIndex], pointerType, eventType, true, modifiers, modifierKeys) : + null + ); } - _createInputInfo(index, empty, input, type, cause, modifiers, modifierKeys) { - return {index, empty, input, type, cause, modifiers, modifierKeys}; + _createInputInfo(input, pointerType, eventType, passive, modifiers, modifierKeys, detail) { + return {input, pointerType, eventType, passive, modifiers, modifierKeys, detail}; } _setHasAll(set, values) { |