Skip to content

Commit

Permalink
Restore searchOnClick for search page (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuuuube authored Nov 5, 2024
1 parent 093a2f3 commit 37513b1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,8 @@ export class Display extends EventDispatcher {
getSearchContext: this._getSearchContext.bind(this),
searchTerms: true,
searchKanji: false,
searchOnClick: true,
searchOnClickOnly: true,
textSourceGenerator: this._textSourceGenerator,
});
this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *';
Expand Down
1 change: 1 addition & 0 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class QueryParser extends EventDispatcher {
getSearchContext,
searchTerms: true,
searchKanji: false,
searchOnClick: true,
textSourceGenerator,
});
/** @type {?(import('../language/ja/japanese-wanakana.js'))} */
Expand Down
67 changes: 66 additions & 1 deletion ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class TextScanner extends EventDispatcher {
ignorePoint = null,
searchTerms = false,
searchKanji = false,
searchOnClick = false,
searchOnClickOnly = false,
textSourceGenerator,
}) {
super();
Expand All @@ -56,6 +58,10 @@ export class TextScanner extends EventDispatcher {
this._searchTerms = searchTerms;
/** @type {boolean} */
this._searchKanji = searchKanji;
/** @type {boolean} */
this._searchOnClick = searchOnClick;
/** @type {boolean} */
this._searchOnClickOnly = searchOnClickOnly;
/** @type {import('../dom/text-source-generator').TextSourceGenerator} */
this._textSourceGenerator = textSourceGenerator;

Expand Down Expand Up @@ -658,6 +664,7 @@ export class TextScanner extends EventDispatcher {

switch (e.button) {
case 0: // Primary
if (this._searchOnClick) { this._resetPreventNextClickScan(); }
this._scanTimerClear();
this._triggerClear('mousedown');
break;
Expand Down Expand Up @@ -691,9 +698,33 @@ export class TextScanner extends EventDispatcher {
return false;
}

if (this._searchOnClick) {
this._onSearchClick(e);
return;
}

this._onMouseMove(e);
}

/**
* @param {MouseEvent} e
*/
_onSearchClick(e) {
const preventNextClickScan = this._preventNextClickScan;
this._preventNextClickScan = false;
if (this._preventNextClickScanTimer !== null) {
clearTimeout(this._preventNextClickScanTimer);
this._preventNextClickScanTimer = null;
}

if (preventNextClickScan) { return; }

const modifiers = getActiveModifiersAndButtons(e);
const modifierKeys = getActiveModifiers(e);
const inputInfo = this._createInputInfo(null, 'mouse', 'click', false, modifiers, modifierKeys);
void this._searchAt(e.clientX, e.clientY, inputInfo);
}

/** */
_onAuxClick() {
this._preventNextContextMenu = false;
Expand Down Expand Up @@ -1117,7 +1148,9 @@ export class TextScanner extends EventDispatcher {
const capture = true;
/** @type {import('event-listener-collection').AddEventListenerArgs[]} */
let eventListenerInfos;
if (this._arePointerEventsSupported()) {
if (this._searchOnClickOnly) {
eventListenerInfos = this._getMouseClickOnlyEventListeners(capture);
} else if (this._arePointerEventsSupported()) {
eventListenerInfos = this._getPointerEventListeners(capture);
} else {
eventListenerInfos = [...this._getMouseEventListeners(capture)];
Expand All @@ -1128,6 +1161,9 @@ export class TextScanner extends EventDispatcher {
eventListenerInfos.push(...this._getTouchEventListeners(capture));
}
}
if (this._searchOnClick) {
eventListenerInfos.push(...this._getMouseClickOnlyEventListeners2(capture));
}

eventListenerInfos.push(this._getSelectionChangeCheckUserSelectionListener());

Expand Down Expand Up @@ -1194,6 +1230,35 @@ export class TextScanner extends EventDispatcher {
];
}

/**
* @param {boolean} capture
* @returns {import('event-listener-collection').AddEventListenerArgs[]}
*/
_getMouseClickOnlyEventListeners(capture) {
return [
[this._node, 'click', this._onClick.bind(this), capture],
];
}

/**
* @param {boolean} capture
* @returns {import('event-listener-collection').AddEventListenerArgs[]}
*/
_getMouseClickOnlyEventListeners2(capture) {
const {documentElement} = document;
/** @type {import('event-listener-collection').AddEventListenerArgs[]} */
const entries = [
[document, 'selectionchange', this._onSelectionChange.bind(this)],
];
if (documentElement !== null) {
entries.push([documentElement, 'mousedown', this._onSearchClickMouseDown.bind(this), capture]);
if (this._touchInputEnabled) {
entries.push([documentElement, 'touchstart', this._onSearchClickTouchStart.bind(this), {passive: true, capture}]);
}
}
return entries;
}

/**
* @returns {import('event-listener-collection').AddEventListenerArgs}
*/
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 @@ -154,6 +154,8 @@ export type ConstructorDetails = {
ignorePoint?: ((x: number, y: number) => Promise<boolean>) | null;
searchTerms?: boolean;
searchKanji?: boolean;
searchOnClick?: boolean;
searchOnClickOnly?: boolean;
textSourceGenerator: TextSourceGenerator;
};

Expand Down

0 comments on commit 37513b1

Please sign in to comment.