Skip to content

Commit

Permalink
fix(): insert a space (if necessary) when pasting text (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodenn authored Dec 27, 2023
1 parent 8b9984d commit ab5971d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-games-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lexical-beautiful-mentions": patch
---

fix(): insert a space (if necessary) when pasting text
15 changes: 15 additions & 0 deletions plugin/src/BeautifulMentionsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
KEY_BACKSPACE_COMMAND,
KEY_DOWN_COMMAND,
KEY_SPACE_COMMAND,
PASTE_COMMAND,
TextNode,
} from "lexical";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
Expand Down Expand Up @@ -482,6 +483,18 @@ export function BeautifulMentionsPlugin(props: BeautifulMentionsPluginProps) {
[insertSpaceIfNecessary, punctuation, triggers],
);

const handlePaste = useCallback(
(event: ClipboardEvent) => {
const text = event.clipboardData?.getData("text/plain");
const firstChar = text && text.charAt(0);
if (firstChar && isWordChar(firstChar, triggers, punctuation)) {
insertSpaceIfNecessary();
}
return false; // will be handled by the lexical clipboard module
},
[insertSpaceIfNecessary, punctuation, triggers],
);

useEffect(() => {
return mergeRegister(
editor.registerCommand(
Expand Down Expand Up @@ -562,6 +575,7 @@ export function BeautifulMentionsPlugin(props: BeautifulMentionsPluginProps) {
},
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(PASTE_COMMAND, handlePaste, COMMAND_PRIORITY_LOW),
);
}, [
editor,
Expand All @@ -576,6 +590,7 @@ export function BeautifulMentionsPlugin(props: BeautifulMentionsPluginProps) {
archiveSelection,
handleKeyDown,
handleDeleteMention,
handlePaste,
]);

useEffect(() => {
Expand Down

1 comment on commit ab5971d

@vercel
Copy link

@vercel vercel bot commented on ab5971d Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.