'use client'; import React from 'react'; import type { TEditor } from '@udecode/plate-common'; import type { DropTargetMonitor } from 'react-dnd'; import { cn, withRef } from '@udecode/cn'; import { type PlateElementProps, MemoizedChildren, useEditorPlugin, useEditorRef, withHOC, } from '@udecode/plate-common/react'; import { type DragItemNode, DraggableProvider, useDraggable, useDraggableGutter, useDraggableState, useDropLine, } from '@udecode/plate-dnd'; import { BlockSelectionPlugin } from '@udecode/plate-selection/react'; import { GripVertical } from 'lucide-react'; import { useSelected } from 'slate-react'; import { useMounted } from '~/lib/hooks/use-mounted'; import { Tooltip, TooltipContent, TooltipPortal, TooltipProvider, TooltipTrigger, } from './tooltip'; export interface DraggableProps extends PlateElementProps { /** * Intercepts the drop handling. If `false` is returned, the default drop * behavior is called after. If `true` is returned, the default behavior is * not called. */ onDropHandler?: ( editor: TEditor, props: { id: string; dragItem: DragItemNode; monitor: DropTargetMonitor; nodeRef: any; } ) => boolean; } export const Draggable = withHOC( DraggableProvider, withRef<'div', DraggableProps>( ({ className, onDropHandler, ...props }, ref) => { const { children, element } = props; const state = useDraggableState({ element, onDropHandler }); const { isDragging } = state; const { previewRef, handleRef } = useDraggable(state); const mounted = useMounted(); return (
{children}
); } ) ); const Gutter = React.forwardRef< HTMLDivElement, React.HTMLAttributes >(({ children, className, ...props }, ref) => { const { useOption } = useEditorPlugin(BlockSelectionPlugin); const isSelectionAreaVisible = useOption('isSelectionAreaVisible'); const gutter = useDraggableGutter(); const selected = useSelected(); return (
{children}
); }); const DragHandle = React.memo(() => { const editor = useEditorRef(); return ( { event.stopPropagation(); event.preventDefault(); }} onMouseDown={() => { editor .getApi(BlockSelectionPlugin) .blockSelection?.resetSelectedIds(); }} /> Drag to move ); }); const DropLine = React.memo( React.forwardRef>( ({ children, className, ...props }, ref) => { const state = useDropLine(); if (!state.dropLine) return null; return (
{children}
); } ) );