Skip to content

Commit

Permalink
fix(richtext-lexical): cannot delete multiple selected nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Sep 28, 2024
1 parent 4c6bee9 commit 1ee1486
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {

const $onDelete = useCallback(
(event: KeyboardEvent) => {
if (isSelected && $isNodeSelection($getSelection())) {
const deleteSelection = $getSelection()
if (isSelected && $isNodeSelection(deleteSelection)) {
event.preventDefault()
const node = $getNodeByKey(nodeKey)
if ($isInlineBlockNode(node)) {
node.remove()
return true
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isInlineBlockNode(node)) {
node.remove()
}
})
})
}
return false
},
[isSelected, nodeKey],
[editor, isSelected],
)
const onClick = useCallback(
(payload: MouseEvent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext
import { useLexicalNodeSelection } from '@lexical/react/useLexicalNodeSelection.js'
import { addClassNamesToElement, mergeRegister, removeClassNamesFromElement } from '@lexical/utils'
import {
$getNodeByKey,
$getSelection,
$isNodeSelection,
CLICK_COMMAND,
Expand All @@ -31,17 +30,20 @@ export function HorizontalRuleComponent({ nodeKey }: { nodeKey: NodeKey }) {

const $onDelete = useCallback(
(event: KeyboardEvent) => {
if (isSelected && $isNodeSelection($getSelection())) {
const deleteSelection = $getSelection()
if (isSelected && $isNodeSelection(deleteSelection)) {
event.preventDefault()
const node = $getNodeByKey(nodeKey)
if ($isHorizontalRuleNode(node)) {
node.remove()
return true
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isHorizontalRuleNode(node)) {
node.remove()
}
})
})
}
return false
},
[isSelected, nodeKey],
[editor, isSelected],
)

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,21 @@ const Component: React.FC<Props> = (props) => {

const $onDelete = useCallback(
(payload: KeyboardEvent) => {
if (isSelected && $isNodeSelection($getSelection())) {
const deleteSelection = $getSelection()
if (isSelected && $isNodeSelection(deleteSelection)) {
const event: KeyboardEvent = payload
event.preventDefault()
const node = $getNodeByKey(nodeKey!)
if ($isRelationshipNode(node)) {
node.remove()
return true
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isRelationshipNode(node)) {
node.remove()
}
})
})
}
return false
},
[isSelected, nodeKey],
[editor, isSelected],
)
const onClick = useCallback(
(payload: MouseEvent) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,20 @@ const Component: React.FC<ElementProps> = (props) => {

const $onDelete = useCallback(
(event: KeyboardEvent) => {
if (isSelected && $isNodeSelection($getSelection())) {
const deleteSelection = $getSelection()
if (isSelected && $isNodeSelection(deleteSelection)) {
event.preventDefault()
const node = $getNodeByKey(nodeKey)
if ($isUploadNode(node)) {
node.remove()
return true
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isUploadNode(node)) {
node.remove()
}
})
})
}
return false
},
[isSelected, nodeKey],
[editor, isSelected],
)

useEffect(() => {
Expand Down

0 comments on commit 1ee1486

Please sign in to comment.