TextScanner refactor (#2174)

* Refactor input conversion, add additional validation

* Simplify input data structure
This commit is contained in:
toasted-nutbread 2022-06-05 22:46:00 -04:00 committed by GitHub
parent 6cc57d953d
commit 4545900f70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -158,43 +158,7 @@ class TextScanner extends EventDispatcher {
matchTypePrefix matchTypePrefix
}) { }) {
if (Array.isArray(inputs)) { if (Array.isArray(inputs)) {
this._inputs = inputs.map(({ this._inputs = inputs.map((input) => this._convertInput(input));
include,
exclude,
types,
options: {
searchTerms,
searchKanji,
scanOnTouchMove,
scanOnTouchPress,
scanOnTouchRelease,
scanOnPenMove,
scanOnPenHover,
scanOnPenReleaseHover,
scanOnPenPress,
scanOnPenRelease,
preventTouchScrolling,
preventPenScrolling
}
}) => ({
include: this._getInputArray(include),
exclude: this._getInputArray(exclude),
types: this._getInputTypeSet(types),
options: {
searchTerms,
searchKanji,
scanOnTouchMove,
scanOnTouchPress,
scanOnTouchRelease,
scanOnPenMove,
scanOnPenHover,
scanOnPenReleaseHover,
scanOnPenPress,
scanOnPenRelease,
preventTouchScrolling,
preventPenScrolling
}
}));
} }
if (typeof deepContentScan === 'boolean') { if (typeof deepContentScan === 'boolean') {
this._deepContentScan = deepContentScan; this._deepContentScan = deepContentScan;
@ -526,7 +490,7 @@ class TextScanner extends EventDispatcher {
if (this._pendingLookup) { return; } if (this._pendingLookup) { return; }
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchStart', e); const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchStart', e);
if (inputInfo === null || !inputInfo.input.options.scanOnTouchPress) { return; } if (inputInfo === null || !inputInfo.input.scanOnTouchPress) { return; }
this._searchAtFromTouchStart(x, y, inputInfo); this._searchAtFromTouchStart(x, y, inputInfo);
} }
@ -551,7 +515,7 @@ class TextScanner extends EventDispatcher {
if (!allowSearch) { return; } if (!allowSearch) { return; }
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchEnd', e); const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchEnd', e);
if (inputInfo === null || !inputInfo.input.options.scanOnTouchRelease) { return; } if (inputInfo === null || !inputInfo.input.scanOnTouchRelease) { return; }
this._searchAtFromTouchEnd(x, y, inputInfo); this._searchAtFromTouchEnd(x, y, inputInfo);
} }
@ -581,7 +545,7 @@ class TextScanner extends EventDispatcher {
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e); const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
if (inputInfo.input.options.scanOnTouchMove) { if (inputInfo.input.scanOnTouchMove) {
this._searchAt(primaryTouch.clientX, primaryTouch.clientY, inputInfo); this._searchAt(primaryTouch.clientX, primaryTouch.clientY, inputInfo);
} }
@ -688,7 +652,7 @@ class TextScanner extends EventDispatcher {
} }
const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e); const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
if (inputInfo === null || !inputInfo.input.options.scanOnTouchMove) { return; } if (inputInfo === null || !inputInfo.input.scanOnTouchMove) { return; }
this._searchAt(e.clientX, e.clientY, inputInfo); this._searchAt(e.clientX, e.clientY, inputInfo);
} }
@ -940,8 +904,8 @@ class TextScanner extends EventDispatcher {
let searchTerms = this._searchTerms; let searchTerms = this._searchTerms;
let searchKanji = this._searchKanji; let searchKanji = this._searchKanji;
if (sourceInput !== null) { if (sourceInput !== null) {
if (searchTerms && !sourceInput.options.searchTerms) { searchTerms = false; } if (searchTerms && !sourceInput.searchTerms) { searchTerms = false; }
if (searchKanji && !sourceInput.options.searchKanji) { searchKanji = false; } if (searchKanji && !sourceInput.searchKanji) { searchKanji = false; }
} }
this._pendingLookup = true; this._pendingLookup = true;
@ -981,7 +945,7 @@ class TextScanner extends EventDispatcher {
async _searchAtFromTouchStart(x, y, inputInfo) { async _searchAtFromTouchStart(x, y, inputInfo) {
const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null; const textSourceCurrentPrevious = this._textSourceCurrent !== null ? this._textSourceCurrent.clone() : null;
const preventScroll = inputInfo.input.options.preventTouchScrolling; const preventScroll = inputInfo.input.preventTouchScrolling;
await this._searchAt(x, y, inputInfo); await this._searchAt(x, y, inputInfo);
@ -1005,10 +969,10 @@ class TextScanner extends EventDispatcher {
const inputInfo = this._getMatchingInputGroupFromEvent('pen', eventType, e); const inputInfo = this._getMatchingInputGroupFromEvent('pen', eventType, e);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
const {options} = inputInfo.input; const {input} = inputInfo;
if (!this._isPenEventSupported(eventType, options)) { return; } if (!this._isPenEventSupported(eventType, input)) { return; }
const preventScroll = options.preventPenScrolling; const preventScroll = input.preventPenScrolling;
await this._searchAt(e.clientX, e.clientY, inputInfo); await this._searchAt(e.clientX, e.clientY, inputInfo);
@ -1023,20 +987,20 @@ class TextScanner extends EventDispatcher {
} }
} }
_isPenEventSupported(eventType, options) { _isPenEventSupported(eventType, input) {
switch (eventType) { switch (eventType) {
case 'pointerDown': case 'pointerDown':
return options.scanOnPenPress; return input.scanOnPenPress;
case 'pointerUp': case 'pointerUp':
return options.scanOnPenRelease; return input.scanOnPenRelease;
} }
switch (this._penPointerState) { switch (this._penPointerState) {
case 1: // hovering case 1: // hovering
return options.scanOnPenHover; return input.scanOnPenHover;
case 2: // touching case 2: // touching
return options.scanOnPenMove; return input.scanOnPenMove;
case 3: // hovering after touching case 3: // hovering after touching
return options.scanOnPenReleaseHover; return input.scanOnPenReleaseHover;
default: // not active default: // not active
return false; return false;
} }
@ -1084,6 +1048,27 @@ class TextScanner extends EventDispatcher {
return true; return true;
} }
_convertInput(input) {
const {options} = input;
return {
include: this._getInputArray(input.include),
exclude: this._getInputArray(input.exclude),
types: this._getInputTypeSet(input.types),
searchTerms: this._getInputBoolean(options.searchTerms),
searchKanji: this._getInputBoolean(options.searchKanji),
scanOnTouchMove: this._getInputBoolean(options.scanOnTouchMove),
scanOnTouchPress: this._getInputBoolean(options.scanOnTouchPress),
scanOnTouchRelease: this._getInputBoolean(options.scanOnTouchRelease),
scanOnPenMove: this._getInputBoolean(options.scanOnPenMove),
scanOnPenHover: this._getInputBoolean(options.scanOnPenHover),
scanOnPenReleaseHover: this._getInputBoolean(options.scanOnPenReleaseHover),
scanOnPenPress: this._getInputBoolean(options.scanOnPenPress),
scanOnPenRelease: this._getInputBoolean(options.scanOnPenRelease),
preventTouchScrolling: this._getInputBoolean(options.preventTouchScrolling),
preventPenScrolling: this._getInputBoolean(options.preventPenScrolling)
};
}
_getInputArray(value) { _getInputArray(value) {
return ( return (
typeof value === 'string' ? typeof value === 'string' ?
@ -1100,6 +1085,10 @@ class TextScanner extends EventDispatcher {
return set; return set;
} }
_getInputBoolean(value) {
return typeof value === 'boolean' && value;
}
_getPointerEventType(e) { _getPointerEventType(e) {
// Workaround for Firefox bug not detecting certain 'touch' events as 'pen' events. // Workaround for Firefox bug not detecting certain 'touch' events as 'pen' events.
const cachedPointerType = this._pointerIdTypeMap.get(e.pointerId); const cachedPointerType = this._pointerIdTypeMap.get(e.pointerId);