Options context updates (#1017)

* Update event order

* Update inputs

* Add type and cause to input info

* Update input info args

* Simplify arguments

* Update argument name

* Change order
This commit is contained in:
toasted-nutbread 2020-11-08 22:55:43 -05:00 committed by GitHub
parent 3a23f081d1
commit eb8069a494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 38 deletions

View File

@ -78,7 +78,7 @@ class QueryParser extends EventDispatcher {
// Private // Private
_onSearched({type, definitions, sentence, input, textSource, optionsContext, error}) { _onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, error}) {
if (error !== null) { if (error !== null) {
yomichan.logError(error); yomichan.logError(error);
return; return;
@ -89,7 +89,7 @@ class QueryParser extends EventDispatcher {
type, type,
definitions, definitions,
sentence, sentence,
input, inputInfo,
textSource, textSource,
optionsContext optionsContext
}); });

View File

@ -250,7 +250,7 @@ class Frontend {
await this.updateOptions(); await this.updateOptions();
} }
_onSearched({type, definitions, sentence, input: {cause, empty}, textSource, optionsContext, error}) { _onSearched({type, definitions, sentence, inputInfo: {cause, empty}, textSource, optionsContext, error}) {
const scanningOptions = this._options.scanning; const scanningOptions = this._options.scanning;
if (error !== null) { if (error !== null) {

View File

@ -507,7 +507,7 @@ class Display extends EventDispatcher {
} }
} }
_onQueryParserSearch({type, definitions, sentence, input: {cause}, textSource}) { _onQueryParserSearch({type, definitions, sentence, inputInfo: {cause}, textSource}) {
const query = textSource.text(); const query = textSource.text();
const history = (cause === 'click'); const history = (cause === 'click');
const details = { const details = {

View File

@ -35,7 +35,7 @@ class TextScanner extends EventDispatcher {
this._isPrepared = false; this._isPrepared = false;
this._ignoreNodes = null; this._ignoreNodes = null;
this._inputCurrent = null; this._inputInfoCurrent = null;
this._scanTimerPromise = null; this._scanTimerPromise = null;
this._textSourceCurrent = null; this._textSourceCurrent = null;
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
@ -204,7 +204,7 @@ class TextScanner extends EventDispatcher {
} }
this._textSourceCurrent = null; this._textSourceCurrent = null;
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
this._inputCurrent = null; this._inputInfoCurrent = null;
} }
this.trigger('clearSelection', {passive}); this.trigger('clearSelection', {passive});
} }
@ -224,20 +224,21 @@ class TextScanner extends EventDispatcher {
} }
async searchLast() { async searchLast() {
if (this._textSourceCurrent !== null && this._inputCurrent !== null) { if (this._textSourceCurrent !== null && this._inputInfoCurrent !== null) {
await this._search(this._textSourceCurrent, this._searchTerms, this._searchKanji, this._inputCurrent); await this._search(this._textSourceCurrent, this._searchTerms, this._searchKanji, this._inputInfoCurrent);
return true; return true;
} }
return false; return false;
} }
async search(textSource) { async search(textSource) {
return await this._search(textSource, this._searchTerms, this._searchKanji, {cause: 'script', index: -1, empty: false}); const inputInfo = this._createInputInfo(-1, false, null, 'script', 'script', [], []);
return await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
} }
// Private // Private
async _search(textSource, searchTerms, searchKanji, input) { async _search(textSource, searchTerms, searchKanji, inputInfo) {
let definitions = null; let definitions = null;
let sentence = null; let sentence = null;
let type = null; let type = null;
@ -256,7 +257,7 @@ class TextScanner extends EventDispatcher {
const result = await this._findDefinitions(textSource, searchTerms, searchKanji, optionsContext); const result = await this._findDefinitions(textSource, searchTerms, searchKanji, optionsContext);
if (result !== null) { if (result !== null) {
({definitions, sentence, type} = result); ({definitions, sentence, type} = result);
this._inputCurrent = input; this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource); this.setCurrentTextSource(textSource);
} }
} catch (e) { } catch (e) {
@ -270,7 +271,7 @@ class TextScanner extends EventDispatcher {
type, type,
definitions, definitions,
sentence, sentence,
input, inputInfo,
textSource, textSource,
optionsContext, optionsContext,
error error
@ -286,7 +287,7 @@ class TextScanner extends EventDispatcher {
_onMouseMove(e) { _onMouseMove(e) {
this._scanTimerClear(); this._scanTimerClear();
const inputInfo = this._getMatchingInputGroupFromEvent(e, 'mouse'); const inputInfo = this._getMatchingInputGroupFromEvent('mouse', 'mouseMove', e);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
this._searchAtFromMouseMove(e.clientX, e.clientY, inputInfo); this._searchAtFromMouseMove(e.clientX, e.clientY, inputInfo);
@ -322,7 +323,10 @@ class TextScanner extends EventDispatcher {
_onClick(e) { _onClick(e) {
if (this._searchOnClick) { if (this._searchOnClick) {
this._searchAt(e.clientX, e.clientY, 'mouse', 'click', {index: -1, empty: false, input: null}); const modifiers = DocumentUtil.getActiveModifiersAndButtons(e);
const modifierKeys = DocumentUtil.getActiveModifiers(e);
const inputInfo = this._createInputInfo(-1, false, null, 'mouse', 'click', modifiers, modifierKeys);
this._searchAt(e.clientX, e.clientY, inputInfo);
} }
if (this._preventNextClick) { if (this._preventNextClick) {
@ -403,12 +407,11 @@ class TextScanner extends EventDispatcher {
return; return;
} }
const type = 'touch'; const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
const inputInfo = this._getMatchingInputGroupFromEvent(e, type);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
if (inputInfo.input.options.scanOnTouchMove) { if (inputInfo.input.options.scanOnTouchMove) {
this._searchAt(primaryTouch.clientX, primaryTouch.clientY, type, 'touchMove', inputInfo); this._searchAt(primaryTouch.clientX, primaryTouch.clientY, inputInfo);
} }
e.preventDefault(); // Disable scroll e.preventDefault(); // Disable scroll
@ -513,10 +516,10 @@ class TextScanner extends EventDispatcher {
return; return;
} }
const inputInfo = this._getMatchingInputGroupFromEvent(e, 'touch'); const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchMove', e);
if (inputInfo === null || !inputInfo.input.options.scanOnTouchMove) { return; } if (inputInfo === null || !inputInfo.input.options.scanOnTouchMove) { return; }
this._searchAt(e.clientX, e.clientY, 'touch', 'touchMove', inputInfo); this._searchAt(e.clientX, e.clientY, inputInfo);
} }
_onTouchPointerUp() { _onTouchPointerUp() {
@ -707,11 +710,11 @@ class TextScanner extends EventDispatcher {
return {definitions, sentence, type: 'kanji'}; return {definitions, sentence, type: 'kanji'};
} }
async _searchAt(x, y, type, cause, inputInfo) { async _searchAt(x, y, inputInfo) {
if (this._pendingLookup) { return; } if (this._pendingLookup) { return; }
try { try {
const {index, empty, input: sourceInput} = inputInfo; const sourceInput = inputInfo.input;
let searchTerms = this._searchTerms; let searchTerms = this._searchTerms;
let searchKanji = this._searchKanji; let searchKanji = this._searchKanji;
if (sourceInput !== null) { if (sourceInput !== null) {
@ -719,8 +722,6 @@ class TextScanner extends EventDispatcher {
if (searchKanji && !sourceInput.options.searchKanji) { searchKanji = false; } if (searchKanji && !sourceInput.options.searchKanji) { searchKanji = false; }
} }
const input = {type, cause, index, empty};
this._pendingLookup = true; this._pendingLookup = true;
this._scanTimerClear(); this._scanTimerClear();
@ -730,7 +731,7 @@ class TextScanner extends EventDispatcher {
const textSource = this._documentUtil.getRangeFromPoint(x, y, this._deepContentScan); const textSource = this._documentUtil.getRangeFromPoint(x, y, this._deepContentScan);
try { try {
await this._search(textSource, searchTerms, searchKanji, input); await this._search(textSource, searchTerms, searchKanji, inputInfo);
} finally { } finally {
if (textSource !== null) { if (textSource !== null) {
textSource.cleanup(); textSource.cleanup();
@ -753,21 +754,19 @@ class TextScanner extends EventDispatcher {
} }
} }
await this._searchAt(x, y, 'mouse', 'mouseMove', inputInfo); await this._searchAt(x, y, inputInfo);
} }
async _searchAtFromTouchStart(e, x, y) { async _searchAtFromTouchStart(e, x, y) {
if (this._pendingLookup) { return; } if (this._pendingLookup) { return; }
const type = 'touch'; const inputInfo = this._getMatchingInputGroupFromEvent('touch', 'touchStart', e);
const cause = 'touchStart';
const inputInfo = this._getMatchingInputGroupFromEvent(e, type);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
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.options.preventTouchScrolling;
await this._searchAt(x, y, type, cause, inputInfo); await this._searchAt(x, y, inputInfo);
if ( if (
this._textSourceCurrent !== null && this._textSourceCurrent !== null &&
@ -782,8 +781,7 @@ class TextScanner extends EventDispatcher {
async _searchAtFromPen(e, x, y, cause, prevent) { async _searchAtFromPen(e, x, y, cause, prevent) {
if (this._pendingLookup) { return; } if (this._pendingLookup) { return; }
const type = 'pen'; const inputInfo = this._getMatchingInputGroupFromEvent('pen', cause, e);
const inputInfo = this._getMatchingInputGroupFromEvent(e, type);
if (inputInfo === null) { return; } if (inputInfo === null) { return; }
const {input: {options}} = inputInfo; const {input: {options}} = inputInfo;
@ -796,7 +794,7 @@ class TextScanner extends EventDispatcher {
const preventScroll = inputInfo.input.options.preventTouchScrolling; const preventScroll = inputInfo.input.options.preventTouchScrolling;
await this._searchAt(x, y, type, cause, inputInfo); await this._searchAt(x, y, inputInfo);
if ( if (
prevent && prevent &&
@ -809,13 +807,14 @@ class TextScanner extends EventDispatcher {
} }
} }
_getMatchingInputGroupFromEvent(event, type) { _getMatchingInputGroupFromEvent(type, cause, event) {
const modifiers = DocumentUtil.getActiveModifiersAndButtons(event); const modifiers = DocumentUtil.getActiveModifiersAndButtons(event);
this.trigger('activeModifiersChanged', {modifiers}); const modifierKeys = DocumentUtil.getActiveModifiers(event);
return this._getMatchingInputGroup(modifiers, type); this.trigger('activeModifiersChanged', {modifiers, modifierKeys});
return this._getMatchingInputGroup(type, cause, modifiers, modifierKeys);
} }
_getMatchingInputGroup(modifiers, type) { _getMatchingInputGroup(type, cause, modifiers, modifierKeys) {
let fallback = null; let fallback = null;
const modifiersSet = new Set(modifiers); const modifiersSet = new Set(modifiers);
for (let i = 0, ii = this._inputs.length; i < ii; ++i) { for (let i = 0, ii = this._inputs.length; i < ii; ++i) {
@ -824,15 +823,19 @@ class TextScanner extends EventDispatcher {
if (!types.has(type)) { continue; } if (!types.has(type)) { continue; }
if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) { if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) {
if (include.length > 0) { if (include.length > 0) {
return {index: i, empty: false, input}; return this._createInputInfo(i, false, input, type, cause, modifiers, modifierKeys);
} else if (fallback === null) { } else if (fallback === null) {
fallback = {index: i, empty: true, input}; fallback = this._createInputInfo(i, true, input, type, cause, modifiers, modifierKeys);
} }
} }
} }
return fallback; return fallback;
} }
_createInputInfo(index, empty, input, type, cause, modifiers, modifierKeys) {
return {index, empty, input, type, cause, modifiers, modifierKeys};
}
_setHasAll(set, values) { _setHasAll(set, values) {
for (const value of values) { for (const value of values) {
if (!set.has(value)) { if (!set.has(value)) {