Skip to content

Commit ffdcc0b

Browse files
committed
fix case of tracking modifiers when window loses focus
1 parent 9f982a4 commit ffdcc0b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/SIL.XForge.Scripture/ClientApp/src/app/shared/text/text.component.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { DOCUMENT } from '@angular/common';
12
import {
23
AfterViewInit,
34
ChangeDetectorRef,
45
Component,
56
DestroyRef,
67
EventEmitter,
8+
Inject,
79
Input,
810
OnDestroy,
911
Output
@@ -21,6 +23,7 @@ import { fromEvent, Subject, Subscription, timer } from 'rxjs';
2123
import { takeUntil } from 'rxjs/operators';
2224
import { LocalPresence, Presence } from 'sharedb/lib/sharedb';
2325
import tinyColor from 'tinycolor2';
26+
import { WINDOW } from 'xforge-common/browser-globals';
2427
import { DialogService } from 'xforge-common/dialog.service';
2528
import { LocaleDirection } from 'xforge-common/models/i18n-locale';
2629
import { UserDoc } from 'xforge-common/models/user-doc';
@@ -275,7 +278,9 @@ export class TextComponent implements AfterViewInit, OnDestroy {
275278
private readonly userService: UserService,
276279
readonly viewModel: TextViewModel,
277280
private readonly textDocService: TextDocService,
278-
private readonly quillFormatRegistry: QuillFormatRegistryService
281+
private readonly quillFormatRegistry: QuillFormatRegistryService,
282+
@Inject(DOCUMENT) private document: Document,
283+
@Inject(WINDOW) private window: Window
279284
) {
280285
let localCursorColor = localStorage.getItem(this.cursorColorStorageKey);
281286
if (localCursorColor == null) {
@@ -521,19 +526,19 @@ export class TextComponent implements AfterViewInit, OnDestroy {
521526

522527
// Listening to document 'selectionchange' event allows local cursor to change position on mousedown,
523528
// as opposed to quill 'onSelectionChange' event that doesn't fire until mouseup.
524-
fromEvent<MouseEvent>(document, 'selectionchange')
529+
fromEvent<MouseEvent>(this.document, 'selectionchange')
525530
.pipe(quietTakeUntilDestroyed(this.destroyRef))
526531
.subscribe(() => {
527532
this.updateLocalCursor();
528533
});
529534

530-
fromEvent<KeyboardEvent>(document, 'keydown')
535+
fromEvent<KeyboardEvent>(this.document, 'keydown')
531536
.pipe(quietTakeUntilDestroyed(this.destroyRef))
532537
.subscribe(event => {
533538
this.isShiftDown = event.shiftKey;
534539
});
535540

536-
fromEvent<KeyboardEvent>(document, 'keyup')
541+
fromEvent<KeyboardEvent>(this.document, 'keyup')
537542
.pipe(quietTakeUntilDestroyed(this.destroyRef))
538543
.subscribe(event => {
539544
// Call 'update()' when shift key is released, as update is disabled while shift is down
@@ -544,6 +549,18 @@ export class TextComponent implements AfterViewInit, OnDestroy {
544549

545550
this.isShiftDown = event.shiftKey;
546551
});
552+
553+
fromEvent<FocusEvent>(this.window, 'blur')
554+
.pipe(quietTakeUntilDestroyed(this.destroyRef))
555+
.subscribe(() => {
556+
// Treat window blur as releasing all modifiers
557+
const wasShiftDown: boolean = this.isShiftDown === true;
558+
if (wasShiftDown) {
559+
this.update();
560+
}
561+
562+
this.isShiftDown = false;
563+
});
547564
}
548565

549566
ngOnDestroy(): void {
@@ -564,7 +581,7 @@ export class TextComponent implements AfterViewInit, OnDestroy {
564581
fromEvent(this._editor.root, 'scroll')
565582
.pipe(quietTakeUntilDestroyed(this.destroyRef))
566583
.subscribe(() => this.updateHighlightMarkerVisibility());
567-
fromEvent(window, 'resize')
584+
fromEvent(this.window, 'resize')
568585
.pipe(quietTakeUntilDestroyed(this.destroyRef))
569586
.subscribe(() => this.setHighlightMarkerPosition());
570587
this.viewModel.editor = editor;
@@ -1144,7 +1161,7 @@ export class TextComponent implements AfterViewInit, OnDestroy {
11441161
const cursors: QuillCursors = this.editor.getModule('cursors') as QuillCursors;
11451162
cursors.createCursor(this.presenceId, '', '');
11461163

1147-
this.localCursorElement = document.querySelector(`#ql-cursor-${this.presenceId}`);
1164+
this.localCursorElement = this.document.querySelector(`#ql-cursor-${this.presenceId}`);
11481165

11491166
// Add a specific class to the local cursor
11501167
if (this.localCursorElement != null) {
@@ -1158,7 +1175,7 @@ export class TextComponent implements AfterViewInit, OnDestroy {
11581175
return;
11591176
}
11601177

1161-
const sel: Selection | null = window.getSelection();
1178+
const sel: Selection | null = this.window.getSelection();
11621179
if (sel == null) {
11631180
return;
11641181
}

0 commit comments

Comments
 (0)