From 37ba526de4d696eddb7113aec8fc39b1163a3984 Mon Sep 17 00:00:00 2001 From: James Maa Date: Mon, 4 Nov 2024 09:35:08 -0800 Subject: [PATCH] Fix example text popup not updating correctly when scan resolution is set to `word` (#1527) * Add option to disallowExpandStartOffset * Types --- ext/js/app/frontend.js | 2 +- ext/js/dom/dom-text-scanner.js | 8 ++++---- ext/js/dom/text-source-range.js | 3 ++- ext/js/language/text-scanner.js | 16 +++++++++------- types/ext/text-scanner.d.ts | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ext/js/app/frontend.js b/ext/js/app/frontend.js index 830f50fd14..e58828df13 100644 --- a/ext/js/app/frontend.js +++ b/ext/js/app/frontend.js @@ -224,7 +224,7 @@ export class Frontend { */ async setTextSource(textSource) { this._textScanner.setCurrentTextSource(null); - await this._textScanner.search(textSource); + await this._textScanner.search(textSource, null, false, true); } /** diff --git a/ext/js/dom/dom-text-scanner.js b/ext/js/dom/dom-text-scanner.js index 8171a37140..8413c79b0d 100644 --- a/ext/js/dom/dom-text-scanner.js +++ b/ext/js/dom/dom-text-scanner.js @@ -137,11 +137,11 @@ export class DOMTextScanner { if (nodeType === TEXT_NODE) { lastNode = node; - if (!( - forward ? + const shouldContinueScanning = forward ? this._seekTextNodeForward(/** @type {Text} */ (node), resetOffset) : - this._seekTextNodeBackward(/** @type {Text} */ (node), resetOffset) - )) { + this._seekTextNodeBackward(/** @type {Text} */ (node), resetOffset); + + if (!shouldContinueScanning) { // Length reached break; } diff --git a/ext/js/dom/text-source-range.js b/ext/js/dom/text-source-range.js index 4450d32845..18f49cd733 100644 --- a/ext/js/dom/text-source-range.js +++ b/ext/js/dom/text-source-range.js @@ -166,7 +166,8 @@ export class TextSourceRange { */ setStartOffset(length, layoutAwareScan, stopAtWordBoundary = false) { if (this._disallowExpandSelection) { return 0; } - const state = new DOMTextScanner(this._range.startContainer, this._range.startOffset, !layoutAwareScan, layoutAwareScan, stopAtWordBoundary).seek(-length); + let state = new DOMTextScanner(this._range.startContainer, this._range.startOffset, !layoutAwareScan, layoutAwareScan, stopAtWordBoundary); + state = state.seek(-length); this._range.setStart(state.node, state.offset); this._rangeStartOffset = this._range.startOffset; this._content = state.content + this._content; diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index ea059be856..15e82162a4 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -425,12 +425,13 @@ export class TextScanner extends EventDispatcher { /** * @param {import('text-source').TextSource} textSource - * @param {import('text-scanner').InputInfoDetail} [inputDetail] - * @param {boolean} showEmpty + * @param {import('text-scanner').InputInfoDetail?} [inputDetail] + * @param {boolean} showEmpty shows a "No results found" popup if no results are found + * @param {boolean} disallowExpandStartOffset disallows expanding the start offset of the range */ - async search(textSource, inputDetail, showEmpty = false) { + async search(textSource, inputDetail, showEmpty = false, disallowExpandStartOffset = false) { const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail); - await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo, showEmpty); + await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo, showEmpty, disallowExpandStartOffset); } // Private @@ -455,8 +456,9 @@ export class TextScanner extends EventDispatcher { * @param {boolean} searchKanji * @param {import('text-scanner').InputInfo} inputInfo * @param {boolean} showEmpty shows a "No results found" popup if no results are found + * @param {boolean} disallowExpandStartOffset disallows expanding the start offset of the range */ - async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false) { + async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false, disallowExpandStartOffset = false) { try { const isAltText = textSource instanceof TextSourceElement; if (inputInfo.pointerType === 'touch') { @@ -479,7 +481,7 @@ export class TextScanner extends EventDispatcher { null ); - if (this._scanResolution === 'word') { + if (this._scanResolution === 'word' && !disallowExpandStartOffset) { // Move the start offset to the beginning of the word textSource.setStartOffset(this._scanLength, this._layoutAwareScan, true); } @@ -1497,7 +1499,7 @@ export class TextScanner extends EventDispatcher { * @param {boolean} passive * @param {import('input').Modifier[]} modifiers * @param {import('input').ModifierKey[]} modifierKeys - * @param {import('text-scanner').InputInfoDetail} [detail] + * @param {import('text-scanner').InputInfoDetail?} [detail] * @returns {import('text-scanner').InputInfo} */ _createInputInfo(input, pointerType, eventType, passive, modifiers, modifierKeys, detail) { diff --git a/types/ext/text-scanner.d.ts b/types/ext/text-scanner.d.ts index 859bfca270..54ab47838e 100644 --- a/types/ext/text-scanner.d.ts +++ b/types/ext/text-scanner.d.ts @@ -104,7 +104,7 @@ export type InputInfo = { passive: boolean; modifiers: Input.Modifier[]; modifierKeys: Input.ModifierKey[]; - detail: InputInfoDetail | undefined; + detail: InputInfoDetail | undefined | null; }; export type InputInfoDetail = {