Skip to content

Commit

Permalink
Merge pull request #20 from its-dart/main
Browse files Browse the repository at this point in the history
Move checklist listener management to helper to prevent duplicate events
  • Loading branch information
wobsoriano authored Jul 5, 2023
2 parents 644694b + 0b6269c commit 3c33710
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/components/LexicalCheckListPlugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { $findMatchingParent, mergeRegister } from '@lexical/utils'
import { useEditor } from '../composables'
import { useMounted } from '../composables/useMounted'
import { incrementCheckListListenersCount, decrementCheckListListenersCount } from '../composables/listenerManager'
const editor = useEditor()
Expand Down Expand Up @@ -136,16 +137,15 @@ useMounted(() => {
)
})
let listenersCount = 0
function listenPointerDown() {
if (listenersCount++ === 0) {
if (incrementCheckListListenersCount()) {
// @ts-expect-error: speculation ambiguous
document.addEventListener('click', handleClick)
document.addEventListener('pointerdown', handlePointerDown)
}
return () => {
if (--listenersCount === 0) {
if (decrementCheckListListenersCount()) {
// @ts-expect-error: speculation ambiguous
document.removeEventListener('click', handleClick)
document.removeEventListener('pointerdown', handlePointerDown)
Expand Down
9 changes: 9 additions & 0 deletions src/composables/listenerManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let handleClickAndPointerDownListenersCount = 0

export function incrementCheckListListenersCount() {
return handleClickAndPointerDownListenersCount++ === 0
}

export function decrementCheckListListenersCount() {
return --handleClickAndPointerDownListenersCount === 0
}

0 comments on commit 3c33710

Please sign in to comment.