Do not change selection if user selected text manually (#2186)

* Do not change selection if user selected text manually

* Remove capture argument for selectionchange listener

It is not applicable

* Make a note of user selection when Yomichan is enabled

User might have some text selected on a page prior to enabling Yomichan
via the toolbar icon; let it stay intact.

* Extract selection change by user listener into a method
This commit is contained in:
oakkitten 2022-07-01 22:12:55 +01:00 committed by GitHub
parent 4e7762edac
commit 49c75c3093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,6 +87,9 @@ class TextScanner extends EventDispatcher {
this._pointerIdTypeMap = new Map(); this._pointerIdTypeMap = new Map();
this._canClearSelection = true; this._canClearSelection = true;
this._yomichanIsChangingTextSelectionNow = false;
this._userHasNotSelectedAnythingManually = true;
} }
get canClearSelection() { get canClearSelection() {
@ -141,6 +144,7 @@ class TextScanner extends EventDispatcher {
if (value) { if (value) {
this._hookEvents(); this._hookEvents();
this._userHasNotSelectedAnythingManually = window.getSelection().isCollapsed;
} }
} }
@ -259,8 +263,10 @@ class TextScanner extends EventDispatcher {
setCurrentTextSource(textSource) { setCurrentTextSource(textSource) {
this._textSourceCurrent = textSource; this._textSourceCurrent = textSource;
if (this._selectText) { if (this._selectText && this._userHasNotSelectedAnythingManually) {
this._yomichanIsChangingTextSelectionNow = true;
this._textSourceCurrent.select(); this._textSourceCurrent.select();
setTimeout(() => this._yomichanIsChangingTextSelectionNow = false, 0);
this._textSourceCurrentSelected = true; this._textSourceCurrentSelected = true;
} else { } else {
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
@ -371,6 +377,11 @@ class TextScanner extends EventDispatcher {
this._preventNextClickScan = true; this._preventNextClickScan = true;
} }
_onSelectionChangeCheckUserSelection() {
if (this._yomichanIsChangingTextSelectionNow) { return; }
this._userHasNotSelectedAnythingManually = window.getSelection().isCollapsed;
}
_onSearchClickMouseDown(e) { _onSearchClickMouseDown(e) {
if (e.button !== 0) { return; } if (e.button !== 0) { return; }
this._resetPreventNextClickScan(); this._resetPreventNextClickScan();
@ -754,6 +765,8 @@ class TextScanner extends EventDispatcher {
eventListenerInfos.push(...this._getMouseClickOnlyEventListeners2(capture)); eventListenerInfos.push(...this._getMouseClickOnlyEventListeners2(capture));
} }
eventListenerInfos.push(this._getSelectionChangeCheckUserSelectionListener());
for (const args of eventListenerInfos) { for (const args of eventListenerInfos) {
this._eventListeners.addEventListener(...args); this._eventListeners.addEventListener(...args);
} }
@ -815,6 +828,10 @@ class TextScanner extends EventDispatcher {
return entries; return entries;
} }
_getSelectionChangeCheckUserSelectionListener() {
return [document, 'selectionchange', this._onSelectionChangeCheckUserSelection.bind(this)];
}
_getTouch(touchList, identifier) { _getTouch(touchList, identifier) {
for (const touch of touchList) { for (const touch of touchList) {
if (touch.identifier === identifier) { if (touch.identifier === identifier) {