Fix text selection not always working properly (#2246)

This commit is contained in:
toasted-nutbread 2022-10-15 11:40:22 -04:00 committed by GitHub
parent abb3e5d5d0
commit 5e707248ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,6 +87,7 @@ class TextScanner extends EventDispatcher {
this._canClearSelection = true; this._canClearSelection = true;
this._textSelectionTimer = null;
this._yomichanIsChangingTextSelectionNow = false; this._yomichanIsChangingTextSelectionNow = false;
this._userHasNotSelectedAnythingManually = true; this._userHasNotSelectedAnythingManually = true;
} }
@ -269,7 +270,13 @@ class TextScanner extends EventDispatcher {
if (this._selectText && this._userHasNotSelectedAnythingManually) { if (this._selectText && this._userHasNotSelectedAnythingManually) {
this._yomichanIsChangingTextSelectionNow = true; this._yomichanIsChangingTextSelectionNow = true;
this._textSourceCurrent.select(); this._textSourceCurrent.select();
setTimeout(() => this._yomichanIsChangingTextSelectionNow = false, 0); if (this._textSelectionTimer !== null) { clearTimeout(this._textSelectionTimer); }
// This timeout uses a 50ms delay to ensure that the selectionchange event has time to occur.
// If the delay is 0ms, the timeout will sometimes complete before the event.
this._textSelectionTimer = setTimeout(() => {
this._yomichanIsChangingTextSelectionNow = false;
this._textSelectionTimer = null;
}, 50);
this._textSourceCurrentSelected = true; this._textSourceCurrentSelected = true;
} else { } else {
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;