Skip to content

Commit

Permalink
Split into multiple events
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Feb 2, 2024
1 parent 73ab96e commit 8247811
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 124 deletions.
51 changes: 29 additions & 22 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class Frontend {
chrome.runtime.onMessage.addListener(this._onRuntimeMessage.bind(this));

this._textScanner.on('clear', this._onTextScannerClear.bind(this));
this._textScanner.on('searched', this._onSearched.bind(this));
this._textScanner.on('searchSuccess', this._onSearchSuccess.bind(this));
this._textScanner.on('searchEmpty', this._onSearchEmpty.bind(this));
this._textScanner.on('searchError', this._onSearchError.bind(this));

/* eslint-disable no-multi-spaces */
this._application.crossFrame.registerHandlers([
Expand Down Expand Up @@ -369,31 +371,36 @@ export class Frontend {
}

/**
* @param {import('text-scanner').SearchedEventDetails} details
* @param {import('text-scanner').EventArgument<'searchSuccess'>} details
*/
_onSearched({type, dictionaryEntries, sentence, inputInfo: {eventType, passive, detail: inputInfoDetail}, textSource, optionsContext, detail, error}) {
_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo: {eventType, detail: inputInfoDetail}, textSource, optionsContext, detail}) {
this._stopClearSelectionDelayed();
let focus = (eventType === 'mouseMove');
if (typeof inputInfoDetail === 'object' && inputInfoDetail !== null) {
const focus2 = inputInfoDetail.focus;
if (typeof focus2 === 'boolean') { focus = focus2; }
}
this._showContent(textSource, focus, dictionaryEntries, type, sentence, detail !== null ? detail.documentTitle : null, optionsContext);
}

/** */
_onSearchEmpty() {
const scanningOptions = /** @type {import('settings').ProfileOptions} */ (this._options).scanning;
if (scanningOptions.autoHideResults) {
this._clearSelectionDelayed(scanningOptions.hideDelay, false, false);
}
}

if (error !== null) {
if (this._application.webExtension.unloaded) {
if (textSource !== null && !passive) {
this._showExtensionUnloaded(textSource);
}
} else {
log.error(error);
}
} if (type !== null && optionsContext !== null && textSource !== null) {
this._stopClearSelectionDelayed();
let focus = (eventType === 'mouseMove');
if (typeof inputInfoDetail === 'object' && inputInfoDetail !== null) {
const focus2 = inputInfoDetail.focus;
if (typeof focus2 === 'boolean') { focus = focus2; }
/**
* @param {import('text-scanner').EventArgument<'searchError'>} details
*/
_onSearchError({error, textSource, inputInfo: {passive}}) {
if (this._application.webExtension.unloaded) {
if (textSource !== null && !passive) {
this._showExtensionUnloaded(textSource);
}
this._showContent(textSource, focus, dictionaryEntries, type, sentence, detail !== null ? detail.documentTitle : null, optionsContext);
} else {
if (scanningOptions.autoHideResults) {
this._clearSelectionDelayed(scanningOptions.hideDelay, false, false);
}
log.error(error);
}
}

Expand Down Expand Up @@ -888,7 +895,7 @@ export class Frontend {
}

/**
* @returns {Promise<{optionsContext: import('settings').OptionsContext, detail?: import('text-scanner').SearchResultDetail}>}
* @returns {Promise<import('text-scanner').SearchContext>}
*/
async _getSearchContext() {
let url = window.location.href;
Expand Down
29 changes: 19 additions & 10 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,8 @@ export class Display extends EventDispatcher {
this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
this._contentTextScanner.prepare();
this._contentTextScanner.on('clear', this._onContentTextScannerClear.bind(this));
this._contentTextScanner.on('searched', this._onContentTextScannerSearched.bind(this));
this._contentTextScanner.on('searchSuccess', this._onContentTextScannerSearchSuccess.bind(this));
this._contentTextScanner.on('searchError', this._onContentTextScannerSearchError.bind(this));
}

const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
Expand Down Expand Up @@ -1895,15 +1896,9 @@ export class Display extends EventDispatcher {
}

/**
* @param {import('text-scanner').SearchedEventDetails} details
* @param {import('text-scanner').EventArgument<'searchSuccess'>} details
*/
_onContentTextScannerSearched({type, dictionaryEntries, sentence, textSource, optionsContext, error}) {
if (error !== null && !this._application.webExtension.unloaded) {
log.error(error);
}

if (type === null || textSource === null) { return; }

_onContentTextScannerSearchSuccess({type, dictionaryEntries, sentence, textSource, optionsContext}) {
const query = textSource.text();
const url = window.location.href;
const documentTitle = document.title;
Expand Down Expand Up @@ -1932,11 +1927,25 @@ export class Display extends EventDispatcher {
this.setContent(details);
}

/**
* @param {import('text-scanner').EventArgument<'searchError'>} details
*/
_onContentTextScannerSearchError({error}) {
if (!this._application.webExtension.unloaded) {
log.error(error);
}
}

/**
* @type {import('display').GetSearchContextCallback}
*/
_getSearchContext() {
return {optionsContext: this.getOptionsContext()};
return {
optionsContext: this.getOptionsContext(),
detail: {
documentTitle: document.title
}
};
}

/**
Expand Down
33 changes: 12 additions & 21 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export class QueryParser extends EventDispatcher {
prepare() {
this._textScanner.prepare();
this._textScanner.on('clear', this._onTextScannerClear.bind(this));
this._textScanner.on('searched', this._onSearched.bind(this));
this._textScanner.on('searchSuccess', this._onSearchSuccess.bind(this));
this._textScanner.on('searchError', this._onSearchError.bind(this));
this._queryParserModeSelect.addEventListener('change', this._onParserChange.bind(this), false);
}

Expand Down Expand Up @@ -147,28 +148,11 @@ export class QueryParser extends EventDispatcher {
}

/**
* @param {import('text-scanner').SearchedEventDetails} e
* @param {import('text-scanner').EventArgument<'searchSuccess'>} details
*/
_onSearched(e) {
const {error} = e;
if (error !== null) {
log.error(error);
return;
}

const {
textScanner,
type,
dictionaryEntries,
sentence,
inputInfo,
textSource,
optionsContext
} = e;
if (type === null || dictionaryEntries === null || sentence === null || optionsContext === null || textSource === null) { return; }

_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo, textSource, optionsContext}) {
this.trigger('searched', {
textScanner,
textScanner: this._textScanner,
type,
dictionaryEntries,
sentence,
Expand All @@ -179,6 +163,13 @@ export class QueryParser extends EventDispatcher {
});
}

/**
* @param {import('text-scanner').EventArgument<'searchError'>} details
*/
_onSearchError({error}) {
log.error(error);
}

/**
* @param {Event} e
*/
Expand Down
88 changes: 33 additions & 55 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,6 @@ export class TextScanner extends EventDispatcher {
* @param {import('text-scanner').InputInfo} inputInfo
*/
async _search(textSource, searchTerms, searchKanji, inputInfo) {
/** @type {?import('dictionary').DictionaryEntry[]} */
let dictionaryEntries = null;
/** @type {?import('display').HistoryStateSentence} */
let sentence = null;
/** @type {?import('display').PageType} */
let type = null;
/** @type {?Error} */
let error = null;
let searched = false;
/** @type {?import('settings').OptionsContext} */
let optionsContext = null;
/** @type {?import('text-scanner').SearchResultDetail} */
let detail = null;

try {
const inputInfoDetail = inputInfo.detail;
const selectionRestoreInfo = (
Expand All @@ -451,62 +437,54 @@ export class TextScanner extends EventDispatcher {

const getSearchContextPromise = this._getSearchContext();
const getSearchContextResult = getSearchContextPromise instanceof Promise ? await getSearchContextPromise : getSearchContextPromise;
const {detail: detail2} = getSearchContextResult;
if (typeof detail2 !== 'undefined') { detail = detail2; }
optionsContext = this._createOptionsContextForInput(getSearchContextResult.optionsContext, inputInfo);

searched = true;

let valid = false;
const {detail} = getSearchContextResult;
const optionsContext = this._createOptionsContextForInput(getSearchContextResult.optionsContext, inputInfo);

/** @type {?import('dictionary').DictionaryEntry[]} */
let dictionaryEntries = null;
/** @type {?import('display').HistoryStateSentence} */
let sentence = null;
/** @type {'terms'|'kanji'} */
let type = 'terms';
const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext);
if (result !== null) {
({dictionaryEntries, sentence, type} = result);
valid = true;
} else if (textSource !== null && textSource instanceof TextSourceElement && await this._hasJapanese(textSource.fullContent)) {
dictionaryEntries = [];
sentence = {text: '', offset: 0};
type = 'terms';
valid = true;
}

if (valid) {
if (dictionaryEntries !== null && sentence !== null) {
this._inputInfoCurrent = inputInfo;
this.setCurrentTextSource(textSource);
if (typeof selectionRestoreInfo !== 'undefined') {
this._selectionRestoreInfo = selectionRestoreInfo;
}
this._selectionRestoreInfo = selectionRestoreInfo;

this.trigger('searchSuccess', {
type,
dictionaryEntries,
sentence,
inputInfo,
textSource,
optionsContext,
detail
});
} else {
this._triggerSearchEmpty(inputInfo);
}
} catch (e) {
error = e instanceof Error ? e : new Error(`A search error occurred: ${e}`);
} catch (error) {
this.trigger('searchError', {
error: error instanceof Error ? error : new Error(`A search error occurred: ${error}`),
textSource,
inputInfo
});
}

if (!searched) { return; }

this._triggerSearched(type, dictionaryEntries, sentence, inputInfo, textSource, optionsContext, detail, error);
}

/**
* @param {?import('display').PageType} type
* @param {?import('dictionary').DictionaryEntry[]} dictionaryEntries
* @param {?import('display').HistoryStateSentence} sentence
* @param {import('text-scanner').InputInfo} inputInfo
* @param {?import('text-source').TextSource} textSource
* @param {?import('settings').OptionsContext} optionsContext
* @param {?import('text-scanner').SearchResultDetail} detail
* @param {?Error} error
*/
_triggerSearched(type, dictionaryEntries, sentence, inputInfo, textSource, optionsContext, detail, error) {
this.trigger('searched', {
textScanner: this,
type,
dictionaryEntries,
sentence,
inputInfo,
textSource,
optionsContext,
detail,
error
});
*/
_triggerSearchEmpty(inputInfo) {
this.trigger('searchEmpty', {inputInfo});
}

/** */
Expand Down Expand Up @@ -1299,7 +1277,7 @@ export class TextScanner extends EventDispatcher {
textSource.cleanup();
}
} else {
this._triggerSearched(null, null, null, inputInfo, null, null, null, null);
this._triggerSearchEmpty(inputInfo);
}
} catch (e) {
log.error(e);
Expand Down
34 changes: 18 additions & 16 deletions types/ext/text-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import type {TextScanner} from '../../ext/js/language/text-scanner';
import type {TextSourceGenerator} from '../../ext/js/dom/text-source-generator';
import type {API} from '../../ext/js/comm/api';
import type * as Dictionary from './dictionary';
Expand Down Expand Up @@ -94,19 +93,6 @@ export type InputConfig = {
preventPenScrolling: boolean;
};

// TODO : this event should be split into 3 different types : searchSuccess, searchEmpty, searchError
export type SearchedEventDetails = {
textScanner: TextScanner;
type: Display.PageType | null;
dictionaryEntries: Dictionary.DictionaryEntry[] | null;
sentence: Display.HistoryStateSentence | null;
inputInfo: InputInfo;
textSource: TextSource.TextSource | null;
optionsContext: Settings.OptionsContext | null;
detail: SearchResultDetail | null;
error: Error | null;
};

export type InputInfo = {
input: InputConfig | null;
pointerType: PointerType;
Expand All @@ -123,10 +109,26 @@ export type InputInfoDetail = {
};

export type Events = {
searched: SearchedEventDetails;
clear: {
reason: ClearReason;
};
searchSuccess: {
type: 'terms' | 'kanji';
dictionaryEntries: Dictionary.DictionaryEntry[];
sentence: Display.HistoryStateSentence;
inputInfo: InputInfo;
textSource: TextSource.TextSource;
optionsContext: Settings.OptionsContext;
detail: SearchResultDetail;
};
searchEmpty: {
inputInfo: InputInfo;
};
searchError: {
error: Error;
textSource: TextSource.TextSource;
inputInfo: InputInfo;
};
};

export type ClearReason = 'mousedown';
Expand Down Expand Up @@ -154,7 +156,7 @@ export type ConstructorDetails = {

export type SearchContext = {
optionsContext: Settings.OptionsContext;
detail?: SearchResultDetail;
detail: SearchResultDetail;
};

export type SelectionRestoreInfo = {
Expand Down

0 comments on commit 8247811

Please sign in to comment.