Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Fix text selection during scanning (#1508)
Browse files Browse the repository at this point in the history
* Move function

* Add _onSearchClick function

* Move _onSearchClick after event prevention

* Prevent search if selection is changed before a click occurs
  • Loading branch information
toasted-nutbread committed Mar 10, 2021
1 parent 3a7cee9 commit ea8fa9c
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class TextScanner extends EventDispatcher {
this._enabledValue = false;
this._eventListeners = new EventListenerCollection();

this._preventNextClickScan = false;
this._preventNextClickScanTimer = null;
this._preventNextClickScanTimerDuration = 50;
this._preventNextClickScanTimerCallback = this._onPreventNextClickScanTimeout.bind(this);

this._primaryTouchIdentifier = null;
this._preventNextContextMenu = false;
this._preventNextMouseDown = false;
Expand Down Expand Up @@ -350,6 +355,30 @@ class TextScanner extends EventDispatcher {
return results;
}

_resetPreventNextClickScan() {
this._preventNextClickScan = false;
if (this._preventNextClickScanTimer !== null) { clearTimeout(this._preventNextClickScanTimer); }
this._preventNextClickScanTimer = setTimeout(this._preventNextClickScanTimerCallback, this._preventNextClickScanTimerDuration);
}

_onPreventNextClickScanTimeout() {
this._preventNextClickScanTimer = null;
}

_onSelectionChange() {
if (this._preventNextClickScanTimer !== null) { return; } // Ignore deselection that occurs at the start of the click
this._preventNextClickScan = true;
}

_onSearchClickMouseDown(e) {
if (e.button !== 0) { return; }
this._resetPreventNextClickScan();
}

_onSearchClickTouchStart() {
this._resetPreventNextClickScan();
}

_onMouseOver(e) {
if (this._ignoreElements !== null && this._ignoreElements().includes(e.target)) {
this._scanTimerClear();
Expand All @@ -376,6 +405,7 @@ class TextScanner extends EventDispatcher {

switch (e.button) {
case 0: // Primary
if (this._searchOnClick) { this._resetPreventNextClickScan(); }
this._scanTimerClear();
this.clearSelection(false);
break;
Expand All @@ -394,19 +424,32 @@ class TextScanner extends EventDispatcher {
}

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

if (this._preventNextClick) {
this._preventNextClick = false;
e.preventDefault();
e.stopPropagation();
return false;
}

if (this._searchOnClick) {
this._onSearchClick(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 = DocumentUtil.getActiveModifiersAndButtons(e);
const modifierKeys = DocumentUtil.getActiveModifiers(e);
const inputInfo = this._createInputInfo(null, 'mouse', 'click', false, modifiers, modifierKeys);
this._searchAt(e.clientX, e.clientY, inputInfo);
}

_onAuxClick() {
Expand Down Expand Up @@ -687,6 +730,9 @@ class TextScanner extends EventDispatcher {
eventListenerInfos.push(...this._getTouchEventListeners());
}
}
if (this._searchOnClick) {
eventListenerInfos.push(...this._getMouseClickOnlyEventListeners2());
}

for (const args of eventListenerInfos) {
this._eventListeners.addEventListener(...args);
Expand Down Expand Up @@ -718,11 +764,6 @@ class TextScanner extends EventDispatcher {
];
}

_getMouseClickOnlyEventListeners() {
return [
[this._node, 'click', this._onClick.bind(this)]
];
}
_getTouchEventListeners() {
return [
[this._node, 'auxclick', this._onAuxClick.bind(this)],
Expand All @@ -734,6 +775,26 @@ class TextScanner extends EventDispatcher {
];
}

_getMouseClickOnlyEventListeners() {
return [
[this._node, 'click', this._onClick.bind(this)]
];
}

_getMouseClickOnlyEventListeners2() {
const {documentElement} = document;
const entries = [
[document, 'selectionchange', this._onSelectionChange.bind(this)]
];
if (documentElement !== null) {
entries.push([documentElement, 'mousedown', this._onSearchClickMouseDown.bind(this)]);
if (this._touchInputEnabled) {
entries.push([documentElement, 'touchstart', this._onSearchClickTouchStart.bind(this)]);
}
}
return entries;
}

_getTouch(touchList, identifier) {
for (const touch of touchList) {
if (touch.identifier === identifier) {
Expand Down

0 comments on commit ea8fa9c

Please sign in to comment.