Skip to content

Commit

Permalink
Fix issue with removeing selectionchange correctly (#5848)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegreatercurve authored Apr 8, 2024
1 parent e2f2e3d commit d306095
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,10 @@ export function addRootElementEvents(
// between all editor instances.
const doc = rootElement.ownerDocument;
const documentRootElementsCount = rootElementsRegistered.get(doc);
if (documentRootElementsCount === undefined) {
if (
documentRootElementsCount === undefined ||
documentRootElementsCount < 1
) {
doc.addEventListener('selectionchange', onDocumentSelectionChange);
}
rootElementsRegistered.set(doc, documentRootElementsCount || 0 + 1);
Expand Down Expand Up @@ -1307,10 +1310,11 @@ export function removeRootElementEvents(rootElement: HTMLElement): void {
documentRootElementsCount !== undefined,
'Root element not registered',
);

// We only want to have a single global selectionchange event handler, shared
// between all editor instances.
rootElementsRegistered.set(doc, documentRootElementsCount - 1);
if (rootElementsRegistered.get(doc) === 1) {
if (rootElementsRegistered.get(doc) === 0) {
doc.removeEventListener('selectionchange', onDocumentSelectionChange);
}

Expand Down

0 comments on commit d306095

Please sign in to comment.