Skip to content

Commit

Permalink
fix(): mention selection not being cleared when clicking outside the …
Browse files Browse the repository at this point in the history
…editor and focus the editor again (#475)

* fix(): mention selection not being cleared when clicking outside the editor and focus the editor again

* don't reset selection if the current selection is not a node selection
  • Loading branch information
sodenn authored May 7, 2024
1 parent 722355a commit eb6b1a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-mice-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lexical-beautiful-mentions": patch
---

fix(): mention selection not being cleared when clicking outside the editor and focus the editor again
19 changes: 19 additions & 0 deletions plugin/src/MentionComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
$isElementNode,
$isNodeSelection,
$isTextNode,
$setSelection,
BLUR_COMMAND,
CLICK_COMMAND,
COMMAND_PRIORITY_LOW,
KEY_ARROW_LEFT_COMMAND,
Expand Down Expand Up @@ -165,6 +167,21 @@ export default function BeautifulMentionComponent(
[clearSelection, setSelected],
);

const onBlur = useCallback(() => {
const node = $getNodeByKey(nodeKey);
if (!node || !node.isSelected()) {
return false;
}

const selection = $getSelection();
if (!$isNodeSelection(selection)) {
return false;
}

$setSelection(null);
return false;
}, [nodeKey]);

const onSelectionChange = useCallback(() => {
if (IS_IOS && isSelected) {
// needed to keep the cursor in the editor when clicking next to a selected mention
Expand Down Expand Up @@ -201,6 +218,7 @@ export default function BeautifulMentionComponent(
onArrowRightPress,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(BLUR_COMMAND, onBlur, COMMAND_PRIORITY_LOW),
editor.registerCommand(
SELECTION_CHANGE_COMMAND,
onSelectionChange,
Expand All @@ -216,6 +234,7 @@ export default function BeautifulMentionComponent(
onArrowRightPress,
onClick,
onDelete,
onBlur,
onSelectionChange,
]);

Expand Down

0 comments on commit eb6b1a3

Please sign in to comment.