Skip to content

Commit c59085f

Browse files
Palanikannan1437sriramveeraghanta
authored andcommitted
🐛 fix: Hide drag handle when cursor leaves the editor container (#3401)
This fix adds support for hiding the drag handle when the cursor leaves the editor container. It improves the user experience by providing a cleaner interface and removing unnecessary visual elements especially while scrolling. - Add `hideDragHandle` prop to `EditorContainer` component in `editor-container.tsx`. - Implement `onMouseLeave` event handler in `EditorContainer` to invoke `hideDragHandle` function. - Update `DragAndDrop` extension in `drag-drop.tsx` to accept a `setHideDragHandle` function as an optional parameter. - Pass the `setHideDragHandle` function from `RichTextEditor` component to `DragAndDrop` extension in `RichTextEditorExtensions` function in `index.tsx`. - Set `hideDragHandleOnMouseLeave` state in `RichTextEditor` component to store the `hideDragHandlerFromDragDrop` function. - Create `setHideDragHandleFunction` callback function in `RichTextEditor` to update the `hideDragHandleOnMouseLeave` state. - Pass `hideDragHandleOnMouseLeave` as `hideDragHandle` prop to `EditorContainer` component in `RichTextEditor`.
1 parent 8d94375 commit c59085f

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

packages/editor/core/src/ui/components/editor-container.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ interface EditorContainerProps {
55
editor: Editor | null;
66
editorClassNames: string;
77
children: ReactNode;
8+
hideDragHandle?: () => void;
89
}
910

10-
export const EditorContainer = ({ editor, editorClassNames, children }: EditorContainerProps) => (
11+
export const EditorContainer = ({ editor, editorClassNames, hideDragHandle, children }: EditorContainerProps) => (
1112
<div
1213
id="editor-container"
1314
onClick={() => {
1415
editor?.chain().focus().run();
1516
}}
17+
onMouseLeave={() => {
18+
hideDragHandle?.();
19+
}}
1620
className={`cursor-text ${editorClassNames}`}
1721
>
1822
{children}

packages/editor/extensions/src/extensions/drag-drop.tsx

+17-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Extension } from "@tiptap/core";
33
import { PluginKey, NodeSelection, Plugin } from "@tiptap/pm/state";
44
// @ts-ignore
55
import { __serializeForClipboard, EditorView } from "@tiptap/pm/view";
6+
import React from "react";
67

78
function createDragHandleElement(): HTMLElement {
89
const dragHandleElement = document.createElement("div");
@@ -30,6 +31,7 @@ function createDragHandleElement(): HTMLElement {
3031

3132
export interface DragHandleOptions {
3233
dragHandleWidth: number;
34+
setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void;
3335
}
3436

3537
function absoluteRect(node: Element) {
@@ -151,6 +153,8 @@ function DragHandle(options: DragHandleOptions) {
151153
}
152154
}
153155

156+
options.setHideDragHandle?.(hideDragHandle);
157+
154158
return new Plugin({
155159
key: new PluginKey("dragHandle"),
156160
view: (view) => {
@@ -238,14 +242,16 @@ function DragHandle(options: DragHandleOptions) {
238242
});
239243
}
240244

241-
export const DragAndDrop = Extension.create({
242-
name: "dragAndDrop",
243-
244-
addProseMirrorPlugins() {
245-
return [
246-
DragHandle({
247-
dragHandleWidth: 24,
248-
}),
249-
];
250-
},
251-
});
245+
export const DragAndDrop = (setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void) =>
246+
Extension.create({
247+
name: "dragAndDrop",
248+
249+
addProseMirrorPlugins() {
250+
return [
251+
DragHandle({
252+
dragHandleWidth: 24,
253+
setHideDragHandle,
254+
}),
255+
];
256+
},
257+
});

packages/editor/rich-text-editor/src/ui/extensions/index.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { SlashCommand, DragAndDrop } from "@plane/editor-extensions";
2-
import Placeholder from "@tiptap/extension-placeholder";
31
import { UploadImage } from "@plane/editor-core";
2+
import { DragAndDrop, SlashCommand } from "@plane/editor-extensions";
3+
import Placeholder from "@tiptap/extension-placeholder";
44

55
export const RichTextEditorExtensions = (
66
uploadFile: UploadImage,
77
setIsSubmitting?: (isSubmitting: "submitting" | "submitted" | "saved") => void,
8-
dragDropEnabled?: boolean
8+
dragDropEnabled?: boolean,
9+
setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void
910
) => [
1011
SlashCommand(uploadFile, setIsSubmitting),
11-
dragDropEnabled === true && DragAndDrop,
12+
dragDropEnabled === true && DragAndDrop(setHideDragHandle),
1213
Placeholder.configure({
1314
placeholder: ({ node }) => {
1415
if (node.type.name === "heading") {

packages/editor/rich-text-editor/src/ui/index.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"use client";
2-
import * as React from "react";
32
import {
43
DeleteImage,
54
EditorContainer,
@@ -10,8 +9,9 @@ import {
109
UploadImage,
1110
useEditor,
1211
} from "@plane/editor-core";
13-
import { EditorBubbleMenu } from "src/ui/menus/bubble-menu";
12+
import * as React from "react";
1413
import { RichTextEditorExtensions } from "src/ui/extensions";
14+
import { EditorBubbleMenu } from "src/ui/menus/bubble-menu";
1515

1616
export type IRichTextEditor = {
1717
value: string;
@@ -66,6 +66,14 @@ const RichTextEditor = ({
6666
rerenderOnPropsChange,
6767
mentionSuggestions,
6868
}: RichTextEditorProps) => {
69+
const [hideDragHandleOnMouseLeave, setHideDragHandleOnMouseLeave] = React.useState<() => void>(() => {});
70+
71+
// this essentially sets the hideDragHandle function from the DragAndDrop extension as the Plugin
72+
// loads such that we can invoke it from react when the cursor leaves the container
73+
const setHideDragHandleFunction = (hideDragHandlerFromDragDrop: () => void) => {
74+
setHideDragHandleOnMouseLeave(() => hideDragHandlerFromDragDrop);
75+
};
76+
6977
const editor = useEditor({
7078
onChange,
7179
debouncedUpdatesEnabled,
@@ -78,7 +86,7 @@ const RichTextEditor = ({
7886
restoreFile,
7987
forwardedRef,
8088
rerenderOnPropsChange,
81-
extensions: RichTextEditorExtensions(uploadFile, setIsSubmitting, dragDropEnabled),
89+
extensions: RichTextEditorExtensions(uploadFile, setIsSubmitting, dragDropEnabled, setHideDragHandleFunction),
8290
mentionHighlights,
8391
mentionSuggestions,
8492
});
@@ -92,7 +100,7 @@ const RichTextEditor = ({
92100
if (!editor) return null;
93101

94102
return (
95-
<EditorContainer editor={editor} editorClassNames={editorClassNames}>
103+
<EditorContainer hideDragHandle={hideDragHandleOnMouseLeave} editor={editor} editorClassNames={editorClassNames}>
96104
{editor && <EditorBubbleMenu editor={editor} />}
97105
<div className="flex flex-col">
98106
<EditorContentWrapper editor={editor} editorContentCustomClassNames={editorContentCustomClassNames} />

0 commit comments

Comments
 (0)