From be590004fee5bbbbaefa8a11ddecdb4fca6f7da7 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 17 Jan 2021 11:43:05 -0500 Subject: 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 --- ext/bg/js/query-parser.js | 17 +++++------------ ext/fg/js/frontend.js | 10 +++++++--- ext/mixed/js/display.js | 4 ++-- 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) { -- cgit v1.2.3