From 6e8c6f878da8de35d19506bc9c34618aee72c59d Mon Sep 17 00:00:00 2001 From: John Flockton Date: Wed, 1 May 2024 20:03:22 +0100 Subject: [PATCH 1/4] Clamp number of registered editor above 0 --- packages/lexical/src/LexicalEvents.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 96cf9ad7c86..efeddf1ce2e 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -1210,7 +1210,10 @@ export function addRootElementEvents( ) { doc.addEventListener('selectionchange', onDocumentSelectionChange); } - rootElementsRegistered.set(doc, documentRootElementsCount || 0 + 1); + rootElementsRegistered.set( + doc, + Math.max(documentRootElementsCount ?? 0, 0) + 1, + ); // @ts-expect-error: internal field rootElement.__lexicalEditor = editor; @@ -1319,7 +1322,7 @@ export function removeRootElementEvents(rootElement: HTMLElement): void { // We only want to have a single global selectionchange event handler, shared // between all editor instances. - rootElementsRegistered.set(doc, documentRootElementsCount - 1); + rootElementsRegistered.set(doc, Math.max(documentRootElementsCount - 1, 0)); if (rootElementsRegistered.get(doc) === 0) { doc.removeEventListener('selectionchange', onDocumentSelectionChange); } From 3bd3837cd4c5078f13aee5d47eb6072dac67f06c Mon Sep 17 00:00:00 2001 From: John Flockton Date: Wed, 1 May 2024 21:02:30 +0100 Subject: [PATCH 2/4] Fix nullish coelscing --- packages/lexical/src/LexicalEvents.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index efeddf1ce2e..380dbc96ab5 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -1212,7 +1212,10 @@ export function addRootElementEvents( } rootElementsRegistered.set( doc, - Math.max(documentRootElementsCount ?? 0, 0) + 1, + Math.max( + documentRootElementsCount !== undefined ? documentRootElementsCount : 0, + 0, + ) + 1, ); // @ts-expect-error: internal field From adef270927d0151ee7119c2131a2f3503e6d6680 Mon Sep 17 00:00:00 2001 From: John Flockton Date: Wed, 1 May 2024 21:33:06 +0100 Subject: [PATCH 3/4] should be 1 --- packages/lexical/src/LexicalEvents.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 380dbc96ab5..5e68d776e22 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -1213,8 +1213,8 @@ export function addRootElementEvents( rootElementsRegistered.set( doc, Math.max( - documentRootElementsCount !== undefined ? documentRootElementsCount : 0, - 0, + documentRootElementsCount !== undefined ? documentRootElementsCount : 1, + 1, ) + 1, ); From 9ade51cfcf649cdf0a756919afe31d8366713c4a Mon Sep 17 00:00:00 2001 From: John Flockton Date: Wed, 1 May 2024 21:35:49 +0100 Subject: [PATCH 4/4] That was incorrect --- packages/lexical/src/LexicalEvents.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/lexical/src/LexicalEvents.ts b/packages/lexical/src/LexicalEvents.ts index 5e68d776e22..380dbc96ab5 100644 --- a/packages/lexical/src/LexicalEvents.ts +++ b/packages/lexical/src/LexicalEvents.ts @@ -1213,8 +1213,8 @@ export function addRootElementEvents( rootElementsRegistered.set( doc, Math.max( - documentRootElementsCount !== undefined ? documentRootElementsCount : 1, - 1, + documentRootElementsCount !== undefined ? documentRootElementsCount : 0, + 0, ) + 1, );