From 564497ec2a9c69a9b0b0d8ae64823f5123a9d690 Mon Sep 17 00:00:00 2001
From: Sy Sagar <93787385+SySagar@users.noreply.github.com>
Date: Mon, 24 Feb 2025 14:12:12 +0530
Subject: [PATCH] feat(lexicalEditor): custom enter-key plugin (#15952)
* fix(team-description): break lines rendering
* chore(EventTypeDescription): useBreakLine hook for handling html line breaks
* feat(lexicalEditor): custom enter-key plugin
* test(customEditorPlugin): enter key usage
* Update customEnterKeyPlugin.tsx
---------
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
---
packages/ui/components/editor/Editor.tsx | 2 +
.../plugins/CustomEnterKeyPlugin.test.tsx | 81 +++++++++++++++++++
.../editor/plugins/customEnterKeyPlugin.tsx | 40 +++++++++
3 files changed, 123 insertions(+)
create mode 100644 packages/ui/components/editor/plugins/CustomEnterKeyPlugin.test.tsx
create mode 100644 packages/ui/components/editor/plugins/customEnterKeyPlugin.tsx
diff --git a/packages/ui/components/editor/Editor.tsx b/packages/ui/components/editor/Editor.tsx
index 66c88c5274..1f9cc24a61 100644
--- a/packages/ui/components/editor/Editor.tsx
+++ b/packages/ui/components/editor/Editor.tsx
@@ -23,6 +23,7 @@ import AutoLinkPlugin from "./plugins/AutoLinkPlugin";
import EditablePlugin from "./plugins/EditablePlugin";
import PlainTextPlugin from "./plugins/PlainTextPlugin";
import ToolbarPlugin from "./plugins/ToolbarPlugin";
+import CustomEnterKeyPlugin from "./plugins/customEnterKeyPlugin";
import "./stylesEditor.css";
/*
@@ -111,6 +112,7 @@ export const Editor = (props: TextEditorProps) => {
+
{props?.variables ? : null}
(
+
+ }
+ placeholder={Enter some text...
}
+ ErrorBoundary={LexicalErrorBoundary}
+ />
+
+
+);
+
+describe("Editor", () => {
+ describe("CustomEnterKeyPlugin", () => {
+ let container: HTMLElement | null = null;
+
+ beforeEach(() => {
+ container = render().container;
+ });
+
+ afterEach(() => {
+ container = null;
+ });
+
+ it("should insert a line break on Enter key press", async () => {
+ if (container === null) {
+ throw new Error("Container is null");
+ }
+ const editorElement = container.querySelector('[contenteditable="true"]') as HTMLElement;
+
+ editorElement.focus();
+
+ const paragraphElement = editorElement.querySelector("p") as HTMLElement;
+ fireEvent.keyDown(editorElement, { key: "Enter", code: "Enter", keycode: 13 });
+
+ await waitFor(() => {
+ const brElements = paragraphElement?.querySelectorAll("br");
+
+ expect(brElements?.length).toBe(1);
+ });
+ });
+ });
+});
diff --git a/packages/ui/components/editor/plugins/customEnterKeyPlugin.tsx b/packages/ui/components/editor/plugins/customEnterKeyPlugin.tsx
new file mode 100644
index 0000000000..177e08e7f8
--- /dev/null
+++ b/packages/ui/components/editor/plugins/customEnterKeyPlugin.tsx
@@ -0,0 +1,40 @@
+"use client";
+
+import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
+import {
+ $createLineBreakNode,
+ $getSelection,
+ $isRangeSelection,
+ COMMAND_PRIORITY_HIGH,
+ KEY_ENTER_COMMAND,
+} from "lexical";
+import { useEffect } from "react";
+
+const CustomEnterKeyPlugin = () => {
+ const [editor] = useLexicalComposerContext();
+
+ useEffect(() => {
+ const removeEnterCommand = editor.registerCommand(
+ KEY_ENTER_COMMAND,
+ (event: any) => {
+ const selection = $getSelection();
+ if ($isRangeSelection(selection)) {
+ event.preventDefault();
+ const lineBreakNode = $createLineBreakNode();
+ selection.insertNodes([lineBreakNode]);
+ return true;
+ }
+ return false; // any failure case of not selection
+ },
+ COMMAND_PRIORITY_HIGH
+ );
+
+ return () => {
+ removeEnterCommand();
+ };
+ }, [editor]);
+
+ return null;
+};
+
+export default CustomEnterKeyPlugin;