Skip to content

Commit

Permalink
Fix example text popup not updating correctly when scan resolution is…
Browse files Browse the repository at this point in the history
… set to `word` (#1527)

* Add option to disallowExpandStartOffset

* Types
  • Loading branch information
jamesmaa authored Nov 4, 2024
1 parent d09cd38 commit 37ba526
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions ext/js/dom/dom-text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion ext/js/dom/text-source-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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') {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion types/ext/text-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 37ba526

Please sign in to comment.