From ab5971d24327ed5dfb4c3664504f20ffcf223e9c Mon Sep 17 00:00:00 2001 From: Dennis Soehnen Date: Wed, 27 Dec 2023 17:03:07 +0100 Subject: [PATCH] fix(): insert a space (if necessary) when pasting text (#294) --- .changeset/mean-games-grow.md | 5 +++++ plugin/src/BeautifulMentionsPlugin.tsx | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .changeset/mean-games-grow.md diff --git a/.changeset/mean-games-grow.md b/.changeset/mean-games-grow.md new file mode 100644 index 00000000..14dd2c2d --- /dev/null +++ b/.changeset/mean-games-grow.md @@ -0,0 +1,5 @@ +--- +"lexical-beautiful-mentions": patch +--- + +fix(): insert a space (if necessary) when pasting text diff --git a/plugin/src/BeautifulMentionsPlugin.tsx b/plugin/src/BeautifulMentionsPlugin.tsx index 280b19ad..d5b28fe3 100644 --- a/plugin/src/BeautifulMentionsPlugin.tsx +++ b/plugin/src/BeautifulMentionsPlugin.tsx @@ -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"; @@ -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( @@ -562,6 +575,7 @@ export function BeautifulMentionsPlugin(props: BeautifulMentionsPluginProps) { }, COMMAND_PRIORITY_LOW, ), + editor.registerCommand(PASTE_COMMAND, handlePaste, COMMAND_PRIORITY_LOW), ); }, [ editor, @@ -576,6 +590,7 @@ export function BeautifulMentionsPlugin(props: BeautifulMentionsPluginProps) { archiveSelection, handleKeyDown, handleDeleteMention, + handlePaste, ]); useEffect(() => {