🐛 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`.
This commit is contained in:
M. Palanikannan 2024-01-18 12:43:43 +05:30 committed by sriram veeraghanta
parent 1adb38655a
commit 04b2214bf2
4 changed files with 38 additions and 19 deletions

View File

@ -5,14 +5,18 @@ interface EditorContainerProps {
editor: Editor | null; editor: Editor | null;
editorClassNames: string; editorClassNames: string;
children: ReactNode; children: ReactNode;
hideDragHandle?: () => void;
} }
export const EditorContainer = ({ editor, editorClassNames, children }: EditorContainerProps) => ( export const EditorContainer = ({ editor, editorClassNames, hideDragHandle, children }: EditorContainerProps) => (
<div <div
id="editor-container" id="editor-container"
onClick={() => { onClick={() => {
editor?.chain().focus().run(); editor?.chain().focus().run();
}} }}
onMouseLeave={() => {
hideDragHandle?.();
}}
className={`cursor-text ${editorClassNames}`} className={`cursor-text ${editorClassNames}`}
> >
{children} {children}

View File

@ -3,6 +3,7 @@ import { Extension } from "@tiptap/core";
import { PluginKey, NodeSelection, Plugin } from "@tiptap/pm/state"; import { PluginKey, NodeSelection, Plugin } from "@tiptap/pm/state";
// @ts-ignore // @ts-ignore
import { __serializeForClipboard, EditorView } from "@tiptap/pm/view"; import { __serializeForClipboard, EditorView } from "@tiptap/pm/view";
import React from "react";
function createDragHandleElement(): HTMLElement { function createDragHandleElement(): HTMLElement {
const dragHandleElement = document.createElement("div"); const dragHandleElement = document.createElement("div");
@ -30,6 +31,7 @@ function createDragHandleElement(): HTMLElement {
export interface DragHandleOptions { export interface DragHandleOptions {
dragHandleWidth: number; dragHandleWidth: number;
setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void;
} }
function absoluteRect(node: Element) { function absoluteRect(node: Element) {
@ -151,6 +153,8 @@ function DragHandle(options: DragHandleOptions) {
} }
} }
options.setHideDragHandle?.(hideDragHandle);
return new Plugin({ return new Plugin({
key: new PluginKey("dragHandle"), key: new PluginKey("dragHandle"),
view: (view) => { view: (view) => {
@ -238,13 +242,15 @@ function DragHandle(options: DragHandleOptions) {
}); });
} }
export const DragAndDrop = Extension.create({ export const DragAndDrop = (setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void) =>
Extension.create({
name: "dragAndDrop", name: "dragAndDrop",
addProseMirrorPlugins() { addProseMirrorPlugins() {
return [ return [
DragHandle({ DragHandle({
dragHandleWidth: 24, dragHandleWidth: 24,
setHideDragHandle,
}), }),
]; ];
}, },

View File

@ -1,14 +1,15 @@
import { SlashCommand, DragAndDrop } from "@plane/editor-extensions";
import Placeholder from "@tiptap/extension-placeholder";
import { UploadImage } from "@plane/editor-core"; import { UploadImage } from "@plane/editor-core";
import { DragAndDrop, SlashCommand } from "@plane/editor-extensions";
import Placeholder from "@tiptap/extension-placeholder";
export const RichTextEditorExtensions = ( export const RichTextEditorExtensions = (
uploadFile: UploadImage, uploadFile: UploadImage,
setIsSubmitting?: (isSubmitting: "submitting" | "submitted" | "saved") => void, setIsSubmitting?: (isSubmitting: "submitting" | "submitted" | "saved") => void,
dragDropEnabled?: boolean dragDropEnabled?: boolean,
setHideDragHandle?: (hideDragHandlerFromDragDrop: () => void) => void
) => [ ) => [
SlashCommand(uploadFile, setIsSubmitting), SlashCommand(uploadFile, setIsSubmitting),
dragDropEnabled === true && DragAndDrop, dragDropEnabled === true && DragAndDrop(setHideDragHandle),
Placeholder.configure({ Placeholder.configure({
placeholder: ({ node }) => { placeholder: ({ node }) => {
if (node.type.name === "heading") { if (node.type.name === "heading") {

View File

@ -1,5 +1,4 @@
"use client"; "use client";
import * as React from "react";
import { import {
DeleteImage, DeleteImage,
EditorContainer, EditorContainer,
@ -10,8 +9,9 @@ import {
UploadImage, UploadImage,
useEditor, useEditor,
} from "@plane/editor-core"; } from "@plane/editor-core";
import { EditorBubbleMenu } from "src/ui/menus/bubble-menu"; import * as React from "react";
import { RichTextEditorExtensions } from "src/ui/extensions"; import { RichTextEditorExtensions } from "src/ui/extensions";
import { EditorBubbleMenu } from "src/ui/menus/bubble-menu";
export type IRichTextEditor = { export type IRichTextEditor = {
value: string; value: string;
@ -66,6 +66,14 @@ const RichTextEditor = ({
rerenderOnPropsChange, rerenderOnPropsChange,
mentionSuggestions, mentionSuggestions,
}: RichTextEditorProps) => { }: RichTextEditorProps) => {
const [hideDragHandleOnMouseLeave, setHideDragHandleOnMouseLeave] = React.useState<() => void>(() => {});
// this essentially sets the hideDragHandle function from the DragAndDrop extension as the Plugin
// loads such that we can invoke it from react when the cursor leaves the container
const setHideDragHandleFunction = (hideDragHandlerFromDragDrop: () => void) => {
setHideDragHandleOnMouseLeave(() => hideDragHandlerFromDragDrop);
};
const editor = useEditor({ const editor = useEditor({
onChange, onChange,
debouncedUpdatesEnabled, debouncedUpdatesEnabled,
@ -78,7 +86,7 @@ const RichTextEditor = ({
restoreFile, restoreFile,
forwardedRef, forwardedRef,
rerenderOnPropsChange, rerenderOnPropsChange,
extensions: RichTextEditorExtensions(uploadFile, setIsSubmitting, dragDropEnabled), extensions: RichTextEditorExtensions(uploadFile, setIsSubmitting, dragDropEnabled, setHideDragHandleFunction),
mentionHighlights, mentionHighlights,
mentionSuggestions, mentionSuggestions,
}); });
@ -92,7 +100,7 @@ const RichTextEditor = ({
if (!editor) return null; if (!editor) return null;
return ( return (
<EditorContainer editor={editor} editorClassNames={editorClassNames}> <EditorContainer hideDragHandle={hideDragHandleOnMouseLeave} editor={editor} editorClassNames={editorClassNames}>
{editor && <EditorBubbleMenu editor={editor} />} {editor && <EditorBubbleMenu editor={editor} />}
<div className="flex flex-col"> <div className="flex flex-col">
<EditorContentWrapper editor={editor} editorContentCustomClassNames={editorContentCustomClassNames} /> <EditorContentWrapper editor={editor} editorContentCustomClassNames={editorContentCustomClassNames} />