Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with removing selectionchange correctly #5848

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading