From 94d893703191f5bf08bd7051d6ecec41703494e4 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Fri, 31 May 2024 08:40:24 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Make=20all=20event=20liste?= =?UTF-8?q?ners=20passive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/reedsy/quill-cursors/issues/92 If an event listener never invokes `preventDefault()`, it can be marked as [passive][1]. We don't call `preventDefault()` anywhere in our code, so we can safely mark all of our event listeners as passive. [1]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners --- src/quill-cursors/cursor.ts | 4 ++-- src/quill-cursors/quill-cursors.spec.ts | 2 +- src/quill-cursors/quill-cursors.ts | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/quill-cursors/cursor.ts b/src/quill-cursors/cursor.ts index 7e62c65..82cccc9 100644 --- a/src/quill-cursors/cursor.ts +++ b/src/quill-cursors/cursor.ts @@ -67,7 +67,7 @@ export default class Cursor { this._caretEl = caretContainerElement; this._flagEl = flagElement; - caretContainerElement.addEventListener('mouseover', this._setHoverState); + caretContainerElement.addEventListener('mouseover', this._setHoverState, {passive: true}); return this._el; } @@ -127,7 +127,7 @@ export default class Cursor { } private _setHoverState(): void { - document.addEventListener('mousemove', this._toggleOpenedCursor); + document.addEventListener('mousemove', this._toggleOpenedCursor, {passive: true}); } private _toggleOpenedCursor(e: MouseEvent): void { diff --git a/src/quill-cursors/quill-cursors.spec.ts b/src/quill-cursors/quill-cursors.spec.ts index 4ad3842..1779bae 100644 --- a/src/quill-cursors/quill-cursors.spec.ts +++ b/src/quill-cursors/quill-cursors.spec.ts @@ -70,7 +70,7 @@ describe('QuillCursors', () => { const editor = quill.container.getElementsByClassName('ql-editor')[0]; jest.spyOn(editor, 'addEventListener'); const cursors = new QuillCursors(quill); - expect(editor.addEventListener).toHaveBeenCalledWith('scroll', expect.anything()); + expect(editor.addEventListener).toHaveBeenCalledWith('scroll', expect.anything(), {passive: true}); jest.spyOn(cursors, 'update'); const scroll = new Event('scroll'); diff --git a/src/quill-cursors/quill-cursors.ts b/src/quill-cursors/quill-cursors.ts index 10fa038..3e1eca7 100644 --- a/src/quill-cursors/quill-cursors.ts +++ b/src/quill-cursors/quill-cursors.ts @@ -109,9 +109,9 @@ export default class QuillCursors { } private _registerDomListeners(): void { - const editor = this.quill.container.getElementsByClassName('ql-editor')[0]; - editor.addEventListener('scroll', () => this.update()); - editor.addEventListener('touchstart', this._handleCursorTouch); + const editor = this.quill.container.getElementsByClassName('ql-editor')[0] as HTMLElement; + editor.addEventListener('scroll', () => this.update(), {passive: true}); + editor.addEventListener('touchstart', this._handleCursorTouch, {passive: true}); } private _handleCursorTouch(e: MouseEvent): void {