Skip to content

Commit

Permalink
not trigger search when touch on gloss link
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Sep 17, 2024
1 parent 547a628 commit be93bfe
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,18 @@ export class TextScanner extends EventDispatcher {
* @param {import('text-source').TextSource} textSource
* @param {number} length
* @param {boolean} layoutAwareScan
* @param {import('text-scanner').PointerType | undefined} pointerType
* @returns {string}
*/
getTextSourceContent(textSource, length, layoutAwareScan) {
getTextSourceContent(textSource, length, layoutAwareScan, pointerType) {
const clonedTextSource = textSource.clone();

clonedTextSource.setEndOffset(length, false, layoutAwareScan);

const includeSelector = this._includeSelector;
const excludeSelector = this._excludeSelector;
if (includeSelector !== null || excludeSelector !== null) {
this._constrainTextSource(clonedTextSource, includeSelector, excludeSelector, layoutAwareScan);
this._constrainTextSource(clonedTextSource, includeSelector, excludeSelector, layoutAwareScan, pointerType);
}

return clonedTextSource.text();
Expand Down Expand Up @@ -442,9 +443,10 @@ export class TextScanner extends EventDispatcher {
*/
_createOptionsContextForInput(baseOptionsContext, inputInfo) {
const optionsContext = clone(baseOptionsContext);
const {modifiers, modifierKeys} = inputInfo;
const {modifiers, modifierKeys, pointerType} = inputInfo;
optionsContext.modifiers = [...modifiers];
optionsContext.modifierKeys = [...modifierKeys];
optionsContext.pointerType = pointerType;
return optionsContext;
}

Expand Down Expand Up @@ -1292,7 +1294,7 @@ export class TextScanner extends EventDispatcher {
const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap;
const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap;
const layoutAwareScan = this._layoutAwareScan;
const searchText = this.getTextSourceContent(textSource, scanLength, layoutAwareScan);
const searchText = this.getTextSourceContent(textSource, scanLength, layoutAwareScan, optionsContext.pointerType);

Check failure on line 1297 in ext/js/language/text-scanner.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Unsafe argument of type `any` assigned to a parameter of type `PointerType | undefined`
if (searchText.length === 0) { return null; }

/** @type {import('api').FindTermsDetails} */
Expand Down Expand Up @@ -1327,7 +1329,7 @@ export class TextScanner extends EventDispatcher {
const sentenceForwardQuoteMap = this._sentenceForwardQuoteMap;
const sentenceBackwardQuoteMap = this._sentenceBackwardQuoteMap;
const layoutAwareScan = this._layoutAwareScan;
const searchText = this.getTextSourceContent(textSource, 1, layoutAwareScan);
const searchText = this.getTextSourceContent(textSource, 1, layoutAwareScan, optionsContext.pointerType);

Check failure on line 1332 in ext/js/language/text-scanner.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Unsafe argument of type `any` assigned to a parameter of type `PointerType | undefined`
if (searchText.length === 0) { return null; }

const dictionaryEntries = await this._api.kanjiFind(searchText, optionsContext);
Expand Down Expand Up @@ -1635,9 +1637,12 @@ export class TextScanner extends EventDispatcher {
* @param {?string} includeSelector
* @param {?string} excludeSelector
* @param {boolean} layoutAwareScan
* @param {import('text-scanner').PointerType | undefined} pointerType
*/
_constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan) {
_constrainTextSource(textSource, includeSelector, excludeSelector, layoutAwareScan, pointerType) {
let length = textSource.text().length;
excludeSelector = this._getExcludeSelectorForPointerType(excludeSelector, pointerType);

while (length > 0) {
const nodes = textSource.getNodesInRange();
if (
Expand All @@ -1652,6 +1657,18 @@ export class TextScanner extends EventDispatcher {
}
}

/**
* @param {?string} excludeSelector
* @param {import('text-scanner').PointerType | undefined} pointerType
* @returns {?string}
*/
_getExcludeSelectorForPointerType(excludeSelector, pointerType) {
if (pointerType === 'touch') {
return '.gloss-link,.gloss-link *,' + (excludeSelector ?? '');
}
return excludeSelector;
}

/**
* @param {string} text
* @returns {Promise<boolean>}
Expand Down

0 comments on commit be93bfe

Please sign in to comment.