From 5e707248ce4eb595056fd9c46acaf9a0d83678c6 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 15 Oct 2022 11:40:22 -0400 Subject: Fix text selection not always working properly (#2246) --- ext/js/language/text-scanner.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'ext/js/language') diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index 826c8c5b..fcba47a4 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -87,6 +87,7 @@ class TextScanner extends EventDispatcher { this._canClearSelection = true; + this._textSelectionTimer = null; this._yomichanIsChangingTextSelectionNow = false; this._userHasNotSelectedAnythingManually = true; } @@ -269,7 +270,13 @@ class TextScanner extends EventDispatcher { if (this._selectText && this._userHasNotSelectedAnythingManually) { this._yomichanIsChangingTextSelectionNow = true; 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; } else { this._textSourceCurrentSelected = false; -- cgit v1.2.3