Skip to content

Commit

Permalink
Use TextSourceGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Jan 28, 2024
1 parent 9245bc6 commit 941d614
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
8 changes: 6 additions & 2 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {log} from '../core/logger.js';
import {promiseAnimationFrame} from '../core/utilities.js';
import {DocumentUtil} from '../dom/document-util.js';
import {TextSourceElement} from '../dom/text-source-element.js';
import {TextSourceGenerator} from '../dom/text-source-generator.js';
import {TextSourceRange} from '../dom/text-source-range.js';
import {TextScanner} from '../language/text-scanner.js';
import {yomitan} from '../yomitan.js';
Expand Down Expand Up @@ -84,14 +85,17 @@ export class Frontend {
this._contentScale = 1.0;
/** @type {Promise<void>} */
this._lastShowPromise = Promise.resolve();
/** @type {TextSourceGenerator} */
this._textSourceGenerator = new TextSourceGenerator();
/** @type {TextScanner} */
this._textScanner = new TextScanner({
node: window,
ignoreElements: this._ignoreElements.bind(this),
ignorePoint: this._ignorePoint.bind(this),
getSearchContext: this._getSearchContext.bind(this),
searchTerms: true,
searchKanji: true
searchKanji: true,
textSourceGenerator: this._textSourceGenerator
});
/** @type {boolean} */
this._textScannerHasBeenEnabled = false;
Expand Down Expand Up @@ -950,6 +954,6 @@ export class Frontend {
async _prepareGoogleDocs() {
const {GoogleDocsUtil} = await import('../accessibility/google-docs-util.js');
const googleDocsUtil = new GoogleDocsUtil();
DocumentUtil.registerGetRangeFromPointHandler(googleDocsUtil.getRangeFromPoint.bind(googleDocsUtil));
this._textSourceGenerator.registerGetRangeFromPointHandler(googleDocsUtil.getRangeFromPoint.bind(googleDocsUtil));
}
}
9 changes: 7 additions & 2 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {clone, deepEqual, promiseTimeout} from '../core/utilities.js';
import {PopupMenu} from '../dom/popup-menu.js';
import {querySelectorNotNull} from '../dom/query-selector.js';
import {ScrollElement} from '../dom/scroll-element.js';
import {TextSourceGenerator} from '../dom/text-source-generator.js';
import {HotkeyHelpController} from '../input/hotkey-help-controller.js';
import {TextScanner} from '../language/text-scanner.js';
import {yomitan} from '../yomitan.js';
Expand Down Expand Up @@ -126,9 +127,12 @@ export class Display extends EventDispatcher {
this._queryParserVisibleOverride = null;
/** @type {HTMLElement} */
this._queryParserContainer = querySelectorNotNull(document, '#query-parser-container');
/** @type {TextSourceGenerator} */
this._textSourceGenerator = new TextSourceGenerator();
/** @type {QueryParser} */
this._queryParser = new QueryParser({
getSearchContext: this._getSearchContext.bind(this)
getSearchContext: this._getSearchContext.bind(this),
textSourceGenerator: this._textSourceGenerator
});
/** @type {HTMLElement} */
this._contentScrollElement = querySelectorNotNull(document, '#content-scroll');
Expand Down Expand Up @@ -1829,7 +1833,8 @@ export class Display extends EventDispatcher {
searchTerms: true,
searchKanji: false,
searchOnClick: true,
searchOnClickOnly: true
searchOnClickOnly: true,
textSourceGenerator: this._textSourceGenerator
});
this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *';
this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
Expand Down
5 changes: 3 additions & 2 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class QueryParser extends EventDispatcher {
/**
* @param {import('display').QueryParserConstructorDetails} details
*/
constructor({getSearchContext}) {
constructor({getSearchContext, textSourceGenerator}) {
super();
/** @type {import('display').GetSearchContextCallback} */
this._getSearchContext = getSearchContext;
Expand Down Expand Up @@ -62,7 +62,8 @@ export class QueryParser extends EventDispatcher {
getSearchContext,
searchTerms: true,
searchKanji: false,
searchOnClick: true
searchOnClick: true,
textSourceGenerator
});
/** @type {?(import('../language/japanese-wanakana.js'))} */
this._japaneseWanakanaModule = null;
Expand Down
7 changes: 5 additions & 2 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class TextScanner extends EventDispatcher {
searchTerms = false,
searchKanji = false,
searchOnClick = false,
searchOnClickOnly = false
searchOnClickOnly = false,
textSourceGenerator
}) {
super();
/** @type {HTMLElement|Window} */
Expand All @@ -58,6 +59,8 @@ export class TextScanner extends EventDispatcher {
this._searchOnClick = searchOnClick;
/** @type {boolean} */
this._searchOnClickOnly = searchOnClickOnly;
/** @type {import('../dom/text-source-generator').TextSourceGenerator} */
this._textSourceGenerator = textSourceGenerator;

/** @type {boolean} */
this._isPrepared = false;
Expand Down Expand Up @@ -1274,7 +1277,7 @@ export class TextScanner extends EventDispatcher {
return;
}

const textSource = DocumentUtil.getRangeFromPoint(x, y, {
const textSource = this._textSourceGenerator.getRangeFromPoint(x, y, {
deepContentScan: this._deepContentScan,
normalizeCssZoom: this._normalizeCssZoom
});
Expand Down
2 changes: 2 additions & 0 deletions types/ext/display.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import type {DisplayContentManager} from '../../ext/js/display/display-content-manager';
import type {HotkeyHelpController} from '../../ext/js/input/hotkey-help-controller';
import type {TextSourceGenerator} from '../../ext/js/dom/text-source-generator';
import type * as Dictionary from './dictionary';
import type * as Extension from './extension';
import type * as Settings from './settings';
Expand Down Expand Up @@ -127,6 +128,7 @@ export type GetSearchContextCallback = TextScannerTypes.GetSearchContextCallback

export type QueryParserConstructorDetails = {
getSearchContext: GetSearchContextCallback;
textSourceGenerator: TextSourceGenerator;
};

export type QueryParserOptions = {
Expand Down
16 changes: 0 additions & 16 deletions types/ext/document-util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import type * as TextSource from './text-source';

export type NormalizedWritingMode = 'horizontal-tb' | 'vertical-rl' | 'vertical-lr' | 'sideways-rl' | 'sideways-lr';

/**
Expand All @@ -34,20 +32,6 @@ export type GetRangeFromPointOptions = {
normalizeCssZoom: boolean;
};

/**
* Scans the document for text or elements with text information at the given coordinate.
* Coordinates are provided in [client space](https://developer.mozilla.org/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems).
* @returns A range for the hovered text or element, or `null` if no applicable content was found.
*/
export type GetRangeFromPointHandler = (
/** The x coordinate to search at. */
x: number,
/** The y coordinate to search at. */
y: number,
/** Options to configure how element detection is performed. */
options: GetRangeFromPointOptions,
) => (TextSource.TextSource | null);

export type ToNumberConstraints = {
min?: string | number;
max?: string | number;
Expand Down
2 changes: 2 additions & 0 deletions types/ext/text-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import type {TextScanner} from '../../ext/js/language/text-scanner';
import type {TextSourceGenerator} from '../../ext/js/dom/text-source-generator';
import type * as Dictionary from './dictionary';
import type * as Display from './display';
import type * as Input from './input';
Expand Down Expand Up @@ -145,6 +146,7 @@ export type ConstructorDetails = {
searchKanji?: boolean;
searchOnClick?: boolean;
searchOnClickOnly?: boolean;
textSourceGenerator: TextSourceGenerator;
};

export type SearchContext = {
Expand Down

0 comments on commit 941d614

Please sign in to comment.