Skip to content

Commit

Permalink
chore: fix issue with focus getting lost after pressing up/down after…
Browse files Browse the repository at this point in the history
… clicking inside checklist [skip e2e]
  • Loading branch information
amanharwara committed Jul 17, 2024
1 parent 5a7641c commit 6e75d29
Showing 1 changed file with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $isListItemNode, $isListNode, INSERT_CHECK_LIST_COMMAND, insertList, ListNode } from '@lexical/list'
import { $isListItemNode, INSERT_CHECK_LIST_COMMAND, insertList, ListNode } from '@lexical/list'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { calculateZoomLevel, isHTMLElement, mergeRegister } from '@lexical/utils'
import {
Expand Down Expand Up @@ -47,38 +47,36 @@ export function CheckListPlugin(): null {
return
}

editor.update(() => {
const targetNode = $getNearestNodeFromDOMNode(target)
editor.getRootElement()?.focus()

const parentNode = targetNode?.getParent()

if (!$isListNode(parentNode) || parentNode.getListType() !== 'check') {
return
}
const parentNode = target.parentNode
// @ts-expect-error internal field
if (!parentNode || parentNode.__lexicalListType !== 'check') {
return
}

const rect = target.getBoundingClientRect()
const rect = target.getBoundingClientRect()

const listItemElementStyles = getComputedStyle(target)
const paddingLeft = parseFloat(listItemElementStyles.paddingLeft) || 0
const paddingRight = parseFloat(listItemElementStyles.paddingRight) || 0
const lineHeight = parseFloat(listItemElementStyles.lineHeight) || 0
const listItemElementStyles = getComputedStyle(target)
const paddingLeft = parseFloat(listItemElementStyles.paddingLeft) || 0
const paddingRight = parseFloat(listItemElementStyles.paddingRight) || 0
const lineHeight = parseFloat(listItemElementStyles.lineHeight) || 0

const checkStyles = getComputedStyle(target, ':before')
const checkWidth = parseFloat(checkStyles.width) || 0
const checkStyles = getComputedStyle(target, ':before')
const checkWidth = parseFloat(checkStyles.width) || 0

const pageX = event.pageX / calculateZoomLevel(target)
const pageX = event.pageX / calculateZoomLevel(target)

const isWithinHorizontalThreshold =
target.dir === 'rtl'
? pageX < rect.right && pageX > rect.right - paddingRight
: pageX > rect.left && pageX < rect.left + (checkWidth || paddingLeft)
const isWithinHorizontalThreshold =
target.dir === 'rtl'
? pageX < rect.right && pageX > rect.right - paddingRight
: pageX > rect.left && pageX < rect.left + (checkWidth || paddingLeft)

const isWithinVerticalThreshold = event.clientY > rect.top && event.clientY < rect.top + lineHeight
const isWithinVerticalThreshold = event.clientY > rect.top && event.clientY < rect.top + lineHeight

if (isWithinHorizontalThreshold && isWithinVerticalThreshold) {
callback()
}
})
if (isWithinHorizontalThreshold && isWithinVerticalThreshold) {
callback()
}
}

function handleClick(event: Event) {
Expand All @@ -100,7 +98,6 @@ export function CheckListPlugin(): null {
return
}

domNode.focus()
node.toggleChecked()
})
})
Expand Down

0 comments on commit 6e75d29

Please sign in to comment.