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

[lexical-react] Fix multiple node selection deletion #6538

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ export default function ExcalidrawComponent({
useLexicalNodeSelection(nodeKey);
const [isResizing, setIsResizing] = useState<boolean>(false);

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

// Set editor to readOnly if excalidraw is open to prevent unwanted changes
Expand Down Expand Up @@ -103,16 +104,16 @@ export default function ExcalidrawComponent({
),
editor.registerCommand(
KEY_DELETE_COMMAND,
onDelete,
$onDelete,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
KEY_BACKSPACE_COMMAND,
onDelete,
$onDelete,
COMMAND_PRIORITY_LOW,
),
);
}, [clearSelection, editor, isSelected, isResizing, onDelete, setSelected]);
}, [clearSelection, editor, isSelected, isResizing, $onDelete, setSelected]);

const deleteNode = useCallback(() => {
setModalOpen(false);
Expand Down
17 changes: 10 additions & 7 deletions packages/lexical-playground/src/nodes/ImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,21 @@ export default function ImageComponent({

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 ($isImageNode(node)) {
node.remove();
return true;
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isImageNode(node)) {
node.remove();
}
});
});
}
return false;
},
[isSelected, nodeKey],
[editor, isSelected],
);

const $onEnter = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,23 @@ export default function InlineImageComponent({

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 ($isInlineImageNode(node)) {
node.remove();
return true;
if (isSelected && $isNodeSelection(deleteSelection)) {
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isInlineImageNode(node)) {
node.remove();
}
});
});
}
}
return false;
},
[isSelected, nodeKey],
[editor, isSelected],
);

const $onEnter = useCallback(
Expand Down
18 changes: 10 additions & 8 deletions packages/lexical-playground/src/nodes/PageBreakNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
import {mergeRegister} from '@lexical/utils';
import {
$getNodeByKey,
$getSelection,
$isNodeSelection,
CLICK_COMMAND,
Expand Down Expand Up @@ -39,16 +38,19 @@ function PageBreakComponent({nodeKey}: {nodeKey: NodeKey}) {
const $onDelete = useCallback(
(event: KeyboardEvent) => {
event.preventDefault();
if (isSelected && $isNodeSelection($getSelection())) {
const node = $getNodeByKey(nodeKey);
if ($isPageBreakNode(node)) {
node.remove();
return true;
}
const deleteSelection = $getSelection();
if (isSelected && $isNodeSelection(deleteSelection)) {
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isPageBreakNode(node)) {
node.remove();
}
});
});
}
return false;
},
[isSelected, nodeKey],
[editor, isSelected],
);

useEffect(() => {
Expand Down
17 changes: 10 additions & 7 deletions packages/lexical-playground/src/nodes/PollComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,21 @@ export default function PollComponent({

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 ($isPollNode(node)) {
node.remove();
return true;
}
editor.update(() => {
deleteSelection.getNodes().forEach((node) => {
if ($isPollNode(node)) {
node.remove();
}
});
});
}
return false;
},
[isSelected, nodeKey],
[editor, isSelected],
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ export function BlockWithAlignableContents({

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

return false;
},
[isSelected, nodeKey],
[editor, isSelected],
);

useEffect(() => {
Expand Down
18 changes: 10 additions & 8 deletions packages/lexical-react/src/LexicalHorizontalRuleNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from '@lexical/utils';
import {
$applyNodeReplacement,
$getNodeByKey,
$getSelection,
$isNodeSelection,
CLICK_COMMAND,
Expand All @@ -51,17 +50,20 @@ 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
Loading